-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgenerate_hugrs.py
More file actions
266 lines (212 loc) · 5.62 KB
/
generate_hugrs.py
File metadata and controls
266 lines (212 loc) · 5.62 KB
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
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "guppylang ==0.21.9",
# "tket",
# ]
# ///
from pathlib import Path
from guppylang import guppy
from guppylang.std.angles import pi
from guppylang.std.builtins import array, exit, panic, result
from guppylang.std.qsystem.random import RNG
from guppylang.std.qsystem.utils import get_current_shot
from guppylang.std.quantum import (
crz,
cx,
discard,
discard_array,
h,
measure,
measure_array,
qubit,
t,
tdg,
x,
z,
)
resources_dir = Path(__file__).parent / "resources"
def check() -> bytes:
@guppy
def foo() -> None:
q = qubit()
discard(q)
return foo.compile().to_bytes()
def unsupported_pytket_ops() -> bytes:
from pytket._tket.circuit import Circuit
# A pytket circuit with an unsupported op.
circ = Circuit(2).CSXdg(0, 1)
guppy_circ = guppy.load_pytket("guppy_circ", circ, use_arrays=False)
@guppy
def foo() -> None:
a, b = qubit(), qubit()
guppy_circ(a, b)
discard(a)
discard(b)
return foo.compile().to_bytes()
def no_results() -> bytes:
@guppy
def bar() -> None:
q0: qubit = qubit()
h(q0)
measure(q0)
return bar.compile().to_bytes()
def flip_some() -> bytes:
@guppy
def main() -> None:
q0: qubit = qubit()
q1: qubit = qubit()
q2: qubit = qubit()
q3: qubit = qubit()
x(q0)
x(q2)
x(q3)
result("c0", measure(q0))
result("c1", measure(q1))
result("c2", measure(q2))
result("c3", measure(q3))
return main.compile().to_bytes()
def discard_qb_array() -> bytes:
@guppy
def main() -> None:
qs = array(qubit() for _ in range(10))
discard_array(qs)
return main.compile().to_bytes()
def measure_qb_array() -> bytes:
@guppy
def main() -> None:
qs = array(qubit() for _ in range(10))
x(qs[0])
x(qs[2])
x(qs[3])
x(qs[9])
measure_array(qs)
return main.compile().to_bytes()
def print_array() -> bytes:
# TODO: Currently fails due to <https://github.com/Quantinuum/guppylang/issues/1408>.
@guppy
def main() -> None:
qs = array(qubit() for _ in range(10))
x(qs[0])
x(qs[2])
x(qs[3])
x(qs[9])
cs = measure_array(qs)
result("cs", cs)
result("is", array(i for i in range(100)))
result("fs", array(i * 0.0625 for i in range(100)))
return main.compile().to_bytes()
def postselect_exit() -> bytes:
@guppy
def main() -> None:
q = qubit()
h(q)
outcome = measure(q)
if outcome:
exit("Postselection failed", 42)
result("c", outcome)
return main.compile().to_bytes()
def postselect_panic() -> bytes:
@guppy
def main() -> None:
q = qubit()
h(q)
outcome = measure(q)
if outcome:
panic("Postselection failed")
result("c", outcome)
return main.compile().to_bytes()
def rus() -> bytes:
@guppy
def rus(q: qubit) -> None:
while True:
# Prepare ancillary qubits
a, b = qubit(), qubit()
h(a)
h(b)
tdg(a)
cx(b, a)
t(a)
if not measure(a):
# First part failed; try again
discard(b)
continue
t(q)
z(q)
cx(q, b)
t(b)
if measure(b):
# Success, we are done
break
# Otherwise, apply correction
x(q)
@guppy
def main() -> None:
q = qubit()
rus(q)
result("result", measure(q))
return main.compile().to_bytes()
def print_current_shot() -> bytes:
@guppy
def main() -> None:
result("shot", get_current_shot())
return main.compile().to_bytes()
def rng() -> bytes:
@guppy
def main() -> None:
rng = RNG(42)
rint = rng.random_int()
rint1 = rng.random_int()
rfloat = rng.random_float()
rint_bnd = rng.random_int_bounded(100)
rng.discard()
result("rint", rint)
result("rint1", rint1)
result("rfloat", rfloat)
result("rint_bnd", rint_bnd)
rng = RNG(84)
rint = rng.random_int()
rfloat = rng.random_float()
rint_bnd = rng.random_int_bounded(200)
rng.discard()
result("rint2", rint)
result("rfloat2", rfloat)
result("rint_bnd2", rint_bnd)
return main.compile().to_bytes()
def qft_32() -> bytes:
@guppy
def main() -> None:
qs = array(qubit() for _ in range(32))
for i in range(32):
h(qs[i])
angle = pi / 2
for j in range(31 - i):
crz(qs[i], qs[i + j + 1], angle)
angle /= 2
result("cs", measure_array(qs))
return main.compile().to_bytes()
def entry_args() -> bytes:
@guppy
def foo(a: int) -> None:
result("a", a)
return foo.compile_function().to_bytes()
if __name__ == "__main__":
for func in [
check,
unsupported_pytket_ops,
no_results,
flip_some,
discard_qb_array,
measure_qb_array,
# TODO: Currently fails due to <https://github.com/Quantinuum/guppylang/issues/1408>.
# print_array,
postselect_exit,
postselect_panic,
rus,
print_current_shot,
qft_32,
rng,
entry_args,
]:
envelope = func()
(resources_dir / f"{func.__name__}.hugr").write_bytes(envelope)