Skip to content

Commit c1fc631

Browse files
committed
Improved code.
1 parent 42286a0 commit c1fc631

23 files changed

+2433
-2303
lines changed

examples/example1.scm

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,110 +2,109 @@
22
!#
33

44

5-
; ==============================================================================
6-
;
7-
; example1.scm
8-
;
9-
; A sample of different gates and functions of g2q. Note that this program does
10-
; not intend to be a functional one. It is just a way to demostrate what gates
11-
; and functions you can write and how. Don't worry about the sense it doesn't
12-
; make as a practical program.
13-
;
14-
; Compilation:
15-
;
16-
; - cd to your /examples folder.
17-
;
18-
; - Enter the following:
19-
;
20-
; guile example1.scm
21-
;
22-
; Note that quantum processors in practice may or may not have full connectivity
23-
; and thus not all connections between qubits may work. You may need to modify
24-
; qubit numbers or generate new gate sequences to work around such limitations
25-
; in this and other programs.
26-
;
27-
; ==============================================================================
28-
;
29-
; Copyright (C) 2018 - 2020 Pablo Edronkin (pablo.edronkin at yahoo.com)
30-
;
31-
; This program is free software: you can redistribute it and/or modify
32-
; it under the terms of the GNU Lesser General Public License as published by
33-
; the Free Software Foundation, either version 3 of the License, or
34-
; (at your option) any later version.
35-
;
36-
; This program is distributed in the hope that it will be useful,
37-
; but WITHOUT ANY WARRANTY; without even the implied warranty of
38-
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39-
; GNU Lesser General Public License for more details.
40-
;
41-
; You should have received a copy of the GNU Lesser General Public License
42-
; along with this program. If not, see <https://www.gnu.org/licenses/>.
43-
;
44-
; ==============================================================================
45-
46-
47-
; Modules. These will be almost always required.
5+
;; =============================================================================
6+
;;
7+
;; example1.scm
8+
;;
9+
;; A sample of different gates and functions of g2q. Note that this program does
10+
;; not intend to be a functional one. It is just a way to demostrate what gates
11+
;; and functions you can write and how. Don't worry about the sense it doesn't
12+
;; make as a practical program.
13+
;;
14+
;; Compilation:
15+
;; - cd to your /examples folder.
16+
;;
17+
;; - Enter the following:
18+
;;
19+
;; guile example1.scm
20+
;;
21+
;; Note that quantum processors in practice may not have full connectivity
22+
;; and thus not all connections between qubits may work. You may need to modify
23+
;; qubit numbers or generate new gate sequences to work around such limitations
24+
;; in this and other programs.
25+
;;
26+
;; =============================================================================
27+
;;
28+
;; Copyright (C) 2018 - 2020 Pablo Edronkin (pablo.edronkin at yahoo.com)
29+
;;
30+
;; This program is free software: you can redistribute it and/or modify
31+
;; it under the terms of the GNU Lesser General Public License as published by
32+
;; the Free Software Foundation, either version 3 of the License, or
33+
;; (at your option) any later version.
34+
;;
35+
;; This program is distributed in the hope that it will be useful,
36+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
37+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38+
;; GNU Lesser General Public License for more details.
39+
;;
40+
;; You should have received a copy of the GNU Lesser General Public License
41+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
42+
;;
43+
;; =============================================================================
44+
45+
46+
;; Modules. These will be almost always required.
4847
(use-modules (g2q g2q0)
4948
(g2q g2q1)
5049
(g2q g2q2))
5150

5251

53-
; Vars and initial stuff.
52+
;; Vars and initial stuff.
5453
(define fname "example1.qasm")
5554
(define cname "example1.qreg")
5655
(define qver 2.0)
5756
(define q "q")
5857
(define c "c")
5958

60-
; This configures the output to be sent a file instead of the console. If you
61-
; take out or disable these lines, and those closing the output port (see at
62-
; the bottom) instead of getting a qasm file you will see the compiled lines
63-
; on the console.
59+
;; This configures the output to be sent a file instead of the console. If you
60+
;; take out or disable these lines, and those closing the output port (see at
61+
;; the bottom) instead of getting a qasm file you will see the compiled lines
62+
;; on the console.
6463
(define port1 (current-output-port))
6564
(define port2 (open-output-file fname))
6665
(set-current-output-port port2)
6766

68-
; Remember what this program is about (optional).
67+
;; Remember what this program is about (optional).
6968
(qcomm "This program does nothing in particular, but shows how to use g2q functions.")
7069

71-
; Creating header and required vectors.
70+
;; Creating header and required vectors.
7271
(qhead fname qver)
7372
(qregdef q 5 c 5)
7473

75-
; Reset gates.
74+
;; Reset gates.
7675
(qcomm "Five reset gates.")
7776
(g1y "reset" q 0 4)
7877

79-
; A swap gate.
78+
;; A swap gate.
8079
(qcomm "A swap gate.")
8180
(swap q 0 1)
8281

83-
; A swap gate. Swap fast gates are better than their non-fast counterparts - in
84-
; this case this means that swap-fast is faster in execution than swap - but
85-
; might not be recognized by all sorts of QPU, be them simulators or real
86-
; processors. For such cases, I have written non-fast versions of several gates.
87-
; These non-fast gates are atomic decompositions of swap-fast gates into gates
88-
; that can be recognized by a wider variety of QPU. So, in principle, if you
89-
; can, use the fast version of a gate when it is available because it will run
90-
; faster and with a lesser error rate, but if you can't or your QPU does not
91-
; recongize the fast gate in question, use the non-fast version.
82+
;; A swap gate. Swap fast gates are better than their non-fast counterparts - in
83+
;; this case this means that swap-fast is faster in execution than swap - but
84+
;; might not be recognized by all sorts of QPU, be them simulators or real
85+
;; processors. For such cases, I have written non-fast versions of several gates.
86+
;; These non-fast gates are atomic decompositions of swap-fast gates into gates
87+
;; that can be recognized by a wider variety of QPU. So, in principle, if you
88+
;; can, use the fast version of a gate when it is available because it will run
89+
;; faster and with a lesser error rate, but if you can't or your QPU does not
90+
;; recongize the fast gate in question, use the non-fast version.
9291
(qcomm "A swap-fast gate.")
9392
(swap-fast q 0 1)
9493

95-
; This places five Hadamard gates on y axis (i.e. in parallell), from register
96-
; 0 to register 4. Notice that function qcomm will print comments on the qasm
97-
; file, while these comments, starting will ; only appear in source files such
98-
; as this one.
94+
;; This places five Hadamard gates on y axis (i.e. in parallell), from register
95+
;; 0 to register 4. Notice that function qcomm will print comments on the qasm
96+
;; file, while these comments, starting will ; only appear in source files such
97+
;; as this one.
9998
(qcomm "Five h gates.")
10099
(g1y "h" q 0 4)
101100

102-
; Now we do the same for a Pauli X, just for fun, but on the three first
103-
; registers. You can view the source code for modules g2q0, g2q1 and g2q2 for
104-
; more specific info about these funtctions. Each one is commented there.
101+
;; Now we do the same for a Pauli X, just for fun, but on the three first
102+
;; registers. You can view the source code for modules g2q0, g2q1 and g2q2 for
103+
;; more specific info about these funtctions. Each one is commented there.
105104
(qcomm "Two x gates.")
106105
(g1y "x" q 1 2)
107106

108-
; And we add other gates, one by one.
107+
;; And we add other gates, one by one.
109108
(qcomm "Various basic gates.")
110109
(g1 "id" q 3)
111110
(g1 "y" q 4)
@@ -118,7 +117,7 @@
118117
(u2 1.6 1.6 q 1)
119118
(u3 1.6 1.6 1.6 q 2)
120119

121-
; cx, cy, cz and ch gates.
120+
;; cx, cy, cz and ch gates.
122121
(qcomm "Controlled x.")
123122
(cx q 2 q 0)
124123
(qcomm "Controlled y.")
@@ -134,13 +133,13 @@
134133
(qcomm "Controlled h in fast form.")
135134
(ch-fast q 2 q 0)
136135

137-
; Toffoli stuff.
136+
;; Toffoli stuff.
138137
(qcomm "Toffoli (ccx).")
139138
(ccx q 0 q 1 q 2)
140139
(qcomm "Toffoli in fast form.")
141140
(ccx-fast q 0 q 1 q 2)
142141

143-
; Rotations and others.
142+
;; Rotations and others.
144143
(qcomm "rx.")
145144
(rx 1.6 q 1)
146145
(qcomm "rx in fast form.")
@@ -166,40 +165,40 @@
166165
(qcomm "Controlled u3 in fast form.")
167166
(cu3-fast 1.6 1.6 1.6 q 2 q 0)
168167

169-
; if (Not yet available on IBM Q machines yet - uncomment to see how they
170-
; compile but the resulting program may not run on a Q series machine at
171-
; least for now.
168+
;; if (Not yet available on IBM Q machines yet - uncomment to see how they
169+
;; compile but the resulting program may not run on a Q series machine at
170+
;; least for now.
172171
(qcomm "Conditional1 eq.")
173172
(qcond1 "==" q 1)(g1 "y" q 2)
174173
(qcomm "Conditional2 eq.")
175174
(qcond2 "!=" q 2 1)(g1 "y" q 2)
176175

177-
; GHZ state stuff.
176+
;; GHZ state stuff.
178177
(qcomm "Three qubit GHZ state generation")
179178
(ghzy "h" "x" q 0 2 1)
180179
(ghzy "h" "x" q 0 2 2)
181180

182-
; Using lists.
181+
;; Using lists.
183182
(g1yl q '("h" "h" "h" "h" "sdg") 0)
184183
(g1yl q '("x" "h" "h" "sdg") 1)
185184

186-
; Let's put a barrier.
185+
;; Let's put a barrier.
187186
(qcomm "Barriers.")
188187
(g1y "barrier" q 0 4)
189188

190-
; And now measure.
189+
;; And now measure.
191190
(qcomm "Measuring")
192191
(qmeas q 0 c 0)
193192
(qmeas q 1 c 1)
194193
(qmeas q 2 c 2)
195194
(qmeas q 3 c 3)
196195
(qmeas q 4 c 4)
197196

198-
; Message
197+
;; Message
199198
(qcomm "This message will appear at the end of the qasm file.")
200199

201-
; Sets the output port again to the console. Don't forget to check if the
202-
; compilation is error free or you have some bugs to kill.
200+
;; Sets the output port again to the console. Don't forget to check if the
201+
;; compilation is error free or you have some bugs to kill.
203202
(set-current-output-port port1)
204203
(close port2)
205204
(qendc)

0 commit comments

Comments
 (0)