-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
5413 lines (4317 loc) · 168 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="A quantum circuit designer">
<meta name="keywords" content="quantum, quantum simulator, quantum computing, quantum circuit, circuits, quantum physics, physics">
<meta name="author" content="Lumorti">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="data:image/svg+xml,%3C%3Fxml version='1.0' encoding='UTF-8' standalone='no'%3F%3E%3C!-- Created with Inkscape (http://www.inkscape.org/) --%3E%3Csvg xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:cc='http://creativecommons.org/ns%23' xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns%23' xmlns:svg='http://www.w3.org/2000/svg' xmlns='http://www.w3.org/2000/svg' xmlns:sodipodi='http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd' xmlns:inkscape='http://www.inkscape.org/namespaces/inkscape' width='41.577381mm' height='41.577381mm' viewBox='0 0 41.577381 41.577381' version='1.1' id='svg8' inkscape:version='0.92.5 (2060ec1f9f, 2020-04-08)' sodipodi:docname='logo.svg'%3E%3Cdefs id='defs2' /%3E%3Csodipodi:namedview id='base' pagecolor='%23ffffff' bordercolor='%23666666' borderopacity='1.0' inkscape:pageopacity='0.0' inkscape:pageshadow='2' inkscape:zoom='1.4' inkscape:cx='78.784843' inkscape:cy='54.583581' inkscape:document-units='mm' inkscape:current-layer='layer1' showgrid='false' inkscape:snap-global='false' inkscape:window-width='931' inkscape:window-height='1016' inkscape:window-x='58' inkscape:window-y='27' inkscape:window-maximized='0' /%3E%3Cmetadata id='metadata5'%3E%3Crdf:RDF%3E%3Ccc:Work rdf:about=''%3E%3Cdc:format%3Eimage/svg+xml%3C/dc:format%3E%3Cdc:type rdf:resource='http://purl.org/dc/dcmitype/StillImage' /%3E%3Cdc:title /%3E%3C/cc:Work%3E%3C/rdf:RDF%3E%3C/metadata%3E%3Cg inkscape:label='Capa 1' inkscape:groupmode='layer' id='layer1' transform='translate(-48.380955,-84.577377)'%3E%3Cpath style='opacity:1;vector-effect:none;fill:%2361baff;fill-opacity:0.98823529;stroke:none;stroke-width:2.89133859;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1' d='M -0.4375 0 L -0.4375 157.14258 L 156.70508 157.14258 L 156.70508 0 L -0.4375 0 z M 44.509766 44.947266 L 111.75781 44.947266 L 111.75781 112.19531 L 44.509766 112.19531 L 44.509766 44.947266 z ' transform='matrix(0.26458333,0,0,0.26458333,48.380955,84.577377)' id='rect815' /%3E%3Cpath style='opacity:1;vector-effect:none;fill:%23fbb400;fill-opacity:0.98823529;stroke:none;stroke-width:3.36025715;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1' d='M 14.152344 14.589844 L 14.152344 142.55273 L 142.11523 142.55273 L 142.11523 14.589844 L 14.152344 14.589844 z M 44.509766 44.947266 L 111.75781 44.947266 L 111.75781 112.19531 L 44.509766 112.19531 L 44.509766 44.947266 z ' transform='matrix(0.26458333,0,0,0.26458333,48.380955,84.577377)' id='rect817' /%3E%3Cpath style='opacity:1;vector-effect:none;fill:%2300a602;fill-opacity:0.98823529;stroke:none;stroke-width:1.79890728;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1' d='M 29.248047 29.6875 L 29.248047 127.45703 L 127.01758 127.45703 L 127.01758 29.6875 L 29.248047 29.6875 z M 44.509766 44.947266 L 111.75781 44.947266 L 111.75781 112.19531 L 44.509766 112.19531 L 44.509766 44.947266 z ' transform='matrix(0.26458333,0,0,0.26458333,48.380955,84.577377)' id='rect815-3' /%3E%3C/g%3E%3C/svg%3E" type="image/svg+xml" />
<title>Agate</title>
<style>
body {margin: 0; padding: 0; overflow: hidden;}
#view {background-color: #ffffff; position: absolute; height: 100vh; width: 100%;}
*:focus {outline: none;}
</style>
<script>
// List of the loadable presets
var loadOpen = false;
var inputBox = null;
var openHover = false;
var openDims = [0, 0, 0, 0];
var presets = [
["QASM File", ""],
["Preset: Tutorial", "statevectorOPENQASM%203.0%3B%0Ainclude%20%22stdgates.inc%22%3B%0Agate%20f0%20q0%2Cq1%20%2F%2F%200%208%0A%7B%0Ax%20q0%3B%0Ax%20q1%3B%0A%7D%0A%2F%2F%20-4%20-14%20Add%20gates%20by%20dragging%20them%20from%20the%20toolbar%20at%20the%20top%0A%2F%2F%20-4%20-12%20Double-click%20and%20drag%20on%20the%20background%20to%20select%20multiple%20gates%0A%2F%2F%20-5%20-14%20%3E%0A%2F%2F%200%20-24%20Agate%20Tutorial%0A%2F%2F%20-5%20-12%20%3E%0A%2F%2F%20-5%20-21%20%3E%0A%2F%2F%20-4%20-5%20Controls%20can%20be%20added%20to%20any%20gate%20by%20double-clicking%20and%20dragging%0A%2F%2F%20-4%20-4%20Toggle%20controls%20by%20double-clicking%20on%20them%0A%2F%2F%20-5%20-5%20%3E%0A%2F%2F%20-5%20-4%20%3E%0A%2F%2F%20-4%20-3%20Remove%20controls%20by%20dragging%20them%20back%20to%20their%20parent%20gate%0A%2F%2F%20-5%20-3%20%3E%0A%2F%2F%20-4%20-13%20Remove%20gates%20by%20dragging%20them%20back%20to%20the%20toolbar%20(or%20with%20the%20delete%20key)%0A%2F%2F%20-5%20-13%20%3E%0A%2F%2F%20-4%20-20%20Zoom%20with%20the%20mouse%20wheel%20(or%20pinching%20on%20mobile)%0A%2F%2F%20-4%20-21%20Move%20the%20camera%20by%20dragging%20the%20background%0A%2F%2F%20-5%20-20%20%3E%0A%2F%2F%20-4%204%20Function%20definition%20gates%20will%20snap%20to%20existing%20circuits%0A%2F%2F%20-4%205%20They%20can%20then%20be%20double-clicked%20and%20dragged%20to%20create%20a%20function%20call%20gate%0A%2F%2F%20-5%204%20%3E%0A%2F%2F%20-5%205%20%3E%0A%2F%2F%200%20-23%20by%20Lumorti%0A%2F%2F%20-4%206%20For%20now%20you%20can't%20place%20a%20function%20call%20inside%20a%20function%20definition%20(to%20prevent%20recursion)%0A%2F%2F%20-5%206%20%3E%0A%2F%2F%20-1%2026%20https%3A%2F%2Fgithub.com%2FLumorti%2FAgate%0A%2F%2F%20-4%2024%20Some%20LaTeX%20commands%20are%20also%20accepted%2C%20following%20a%20backslash%0A%2F%2F%20-6%2028%20%5C%20braket%7B1%7C4%7D%0A%2F%2F%20-6%2029%20%5C%20bra%7B1%7D%0A%2F%2F%20-2%2029%20%5Cbra%7B1%7D%0A%2F%2F%20-2%2028%20%5Cbraket%7B1%7C4%7D%0A%2F%2F%20-6%2030%20%5C%20ket%7B1%7D%0A%2F%2F%20-2%2030%20%5Cket%7B1%7D%0A%2F%2F%201%2028%20%5C%20sqrt2%0A%2F%2F%201%2029%201%20%5C%20pm%202%0A%2F%2F%205%2028%20%5Csqrt2%0A%2F%2F%205%2029%201%20%5Cpm%202%0A%2F%2F%205%2030%202%5Ctimes2%0A%2F%2F%201%2030%201%5C%20times2%0A%2F%2F%205%2031%201%5Ccdot2%0A%2F%2F%201%2031%201%5C%20cdot2%0A%2F%2F%201%2032%201%20%5C%20to%202%0A%2F%2F%205%2032%201%20%5Cto%202%0A%2F%2F%20-6%2031%20%5C%20langle%0A%2F%2F%20-2%2031%20%5Clangle%0A%2F%2F%20-6%2032%20%5C%20rangle%0A%2F%2F%20-2%2032%20%5Crangle%0A%2F%2F%20-5%2024%20%3E%0A%2F%2F%20-4%2034%20Keyboard%20shortcuts%20are%20also%20available%0A%2F%2F%20-5%2034%20%3E%0A%2F%2F%20-3%2039%20ctrl-z%0A%2F%2F%20-3%2036%20ctrl-c%0A%2F%2F%20-3%2037%20ctrl-x%0A%2F%2F%20-3%2038%20ctrl-v%0A%2F%2F%20-3%2035%20delete%0A%2F%2F%20-3%2040%20ctrl-shift-z%0A%2F%2F%201%2035%20delete%20the%20selected%20gate%2Fgates%0A%2F%2F%201%2036%20copy%20the%20selected%20gate%2Fgates%0A%2F%2F%201%2037%20cut%20the%20selected%20gate%2Fgates%0A%2F%2F%201%2038%20paste%20the%20stored%20gates%0A%2F%2F%201%2039%20undo%20the%20last%20change%0A%2F%2F%201%2040%20redo%20the%20last%20undo%0A%2F%2F%20-5%2023%20%3E%0A%2F%2F%20-4%2023%20Double%20click%20on%20a%20text%20object%20to%20edit%2C%20links%20are%20auto%20detected%0A%2F%2F%20-4%2017%20Gates%20with%20parameters%20can%20be%20editted%20by%20triple%20clicking%0A%2F%2F%20-5%2017%20%3E%0A%2F%2F%209%2028%20%5C%20pi%0A%2F%2F%2012%2028%20%5Cpi%0A%2F%2F%209%2029%20%5C%20theta%0A%2F%2F%2012%2029%20%5Ctheta%0A%2F%2F%209%2030%20%5C%20psi%0A%2F%2F%2012%2030%20%5Cpsi%0A%2F%2F%209%2031%20%5C%20phi%0A%2F%2F%2012%2031%20%5Cphi%0Aqubit%20q%5B2%5D%3B%20%2F%2F%20-1%20-9%0Ah%20q%5B0%5D%3B%0Ax%20q%5B0%5D%3B%0Aid%20q%5B1%5D%3B%0Aid%20q%5B1%5D%3B%0Aid%20q%5B1%5D%3B%0Aid%20q%5B1%5D%3B%0Ax%20q%5B1%5D%3B%0Aqubit%20q%5B2%5D%3B%20%2F%2F%202%2013%0Af0%20q%5B0%5D%2Cq%5B1%5D%3B%0Aqubit%20q%5B2%5D%3B%20%2F%2F%200%200%0Ah%20q%5B0%5D%3B%0Actrl%20%40%20x%20q%5B0%5D%2Cq%5B1%5D%3B%0Anegctrl%20%40%20x%20q%5B0%5D%2Cq%5B1%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%201%20-17%0Ax%20q%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%201%2020%0Arx(2)%20q%5B0%5D%3B%0Ary(3.14)%20q%5B0%5D%3B%0Arz(-1)%20q%5B0%5D%3B%0A"],
["Preset: Gates", "statevectorOPENQASM%203.0%3B%0Ainclude%20%22stdgates.inc%22%3B%0A%2F%2F%206%200%20Hadamard%0A%2F%2F%207%201%20This%20allows%20the%20creation%20of%20superposition%2C%20where%0A%2F%2F%2015%205%20input%0A%2F%2F%2018%205%20output%20(unnormalised)%0A%2F%2F%2015%206%20%5Cket%7B0%7D%0A%2F%2F%2018%206%20%5Cket%7B0%7D%20%2B%20%5Cket%7B1%7D%0A%2F%2F%2015%207%20%5Cket%7B1%7D%0A%2F%2F%2018%207%20%5Cket%7B0%7D%20-%20%5Cket%7B1%7D%0A%2F%2F%207%202%20the%20circuit%20exists%20in%20multiple%20states%20at%20once%0A%2F%2F%206%20-7%20Pauli%20X%0A%2F%2F%207%20-6%20This%20is%20the%20equivalent%20of%20the%20classical%20NOT%20gate%0A%2F%2F%2015%20-4%20input%0A%2F%2F%2018%20-4%20output%0A%2F%2F%2015%20-3%20%5Cket%7B0%7D%0A%2F%2F%2018%20-3%20%5Cket%7B1%7D%0A%2F%2F%2015%20-2%20%5Cket%7B1%7D%0A%2F%2F%2018%20-2%20%5Cket%7B0%7D%0A%2F%2F%206%209%20Pauli%20Z%20(and%20other%20phase%20gates)%0A%2F%2F%207%2010%20These%20all%20add%20some%20kind%20of%20global%20phase%20to%20the%20state%2C%0A%2F%2F%207%2011%20but%20only%20if%20the%20input%20is%20%5Cket%7B1%7D%0A%2F%2F%2015%2028%20input%0A%2F%2F%2015%2013%20input%0A%2F%2F%2015%2017%20input%0A%2F%2F%2015%2021%20input%0A%2F%2F%2018%2028%20output%0A%2F%2F%2018%2013%20output%0A%2F%2F%2018%2017%20output%0A%2F%2F%2018%2021%20output%20(unnormalised)%0A%2F%2F%2015%2029%20%5Cket%7B0%7D%0A%2F%2F%2015%2030%20%5Cket%7B1%7D%0A%2F%2F%2015%2014%20%5Cket%7B0%7D%0A%2F%2F%2015%2015%20%5Cket%7B1%7D%0A%2F%2F%2015%2018%20%5Cket%7B0%7D%0A%2F%2F%2015%2019%20%5Cket%7B1%7D%0A%2F%2F%2015%2022%20%5Cket%7B0%7D%0A%2F%2F%2015%2023%20%5Cket%7B1%7D%0A%2F%2F%2018%2029%20i%20%5Cket%7B1%7D%0A%2F%2F%2018%2030%20-i%20%5Cket%7B0%7D%0A%2F%2F%2018%2014%20%5Cket%7B0%7D%0A%2F%2F%2018%2015%20-%20%5Cket%7B1%7D%0A%2F%2F%2018%2018%20%5Cket%7B0%7D%0A%2F%2F%2018%2019%20i%20%5Cket%7B1%7D%0A%2F%2F%2018%2023%20(1%2Bi)%20%5Cket%7B1%7D%0A%2F%2F%2018%2022%20%5Cket%7B0%7D%0A%2F%2F%206%2025%20Pauli%20Y%0A%2F%2F%207%2026%20Performs%20a%20NOT%20as%20well%20as%20adding%20a%20phase%0A%2F%2F%2010%20-10%20Agate%20Gates%20List%0A%2F%2F%2011%20-9%20by%20Lumorti%0A%2F%2F%207%203%20Results%20in%20a%2050%2F50%20chance%20of%20measuring%20either%0A%2F%2F%2015%2035%20input%0A%2F%2F%2018%2035%20output%0A%2F%2F%2015%2036%20%5Cket%7B00%7D%0A%2F%2F%2018%2036%20%5Cket%7B00%7D%0A%2F%2F%206%2032%20Swap%0A%2F%2F%207%2033%20Swaps%20the%20two%20qubits%0A%2F%2F%2015%2037%20%5Cket%7B01%7D%0A%2F%2F%2018%2037%20%5Cket%7B10%7D%0A%2F%2F%2015%2038%20%5Cket%7B10%7D%0A%2F%2F%2018%2038%20%5Cket%7B01%7D%0A%2F%2F%2015%2039%20%5Cket%7B11%7D%0A%2F%2F%2018%2039%20%5Cket%7B11%7D%0A%2F%2F%206%2010%20%3E%0A%2F%2F%206%201%20%3E%0A%2F%2F%206%203%20%3E%0A%2F%2F%206%20-6%20%3E%0A%2F%2F%206%2026%20%3E%0A%2F%2F%206%2033%20%3E%0A%2F%2F%206%2041%20Rotation%20Gates%0A%2F%2F%206%2042%20%3E%0A%2F%2F%207%2042%20These%20perform%20a%20rotation%20in%20a%20certain%20direction%20with%20a%20certain%20angle%0A%2F%2F%207%2043%20Combining%20all%20three%20of%20these%2C%20any%20single%20qubit%20gate%20can%20be%20created%0A%2F%2F%206%2043%20%3E%0A%2F%2F%2015%2045%20input%0A%2F%2F%2018%2045%20output%0A%2F%2F%2015%2046%20%5Cket%7B0%7D%0A%2F%2F%2015%2047%20%5Cket%7B1%7D%0A%2F%2F%2018%2046%20cos(%5Ctheta%2F2)%20%5Cket%7B0%7D%20-%20i%20sin(%5Ctheta%2F2)%20%5Cket%7B1%7D%0A%2F%2F%2018%2047%20cos(%5Ctheta%2F2)%20%5Cket%7B1%7D%20-%20i%20sin(%5Ctheta%2F2)%20%5Cket%7B0%7D%0A%2F%2F%2015%2049%20input%0A%2F%2F%2018%2049%20output%0A%2F%2F%2015%2050%20%5Cket%7B0%7D%0A%2F%2F%2015%2051%20%5Cket%7B1%7D%0A%2F%2F%2018%2050%20cos(%5Ctheta%2F2)%20%5Cket%7B0%7D%20%2B%20sin(%5Ctheta%2F2)%20%5Cket%7B1%7D%0A%2F%2F%2018%2051%20cos(%5Ctheta%2F2)%20%5Cket%7B1%7D%20-%20sin(%5Ctheta%2F2)%20%5Cket%7B0%7D%0A%2F%2F%2015%2053%20input%0A%2F%2F%2018%2053%20output%0A%2F%2F%2015%2054%20%5Cket%7B0%7D%0A%2F%2F%2015%2055%20%5Cket%7B1%7D%0A%2F%2F%2018%2054%20(cos(%5Ctheta%2F2)%20-%20i%20sin(%5Ctheta%2F2))%20%5Cket%7B0%7D%0A%2F%2F%2018%2055%20(cos(%5Ctheta%2F2)%20%2B%20i%20sin(%5Ctheta%2F2))%20%5Cket%7B1%7D%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%206%0Ah%20q%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%20-3%0Ax%20q%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%2029%0Ay%20q%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%2014%0Az%20q%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%2018%0As%20q%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%2022%0At%20q%5B0%5D%3B%0Aqubit%20q%5B2%5D%3B%20%2F%2F%209%2036%0Aswap%20q%5B1%5D%2Cq%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%2046%0Arx(1)%20q%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%2050%0Ary(1)%20q%5B0%5D%3B%0Aqubit%20q%5B1%5D%3B%20%2F%2F%209%2054%0Arz(1)%20q%5B0%5D%3B%0A"],
["Preset: Grover's", "statevectorOPENQASM%203.0%3B%0Ainclude%20%22stdgates.inc%22%3B%0Agate%20f2%20q0%2Cq1%20%2F%2F%20-2%2015%0A%7B%0Az%20q0%3B%0Az%20q1%3B%0Actrl%20%40%20z%20q0%2Cq1%3B%0A%7D%0Agate%20f1%20q0%2Cq1%20%2F%2F%20-2%2010%0A%7B%0Actrl%20%40%20z%20q0%2Cq1%3B%0A%7D%0Agate%20f0%20q0%2Cq1%20%2F%2F%20-2%205%0A%7B%0Ah%20q0%3B%0Ah%20q1%3B%0A%7D%0A%2F%2F%203%206%20this%20function%20create%20or%20destroys%20the%20search%20space%0A%2F%2F%203%2011%20this%20function%20adds%20a%20negative%20phase%20to%20the%20state%20we%20want%20to%20find%0A%2F%2F%203%2012%20(in%20this%20case%3A%20%5Cket%7B11%7D%20%5Cto%20-%5Cket%7B11%7D)%0A%2F%2F%204%2016%20this%20function%20rotates%20anything%20that's%20not%20%5Cket%7B00%7D%20to%20have%20negative%20phase%0A%2F%2F%204%2017%20(i.e.%20anything%20that%20didn't%20become%20zero%20when%20we%20destroy%20the%20search%20space)%0A%2F%2F%20-3%20-6%20This%20famous%20quantum%20algorithm%20amplifies%20the%20chance%20of%20measuring%20%0A%2F%2F%20-3%20-5%20a%20certain%20state%20from%20a%20big%20list%20of%20possible%20states%2C%20effectively%20%22finding%22%20a%20state%0A%2F%2F%203%207%20(in%20this%20case%3A%20a%20full%20superposition)%0A%2F%2F%200%20-9%20Grover's%20Algorithm%20Implementation%0A%2F%2F%20-1%2020%20Sources%3A%0A%2F%2F%200%2021%20https%3A%2F%2Fqiskit.org%2Ftextbook%2Fch-algorithms%2Fgrover.html%0A%2F%2F%200%2022%20https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGrover%2527s_algorithm%0A%2F%2F%200%20-8%20by%20Lumorti%0A%2F%2F%20-3%20-3%20Whilst%20classically%20searching%20a%20list%20of%20N%20items%20takes%20at%20most%20N%20checks%2C%0A%2F%2F%20-3%20-2%20this%20algorithm%20only%20requires%20at%20most%20%5CsqrtN%20checks%0Aqubit%20q%5B2%5D%3B%20%2F%2F%202%201%0Af0%20q%5B0%5D%2Cq%5B1%5D%3B%0Af1%20q%5B0%5D%2Cq%5B1%5D%3B%0Af0%20q%5B0%5D%2Cq%5B1%5D%3B%0Af2%20q%5B0%5D%2Cq%5B1%5D%3B%0Af0%20q%5B0%5D%2Cq%5B1%5D%3B%0A"],
["Preset: Blank", ""],
];
// Stuff for svgs
var svgPadding = 30;
var svgString = "";
var svgMinX = 99999;
var svgMaxX = -99999;
var svgMinY = 99999;
var svgMaxY = -99999;
// Save types
var saveOpen = false;
var outputBox = null;
var saveHover = false;
var saveDims = [0, 0, 0, 0];
var saveTypes = [
"QASM 3.0",
"QASM 2.0",
"SVG",
"Unitary CSV",
];
// The gates to put on the toolbar
var numGatesToShow = 5;
var toolbarIndex = 0;
var toolbarGates = [
"H",
"X",
"Y",
"Z",
"sub",
"S",
"T",
"I",
"swap",
"text",
"Rx(0)",
"Ry(0)",
"Rz(0)",
];
// User-modifiable settings
var canEdit = true;
var showFPS = false;
var drawGrid = true;
var simMethod = "Measurements";
var numRepeats = 1000;
var cutoffThreshMeasure = 0.00000001;
var cutoffThresh = 0.00000001;
var doubleClickMilli = 300;
var tripleClickMilli = 300;
var initialZoom = 1.0;
var qasmVersion = 3;
var maxHistory = 10;
// Stuff for the settings menu
var settingsOpen = false;
var fpsBox = null;
var gridBox = null;
var simBox = null;
var repeatsBox = null;
// Empty global vars to be initialised later
var canvas = null;
var ctx = null;
var copyElement = null;
var linkObject = null;
// For managing the history
var qasmHistory = [];
var historyIndex = -1;
var noHistory = false;
// The list of gates
var gates = [];
var gatesInit = [];
var gateOptions = -1;
// Which gate is current selected
var justCreateMulti = false;
var selected = -1;
var hover = -1;
var nextID = 0;
var nextFunctionID = 0;
var dragOffset = 0;
var showDelete = false;
var selectStartX = 0;
var selectEndX = 0;
var selectStartY = 0;
var selectEndY = 0;
var selectionArray = [];
var lineStartEnds = [];
var qubitsWithGates = [];
var funcStartEnds = [];
var funcUnitaries = [];
// General vars
var mx = 0;
var my = 0;
var onMobile = 0;
var gateSizeFixed = 50;
var debugData = "";
var gridYFixed = 60;
var gridXFixed = 60;
var fontSize = initialZoom*35;
var gateSize = initialZoom*gateSizeFixed;
var gridY = initialZoom*gridYFixed;
var gridX = initialZoom*gridXFixed;
var spacing = 0.8;
var toolbarWidth = -1;
var toolbarHeight = -1;
var maxRecDepth = 5;
// For FPS checking
var frames = 0;
var fps = 0;
var lastFPSCheck = 0;
// For dragging the canvas around
var offsetX = 0;
var offsetY = 0;
var prevTouchX = 0;
var prevTouchY = 0;
// Stuff for mobile
var isPinching = false;
var mobileOffsetY = 0;
var lastPinchDist = -1;
var browser = "";
var inPrompt = false;
// For the tips window
var helpOpen = true;
var tipInd = 0;
var tips = [
["open", "If you're new, load the tutorial", "using the open icon at the top"],
["H", "Create gates by dragging them", "from the toolbar at the top"],
["controlFilled", "Double-click and drag on a", "gate to create a control"],
["controlUnfilled", "Double-click on controls", "to toggle them"],
["sub", "Function definition gates", "snap to existing circuits"],
["fun0", "Double-click and drag a function", "to create a call gate"],
["X", "Double-click and drag nothing", "to select multiple gates"],
];
// For timings
var lastClickTime = -1;
var lastDoubleClickTime = -1;
var lastSimTime = -1;
var lastRedrawTime = -1;
var minRedrawWait = 5;
var currentlyRedrawing = false;
var circuitUpdates = 1;
var recalcRecs = true;
var inputs = [[]];
var results = [[]];
// Called on body ready
function init(newInstance=false, recalc=false) {
// If this is triggering from the Firefox mobile prompt resize
if (inPrompt) {return;}
// Reset things
loadOpen = false;
saveOpen = false;
canEdit = true;
showFPS = false;
// Get the browser
if (navigator.userAgent.indexOf("Chrome") > 0) {
browser = "Chrome";
} else if (navigator.userAgent.indexOf("Firefox") > 0) {
browser = "Firefox";
}
// Initialise the canvas
canvas = document.getElementById("view");
ctx = canvas.getContext("2d");
// Make sure it's a high DPI canvas
canvas.width = window.innerWidth;
canvas.height = window.innerWidth;
canvas.style.width = window.innerWidth + "px";
canvas.style.height = window.innerHeight + "px";
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
// If not refeshing/resizing
if (newInstance) {
// Add listeners for mouse events
canvas.addEventListener('mousemove', mouseMove);
canvas.addEventListener('mousedown', mouseDown);
canvas.addEventListener('mouseup', mouseUp);
canvas.addEventListener('wheel', mouseWheel);
// For keyboard events
window.addEventListener('keydown', keyDown);
copyElement = document.getElementById("hiddenText");
// And for touch events
canvas.addEventListener("touchstart", touchHandler, true);
canvas.addEventListener("touchmove", touchHandler, true);
canvas.addEventListener("touchend", touchHandler, true);
canvas.addEventListener("touchcancel", touchHandler, true);
// Setup the hidden load element
inputBox = document.createElement("SELECT");
inputBox.style.position = "absolute";
inputBox.style.font = "20px Arial";
inputBox.style.padding = "10px";
inputBox.style.zIndex = "100";
for (var k=0; k<presets.length; k++) {
var option = document.createElement("option");
option.text = presets[k][0];
inputBox.add(option);
}
document.getElementsByTagName("BODY")[0].appendChild(inputBox);
// Setup the hidden save element
outputBox = document.createElement("SELECT");
outputBox.style.position = "absolute";
outputBox.style.font = "20px Arial";
outputBox.style.padding = "10px";
outputBox.style.zIndex = "101";
for (var k=0; k<saveTypes.length; k++) {
var option = document.createElement("option");
option.text = saveTypes[k];
outputBox.add(option);
}
document.getElementsByTagName("BODY")[0].appendChild(outputBox);
// Setup the hidden FPS checkbox
fpsBox = document.createElement("INPUT");
fpsBox.setAttribute("type", "checkbox");
fpsBox.style.position = "absolute";
fpsBox.style.font = "20px Arial";
fpsBox.style.padding = "10px";
fpsBox.style.zIndex = "101";
fpsBox.style.height = "25px";
fpsBox.style.width = "25px";
fpsBox.onchange = function() {
showFPS = !showFPS;
lastRedrawTime = new Date().getTime();
redraw();
};
document.getElementsByTagName("BODY")[0].appendChild(fpsBox);
// Setup the hidden grid checkbox
gridBox = document.createElement("INPUT");
gridBox.setAttribute("type", "checkbox");
gridBox.style.position = "absolute";
gridBox.style.font = "20px Arial";
gridBox.style.padding = "10px";
gridBox.style.zIndex = "101";
gridBox.style.height = "25px";
gridBox.style.width = "25px";
gridBox.checked = drawGrid;
gridBox.onchange = function() {
drawGrid = !drawGrid;
lastRedrawTime = new Date().getTime();
redraw();
};
document.getElementsByTagName("BODY")[0].appendChild(gridBox);
// Setup the hidden sim method box
simBox = document.createElement("SELECT");
simBox.style.position = "absolute";
simBox.style.font = "20px Arial";
simBox.style.padding = "10px";
simBox.style.zIndex = "101";
simBox.style.boxSizing = "border-box";
opts = ["Statevector", "Measurements", "Unitary"];
for (var i=0; i<opts.length; i++) {
var option = document.createElement("option");
option.text = opts[i];
simBox.add(option);
}
simBox.onchange = function() {
simMethod = simBox.options[simBox.selectedIndex].text;
circuitUpdates = 1;
lastRedrawTime = new Date().getTime();
redraw();
};
document.getElementsByTagName("BODY")[0].appendChild(simBox);
// Setup the hidden repeats box
repeatsBox = document.createElement("INPUT");
repeatsBox.setAttribute("type", "number");
repeatsBox.style.position = "absolute";
repeatsBox.style.font = "20px Arial";
repeatsBox.style.padding = "10px";
repeatsBox.style.zIndex = "101";
repeatsBox.style.boxSizing = "border-box";
repeatsBox.style.display = "none";
repeatsBox.value = numRepeats;
repeatsBox.onchange = function() {
numRepeats = repeatsBox.value;
circuitUpdates = 1;
lastRedrawTime = new Date().getTime();
redraw();
};
document.getElementsByTagName("BODY")[0].appendChild(repeatsBox);
// Get the link object
linkObject = document.getElementById("hiddenURL");
}
// Check if mobile
if (gridXFixed*(numGatesToShow+4)+10 > 0.9*window.innerWidth) {
onMobile = 1;
} else if (window.innerHeight < 400) {
onMobile = 2;
}
// Changes if on mobile (vertical)
if (onMobile == 1) {
// Zoom out a bit
var change = 1.0 - 0.4;
gridY = initialZoom * gridYFixed * change;
gridX = initialZoom * gridXFixed * change;
gateSize = initialZoom * gateSizeFixed * change;
fontSize = initialZoom * 35 * change;
// Show less gates at once on mobile
numGatesToShow = 3;
// Add the gate summoning buttons
toolbarHeight = gridYFixed*2+10;
toolbarWidth = gridXFixed*(numGatesToShow+2)+10;
toolbarOffsetX = window.innerWidth / 2 - toolbarWidth / 2;
toolbarRel = (toolbarOffsetX / gridXFixed);
gates = [];
toolbarIndex = 0;
gates.push({"letter": "left", "y": 0.33, "x": 0.15+toolbarRel, "size": 1, "attached": [], "og": -1, "draggable": false})
for (var i=0; i<toolbarGates.length; i++) {
gates.push({"letter": toolbarGates[i], "attached": [], "og": -1, "y": 0.33, "x": (i%numGatesToShow)+1.15+toolbarRel, "size": 1, "draggable": false})
}
gates.push({"letter": "right", "y": 0.33, "x": numGatesToShow+1.2+toolbarRel, "attached": [], "og": -1, "size": 1, "draggable": false})
gates.push({"letter": "open", "attached": [], "og": -1, "y": 1.27, "x": 1.25+toolbarRel, "size": 1, "draggable": false})
gates.push({"letter": "save", "attached": [], "og": -1, "y": 1.43, "x": 2.25+toolbarRel, "size": 1, "draggable": false})
gates.push({"letter": "settings", "attached": [], "og": -1, "y": 1.33, "x": 3.15+toolbarRel, "size": 1, "draggable": false})
gatesInit = gates.slice();
gateOptions = gates.length;
} else {
// Zoom out a bit
var change = 1.0 - 0.2;
gridY = initialZoom * gridYFixed * change;
gridX = initialZoom * gridXFixed * change;
gateSize = initialZoom * gateSizeFixed * change;
fontSize = initialZoom * 35 * change;
// Show more gates if possible
numGatesToShow = 5;
// Add the gate summoning buttons
toolbarHeight = gateSizeFixed+20;
toolbarWidth = gridXFixed*(numGatesToShow+5);
toolbarOffsetX = window.innerWidth / 2 - toolbarWidth / 2;
toolbarRel = (toolbarOffsetX / gridXFixed);
gates = [];
toolbarIndex = 0;
gates.push({"letter": "left", "y": 0.33, "x": 0.2+toolbarRel, "size": 1, "attached": [], "og": -1, "draggable": false})
for (var i=0; i<toolbarGates.length; i++) {
gates.push({"letter": toolbarGates[i], "attached": [], "og": -1, "y": 0.33, "x": (i%numGatesToShow)+1.2+toolbarRel, "size": 1, "draggable": false})
}
gates.push({"letter": "right", "y": 0.33, "x": numGatesToShow+1.2+toolbarRel, "attached": [], "og": -1, "size": 1, "draggable": false})
gates.push({"letter": "open", "attached": [], "og": -1, "y": 0.27, "x": numGatesToShow+1.9+toolbarRel, "size": 1, "draggable": false})
gates.push({"letter": "save", "attached": [], "og": -1, "y": 0.43, "x": numGatesToShow+2.9+toolbarRel, "size": 1, "draggable": false})
gates.push({"letter": "settings", "attached": [], "og": -1, "y": 0.35, "x": numGatesToShow+3.8+toolbarRel, "size": 1, "draggable": false})
gatesInit = gates.slice();
gateOptions = gates.length;
}
// If URL contains QASM info, use it
if (document.location.hash.length > 1) {
// Start with the whole hash
var toProcess = document.location.hash.substr(1);
// Prevent edits, hide toolbar etc.
if (toProcess.substr(0,4) == "view") {
canEdit = false;
toProcess = toProcess.substr(4);
}
// Override the sim method
if (toProcess.substr(0,7) == "unitary") {
simMethod = "Unitary";
toProcess = toProcess.substr(7);
}
if (toProcess.substr(0,12) == "measurements") {
simMethod = "Measurements";
toProcess = toProcess.substr(12);
}
if (toProcess.substr(0,11) == "statevector") {
simMethod = "Statevector";
toProcess = toProcess.substr(11);
}
// Get it and process it
asQasm = decodeURIComponent(toProcess);
// Load the gates from this
fromQASM(asQasm);
// Don't show the help
helpOpen = false;
}
// Update the sim method drop down
for (var i=0; i<simBox.options.length; i++) {
if (simBox.options[i].text == simMethod) {
simBox.selectedIndex = i;
break;
}
}
// Whether to resim the circuit
if (recalc) {
circuitUpdates = 2;
} else {
circuitUpdates = 0;
}
// The first redraw
lastRedrawTime = new Date().getTime();
redraw();
}
// Given a letter and a position, draw a gate
function drawGate(letter, x, y, isSelected, size, fixedSize=false) {
// Only draw gates which are current within the canvas dimensions
var extraEdge = gateSize/2 + 10;
if (letter.substr(0,4) == "text") {
if (x > canvas.width+extraEdge || y+gateSize*size < -extraEdge || y > canvas.height+extraEdge) {
return;
}
} else {
if (x < -extraEdge || x > canvas.width+extraEdge || y+gateSize*size < -extraEdge || y > canvas.height+extraEdge) {
return;
}
}
// If it's a filled control
if (letter == "controlFilled") {
// Draw the circle
ctx.lineWidth = fontSize/7;
if (!isSelected) {
ctx.fillStyle = "#555555";
ctx.strokeStyle = "#555555";
} else {
ctx.fillStyle = "#888888";
ctx.strokeStyle = "#888888";
}
ctx.beginPath();
ctx.arc(x, y, gateSize/4, 0, 2 * Math.PI, false);
ctx.fill();
ctx.stroke();
svgCircle(x, y, gateSize/4, ctx.fillStyle, ctx.strokeStyle, ctx.lineWidth);
// If it's an unfilled control
} else if (letter == "controlUnfilled") {
// Draw the circle
ctx.lineWidth = fontSize/7;
if (!isSelected) {
ctx.strokeStyle = "#555555";
} else {
ctx.strokeStyle = "#888888";
}
ctx.fillStyle = "#ffffff";
ctx.beginPath();
ctx.arc(x, y, gateSize/4, 0, 2 * Math.PI, false);
ctx.fill();
ctx.stroke();
svgCircle(x, y, gateSize/4, ctx.fillStyle, ctx.strokeStyle, ctx.lineWidth);
// If it's an subroutine definition
} else if (letter.substr(0,3) == "sub") {
// If not a toolbar gate
if (!fixedSize) {
// Draw the box
if (!isSelected) {
ctx.fillStyle = "#16a300";
} else {
ctx.fillStyle = "#107800";
}
ctx.fillRect(x-gateSize/2, y-gateSize/2, gateSize, gateSize+gridY*(size-1));
svgRect(x-gateSize/2, y-gateSize/2, gateSize, gateSize+gridY*(size-1), ctx.fillStyle);
// Draw the letter
ctx.fillStyle = "#ffffff";
if (letter.length > 3) {
ctx.font = (fontSize*0.9-2*(letter.length-4)) + "px Arial";
ctx.fillText(letter.substr(3), gridX*0.28+x-4*(letter.length-4)-gateSize/2, gridY*0.59+y-gateSize/2);
svgText(letter.substr(3), gridX*0.28+x-4*(letter.length-4)-gateSize/2, gridY*0.59+y-gateSize/2, fontSize*0.9-2*(letter.length-4), ctx.measureText(letter.substr(3)).width, ctx.fillStyle);
} else {
ctx.font = (fontSize*0.9)+"px Arial";
ctx.fillText("F", gridX*0.28+x-gateSize/2, gridY*0.59+y-gateSize/2);
}
// If a toolbar gate (and thus fixed sizes should be used)
} else {
// Draw the box
if (!isSelected) {
ctx.fillStyle = "#16a300";
} else {
ctx.fillStyle = "#107800";
}
ctx.fillRect(x-gateSizeFixed/2, y-gateSizeFixed/2, gateSizeFixed, gateSizeFixed+gridY*(size-1));
// Draw the letter
ctx.fillStyle = "#ffffff";
if (letter.length > 3) {
ctx.font = (30/(letter.length-3)) + "px Arial";
ctx.fillText(letter.substr(3), gridXFixed*0.28+x-gateSizeFixed/2, gridYFixed*0.59+y-gateSizeFixed/2);
} else {
ctx.font = "30px Arial";
ctx.fillText("F", gridXFixed*0.28+x-gateSizeFixed/2, gridYFixed*0.59+y-gateSizeFixed/2);
}
}
// If it's an subroutine call
} else if (letter.substr(0,3) == "fun" || letter == "?") {
// If not a toolbar gate
if (!fixedSize) {
// Draw the box
if (!isSelected) {
ctx.fillStyle = "#16a300";
} else {
ctx.fillStyle = "#107800";
}
ctx.fillRect(x-gateSize/2, y-gateSize/2, gateSize, gateSize+gridY*(size-1));
svgRect(x-gateSize/2, y-gateSize/2, gateSize, gateSize+gridY*(size-1), ctx.fillStyle);
// Draw the letter
ctx.fillStyle = "#ffffff";
if (letter == "?") {
ctx.font = (fontSize*0.9)+"px Arial";
ctx.fillText("?", 0.28*gridX+x-gateSize/2, 0.59*gridY+y-gateSize/2);
svgText("?", 0.28*gridX+x-gateSize/2, 0.28*gridX+x-gateSize/2, (fontSize*0.9), ctx.measureText("?").width, ctx.fillStyle);
} else {
ctx.font = (fontSize*0.9-2*(letter.length-4)) + "px Arial";
ctx.fillText(letter.substr(3), 0.28*gridX-4*(letter.length-4)+x-gateSize/2, 0.59*gridY+y-gateSize/2);
svgText(letter.substr(3), 0.28*gridX-4*(letter.length-4)+x-gateSize/2, 0.59*gridY+y-gateSize/2, (fontSize*0.9), ctx.measureText(letter.substr(3)).width, ctx.fillStyle);
}
// If a toolbar gate (and thus fixed sizes should be used)
} else {
// Draw the box
if (!isSelected) {
ctx.fillStyle = "#16a300";
} else {
ctx.fillStyle = "#107800";
}
ctx.fillRect(x-gateSizeFixed/2, y-gateSizeFixed/2, gateSizeFixed, gateSizeFixed+gridYFixed*(size-1));
// Draw the letter
ctx.fillStyle = "#ffffff";
if (letter == "?") {
ctx.font = "30px Arial";
ctx.fillText("?", 0.28*gridXFixed+x-gateSizeFixed/2, 0.59*gridYFixed+y-gateSizeFixed/2);
} else {
ctx.font = (fontSize*0.9-2*(letter.length-4)) + "px Arial";
ctx.fillText(letter.substr(3), 0.28*gridXFixed-4*(letter.length-4)+x-gateSizeFixed/2, 0.59*gridYFixed+y-gateSizeFixed/2);
}
}
// If it's the link go button
} else if (letter == "go") {
// Draw the arrow
if (!isSelected) {
ctx.fillStyle = "#555555";
} else {
ctx.fillStyle = "#888888";
}
ctx.beginPath();
ctx.moveTo(x-gateSize/3, y-gateSize/3);
ctx.lineTo(x-gateSize/3, y+gateSize/3);
ctx.lineTo(x, y);
ctx.fill();
// If it's the whole swap gate
} else if (letter == "swap") {
// Draw the curved section
if (!isSelected) {
ctx.strokeStyle = "#555555";
} else {
ctx.strokeStyle = "#888888";
}
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(x-gateSizeFixed/3, y, gateSizeFixed/3, 1.5*Math.PI, 0);
ctx.stroke();
ctx.beginPath();
ctx.arc(x+gateSizeFixed/3, y, gateSizeFixed/3, 1*Math.PI, 1.5*Math.PI);
ctx.stroke();
ctx.beginPath();
ctx.arc(x-gateSizeFixed/3, y, gateSizeFixed/3, 0*Math.PI, 0.5*Math.PI);
ctx.stroke();
ctx.beginPath();
ctx.arc(x+gateSizeFixed/3, y, gateSizeFixed/3, 0.5*Math.PI, 1.0*Math.PI);
ctx.stroke();
// If it's the top part of a swap gate
} else if (letter == "swaptop") {
// Clear the standard qubit line
ctx.fillStyle = "#ffffff";
ctx.fillRect(x-gridX/2+fontSize/70, y-gridY/8, gridX-fontSize/35, gridY/2);
svgRect(x-gridX/2+fontSize/70, y-gridY/8, gridX-fontSize/35, gridY/2, ctx.fillStyle);
// Draw the curved section
if (!isSelected) {
ctx.strokeStyle = "#aaaaaa";
} else {
ctx.strokeStyle = "#888888";
}
ctx.lineWidth = fontSize/7;
ctx.beginPath();
ctx.arc(x-gridX/2, y+gridY/2, gridX/2, 1.5*Math.PI, 0);
ctx.stroke();
svgArc(x-gridX/2, y, x, y, x, y+gridY/2, ctx.strokeStyle, ctx.lineWidth);
ctx.beginPath();
ctx.arc(x+gridX/2, y+gridY/2, gridX/2, 1*Math.PI, 1.5*Math.PI);
ctx.stroke();
svgArc(x+gridX/2, y, x, y, x, y+gridY/2, ctx.strokeStyle, ctx.lineWidth);
// If it's the bottom part of a swap gate
} else if (letter == "swapbottom") {
// Clear the standard qubit line
ctx.fillStyle = "#ffffff";
ctx.fillRect(x-gridX/2+fontSize/70, y-gridY/3, gridX-fontSize/35, gridY/2);
svgRect(x-gridX/2+fontSize/70, y-gridY/3, gridX-fontSize/35, gridY/2, ctx.fillStyle);
// Draw the curved section
if (!isSelected) {
ctx.strokeStyle = "#aaaaaa";
} else {
ctx.strokeStyle = "#888888";
}
ctx.lineWidth = fontSize/7;
ctx.beginPath();
ctx.arc(x-gridX/2, y-gridY/2, gridX/2, 0*Math.PI, 0.5*Math.PI);
ctx.stroke();
svgArc(x-gridX/2, y, x, y, x, y-gridY/2, ctx.strokeStyle, ctx.lineWidth);
ctx.beginPath();
ctx.arc(x+gridX/2, y-gridY/2, gridX/2, 0.5*Math.PI, 1.0*Math.PI);
ctx.stroke();
svgArc(x+gridX/2, y, x, y, x, y-gridY/2, ctx.strokeStyle, ctx.lineWidth);
// If it's the create text icon
} else if (letter == "text") {
// Draw the text
ctx.font = "40px Serif";
if (!isSelected) {
ctx.fillStyle = "#555555";
} else {
ctx.fillStyle = "#888888";
}
ctx.fillText("T", x-14, y+14);
// If it's a gate-like text object
} else if (letter.substr(0,4) == "text") {
// The text of the gate
var gateText = letter.substr(4);
// If it's a link
if (gateText.substr(0, 4) == "http") {
// Fill a box behind
ctx.font = fontSize + "px Arial";
var textWidth = ctx.measureText(gateText).width;
ctx.fillStyle = "#ffffff";
ctx.fillRect(x-gridX*0.25, y-gridY/2+1, textWidth+gridX*0.05, gridY-2);
// Draw the text
if (!isSelected) {
ctx.fillStyle = "#037a8c";
} else {
ctx.fillStyle = "#003740";
}
ctx.fillText(gateText, floor(x-gridX/2+gridX*0.2), floor(y+gridY/2-fontSize/2));
svgText(gateText, x-gridX/2+gridX*0.2, y+gridY/2-fontSize/2, fontSize, ctx.measureText(gateText).width, ctx.fillStyle);
// If it's normal text
} else {
// More complex LaTeX replaces
gateText = gateText.replace(/\\braket{(.+?)}/g, "\\langle$1\\rangle");
gateText = gateText.replace(/\\bra{(.+?)}/g, "\\langle$1|");
gateText = gateText.replace(/\\ket{(.+?)}/g, "|$1\\rangle");
// Simple LaTeX replaces
gateText = gateText.replace(/\\pm/g, "\u00B1");
gateText = gateText.replace(/\\sqrt/g, "\u221A");
gateText = gateText.replace(/\\rangle/g, "\u27E9");
gateText = gateText.replace(/\\langle/g, "\u27E8");
gateText = gateText.replace(/\\to/g, "\u2192");
gateText = gateText.replace(/\\cdot/g, "\u22C5");
gateText = gateText.replace(/\\times/g, "\u00D7");
gateText = gateText.replace(/\\check/g, "\u2713");
// Greek letters
gateText = gateText.replace(/\\theta/g, "θ");
gateText = gateText.replace(/\\pi/g, "π");
gateText = gateText.replace(/\\psi/g, "ψ");
gateText = gateText.replace(/\\phi/g, "ϕ");
// Fill a box behind
ctx.font = fontSize + "px Arial";
var textWidth = ctx.measureText(gateText).width;
ctx.fillStyle = "#ffffff";
ctx.fillRect(x-gridX*0.25, y-gridY/2+1, textWidth+gridX*0.05, gridY-2);
// Set the colour for both text and LaTeX
if (!isSelected) {
ctx.fillStyle = "#555555";
ctx.strokeStyle = "#555555";
} else {
ctx.fillStyle = "#888888";
ctx.strokeStyle = "#888888";
}
// Draw the text
ctx.fillText(gateText, floor(x-gridX/2+gridX*0.2), floor(y+gridY/2-fontSize/2));
svgText(gateText, x-gridX/2+gridX*0.2, y+gridY/2-fontSize/2, fontSize, ctx.measureText(gateText).width, ctx.fillStyle);
}
// If it's the open settings icon
} else if (letter == "settings") {
// Colours
if (!isSelected) {
ctx.fillStyle = "#555555";
} else {
ctx.fillStyle = "#888888";
}
// Draw the hamburger menu icon
roundRect(ctx, x-15, y-12, 30, 5, 3, true, false);
roundRect(ctx, x-15, y-2, 30, 5, 3, true, false);
roundRect(ctx, x-15, y+8, 30, 5, 3, true, false);
// If it's the logo
} else if (letter == "logo") {
// The outer blue
ctx.fillStyle = "#61baff";
ctx.fillRect(x-gateSize*size*0.5, y-gateSize*size*0.5, gateSize*size, gateSize*size);
// The middle yellow
ctx.fillStyle = "#fbb400";
ctx.fillRect(x-gateSize*size*0.4, y-gateSize*size*0.4, gateSize*size*0.8, gateSize*size*0.8);
// The inner green
ctx.fillStyle = "#00a602";
ctx.fillRect(x-gateSize*size*0.3, y-gateSize*size*0.3, gateSize*size*0.6, gateSize*size*0.6);
// Get rid of the center
ctx.fillStyle = "#dddddd";
ctx.fillRect(x-gateSize*size*0.2, y-gateSize*size*0.2, gateSize*size*0.4, gateSize*size*0.4);
// If it's the load/open icon
} else if (letter == "open") {
// Colours
backCol = "#fcb603";
paperCol = "#ffffff";
frontCol = "#d49f00";
// To save adding this everywhere
x = x-gateSizeFixed/2;
y = y-gateSizeFixed/2+10;
// Draw the back bit of the folder
ctx.fillStyle = backCol;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x, y+40);
ctx.lineTo(x+40, y+40);
ctx.lineTo(x+40, y);
ctx.fill();
// Draw the paper inside the folder
if (!isSelected) {
ctx.fillStyle = paperCol;
ctx.beginPath();
ctx.moveTo(x+10, y+2);
ctx.lineTo(x+10, y+40);
ctx.lineTo(x+35, y+40);
ctx.lineTo(x+35, y+2);
ctx.fill();
} else {
ctx.fillStyle = paperCol;
ctx.beginPath();
ctx.moveTo(x+10, y-5);
ctx.lineTo(x+10, y+40);
ctx.lineTo(x+35, y+40);
ctx.lineTo(x+35, y-5);
ctx.fill();
}
// Draw the front bit of the folder
if (!isSelected) {
ctx.fillStyle = frontCol;
ctx.beginPath();
ctx.moveTo(x+05, y+15);
ctx.lineTo(x, y+40);
ctx.lineTo(x+40, y+40);
ctx.lineTo(x+50, y+15);
ctx.fill();
} else {
ctx.fillStyle = frontCol;
ctx.beginPath();
ctx.moveTo(x+05, y+15);
ctx.lineTo(x, y+40);
ctx.lineTo(x+40, y+40);
ctx.lineTo(x+50, y+15);
ctx.fill();
}
// If it's the save/download icon
} else if (letter == "save") {
// To save adding this everywhere
x = x-gateSizeFixed/2;
y = y-gateSizeFixed/2;
// Main plastic bit
ctx.fillStyle = "#00bbd4";
ctx.beginPath();
ctx.moveTo(x+5, y);
ctx.lineTo(x+5, y+40);
ctx.lineTo(x+40, y+40);
ctx.lineTo(x+40, y+5);
ctx.lineTo(x+35, y);
ctx.fill();
// Top white bit
ctx.fillStyle = "#ffffff";
ctx.beginPath();
ctx.moveTo(x+10, y);
ctx.lineTo(x+10, y+10);
ctx.lineTo(x+30, y+10);
ctx.lineTo(x+30, y);
ctx.fill();
// Black bit inside top white bit
if (!isSelected) {
ctx.fillStyle = "#000000";
ctx.beginPath();
ctx.moveTo(x+22, y);
ctx.lineTo(x+22, y+10);
ctx.lineTo(x+30, y+10);
ctx.lineTo(x+30, y);
ctx.fill();
} else {
ctx.fillStyle = "#000000";
ctx.beginPath();
ctx.moveTo(x+10, y);
ctx.lineTo(x+10, y+10);
ctx.lineTo(x+18, y+10);
ctx.lineTo(x+18, y);
ctx.fill();
}
// Bottom white bit
ctx.fillStyle = "#ffffff";
ctx.beginPath();
ctx.moveTo(x+10, y+20);
ctx.lineTo(x+10, y+40);
ctx.lineTo(x+35, y+40);
ctx.lineTo(x+35, y+20);
ctx.fill();
// Left arrow
} else if (letter == "left") {
// Draw the arrow
if (!isSelected) {
ctx.fillStyle = "#555555";
} else {
ctx.fillStyle = "#888888";
}
ctx.beginPath();
ctx.moveTo(x+gateSizeFixed/3, y-gateSizeFixed/3);
ctx.lineTo(x+gateSizeFixed/3, y+gateSizeFixed/3);
ctx.lineTo(x, y);
ctx.fill();
// Right arrow
} else if (letter == "right") {
// Draw the arrow
if (!isSelected) {
ctx.fillStyle = "#555555";
} else {
ctx.fillStyle = "#888888";
}
ctx.beginPath();
ctx.moveTo(x-gateSizeFixed/3, y-gateSizeFixed/3);
ctx.lineTo(x-gateSizeFixed/3, y+gateSizeFixed/3);
ctx.lineTo(x, y);
ctx.fill();
// If drawing the delete icon
} else if (letter == "delete") {
// To save adding this everywhere
x = x-gateSizeFixed/2;
// Colours
ctx.fillStyle = "#55555588";
ctx.strokeStyle = "#55555588";
// Bottom
ctx.beginPath();
ctx.moveTo(x, y+10);
ctx.lineTo(x+5, y+40);
ctx.lineTo(x+25, y+40);
ctx.lineTo(x+30, y+10);
ctx.fill();
// Lid
ctx.beginPath();
ctx.moveTo(x, y+7);
ctx.lineTo(x+5, y);
ctx.lineTo(x+25, y);
ctx.lineTo(x+30, y+7);
ctx.fill();
// Lid handle
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(x+15, y, 5, Math.PI, 0, false);
ctx.stroke();
// If it's a standard gate
} else {
// If it's a H
if (letter == "H") {
if (!isSelected) {
ctx.fillStyle = "#e3970b";
} else {
ctx.fillStyle = "#bf7e04";
}