-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
548 lines (449 loc) · 19.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<head>
<meta charset="utf-8"/>
<title>Open Surgery (Santa Barbara)</title>
<style>
body {
margin: 0;
background-color:peachpuff;
}
</style>
<!--
<script src="//unpkg.com/3d-force-graph"></script>
<script src="./libs/3d-force-graph.min.js"></script>
-->
<script src="https://unpkg.com/three"></script>
<script src="libs/Detector.js"></script>
<script src="libs/PalerBoxBufferGeometry.js"></script>
<script src="libs/TrackballControls.js"></script>
<script src="libs/SurgeryWebGLVisualiser.js"></script>
<script src="libs/surgery_vis_utils.js"></script>
<script>
const HADAMARD_DECORATOR = "H";
const HADAMARD_COLOR = "";
function boxes_collection_has_color(color)
{
var ret = (color in boxes_collection);
return ret;
}
function boxes_collection_add_color(color)
{
boxes_collection[color] = new PalerBoxBufferGeometry();
}
function boxes_collection_clean()
{
var keys = Object.keys(boxes_collection);
for (key in keys)
{
delete boxes_collection[key];
}
boxes_collection = {};
}
function process_node_to_mesh(cube)
{
//if(cube.fz == 0)
// return;
if(cube.c == "white")
{
return;
}
var sides = cube.s;
var color = cube.c;
var current_operation_color = cube.c;
var scale_factor = 0.25;//0.25;
var decorator_factor = 2;
var dim = 0.8;
if(["green", "magenta"].includes(current_operation_color))
{
scale_factor = 1.0;
dim = 1.0;
}
if(scale_factor < 1)
{
//the full size, if spacing is introduced by scaling
dim = 1.0;
}
var dim1 = dim * scale_factor;
var dim2 = dim * scale_factor;
var dim3 = dim * scale_factor;
// var offset = [0, 0, 0];
var offset = [ cube.fx + dim1/2,
cube.fy + dim2/2,
cube.fz + dim3/2];
/*
These are flat Hadamards
move them to the end of the time of distance D
*/
if (cube.d == HADAMARD_DECORATOR)
{
//flat operation
dim1 = dim1 * decorator_factor;
dim2 = dim2 * decorator_factor;
dim3 = 0.02;
// offset = [0, 0, dim/2];
offset[2] += dim*(1- scale_factor) + dim*scale_factor/2;
}
construct_cuboid(dim1, dim2, dim3, offset, sides, color);
/*
if scaling should be applied
this means that bars between the nodes are introduced into the visualisation
*/
if((!["green", "magenta"].includes(current_operation_color)) && (cube.d != HADAMARD_DECORATOR) && (scale_factor != 1.0))
{
var coordinate_exists = false;
var has_valid_id = false;
var current_op = cube.op;
var allow_extension_hardware = !(["red"].includes(current_operation_color))
coordinate_exists = check_coordinate_exists(coordinates_matrix, cube.fx + 1, cube.fy, cube.fz);
if(coordinate_exists)
{
has_valid_id = (coordinates_matrix[cube.fx + 1][cube.fy][cube.fz] != -1);
/*
Movements (orange) cannot connect on hardware plain to USE_QUBIT(red)
*/
if(current_operation_color == "orange")
{
if (coordinates_matrix[cube.fx + 1][cube.fy][cube.fz].includes("OperationTypes.USE_QUBIT"))
{
allow_extension_hardware = false;
}
}
if(current_operation_color == "yellow")
{
if (! coordinates_matrix[cube.fx + 1][cube.fy][cube.fz].includes("OperationTypes.USE_ANCILLA"))
{
allow_extension_hardware = false;
}
}
}
else
{
has_valid_id = false;
}
if(coordinate_exists && has_valid_id && allow_extension_hardware)
{
var n_offset = Array();
n_offset = offset.slice();
n_offset[0] += dim1/2 + dim*(1 - scale_factor)/2;
construct_cuboid(dim*(1 - scale_factor), dim2, dim3, n_offset, sides, color);
}
allow_extension_hardware = !(["red"].includes(current_operation_color))
coordinate_exists = check_coordinate_exists(coordinates_matrix, cube.fx, cube.fy + 1, cube.fz);
if(coordinate_exists)
{
has_valid_id = (coordinates_matrix[cube.fx][cube.fy + 1][cube.fz] != -1);
/*
Movements (orange) cannot connect on hardware plain to USE_QUBIT(red)
*/
if(current_operation_color == "orange")
{
if (coordinates_matrix[cube.fx][cube.fy + 1][cube.fz].includes("OperationTypes.USE_QUBIT"))
{
allow_extension_hardware = false;
}
}
if(current_operation_color == "yellow")
{
if (! coordinates_matrix[cube.fx][cube.fy + 1][cube.fz].includes("OperationTypes.USE_ANCILLA"))
{
allow_extension_hardware = false;
}
}
}
else
{
has_valid_id = false;
}
if(coordinate_exists && has_valid_id && allow_extension_hardware)
{
var n_offset = Array();
n_offset = offset.slice();
n_offset[1] += dim2/2 + dim*(1 - scale_factor)/2;
construct_cuboid(dim1, dim*(1 - scale_factor), dim3, n_offset, sides, color);
}
var allow_extension_time = !(["yellow", "orange"].includes(current_operation_color))
coordinate_exists = check_coordinate_exists(coordinates_matrix, cube.fx, cube.fy, cube.fz + 1);
if(coordinate_exists)
{
has_valid_id = (coordinates_matrix[cube.fx][cube.fy][cube.fz + 1] != -1);
if(current_operation_color == "orange")
{
if (coordinates_matrix[cube.fx][cube.fy][cube.fz + 1].includes("OperationTypes.USE_QUBIT"))
{
allow_extension_time = true;
}
if (coordinates_matrix[cube.fx][cube.fy][cube.fz + 1].includes("OperationTypes.MOVE_PATCH"))
{
// connect orange-orange
allow_extension_time = true;
}
}
}
else
{
has_valid_id = false;
}
if(coordinate_exists && has_valid_id && allow_extension_time)
{
var n_offset = Array();
n_offset = offset.slice();
n_offset[2] += dim3/2 + dim*(1 - scale_factor)/2;
construct_cuboid(dim1, dim2, dim*(1 - scale_factor), n_offset, sides, color);
}
}
}
function process_link_to_mesh(link)
{
if(!boxes_collection_has_color("links"))
boxes_collection_add_color("links")
boxes_collection["links"].addBox(63, offset /*position*/, [dim1, dim2, dim3] /*dimensions*/);
}
function reload_visualisation(processed_json)
{
// reload JSON
// Graph.graphData(processed_json);
// surgery_visualiser.start_surgery_view(boxes_collection, boxes_collection.keys());
// clean the collection of geometries
boxes_collection_clean();
/*
Collect the 3D objects from the JSON
*/
for (var i = 0; i < processed_json["nodes"].length; i++)
{
process_node_to_mesh(processed_json["nodes"][i]);
}
// /*
// Construct the links where merges and splits are made
// */
// for(var i = 0; i < processed_json["links"].length; i++)
// {
// process_link_to_mesh(processed_json["links"][i])
// }
surgery_visualiser.start_surgery_view(boxes_collection, Object.keys(boxes_collection));
}
function filterEdgeOnColor(color) {
// make active or inactive
active_link_colors[color] = !active_link_colors[color];
// console.log("filter " + color + " " + active_link_colors[color]);
// for all colors generate an object
// only with the ones which are active (True)
processed_json = {};
processed_json.nodes = loaded_json.nodes;
processed_json.links = [];
for (var i = 0; i < loaded_json["links"].length; i++) {
if (loaded_json.links[i].c == color || color == "all") {
processed_json.links.push(loaded_json.links[i]);
}
}
reload_visualisation(processed_json);
}
function filterNodeOnColor(color) {
// make active or inactive
active_link_colors[color] = !active_link_colors[color];
processed_json = {};
//an empty set of nodes
//add only nodes of specific color
active_nodes = {};
processed_json.nodes = [];
for (var i = 0; i < loaded_json["nodes"].length; i++) {
if (loaded_json.nodes[i].c == color || color == "all") {
processed_json.nodes.push(loaded_json.nodes[i]);
active_nodes[loaded_json.nodes[i].id] = true;
}
}
// add links only between nodes which exist in the collection
processed_json.links = [];
for (var i = 0; i < loaded_json["links"].length; i++) {
var key_is_in = loaded_json.links[i].source.id in active_nodes;
key_is_in = key_is_in && loaded_json.links[i].target.id in active_nodes;
if (key_is_in) {
processed_json.links.push(loaded_json.links[i]);
}
}
reload_visualisation(processed_json);
}
function filterNodeOnTime(time) {
processed_json = {};
//an empty set of nodes
//add only nodes of specific time coordinate
active_nodes = {};
processed_json.nodes = [];
for (var i = 0; i < loaded_json["nodes"].length; i++) {
if (loaded_json.nodes[i].fz <= time) {
processed_json.nodes.push(loaded_json.nodes[i]);
active_nodes[loaded_json.nodes[i].id] = true;
}
}
// add links only between nodes which exist in the collection
processed_json.links = [];
for (var i = 0; i < loaded_json["links"].length; i++) {
var key_is_in = loaded_json.links[i].source.id in active_nodes;
key_is_in = key_is_in && loaded_json.links[i].target.id in active_nodes;
if (key_is_in) {
processed_json.links.push(loaded_json.links[i]);
}
}
reload_visualisation(processed_json);
}
function filterNodeOnOp(op_number) {
processed_json = {};
//an empty set of nodes
//add only nodes of specific time coordinate
active_nodes = {};
processed_json.nodes = [];
for (var i = 0; i < loaded_json["nodes"].length; i++) {
if (loaded_json.nodes[i].op == op_number || op_number == "-1") {
processed_json.nodes.push(loaded_json.nodes[i]);
active_nodes[loaded_json.nodes[i].id] = true;
}
}
// add links only between nodes which exist in the collection
processed_json.links = [];
for (var i = 0; i < loaded_json["links"].length; i++) {
var key_is_in = loaded_json.links[i].source.id in active_nodes;
key_is_in = key_is_in && loaded_json.links[i].target.id in active_nodes;
if (key_is_in) {
processed_json.links.push(loaded_json.links[i]);
}
}
reload_visualisation(processed_json);
}
function addControlColor(color, where_id) {
var btn = document.createElement("button");
var t = document.createTextNode(".....");
if (color == "all")
t.textContent = "All"
btn.appendChild(t);
if (color != "all")
btn.style["background-color"] = color;
if (where_id === "edge_buttons")
btn.onclick = function () { filterEdgeOnColor(color) };
else if (where_id === "node_buttons")
btn.onclick = function () { filterNodeOnColor(color) };
document.getElementById(where_id).appendChild(btn);
}
function counterOnChange() {
// reset active colors
active_link_colors = {};
active_node_colors = {};
var myNode = document.getElementById('edge_buttons');
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
var myNode = document.getElementById('node_buttons');
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
var filename = 'layout.json';
console.log("fetch " + filename);
fetch(filename)
.then(function (response) {
return response.json();
})
.then(function (myJson) {
//save to global var?
loaded_json = myJson;
for (var i = 0; i < myJson["nodes"].length; i++)
{
var node = myJson["nodes"][i];
add_coordinates_as_keys(coordinates_matrix, node.fx, node.fy, node.fz);
coordinates_matrix[node.fx][node.fy][node.fz] = node.op;
}
// reload_visualisation(loaded_json);
for (var i = 0; i < myJson["links"].length; i++) {
var curr_link = myJson.links[i];
if (!(curr_link.c in active_link_colors)) {
active_link_colors[curr_link.c] = true;
addControlColor(curr_link.c, "edge_buttons");
}
}
operation_ids = {}
var min_time = 10000;
var max_time = -1000;
for (var i = 0; i < myJson["nodes"].length; i++) {
//collect operation ids
if (!(myJson.nodes[i].op in operation_ids))
{
operation_ids[myJson.nodes[i].op] = myJson.nodes[i].op;
}
var curr_node = myJson.nodes[i];
if (!(curr_node.c in active_node_colors)) {
active_node_colors[curr_node.c] = true;
addControlColor(curr_node.c, "node_buttons");
}
if (min_time > curr_node.fz) {
min_time = curr_node.fz;
}
if (max_time < curr_node.fz) {
max_time = curr_node.fz;
}
}
/*merge geometries for cubes with the same operation ID*/
//add a button to view all again
addControlColor("all", "edge_buttons");
addControlColor("all", "node_buttons");
document.getElementById("number_time").min = min_time;
document.getElementById("number_time").max = max_time;
document.getElementById("number_time").value = max_time;
var select_html = document.getElementById("operation_id")
for (var key in operation_ids)
{
var opt = document.createElement("option");
opt.value = key;
var txt = document.createTextNode(key + "");
opt.appendChild(txt);
select_html.appendChild(opt);
}
reload_visualisation(loaded_json);
});
}
function construct_cuboid(dim1, dim2, dim3, offset, sides, color)
{
// console.log("construct")
if(!boxes_collection_has_color(color))
boxes_collection_add_color(color)
if(!boxes_collection_has_color("black"))
boxes_collection_add_color("black")
if(!boxes_collection_has_color("wireframe"))
boxes_collection_add_color("wireframe")
boxes_collection[color].addBox(sides, offset /*position*/, [dim1, dim2, dim3] /*dimensions*/);
boxes_collection["wireframe"].addBox(63, offset /*position*/, [dim1, dim2, dim3] /*dimensions*/);
boxes_collection["black"].addBox(63 - sides, offset /*position*/, [dim1, dim2, dim3] /*dimensions*/);
}
</script>
</head>
<body>
<div id="graphcounter">
Graph number
<input type="number" min="0" max="1000" step="1" value="0" onchange="counterOnChange(this.value)">
Time Coordinate
<input type="number" id="number_time" min="0" max="1000" step="1" value="0" onchange="filterNodeOnTime(this.value)">
Operation Id:
<select id="operation_id" onchange="filterNodeOnOp(this.value)">
<option value="-1">All</option>
</select>
</div>
<div id="controlpanel">
View Edge <div id="edge_buttons"></div>
View Node <div id="node_buttons"></div>
</div>
<div id="3d-surgery" style="border-style: solid; border-width:3px;"></div>
<script>
var active_link_colors = {};
var active_node_colors = {};
/*
A global variable to hold the loaded JSON
*/
var loaded_json = null;
/*
The JSON is processed into an array of cells
-> I am reconstructing the data structure used in the Python code
*/
var coordinates_matrix = {};
/*
*/
var boxes_collection = {};
var surgery_visualiser = new SurgeryWebGLVisualiser();
surgery_visualiser.setup_visualisation('3d-surgery');
counterOnChange();
</script>
</body>