-
Notifications
You must be signed in to change notification settings - Fork 2
/
g2q2.scm
2026 lines (1841 loc) · 47.1 KB
/
g2q2.scm
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
;; =============================================================================
;;
;; q2q2.scm
;;
;; Structures made of fundamental gates.
;;
;; =============================================================================
;;
;; Copyright (C) 2018 - 2022 Pablo Edronkin (pablo.edronkin at yahoo.com)
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Lesser General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
;; License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;
;; =============================================================================
;;;; General notes:
;;
;; - Read sources for limitations on function parameters.
;; - Read at least the general notes of all scm files in this library before
;; use. Consider these files as your main documentation for g2q.
;;
;; Sources:
;;
;; - [1] Gidney, C. (2019). Breaking Down the Quantum Swap. [online]
;; Algassert.com. Available at: https://algassert.com/post/1717
;; [Accessed 28 Sep. 2019].
;; - [2] Javadi, A. (2019). How can I implement an n-bit Toffoli gate? [online]
;; Quantum Computing Stack Exchange. Available at: https://quantumcomputing.
;; stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
;; [Accessed 1 Oct. 2019].
;; - [3] Bennet, C. (2019). Logical Reversibility of Computation*. [online]
;; Cs.princeton.edu. Available at: http://www.cs.princeton.edu/courses/archive
;; /fall04/cos576/papers/bennett73.html [Accessed 1 Oct. 2019].
;; - [4] Elementary gates for quantum computation. Adriano Barenco (Oxford U.),
;; Charles H. Bennett (IBM Watson Res. Ctr.), Richard Cleve (Calgary U.),
;; David P. DiVincenzo (IBM Watson Res. Ctr.), Norman Margolus (MIT, LNS),
;; Peter Shor (Bell Labs), Tycho Sleator (New York U.), John Smolin (UCLA),
;; Harald Weinfurter (Innsbruck U.). Mar 1995. 39 pp. Published in Phys.Rev.
;; A52 (1995) 3457. DOI: 10.1103/PhysRevA.52.3457
;; - [5] En.wikipedia.org. (2019). Quantum logic gate. [online] Available at:
;; https://en.wikipedia.org/wiki/Quantum_logic_gate [Accessed 1 Oct. 2019].
;; - [6] IBM Q Experience. (2019). IBM Q Experience. [online] Available at:
;; https://quantum-computing.ibm.com/support/guides/quantum-algorithms-with-qiskit?
;; page=5cbc5e2d74a4010049e1a2b0#qiskit-implementation [Accessed 7 Oct. 2019].
;; - [7] En.wikipedia.org. (2019). Quantum Fourier transform. [online]
;; Available at: https://en.wikipedia.org/wiki/Quantum_Fourier_transform
;; [Accessed 7 Oct. 2019].
;; - [8] IBM Q Experience. (2019). IBM Q Experience. [online] Available at:
;; https://quantum-computing.ibm.com/support/guides/quantum-algorithms-with-qiskit?
;; page=5cc0b79786b50d00642353b9#qiskit-implementation-1
;; [Accessed 7 Oct. 2019].
;; - [9] Nguyen, T. and Meter, R. (2019). A Resource-Efficient Design for a Reversible
;; Floating Point Adder in Quantum Computing. [online] Semanticscholar.org.
;; Available at: https://www.semanticscholar.org/paper/A-Resource-Efficient-Design-
;; for-a-Reversible-Point-Nguyen-Meter/697e4fd8282e1b3cc151956bbb302b0b8e7df22b/
;; figure/13 [Accessed 7 Oct. 2019].
;; - [10] IBM Q Experience. (2019). IBM Q Experience. [online] Available at:
;; https://quantum-computing.ibm.com/support/guides/user-guide?
;; page=5ddae9d75d640300671cc60f [Accessed 16 Dec. 2019].
;; - [11] En.wikipedia.org. (2019). Greenberger–Horne–Zeilinger state. [online] Available at:
;; https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state
;; [Accessed 16 Dec. 2019].
;; - [12] Uchida, G., Bertlmann, R. and Hiesmayr, B. (2019). Entangled
;; entanglement: A construction procedure. [online] Arxiv.org. Available at:
;; https://arxiv.org/abs/1410.7145 [Accessed 21 Dec. 2019].
;; - [13] Cruz, D., Fournier, R., Gremion, F., Jeannerot, A., Komagata, K.,
;; Tosic, T., Thiesbrummel, J., Chan, C., Macris, N., Dupertuis, M. and
;; Javerzac‐Galy, C. (2019). Efficient Quantum Algorithms for GHZ and W
;; States, and Implementation
;; on the IBM Quantum Computer. [online] Wiley Online Library. Available at:
;; https://onlinelibrary.wiley.com/doi/full/10.1002/qute.201900015
;; [Accessed 21 Dec. 2019].
;; - [14] IBM Quantum Experience. 2020. IBM Quantum Experience - Docs And
;; Resources. [online] Available at:
;; https://quantum-computing.ibm.com/docs/guide/err-corxn/quantum-repetition-code
;; [Accessed 16 April 2020].
;; - [15] https://arxiv.org/abs/1707.03429 , arXiv:1707.03429v2 [quant-ph], Open
;; Quantum Assembly Language, Andrew W. Cross, Lev S. Bishop, John A. Smolin,
;; Jay M. Gambetta.
;; - [16] Casey, K. (2017). Archived | Quantum computing in action: IBM's Q
;; experience and the quantum shell game. [online] IBM Developer. Available
;; at: https://developer.ibm.com/tutorials/os-quantum-computing-shell-game/
;; [Accessed 30 Sep. 2019].
;; - [17] https://quantum-computing.ibm.com/composer/docs/iqx/operations_glossary#obs-ops
(define-module (g2q g2q2)
#:use-module (g2q g2q0)
#:use-module (g2q g2q1)
#:use-module (grsp grsp0)
#:use-module (grsp grsp1)
#:use-module (ice-9 regex)
#:use-module (ice-9 textual-ports)
#:export (qconst
g1y
g1x
g1xy
qmeasy
cx
cz
cz-fast
cy
cy-fast
ch
ch-fast
ccx
ccx-fast
rx
rx-fast
ry
ry-fast
rz
rz-fast
crz
crz-fast
cu1
cu1-fast
cu3
cu3-fast
g1cxg1
qendc
qregex
qreq
qcnot1
qxor1
qfclvr
qfres
swap-fast
qftyn
qftdgyn
cswap
cx-ladder
swap-fast-ladder
swap-ladder
ghzy
g1yl
ecc1
ecc2
ecc3
hx
hy
hz
hs
hsdg
ht
htdg
barrier
rxx
rzz
rccx
rc3x))
;; qconst - Sets the values of various required constants.
;;
;; Keywords:
;;
;; - constants, invariant, values, required, pi, euler, angular
;;
;; Parameters:
;;
;; - p_s1: string, constant name.
;; - "Pi".
;; - "Pi/2".
;; - "gr".
;; - "e".
;;
;; Notes:
;;
;; - See [17] for more on default angle values for IBM Quantum Composer,
;;
;; Output:
;;
;; - Returns the value of p_s1 if it exists. Zero otherwise.
;;
(define (qconst p_s1)
(let ((res1 0.0))
(cond ((equal? p_s1 "Pi")
(set! res1 (gconst "A000796")))
((equal? p_s1 "Pi/2")
(set! res1 (/ (qconst "Pi") 2)))
((equal? p_s1 "gr")
(set! res1 (gconst "gr")))
((equal? p_s1 "e")
(set! res1 (gconst "A001113"))))
res1))
;; g1y - Repeats placement of gate p_g1 and group p_r1 by repeating the use of
;; qgate1 from qubit p_y1 to qubit p_y2 on y axis (vertically on graphical
;; representation).
;;
;; Keywords:
;;
;; - multiple, gates, base, array, graphic, vertical, representation, paradigm
;;
;; Parameters:
;;
;; - p_g1: string, gate name.
;; - p_r1: string, quantum register name.
;; - p_y1: numeric, ordinal of the initial qubit.
;; - p_y2: numeric, ordinal of the last qubit.
;;
(define (g1y p_g1 p_r1 p_y1 p_y2)
(qcomg "g1y" 0)
(let loop ((i1 p_y1))
(if (= i1 p_y2)
(g1 p_g1 p_r1 i1)
(begin (g1 p_g1 p_r1 i1)
(loop (+ i1 1)))))
(qcomg "g1y" 1))
;; g1x - Repeats placement of gate p_g1 and register p_r1 by repeating the use
;; of qgate1 on regster element p_y1 p_x1 times on x axis.
;;
;; Keywords:
;;
;; - multiple, gates, cloning, horizontal, array, representation, paradigm
;;
;; Parameters:
;;
;; - p_g1: string, gate name.
;; - p_r1: string, quantum register name.
;; - p_y1: register element.
;; - p_x1: numeric, number of iterations on x.
;;
(define (g1x p_g1 p_r1 p_y1 p_x1)
(qcomg "g1x" 0)
(let loop ((i1 1))
(if (= i1 p_x1)
(g1 p_g1 p_r1 p_y1)
(begin (g1 p_g1 p_r1 p_y1)
(loop (+ i1 1)))))
(qcomg "g1x" 1))
;; g1xy - Repeats placement of gate p_g1 and group p_r1 by repeating the use of
;; qgate1 from qubit p_y1 to qubit p_y2 on y axis.
;;
;; Keywords:
;;
;; - multiple, gates, cloning, vertical, horizontal, array, representation
;;
;; Parameters:
;;
;; - p_g1: string, gate name.
;; - p_r1: string, quantum register name.
;; - p_y1: ordinal of the initial qubit.
;; - p_y2: ordinal of the last qubit.
;; - p_x1: numeric, number of iterations that g1y will be repeated along x
;; axis of sequence as a graph.
;;
(define (g1xy p_g1 p_r1 p_y1 p_y2 p_x1)
(qcomg "g1xy" 0)
(let loop ((j1 1))
(if (= j1 p_x1)
(g1y p_g1 p_r1 p_y1 p_y2)
(begin (g1y p_g1 p_r1 p_y1 p_y2)
(loop (+ j1 1)))))
(qcomg "g1xy" 1))
;; qmeasy - Performs measurements on group p_r1 to group p_r2 from p_y1 to p_y2.
;;
;; Keywords:
;;
;; - multiple, measurement. observer, observation, vertical, array,
;; representation
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name.
;; - p_r2: conventional register name.
;; - p_y1: numeric, ordinal of the initial qubit.
;; - p_y2: numeric, ordinal of the last qubit.
;;
(define (qmeasy p_r1 p_r2 p_y1 p_y2)
(qcomg "qmeasy" 0)
(let loop ((i1 p_y1))
(if (= i1 p_y2)
(qmeas p_r1 p_y2 p_r2 p_y2)
(begin (qmeas p_r1 i1 p_r2 i1)
(loop (+ i1 1)))))
(qcomg "qmeasy" 1))
;; cx - Gate cx, performs a NOT operation on the target qubit if the control
;; qubit is |1>. Leaves target qubit as it is otherwise.
;;
;; Keywords:
;;
;; - conditional, operation, not
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name 1.
;; - p_y1: control qubit 1 (represented as a dot in a q diagram).
;; - p_r2: string, quantum register name 2.
;; - p_y2: target qubit 2 (represented as a plus).
;;
(define (cx p_r1 p_y1 p_r2 p_y2)
(g2 "cx" p_r1 p_y1 p_r2 p_y2))
;; cz - Gate cz, controlled phase expressed atomically. Gates expressed in
;; this way are universally compatible but more error - prone than the same
;; gates expressed in fast form.
;;
;; Keywords:
;;
;; - universally, compatible, atomic
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name 1.
;; - p_y1: control qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: target qubit 2.
;;
(define (cz p_r1 p_y1 p_r2 p_y2)
(qcomg "cz" 0)
(g1 "h" p_r1 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(g1 "h" p_r2 p_y2)
(qcomg "cz" 1))
;; cz-fast - Gate cz, controlled y in fast form. Such a form is generally
;; less error - prone that atomic variants, but they might not be
;; compatible with all sorts and makes of qpu.
;;
;; Keywords:
;;
;; - error, free, less, fast, quick, brief
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name 1.
;; - p_y1: control qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: target qubit 2.
;;
;; Notes:
;;
;; - Obsolete in IBM Quantum Composer, 2022, see [17].
;;
(define (cz-fast p_r1 p_y1 p_r2 p_y2)
(g2 "cz-fast" p_r1 p_y1 p_r2 p_y2))
;; cy - Gate cy, controlled y expressed atomically.
;;
;; Keywords:
;;
;; - atomic, controlled, gate
;;
;; Parameters:
;;
;; - p_r1: quantum register name 1.
;; - p_y1: control qubit 1.
;; - p_r2: quantum register name 2.
;; - p_y2: target qubit 2.
;;
;; Notes:
;;
;; - Obsolete in IBM Quantum Composer, 2022, see [17].
;;
(define (cy p_r1 p_y1 p_r2 p_y2)
(qcomg "cy" 0)
(g1 "sdg" p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(g1 "s" p_r2 p_y2)
(qcomg "cy" 1))
;; cy-fast - Gate cy, controlled y in fast form.
;;
;; Keywords:
;;
;; - fast, controlled, gate, quick, brief
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name 1.
;; - p_y1: control qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: target qubit 2.
;;
(define (cy-fast p_r1 p_y1 p_r2 p_y2)
(g2 "cy-fast" p_r1 p_y1 p_r2 p_y2))
;; ch - Gate ch, controlled h expressed atomically.
;;
;; Keywords:
;;
;; - atomic, controlled, gate
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name 1.
;; - p_y1: control qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: target qubit 2.
;;
;; Notes:
;;
;; - Obsolete in IBM Quantum Composer, 2022, see [17].
;;
(define (ch p_r1 p_y1 p_r2 p_y2)
(qcomg "ch" 0)
(g1 "h" p_r2 p_y2)
(g1 "sdg" p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(ht p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(g1 "t" p_r2 p_y2)
(hs p_r2 p_y2)
(g1 "x" p_r2 p_y2)
(g1 "s" p_r1 p_y1)
(qcomg "ch" 1))
;; ch-fast - Gate ch, controlled h in fast form.
;;
;; Keywords:
;;
;; - controled, hadamard, gate, quick, brief
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name 1.
;; - p_y1: control qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: target qubit 2.
;;
(define (ch-fast p_r1 p_y1 p_r2 p_y2)
(g2 "ch-fast" p_r1 p_y1 p_r2 p_y2))
;; ccx - Gate ccx, Toffoli gate expressed atomically, double controlled NOT.
;;
;; Keywords:
;;
;; - atomic, toffoli
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name 1.
;; - p_y1: control qubit 1 (dot).
;; - p_r2: string, quantum register name 2.
;; - p_y2: control qubit 2 (dot).
;; - p_r3: string, quantum register name 3.
;; - p_y3: target qubit 3 (plus).
;;
;; Sources:
;;
;; - [1][2][3][4][5].
;;
(define (ccx p_r1 p_y1 p_r2 p_y2 p_r3 p_y3)
(qcomg "ccx" 0)
(g1 "h" p_r3 p_y3)
(cx p_r2 p_y2 p_r3 p_y3)
(g1 "tdg" p_r3 p_y3)
(cx p_r1 p_y1 p_r3 p_y3)
(g1 "t" p_r3 p_y3)
(cx p_r2 p_y2 p_r3 p_y3)
(g1 "tdg" p_r3 p_y3)
(cx p_r1 p_y1 p_r3 p_y3)
(g1 "t" p_r2 p_y2)
(g1 "t" p_r3 p_y3)
(g1 "h" p_r3 p_y3)
(cx p_r1 p_y1 p_r2 p_y2)
(g1 "t" p_r1 p_y1)
(g1 "tdg" p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(qcomg "ccx" 1))
;; ccx-fast - Toffoli gate in fast form.
;;
;; Keywords:
;;
;; - toffoli, fast, quick, brief
;;
;; Parameters:
;;
;; - p_r1: string, quantum register name 1.
;; - p_y1: control qubit 1 (dot).
;; - p_r2: string, quantum register name 2.
;; - p_y2: control qubit 2 (dot).
;; - p_r3: string, quantum register name 3.
;; - p_y3: target qubit 3 (plus).
;;
(define (ccx-fast p_r1 p_y1 p_r2 p_y2 p_r3 p_y3)
(display (strings-append (list "ccx "
(qbgna p_r1 p_y1)
","
(qbgna p_r1 p_y2)
","
(qbgna p_r3 p_y3)
(g2q-txt 2))
0)))
;; rx - Gate rx, rotation around X-axis.
;;
;; Keywords:
;;
;; - rotation, x, axial, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, theta angle.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;;
(define (rx p_t1 p_r1 p_y1)
(let ((y2 (/ (qconst "Pi") 2)))
(u3 p_t1 (* y2 -1) y2 p_r1 p_y1)))
;; rx-fast - Gate rx, rotation around X-axis, in fast form.
;;
;; Keywords:
;;
;; - rotation, axial, fast, quick, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, theta angle; this is a dummy arument, left for consistency.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;;
(define (rx-fast p_t1 p_r1 p_y1)
(display (strings-append (list "rx("
(grsp-n2s (/ (qconst "Pi") 2))
(g2q-txt 4)
p_r1
"["
(grsp-n2s p_y1)
(g2q-txt 3))
0)))
;; ry - Gate ry, rotation around Y-axis.
;;
;; Keywords:
;;
;; - rotation, axial, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, theta angle.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;;
(define (ry p_t1 p_r1 p_y1)
(u3 p_t1 0 0 p_r1 p_y1))
;; ry-fast - Gate ry, rotation around Y-axis, in fast form.
;;
;; Keywords:
;;
;; - rotation, axial, fast, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, angle (dummy, left for consistency with other functions).
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;;
(define (ry-fast p_t1 p_r1 p_y1)
(display (strings-append (list "ry("
(grsp-n2s (/ (qconst "Pi") 2))
(g2q-txt 4)
p_r1
"["
(grsp-n2s p_y1)
(g2q-txt 3))
0)))
;; rz - Gate rz, rotation around Z-axis.
;;
;; Keywords:
;;
;; - rotation, axial, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, angle 1 (default value should normally be (qconst "Pi/2")).
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;;
;; Notes:
;;
;; - TODO: check p_t1
;; - See [17] for more on default angle values for IBM Quantum Composer,
;;
(define (rz p_t1 p_r1 p_y1)
(display (strings-append (list "rz("
(grsp-n2s p_t1)
(g2q-txt 4)
p_r1
"["
(grsp-n2s p_y1)
(g2q-txt 3))
0)))
;; rz-fast - Gate rz, rotation around Z-axis, in fast form.
;;
;; Keywords:
;;
;; - rotation, axial, fast, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, angle 1 (default value should normally be (qconst "Pi/2")).
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;;
;; Notes:
;;
;; - Obsolete. Deprecated.
;; - See [17] for more on default angle values for IBM Quantum Composer,
;;
(define (rz-fast p_t1 p_r1 p_y1)
(rz p_t1 p_r1 p_y1))
;; crz - Gate crz, controlled rz expressed atomically.
;;
;; Keywords:
;;
;; - controlled, axial, rotation, atomic, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, angle 1.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: qubit 2.
;;
;; Notes:
;;
;; - Obsolete in IBM Quantum Composer, 2022, see [17].
;;
(define (crz p_t1 p_r1 p_y1 p_r2 p_y2)
(let ((t1 (/ p_t1 2)))
(qcomg "crz" 0)
(u1 t1 p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(u1 (* -1.0 t1) p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(qcomg "crz" 1)))
;; crz-fast - Gate crz, controlled rz expressed in fast form.
;;
;; Keywords:
;;
;; - controlled, axial, rotation, fast, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, angle 1. Dummy for compat with gate crz.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: qubit 2.
;;
(define (crz-fast p_t1 p_r1 p_y1 p_r2 p_y2)
(display (strings-append (list "crz("
(grsp-n2s (/ (qconst "Pi") 2))
(g2q-txt 4)
p_r1
"["
(grsp-n2s p_y1)
"],"
p_r2
"["
(grsp-n2s p_y2)
(g2q-txt 3))
0)))
;; cu1 - Gate cu1, controlled phase rotation expressed atomically.
;;
;; Keywords:
;;
;; - phase, rotation, pivot, axial, atomic, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, angle 1.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: qubit 2.
;;
;; Notes:
;;
;; - TODO: check t1 and p_t1.
;; - Obsolete in IBM Quantum Composer, 2022, see [17].
;;
(define (cu1 p_t1 p_r1 p_y1 p_r2 p_y2)
(let ((t1 (* p_y1 0.5))) ;; ***
(qcomg "cu1" 0)
(u1 t1 p_r1 p_y1)
(cx p_r1 p_y1 p_r2 p_y2)
(u1 (* -1.0 t1) p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(u1 t1 p_r1 p_y1)
(qcomg "cu1" 1)))
;; cu1-fast - Gate cu1, controlled phase rotation gate expressed in fast form.
;;
;; Keywords:
;;
;; - phase, rotation, pivot, axial, fast, controlled, rotor
;;
;; Parameters:
;;
;; - p_t1: numeric, angle 1.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: qubit 2.
;;
(define (cu1-fast p_t1 p_r1 p_y1 p_r2 p_y2)
(display (strings-append (list "cu1("
(grsp-n2s p_t1)
(g2q-txt 4)
p_r1
"["
(grsp-n2s p_y1)
"],"
p_r2
"["
(grsp-n2s p_y2)
(g2q-txt 3))
0)))
;; cu3 - Gate cu3, controlled U gate expressed atomically.
;;
;; Keywords:
;;
;; - controlled, atomic, fast, unitary
;;
;; Parameters:
;;
;; - p_t1: numeric, angle 1.
;; - p_t2: numeric, angle 2.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: qubit 2.
;;
;; Notes:
;;
;; - Obsolete in IBM Quantum Composer, 2022, see [17].
;;
(define (cu3 p_t1 p_t2 p_r1 p_y1 p_r2 p_y2)
(qcomg "cu3" 0)
(u1 (* (- p_t1 p_t2) 0.5) p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(u3 (* (* p_t1 0.5) -1.0) 0 (* (* (+ p_t2 p_t1) 0.5) -1.0) p_r2 p_y2)
(cx p_r1 p_y1 p_r2 p_y2)
(u3 (* p_t1 0.5) p_t2 0 p_r2 p_y2)
(qcomg "cu3" 1))
;; cu3-fast - Gate cu3, controlled U gate expressed in fast form.
;;
;; Keywords:
;;
;; - rotation, pivot, axial, fast, rotor, unitary
;;
;; Parameters:
;;
;; - p_t1: numeric, angle 1.
;; - p_t2: numeric, angle 2.
;; - p_t3: numeric, angle 3.
;; - p_r1: string, quantum register name 1.
;; - p_y1: qubit 1.
;; - p_r2: string, quantum register name 2.
;; - p_y2: qubit 2.
;;
(define (cu3-fast p_t1 p_t2 p_t3 p_r1 p_y1 p_r2 p_y2)
(display (strings-append (list "cu3("
(grsp-n2s p_t1)
","
(grsp-n2s p_t2)
","
(grsp-n2s p_t3)
(g2q-txt 4)
p_r1
"["
(grsp-n2s p_y1)
"],"
p_r2
"["
(grsp-n2s p_y2)
(g2q-txt 3))
0)))
;; g1cxg1 - Puts a set of gates in configuration g1 cx g1.
;;
;; Keywords:
;;
;; - setting, state, gates, array, batch
;;
;; Parameters:
;;
;; - p_g1: string, g1 gate name.
;; - p_r1: string, quantum register name (i.e. q).
;; - p_y1: numeric, qubit number 1 (control of cx).
;; - p_y2: numeric, qubit number 2 (target of cx).
;;
(define (g1cxg1 p_g1 p_r1 p_y1 p_y2)
(qcomg "g1cxg1" 0)
(g1 p_g1 p_r1 p_y2)
(cx p_r1 p_y1 p_r1 p_y2)
(g1 p_g1 p_r1 p_y2)
(qcomg "g1cxg1" 1))
;; qendc - Prints a message stating that compilation has ended.
;;
;; Keywords:
;;
;; - messaging, standard
;;
(define (qendc)
(ptit "=" 60 2 "Compilation completed!"))
;; qregex - Prepares a compiled qasm file as a string for passing to ibm q html
;; api.
;;
;; Keywords:
;;
;; - compiler, preparation, processor, processing
;;
;; Parameters:
;;
;; - p_f1: string, name of .qasm file.
;;
;; Output:
;;
;; - It returns p_f1 as a single string variable without any \r or \n
;; characters.
;;
(define (qregex p_f1)
(let ((res1 p_f1))
(set! res1 (regexp-substitute/global #f "[\n]+" res1 'pre "" 'post))
(set! res1 (regexp-substitute/global #f "[\r]+" res1 'pre "" 'post))
(set! res1 (regexp-substitute/global #f "[\"]+" res1 'pre "\\\"" 'post))
res1))
;; qreq - Constructs a qreg file.
;;
;; Keywords:
;;
;; - file, creation, qreg
;;
;; Parameters;
;;
;; - p_f1: string, name of .qasm file.
;; - p_f2: string, name of .qreg file.
;; - p_p1: string, where results will be saved to:
;; - "json" to save results to a json file.
;; - "sqlite3" to save results to a sqlite3 database.
;; - p_d1: device.
;; - p_s6: shots.
;; - p_m1: max credits.
;; - p_e1: seed.
;;
;; Sources:
;;
;; - [16].
;;
(define (qreq p_f1 p_f2 p_p1 p_d1 p_s6 p_m1 p_e1)
(let ((port1 (current-output-port))
(port2 (open-output-file p_f2))
(conf #f)
(s1 "")
(s2 "")
(s3 "")
(s4 "")
(token "")
(dev p_d1)
(data ""))
(set-port-encoding! port2 "UTF-8")
;; Get GX conf data
(set! conf (g2q-ibm-config))
(set! s1 (car conf))
(set! token (cadr conf))
(set! s2 (caddr conf))
;; Write to .comm file
(set! data (read-file-as-string p_f1))
(set! data (qregex data))
(set-current-output-port port2)
(qstr (strings-append (list "base-file=" p_f2) 0))
(qstr " ")
(qstr (strings-append (list "base-data=" data) 0))
(qstr " ")
(qstr (strings-append (list "base-token=" token) 0))
(qstr " ")
(qstr (strings-append (list "base-uri=" s1) 0))
(qstr " ")
(qstr (strings-append (list "base-results-storage=" p_p1) 0))
(qstr " ")
(qstr (strings-append (list "base-device=" p_d1) 0))
(qstr " ")
(qstr (strings-append (list "base-shots=" (number->string p_s6)) 0))
(qstr " ")
(qstr (strings-append (list "base-seed=" (number->string p_e1)) 0))
(qstr " ")
(qstr (strings-append (list "base-max-credits=" (number->string p_m1)) 0))
(qstr " ")
(qstr (strings-append (list "login-data=" "apiToken=" token) 0))
(qstr " ")
(qstr (strings-append (list "login-uri=" s1 "/users/loginWithToken") 0))
(qstr " ")
(qstr (strings-append (list "login-id=" "wait-until-login") 0))
(qstr " ")
(qstr (strings-append (list "post-content-type="
(g2q-txt 8)
(g2q-txt 7))
0))
(qstr " ")
(qstr (strings-append (list "post-uri=" (string-append s1 s2)) 0))
(qstr " ")
(qstr (strings-append (list "get-content-type=" (g2q-txt 8) (g2q-txt 7)) 0))
(qstr " ")
(qstr (strings-append (list "get-uri=" (string-append s1 s3)) 0))
(qstr " ")
(qstr (strings-append (list "delete-content-type="
(g2q-txt 8)
(g2q-txt 7))
0))
(qstr " ")
(qstr (strings-append (list "delete-uri=" (string-append s1 s4)) 0))
(qstr " ")
(set-current-output-port port1)
(close port2)))
;; qcnot1 - A cx based NOT gate expressed atomically.
;;
;; Keywords:
;;
;; - controlled, not, negation, atomic
;;
;; Parameters:
;;
;; - p_r1: string, gate group name 1.
;; - p_y1: target qubit, normally p_y2 - 1.
;; - p_r2: string, gate group name 2.
;; - p_y2: control qubit, value to be inverted, 0 or 1.