|
2 | 2 | !# |
3 | 3 |
|
4 | 4 |
|
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. |
48 | 47 | (use-modules (g2q g2q0) |
49 | 48 | (g2q g2q1) |
50 | 49 | (g2q g2q2)) |
51 | 50 |
|
52 | 51 |
|
53 | | -; Vars and initial stuff. |
| 52 | +;; Vars and initial stuff. |
54 | 53 | (define fname "example1.qasm") |
55 | 54 | (define cname "example1.qreg") |
56 | 55 | (define qver 2.0) |
57 | 56 | (define q "q") |
58 | 57 | (define c "c") |
59 | 58 |
|
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. |
64 | 63 | (define port1 (current-output-port)) |
65 | 64 | (define port2 (open-output-file fname)) |
66 | 65 | (set-current-output-port port2) |
67 | 66 |
|
68 | | -; Remember what this program is about (optional). |
| 67 | +;; Remember what this program is about (optional). |
69 | 68 | (qcomm "This program does nothing in particular, but shows how to use g2q functions.") |
70 | 69 |
|
71 | | -; Creating header and required vectors. |
| 70 | +;; Creating header and required vectors. |
72 | 71 | (qhead fname qver) |
73 | 72 | (qregdef q 5 c 5) |
74 | 73 |
|
75 | | -; Reset gates. |
| 74 | +;; Reset gates. |
76 | 75 | (qcomm "Five reset gates.") |
77 | 76 | (g1y "reset" q 0 4) |
78 | 77 |
|
79 | | -; A swap gate. |
| 78 | +;; A swap gate. |
80 | 79 | (qcomm "A swap gate.") |
81 | 80 | (swap q 0 1) |
82 | 81 |
|
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. |
92 | 91 | (qcomm "A swap-fast gate.") |
93 | 92 | (swap-fast q 0 1) |
94 | 93 |
|
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. |
99 | 98 | (qcomm "Five h gates.") |
100 | 99 | (g1y "h" q 0 4) |
101 | 100 |
|
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. |
105 | 104 | (qcomm "Two x gates.") |
106 | 105 | (g1y "x" q 1 2) |
107 | 106 |
|
108 | | -; And we add other gates, one by one. |
| 107 | +;; And we add other gates, one by one. |
109 | 108 | (qcomm "Various basic gates.") |
110 | 109 | (g1 "id" q 3) |
111 | 110 | (g1 "y" q 4) |
|
118 | 117 | (u2 1.6 1.6 q 1) |
119 | 118 | (u3 1.6 1.6 1.6 q 2) |
120 | 119 |
|
121 | | -; cx, cy, cz and ch gates. |
| 120 | +;; cx, cy, cz and ch gates. |
122 | 121 | (qcomm "Controlled x.") |
123 | 122 | (cx q 2 q 0) |
124 | 123 | (qcomm "Controlled y.") |
|
134 | 133 | (qcomm "Controlled h in fast form.") |
135 | 134 | (ch-fast q 2 q 0) |
136 | 135 |
|
137 | | -; Toffoli stuff. |
| 136 | +;; Toffoli stuff. |
138 | 137 | (qcomm "Toffoli (ccx).") |
139 | 138 | (ccx q 0 q 1 q 2) |
140 | 139 | (qcomm "Toffoli in fast form.") |
141 | 140 | (ccx-fast q 0 q 1 q 2) |
142 | 141 |
|
143 | | -; Rotations and others. |
| 142 | +;; Rotations and others. |
144 | 143 | (qcomm "rx.") |
145 | 144 | (rx 1.6 q 1) |
146 | 145 | (qcomm "rx in fast form.") |
|
166 | 165 | (qcomm "Controlled u3 in fast form.") |
167 | 166 | (cu3-fast 1.6 1.6 1.6 q 2 q 0) |
168 | 167 |
|
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. |
172 | 171 | (qcomm "Conditional1 eq.") |
173 | 172 | (qcond1 "==" q 1)(g1 "y" q 2) |
174 | 173 | (qcomm "Conditional2 eq.") |
175 | 174 | (qcond2 "!=" q 2 1)(g1 "y" q 2) |
176 | 175 |
|
177 | | -; GHZ state stuff. |
| 176 | +;; GHZ state stuff. |
178 | 177 | (qcomm "Three qubit GHZ state generation") |
179 | 178 | (ghzy "h" "x" q 0 2 1) |
180 | 179 | (ghzy "h" "x" q 0 2 2) |
181 | 180 |
|
182 | | -; Using lists. |
| 181 | +;; Using lists. |
183 | 182 | (g1yl q '("h" "h" "h" "h" "sdg") 0) |
184 | 183 | (g1yl q '("x" "h" "h" "sdg") 1) |
185 | 184 |
|
186 | | -; Let's put a barrier. |
| 185 | +;; Let's put a barrier. |
187 | 186 | (qcomm "Barriers.") |
188 | 187 | (g1y "barrier" q 0 4) |
189 | 188 |
|
190 | | -; And now measure. |
| 189 | +;; And now measure. |
191 | 190 | (qcomm "Measuring") |
192 | 191 | (qmeas q 0 c 0) |
193 | 192 | (qmeas q 1 c 1) |
194 | 193 | (qmeas q 2 c 2) |
195 | 194 | (qmeas q 3 c 3) |
196 | 195 | (qmeas q 4 c 4) |
197 | 196 |
|
198 | | -; Message |
| 197 | +;; Message |
199 | 198 | (qcomm "This message will appear at the end of the qasm file.") |
200 | 199 |
|
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. |
203 | 202 | (set-current-output-port port1) |
204 | 203 | (close port2) |
205 | 204 | (qendc) |
|
0 commit comments