forked from JavaFXpert/quantum-music-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqasm_pad.js
2496 lines (2194 loc) · 90.9 KB
/
qasm_pad.js
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Quantum DJ device circuit pad that may be used even when
* a Push 2 device is not connected.
*
* TODO: Implement one-shot (non-looping) clips
* TODO: Implement undo button
* TODO: Fix or remove padsToBlink functionality
* TODO: Verify that musical modes are implemented correctly
* TODO: Identify color scheme that accommodate pi/8
* TODO: Inquire surface number for Push
* TODO: Fix Swap gates, and implement controlled-Swap
* TODO: Verify that fixing swap gates fixed QFTs
* TODO: Clear Push pad when switching between Note and Session
* TODO: Use On/Off graphic on Push?
* TODO: Implement rule-based composition constraints, e.g.:
* - Ending on same note as beginning
* - Removing notes with LSB 111 in a four wire circuit
* - Penultimate note is 7, 1 or 2
* TODO: Populate textbox with QASM for circuit
* - Test result after applying column of Hadamards
*/
// Lowest MIDI pitch on grid
var LOW_MIDI_PITCH = 36;
// Dimensions of controller pad matrix
var CONTR_MAT_ROWS = 8;
var CONTR_MAT_COLS = 8;
// Number of circuit grids
var NUM_GRIDS = 2;
// Controller pad rows and columns reserved for circuit
var NUM_GRID_ROWS = 8;
var NUM_GRID_COLS = 6;
// Number of controller pads reserved for the circuit
var NUM_GRID_CELLS = NUM_GRID_ROWS * NUM_GRID_COLS;
// Controller pad columns reserved for gates
var NUM_GATE_COLS = 2;
// Resolution of calculation from phase to notes or sounds in a kit.
// Also represents resolution of phase.
var NUM_PITCHES = 16;
// Lowest MIDI value of drum pad
var LOW_DRUMPAD_MIDI = 36;
// Maximum number of drum pads
var MAX_DRUMPADS = 16;
// Minimum number of qubits in a circuit
var MIN_CIRCUIT_WIRES = 2;
// Position of circuit grid panel
var CG_PANEL_POS_X = 282;
var CG_PANEL_POS_Y = 3;
var CG_PANEL_HEIGHT = 164;
// Minimum number of visible columns in circuit grid
var MIN_VISIBLE_CIRCUIT_COLS = 6;
// Circuit grid position info
var CG_GRID_POS_X = 302.0;
var CG_GRID_POS_Y = 5.0;
var CG_GATE_WIDTH = 23.0;
var CG_GATE_HEIGHT = 20.0;
// Spacing between rightmost visible circuit grid column and toolbox gates
var CG_TOOLBOX_HORIZ_PADDING = 10;
// Number of tools in gates toolbox
var TOOL_WIDTH = 22;
// Number of tools in gates toolbox
var NUM_TOOLS = 16;
// Number of tools in each row
var NUM_TOOLS_PER_ROW = 2;
// Pitch text positions
var PITCH_TEXT_TOP_POS_Y = 3;
var PITCH_TEXT_WIDTH = 58;
var PITCH_TEXT_HEIGHT = 10;
// Statevector grid position X (other are defined elsewhere)
var svGridPosX = 547.0;
// Circuit node types
var CircuitNodeTypes = {
EMPTY: -1,
HAND: 0,
ERASE: 1,
H: 8,
SWAP: 9,
BARRIER: 10,
CTRL: 11, // "control" part of multi-qubit gate
ANTI_CTRL: 12, // "anti-control" part of multi-qubit gate
TRACE: 13, // In the path between a gate part and a "control" or "swap" part
MEASURE_Z: 14,
IDEN: 15,
CTRL_X: 21, // X gate that is associated with control qubit(s)
RX_0: 30, // Rx
RX_1: 31, // Rx pi/8
RX_2: 32, // Rx pi/4
RX_3: 33, // Rx 3pi/8
RX_4: 34, // Rx pi/2
RX_5: 35, // Rx 5pi/8
RX_6: 36, // Rx 3pi/4
RX_7: 37, // Rx 7pi/8
RX_8: 38, // Rx pi (X)
RX_9: 39, // Rx 9pi/8
RX_10: 40, // Rx 5pi/4
RX_11: 41, // Rx 11pi/8
RX_12: 42, // Rx 3pi/2
RX_13: 43, // Rx 13pi/8
RX_14: 44, // Rx 7pi/4
RX_15: 45, // Rx 15pi/8
RY_0: 50, // Ry
RY_1: 51, // Ry pi/8
RY_2: 52, // Ry pi/4
RY_3: 53, // Ry 3pi/8
RY_4: 54, // Ry pi/2
RY_5: 55, // Ry 5pi/8
RY_6: 56, // Ry 3pi/4
RY_7: 57, // Ry 7pi/8
RY_8: 58, // Ry pi (Y)
RY_9: 59, // Ry 9pi/8
RY_10: 60, // Ry 5pi/4
RY_11: 61, // Ry 11pi/8
RY_12: 62, // Ry 3pi/2
RY_13: 63, // Ry 13pi/8
RY_14: 64, // Ry 7pi/4
RY_15: 65, // Ry 15pi/8
PHASE_0: 70, // Phase
PHASE_1: 71, // Phase pi/8
PHASE_2: 72, // Phase pi/4 (T)
PHASE_3: 73, // Phase 3pi/8
PHASE_4: 74, // Phase pi/2 (S)
PHASE_5: 75, // Phase 5pi/8
PHASE_6: 76, // Phase 3pi/4
PHASE_7: 77, // Phase 7pi/8
PHASE_8: 78, // Phase pi (Z)
PHASE_9: 79, // Phase 9pi/8
PHASE_10: 80, // Phase 5pi/4
PHASE_11: 81, // Phase 11pi/8
PHASE_12: 82, // Phase 3pi/2 (Sdg)
PHASE_13: 83, // Phase 13pi/8
PHASE_14: 84, // Phase 7pi/4 (Tdg)
PHASE_15: 85, // Phase 15pi/8
QFT: 90 // QFT
};
// Inlet 0 receives note messages that include velocity.
// Inlet 1 receives bang message to update clips
// Inlet 2 receives gate rotation messages
// Inlet 3 receives shift gates down messages
// Inlet 4 receives shift gates up messages
// Inlet 5 receives shift gates right messages
// Inlet 6 receives shift gates left messages
// Inlet 7 receives selected grid (0 or 1) messages
this.inlets = 8;
setinletassist(0, "receives note messages that include velocity.");
setinletassist(1, "receives bang message to update clips");
setinletassist(2, "receives gate rotation messages");
setinletassist(3, "receives shift gates down messages");
setinletassist(4, "receives shift gates up messages");
setinletassist(5, "receives shift gates right messages");
setinletassist(6, "receives shift gates left messages");
setinletassist(7, "receives selected grid (0 or 1) messages");
// Outlet 0 sends message to a simulator with generated QASM
// Outlet 1 sends messages to the midi clips list box
// Outlet 2 sends messages to the clip selector dial
// Outlet 3 sends messages to the gate rotator dial
// Outlet 4 sends messages to the grid selection dial
this.outlets = 5;
setoutletassist(0, "sends message to a simulator with generated QASM");
setoutletassist(1, "sends messages to the midi clips list box");
setoutletassist(2, "sends messages to the clip selector dial");
setoutletassist(3, "sends messages to the gate rotator dial");
setoutletassist(4, "sends messages to the grid selection dial");
// Flag that indicates whether the currently displayed pads/notes
// are dirty.
var padNoteNamesDirty = true;
// Flag that tracks whether the circuit should be cleared
// when the CircuitNodeTypes.EMPTY key is net pressed
var clearCircuitWhenEmptyKeyNextPressed = false;
var curCircNodeType = CircuitNodeTypes.HAND;
var highMidiPitch = (NUM_GRID_ROWS - 1) * CONTR_MAT_COLS + NUM_GRID_COLS + LOW_MIDI_PITCH - 1;
// TODO: Allocate the array and call refreshPadNoteNames method?
var padNoteNames = [];
refreshPadNoteNames();
// TODO: Dynamically initialize this array
var circGrid = [
[
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1]
],
[
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1]
]
];
var gateGrid = [
[CircuitNodeTypes.H, CircuitNodeTypes.PHASE_2],
[CircuitNodeTypes.RX_8, CircuitNodeTypes.PHASE_4],
[CircuitNodeTypes.RY_8, CircuitNodeTypes.PHASE_8],
[CircuitNodeTypes.CTRL, CircuitNodeTypes.PHASE_12],
[CircuitNodeTypes.ANTI_CTRL, CircuitNodeTypes.PHASE_14],
[CircuitNodeTypes.IDEN, CircuitNodeTypes.EMPTY],
[CircuitNodeTypes.QFT, CircuitNodeTypes.HAND],
[CircuitNodeTypes.SWAP, CircuitNodeTypes.ERASE]
];
// Index of currently selected grid
var selCircGridNum = 0;
// Currently selected row/column on currently selected grid
var selCircGridRow = -1;
var selCircGridCol = -1;
// Current theme control_fg color
var controlFgColor = [0., 0., 0., 1.];
// Associates clip name to path
var clipsPaths = [];
// Array contain midi values of pads to blink
var padsToBlink = [];
// Tracks number of consecutive QFT gates in a column
var numConsecutiveQftRowsInCol = 0;
sketch.default2d();
var val = 0;
var vbrgb = [1., 1., 1., 1.];
var last_x = 0;
var last_y = 0;
resetCircGrid();
draw();
refresh();
/**
* Represents a control or anti-control, and the wire
* on which it is present
* @param wireNumArg
* @param isAntiCtrlArg
* @constructor
*/
function ControlWire(wireNumArg, isAntiCtrlArg) {
this.wireNum = wireNumArg;
this.isAntiCtrl = isAntiCtrlArg;
}
function bang() {
if (inlet == 1) {
// bang received to refresh list of clips
populateMidiClipsList();
setSelCircGridNum(0);
outlet(4, 'int', selCircGridNum);
}
else if (inlet == 3) {
// bang received to shift all gates down
shiftAllGatesVertically(true);
}
else if (inlet == 4) {
// bang received to shift all gates up
shiftAllGatesVertically(false);
}
else if (inlet == 5) {
// bang received to shift all gates right
shiftAllGatesHorizontally(true);
}
else if (inlet == 6) {
// bang received to shift all gates left
shiftAllGatesHorizontally(false);
}
}
function msg_int(val) {
if (inlet == 2) {
var piOver8Rotation = val;
if (selCircGridRow >= 0 &&
selCircGridRow < NUM_GRID_ROWS &&
selCircGridCol >= 0 &&
selCircGridCol < NUM_GRID_COLS) {
var selNodeType = circGrid[selCircGridNum][selCircGridRow][selCircGridCol];
var newNodeType = CircuitNodeTypes.EMPTY;
var rotateGateSelected = true;
if ((selNodeType >= CircuitNodeTypes.RX_0 && selNodeType <= CircuitNodeTypes.RX_15) ||
selNodeType == CircuitNodeTypes.CTRL_X) {
newNodeType = CircuitNodeTypes.RX_0 + piOver8Rotation;
}
else if (selNodeType >= CircuitNodeTypes.RY_0 &&
selNodeType <= CircuitNodeTypes.RY_15) {
newNodeType = CircuitNodeTypes.RY_0 + piOver8Rotation;
}
else if (selNodeType >= CircuitNodeTypes.PHASE_0 &&
selNodeType <= CircuitNodeTypes.PHASE_15) {
newNodeType = CircuitNodeTypes.PHASE_0 + piOver8Rotation;
}
else {
var rotateGateSelected = false;
}
// Conditionally enable rotate gate dial
enableRotateGateDial(rotateGateSelected);
if (newNodeType != CircuitNodeTypes.EMPTY) {
circGrid[selCircGridNum][selCircGridRow][selCircGridCol] = newNodeType;
refreshCircGrid();
createQasmFromGrid();
}
}
}
else if (inlet == 7) {
setSelCircGridNum(val);
refreshControllerPads();
}
}
function control_fg(themeInfo) {
if (inlet == 4) {
controlFgColor = [arguments[0], arguments[1], arguments[2], arguments[3]];
refreshCircGrid();
}
}
function surface_bg(themeInfo) {
// TODO: Remove a device wire?
}
function getPathByClipNameIdx(clipNameIdx) {
if (clipNameIdx < clipsPaths.length) {
var caratPos = clipsPaths[clipNameIdx].indexOf('^');
if (caratPos > 0) {
var clipPath = clipsPaths[clipNameIdx].substring(caratPos + 1);
return clipPath;
}
else {
return "";
}
}
else {
return "";
}
}
function list(lst) {
if (inlet == 0) {
setCircGridGate(arguments);
}
}
function setCurCircNodeType(circuitNodeType) {
curCircNodeType = circuitNodeType;
}
function enableRotateGateDial(enableArg) {
// Conditionally disable rotate gate dial
var rotateGateDial = this.patcher.getnamed("rotate_gate");
rotateGateDial.setattr('ignoreclick', !enableArg);
var tc = rotateGateDial.getattr('textcolor', 1, 1, 1, 1);
var alpha = enableArg ? 1 : 0.2;
rotateGateDial.setattr('textcolor', tc[0], tc[1], tc[2], alpha);
rotateGateDial.setattr('slidercolor', tc[0], tc[1], tc[2], alpha);
rotateGateDial.setattr('tribordercolor', tc[0], tc[1], tc[2], alpha);
}
function setSelCircGridNum(gridNumIdx) {
//post('\nSetting selCircGridNum from: ' + selCircGridNum + ' to: ' + gridNumIdx);
selCircGridNum = gridNumIdx;
}
/**
* Set all elements to EMPTY
*/
function resetCircGrid() {
for (gridIdx = 0; gridIdx < NUM_GRIDS; gridIdx++) {
for (rowIdx = 0; rowIdx < NUM_GRID_ROWS; rowIdx++) {
for (colIdx = 0; colIdx < NUM_GRID_COLS; colIdx++) {
circGrid[gridIdx][rowIdx][colIdx] = CircuitNodeTypes.EMPTY;
selCircGridRow = -1;
selCircGridCol = -1;
informCircuitBtn(gridIdx, rowIdx, colIdx);
}
}
}
}
/**
* Refresh circuit button, for example after a theme change
*/
function refreshCircGrid() {
for (var gridIdx = 0; gridIdx < NUM_GRIDS; gridIdx++) {
for (var rowIdx = 0; rowIdx < NUM_GRID_ROWS; rowIdx++) {
for (var colIdx = 0; colIdx < NUM_GRID_COLS; colIdx++) {
informCircuitBtn(gridIdx, rowIdx, colIdx);
}
}
}
repositionToolboxGates();
}
function shiftAllGatesVertically(shiftDown) {
if (shiftDown) {
if (rowIsEmpty(NUM_GRID_ROWS - 1)) {
for (var gridIdx = 0; gridIdx < NUM_GRIDS; gridIdx++) {
for (var rowIdx = NUM_GRID_ROWS - 2; rowIdx >= 0; rowIdx--) {
for (var colIdx = 0; colIdx < NUM_GRID_COLS; colIdx++) {
circGrid[gridIdx][rowIdx + 1][colIdx] = circGrid[gridIdx][rowIdx][colIdx];
circGrid[gridIdx][rowIdx][colIdx] = CircuitNodeTypes.EMPTY;
selCircGridRow = -1;
selCircGridCol = -1;
informCircuitBtn(gridIdx, rowIdx, colIdx);
informCircuitBtn(gridIdx, rowIdx + 1, colIdx);
}
}
}
}
}
else {
if (rowIsEmpty(0)) {
for (var gridIdx = 0; gridIdx < NUM_GRIDS; gridIdx++) {
for (var rowIdx = 1; rowIdx < NUM_GRID_ROWS; rowIdx++) {
for (var colIdx = 0; colIdx < NUM_GRID_COLS; colIdx++) {
circGrid[gridIdx][rowIdx - 1][colIdx] = circGrid[gridIdx][rowIdx][colIdx];
circGrid[gridIdx][rowIdx][colIdx] = CircuitNodeTypes.EMPTY;
selCircGridRow = -1;
selCircGridCol = -1;
informCircuitBtn(gridIdx, rowIdx - 1, colIdx);
informCircuitBtn(gridIdx, rowIdx, colIdx);
}
}
}
}
}
createQasmFromGrid();
}
function shiftAllGatesHorizontally(shiftRight) {
if (shiftRight) {
if (colIsEmpty((NUM_GRID_COLS * NUM_GRIDS) - 1)) {
for (var gridIdx = NUM_GRIDS - 1; gridIdx >= 0; gridIdx--) {
for (var colIdx = NUM_GRID_COLS - 1; colIdx >= 0; colIdx--) {
for (var rowIdx = 0; rowIdx < NUM_GRID_ROWS; rowIdx++) {
if (gridIdx == 1 && colIdx == NUM_GRID_COLS - 1) {
// Don't shift right
}
else if (gridIdx == 0 && colIdx == NUM_GRID_COLS - 1) {
// Shift to first column of next grid
circGrid[1][rowIdx][0] = circGrid[0][rowIdx][colIdx];
informCircuitBtn(1, rowIdx, 0);
}
else {
circGrid[gridIdx][rowIdx][colIdx + 1] = circGrid[gridIdx][rowIdx][colIdx];
informCircuitBtn(gridIdx, rowIdx, colIdx + 1);
}
if (gridIdx == 0 && colIdx == 0) {
circGrid[0][rowIdx][0] = CircuitNodeTypes.EMPTY;
informCircuitBtn(0, rowIdx, 0);
}
selCircGridRow = -1;
selCircGridCol = -1;
}
}
}
}
}
else {
if (colIsEmpty(0)) {
for (var gridIdx = 0; gridIdx < NUM_GRIDS; gridIdx++) {
for (var colIdx = 0; colIdx < NUM_GRID_COLS; colIdx++) {
for (var rowIdx = 0; rowIdx < NUM_GRID_ROWS; rowIdx++) {
if (gridIdx == 0 && colIdx == 0) {
// Don't shift left
}
else if (gridIdx == 1 && colIdx == 0) {
// Shift to last column of previous grid
circGrid[0][rowIdx][NUM_GRID_COLS - 1] = circGrid[1][rowIdx][0];
informCircuitBtn(0, rowIdx, NUM_GRID_COLS - 1);
}
else {
circGrid[gridIdx][rowIdx][colIdx - 1] = circGrid[gridIdx][rowIdx][colIdx];
informCircuitBtn(gridIdx, rowIdx, colIdx - 1);
}
if (gridIdx == 1 && colIdx == NUM_GRID_COLS - 1) {
circGrid[1][rowIdx][NUM_GRID_COLS - 1] = CircuitNodeTypes.EMPTY;
informCircuitBtn(1, rowIdx, NUM_GRID_COLS - 1);
}
selCircGridRow = -1;
selCircGridCol = -1;
}
}
}
}
}
refreshCircGrid();
createQasmFromGrid();
}
function rowIsEmpty(rowIdx) {
var rowEmpty = true;
for (var gridIdx = 0; gridIdx < NUM_GRIDS; gridIdx++) {
for (var colIdx = 0; colIdx < NUM_GRID_COLS; colIdx++) {
if (circGrid[gridIdx][rowIdx][colIdx] != CircuitNodeTypes.EMPTY) {
rowEmpty = false;
break;
}
}
}
return rowEmpty;
}
function colIsEmpty(colIdx) {
//post('\ncolIdx: ' + colIdx);
var gridIdx = 0;
if (colIdx >= NUM_GRID_COLS) {
colIdx = colIdx - NUM_GRID_COLS;
gridIdx = 1;
}
var colEmpty = true;
for (var rowIdx = 0; rowIdx < NUM_GRID_ROWS; rowIdx++) {
if (circGrid[gridIdx][rowIdx][colIdx] != CircuitNodeTypes.EMPTY) {
colEmpty = false;
break;
}
}
//post(', colEmpty: ' + colEmpty);
return colEmpty;
}
function lowestOccupiedRow() {
var retLowestOccupiedRow = -1;
for (var rowIdx = 0; rowIdx < NUM_GRID_ROWS; rowIdx++) {
if (!rowIsEmpty(rowIdx)) {
retLowestOccupiedRow = rowIdx;
break;
}
}
return retLowestOccupiedRow;
}
function highestOccupiedCol() {
var retHighestOccupiedCol = -1;
for (var gridIdx = NUM_GRIDS - 1; gridIdx >= 0; gridIdx--) {
for (var colIdx = NUM_GRID_COLS - 1; colIdx >= 0; colIdx--) {
if (!colIsEmpty(gridIdx * NUM_GRID_COLS + colIdx)) {
retHighestOccupiedCol = gridIdx * NUM_GRID_COLS + colIdx;
return retHighestOccupiedCol;
}
}
}
return retHighestOccupiedCol;
}
function repositionToolboxGates() {
// Circuit grid column after which toolbox gates should appear
var cgColIdx = Math.min(Math.max(MIN_VISIBLE_CIRCUIT_COLS - 2, highestOccupiedCol()),
NUM_GRID_COLS * NUM_GRIDS - 2);
var toolboxPosX = CG_GRID_POS_X + ((cgColIdx + 2) * CG_GATE_WIDTH) + CG_TOOLBOX_HORIZ_PADDING;
// Position the circuit grid panel
var circPanelObj = this.patcher.getnamed('circ_panel');
circPanelObj.setattr('presentation_position', CG_PANEL_POS_X, CG_PANEL_POS_Y);
circPanelObj.setattr('presentation_size',
toolboxPosX + (TOOL_WIDTH * NUM_TOOLS_PER_ROW) - CG_PANEL_POS_X,
CG_PANEL_HEIGHT);
// Position the toolbox operation
for (var toolIdx = 0; toolIdx < NUM_TOOLS; toolIdx++) {
var toolObj = this.patcher.getnamed('gate_' + toolIdx);
var offsetX = (toolIdx % NUM_TOOLS_PER_ROW) * TOOL_WIDTH;
var offsetY = Math.floor(toolIdx / NUM_TOOLS_PER_ROW) * CG_GATE_HEIGHT;
toolObj.setattr('presentation_position', toolboxPosX + offsetX, CG_GRID_POS_Y + offsetY);
}
// Position the pitch labels
var pitchTextPosX = toolboxPosX + (TOOL_WIDTH * NUM_TOOLS_PER_ROW);
for (var pitchIdx = NUM_PITCHES - 1; pitchIdx >= 0; pitchIdx--) {
var padNoteObj = this.patcher.getnamed('pad_note[' + pitchIdx + ']');
padNoteObj.setattr('presentation_position',
pitchTextPosX,
PITCH_TEXT_TOP_POS_Y + ((NUM_PITCHES - 1 - pitchIdx) * PITCH_TEXT_HEIGHT));
}
// Position the statevector musical
svGridPosX = pitchTextPosX + PITCH_TEXT_WIDTH;
}
/**
* Given an array with midi pitch and velocity,
* populates the corresponding circuit grid element
*
* @param notePitchVelocity Array containing midi pitch and velocity
*/
function setCircGridGate(notePitchVelocity) {
if (notePitchVelocity.length >= 2) {
var pitch = notePitchVelocity[0];
var velocity = notePitchVelocity[1];
// Only process noteup events (when user releases controller button)
if (velocity > 0) {
return;
}
if (pitch >= LOW_MIDI_PITCH && pitch <= highMidiPitch + 4) {
var gridRow = Math.floor((highMidiPitch - pitch) / CONTR_MAT_COLS);
var gridCol = (highMidiPitch - pitch) % CONTR_MAT_COLS;
if (gridCol >= 0 && gridCol < NUM_GRID_COLS) {
gridCol = NUM_GRID_COLS - gridCol - 1;
// User is selecting on the circuit
outlet(4, 'int', selCircGridNum);
clearCircuitWhenEmptyKeyNextPressed = false;
selCircGridRow = gridRow;
selCircGridCol = gridCol;
if (curCircNodeType != CircuitNodeTypes.HAND &&
(circGrid[selCircGridNum][gridRow][gridCol] == CircuitNodeTypes.EMPTY ||
curCircNodeType == CircuitNodeTypes.EMPTY)) {
circGrid[selCircGridNum][gridRow][gridCol] = curCircNodeType;
}
else {
//post('\nGate already present');
}
var newPiOver8Rotation = 0;
if (circGrid[selCircGridNum][gridRow][gridCol] == CircuitNodeTypes.CTRL_X) {
newPiOver8Rotation = NUM_PITCHES / 2;
}
else if (circGrid[selCircGridNum][gridRow][gridCol] >= CircuitNodeTypes.RX_0 &&
circGrid[selCircGridNum][gridRow][gridCol] <= CircuitNodeTypes.RX_15) {
newPiOver8Rotation = circGrid[selCircGridNum][gridRow][gridCol] - CircuitNodeTypes.RX_0;
}
else if (circGrid[selCircGridNum][gridRow][gridCol] >= CircuitNodeTypes.RY_0 &&
circGrid[selCircGridNum][gridRow][gridCol] <= CircuitNodeTypes.RY_15) {
newPiOver8Rotation = circGrid[selCircGridNum][gridRow][gridCol] - CircuitNodeTypes.RY_0;
}
else if (circGrid[selCircGridNum][gridRow][gridCol] >= CircuitNodeTypes.PHASE_0 &&
circGrid[selCircGridNum][gridRow][gridCol] <= CircuitNodeTypes.PHASE_15) {
newPiOver8Rotation = circGrid[selCircGridNum][gridRow][gridCol] - CircuitNodeTypes.PHASE_0;
}
// Set the current rotation on the gate rotator dial
outlet(3, 'int', newPiOver8Rotation);
refreshCircGrid();
//informCircuitBtn(gridRow, gridCol);
createQasmFromGrid();
}
else {
// User is choosing a gate
if (pitch == 43) {
curCircNodeType = CircuitNodeTypes.EMPTY;
if (clearCircuitWhenEmptyKeyNextPressed) {
resetCircGrid();
createQasmFromGrid();
clearCircuitWhenEmptyKeyNextPressed = false;
}
else {
// TODO: Uncomment next line after making it not easy to accidentally clear the circuit
//clearCircuitWhenEmptyKeyNextPressed = true;
}
}
else if (pitch == 51) {
curCircNodeType = CircuitNodeTypes.HAND;
}
else {
clearCircuitWhenEmptyKeyNextPressed = false;
if (pitch == 98) {
curCircNodeType = CircuitNodeTypes.H;
}
else if (pitch == 99) {
curCircNodeType = CircuitNodeTypes.PHASE_2;
}
else if (pitch == 90) {
curCircNodeType = CircuitNodeTypes.RX_8;
}
else if (pitch == 91) {
curCircNodeType = CircuitNodeTypes.PHASE_4;
}
else if (pitch == 82) {
curCircNodeType = CircuitNodeTypes.RY_8;
}
else if (pitch == 83) {
curCircNodeType = CircuitNodeTypes.PHASE_8;
}
else if (pitch == 74) {
curCircNodeType = CircuitNodeTypes.CTRL;
}
else if (pitch == 75) {
curCircNodeType = CircuitNodeTypes.PHASE_12;
}
else if (pitch == 66) {
curCircNodeType = CircuitNodeTypes.ANTI_CTRL;
}
else if (pitch == 67) {
curCircNodeType = CircuitNodeTypes.PHASE_14;
}
else if (pitch == 58) {
curCircNodeType = CircuitNodeTypes.IDEN;
}
else if (pitch == 50) {
curCircNodeType = CircuitNodeTypes.QFT;
}
else if (pitch == 42) {
curCircNodeType = CircuitNodeTypes.SWAP;
}
}
refreshControllerPads();
}
}
}
else {
post('Unexpected notePitchVelocity.length: ' + notePitchVelocity.length);
}
}
/**
* Analyze the circuit grid and create QASM code, sending
* a statevector simulator message to an outlet.
*/
function createQasmFromGrid() {
padsToBlink = [];
var numCircuitWires = computeNumWires();
var qasmHeaderStr = 'qreg q[' + numCircuitWires + '];' + ' creg c[' + numCircuitWires + '];';
var qasmGatesStr = '';
for (var gridIdx = 0; gridIdx < NUM_GRIDS; gridIdx++) {
for (var colIdx = 0; colIdx < NUM_GRID_COLS; colIdx++) {
numConsecutiveQftRowsInCol = 0;
for (var rowIdx = 0; rowIdx < numCircuitWires; rowIdx++) {
qasmGatesStr = addGateFromGrid(qasmGatesStr, gridIdx, rowIdx, colIdx);
}
}
}
// If circuit is empty, add an identity gate
if (qasmGatesStr.trim().length == 0) {
qasmGatesStr = ' id q[0];'
}
qasm = qasmHeaderStr + qasmGatesStr;
refreshControllerPads();
// Send statevector simulator message with QASM to outlet
outlet(0, 'svsim', qasm);
}
/**
* Creates a quantum gate from an element in the circuit grid
* and adds it to the supplied QuantumCircuit instance
*
* @param qasmStr Current QASM string
* @param gridRow Zero-based row number on circuit grid
* @param gridCol Zero-based column number on circuit grid
* @returns QASM string for the gate
*/
function addGateFromGrid(qasmStr, gridNum, gridRow, gridCol) {
var circNodeType = circGrid[gridNum][gridRow][gridCol];
// TODO: DRY
if (circNodeType == CircuitNodeTypes.QFT) {
numConsecutiveQftRowsInCol++;
if (gridRow + 1 == computeNumWires()) {
qasmStr += constructQftCircuit(gridRow + 1 - numConsecutiveQftRowsInCol,
numConsecutiveQftRowsInCol);
}
}
else {
if (numConsecutiveQftRowsInCol > 0) {
// One or more previous rows had consecutive QFT gates
qasmStr += constructQftCircuit(gridRow - numConsecutiveQftRowsInCol, numConsecutiveQftRowsInCol);
numConsecutiveQftRowsInCol = 0;
}
}
if (circNodeType == CircuitNodeTypes.H) {
var ctrlWires = ctrlWiresInColumn(gridNum, gridCol, gridRow);
// var ctrlSqrtHadStr = ' ry(pi/4) q[' + gridRow + '];' +
// ' crx(pi/4) q[' + ctrlWireNum + '],' + 'q[' + gridRow + '];' +
// ' ry(-pi/4) q[' + gridRow + '];'
//
if (ctrlWires.length == 0) {
qasmStr += ' h q[' + gridRow + '];';
}
else if (ctrlWires.length == 1) {
var ctrlWireNum = ctrlWires[0].wireNum;
var ctrlHadStr = ' ry(pi/4) q[' + gridRow + '];' +
' cx q[' + ctrlWireNum + '],' + 'q[' + gridRow + '];' +
' ry(-pi/4) q[' + gridRow + '];'
if (ctrlWires[0].isAntiCtrl) {
qasmStr += ' x q[' + ctrlWireNum + ']; ' + ctrlHadStr + ' x q[' + ctrlWireNum + '];';
}
else {
qasmStr += ctrlHadStr;
}
}
else if (ctrlWires.length >= 2) {
qasmStr += ctrlWires[0].isAntiCtrl ? ' x q[' + ctrlWires[0].wireNum + ']; ' : '';
qasmStr += ctrlWires[1].isAntiCtrl ? ' x q[' + ctrlWires[1].wireNum + ']; ' : '';
qasmStr += ' ry(pi/4) q[' + gridRow + '];' +
' crx(pi/2) q[' + ctrlWires[1].wireNum + '],' + 'q[' + gridRow + '];' + ' ry(-pi/4) q[' + gridRow + '];';
qasmStr += ' cx q[' + ctrlWires[1].wireNum + '],' + 'q[' + ctrlWires[0].wireNum + '];';
qasmStr += ' ry(pi/4) q[' + gridRow + '];' +
' crx(-pi/2) q[' + ctrlWires[0].wireNum + '],' + 'q[' + gridRow + '];' + ' ry(-pi/4) q[' + gridRow + '];';
qasmStr += ' cx q[' + ctrlWires[1].wireNum + '],' + 'q[' + ctrlWires[0].wireNum + '];';
qasmStr += ' ry(pi/4) q[' + gridRow + '];' +
' crx(pi/2) q[' + ctrlWires[0].wireNum + '],' + 'q[' + gridRow + '];' + ' ry(-pi/4) q[' + gridRow + '];';
// un-NOT the anti-control wires
qasmStr += ctrlWires[0].isAntiCtrl ? ' x q[' + ctrlWires[0].wireNum + ']; ' : '';
qasmStr += ctrlWires[1].isAntiCtrl ? ' x q[' + ctrlWires[1].wireNum + ']; ' : '';
// TODO: Find better way to implement multiple CTRL-H to not introduce a phase?
qasmStr += ctrlWires[0].isAntiCtrl ? ' x q[' + ctrlWires[0].wireNum + ']; ' : '';
qasmStr += ctrlWires[1].isAntiCtrl ? ' x q[' + ctrlWires[1].wireNum + ']; ' : '';
qasmStr += ' cp(pi/2) q[' + ctrlWires[0].wireNum + '],' + 'q[' + ctrlWires[1].wireNum + '];';
qasmStr += ctrlWires[0].isAntiCtrl ? ' x q[' + ctrlWires[0].wireNum + ']; ' : '';
qasmStr += ctrlWires[1].isAntiCtrl ? ' x q[' + ctrlWires[1].wireNum + ']; ' : '';
}
}
else if ((circNodeType >= CircuitNodeTypes.RX_0 && circNodeType <= CircuitNodeTypes.RX_15) ||
circNodeType == CircuitNodeTypes.CTRL_X) {
var ctrlWires = ctrlWiresInColumn(gridNum, gridCol, gridRow);
var rads = 0;
if (circNodeType == CircuitNodeTypes.CTRL_X) {
rads = Math.PI;
}
else {
rads = (circNodeType - CircuitNodeTypes.RX_0) * Math.PI / (NUM_PITCHES / 2);
}
var fracRads = rads / Math.pow(2, ctrlWires.length - 1);
if (circNodeType == CircuitNodeTypes.CTRL_X || circNodeType == CircuitNodeTypes.RX_8) {
if (ctrlWires.length > 0) {
circNodeType = CircuitNodeTypes.CTRL_X;
}
else {
circNodeType = CircuitNodeTypes.RX_8;
}
circGrid[gridNum][gridRow][gridCol] = circNodeType;
refreshCircGrid();
//informCircuitBtn(gridRow, gridCol);
}
if (ctrlWires.length == 0) {
qasmStr += ' rx(' + rads + ') q[' + gridRow + '];';
}
else if (ctrlWires.length == 1) {
ctrlWireNum = ctrlWires[0].wireNum;
if (ctrlWires[0].isAntiCtrl) {
qasmStr += ' x q[' + ctrlWireNum + ']; crx(' + rads + ') q[' + ctrlWireNum + '],' + 'q[' + gridRow + ']; x q[' + ctrlWireNum + '];';
}
else {
qasmStr += ' crx(' + rads + ') q[' + ctrlWireNum + '],' + 'q[' + gridRow + '];';
}
}
else if (ctrlWires.length >= 2) {
qasmStr += ctrlWires[0].isAntiCtrl ? ' x q[' + ctrlWires[0].wireNum + ']; ' : '';
qasmStr += ctrlWires[1].isAntiCtrl ? ' x q[' + ctrlWires[1].wireNum + ']; ' : '';
qasmStr += ' crx(' + fracRads + ') q[' + ctrlWires[1].wireNum + '],' + 'q[' + gridRow + '];';
qasmStr += ' cx q[' + ctrlWires[1].wireNum + '],' + 'q[' + ctrlWires[0].wireNum + '];';
qasmStr += ' crx(' + (-fracRads) + ') q[' + ctrlWires[0].wireNum + '],' + 'q[' + gridRow + '];';
qasmStr += ' cx q[' + ctrlWires[1].wireNum + '],' + 'q[' + ctrlWires[0].wireNum + '];';
qasmStr += ' crx(' + fracRads + ') q[' + ctrlWires[0].wireNum + '],' + 'q[' + gridRow + '];';
// un-NOT the anti-control wires
qasmStr += ctrlWires[0].isAntiCtrl ? ' x q[' + ctrlWires[0].wireNum + ']; ' : '';
qasmStr += ctrlWires[1].isAntiCtrl ? ' x q[' + ctrlWires[1].wireNum + ']; ' : '';
// TODO: Find better way to implement multiple CTRL-X to not introduce a phase?
qasmStr += ctrlWires[0].isAntiCtrl ? ' x q[' + ctrlWires[0].wireNum + ']; ' : '';
qasmStr += ctrlWires[1].isAntiCtrl ? ' x q[' + ctrlWires[1].wireNum + ']; ' : '';
qasmStr += ' cp(pi/2) q[' + ctrlWires[0].wireNum + '],' + 'q[' + ctrlWires[1].wireNum + '];';
qasmStr += ctrlWires[0].isAntiCtrl ? ' x q[' + ctrlWires[0].wireNum + ']; ' : '';
qasmStr += ctrlWires[1].isAntiCtrl ? ' x q[' + ctrlWires[1].wireNum + ']; ' : '';