-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_unparsed.py
More file actions
478 lines (414 loc) · 13 KB
/
Copy pathtest_unparsed.py
File metadata and controls
478 lines (414 loc) · 13 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
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
import yaml
import datetime
from pathlib import Path
from textwrap import dedent
from selene_sim.build import build
from selene_sim import Quest, Stim
from selene_sim.event_hooks import MetricStore
from selene_sim.exceptions import (
SelenePanicError,
SeleneStartupError,
SeleneTimeoutError,
)
from selene_sim.result_handling.parse_shot import postprocess_unparsed_stream
def test_flip_some_unparsed(compiled_guppy):
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, x, measure
from guppylang.std.builtins import result
@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).read())
result("c1", measure(q1).read())
result("c2", measure(q2).read())
result("c3", measure(q3).read())
"""
)
llvm_file = compiled_guppy(
program_name="unparsed_flip_n4",
guppy_source=guppy_source,
)
runner = build(llvm_file)
got = list(runner.run(Quest(), verbose=True, n_qubits=4, parse_results=False))
expected = [
("USER:BOOL:c0", 1),
("USER:BOOL:c1", 0),
("USER:BOOL:c2", 1),
("USER:BOOL:c3", 1),
]
assert got == expected, f"expected {expected}, got {got}"
def test_flip_some_multishot_unparsed(compiled_guppy):
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, x, measure
from guppylang.std.builtins import result
@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).read())
result("c1", measure(q1).read())
result("c2", measure(q2).read())
result("c3", measure(q3).read())
"""
)
llvm_file = compiled_guppy(
program_name="unparsed_flip_n4",
guppy_source=guppy_source,
)
runner = build(llvm_file)
shots = runner.run_shots(
Quest(), verbose=True, n_qubits=4, n_shots=10, parse_results=False
)
expected = [
("USER:BOOL:c0", 1),
("USER:BOOL:c1", 0),
("USER:BOOL:c2", 1),
("USER:BOOL:c3", 1),
]
for shot in shots:
got = list(shot)
assert got == expected, f"expected {expected}, got {got}"
def test_flip_some_with_metrics_unparsed(snapshot, compiled_guppy):
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, x, measure
from guppylang.std.builtins import result
@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).read())
result("c1", measure(q1).read())
result("c2", measure(q2).read())
result("c3", measure(q3).read())
"""
)
llvm_file = compiled_guppy(
program_name="unparsed_flip_n4",
guppy_source=guppy_source,
)
runner = build(llvm_file)
store = MetricStore()
got = list(
runner.run(
Quest(), verbose=True, n_qubits=4, parse_results=False, event_hook=store
)
)
snapshot.assert_match(yaml.dump(got), "unparsed_metrics")
def test_array_results_unparsed(compiled_guppy):
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, x, measure_array, collect_measurements
from guppylang.std.builtins import result
@guppy
def main() -> None:
qs = array(qubit() for _ in range(10))
for i in range(len(qs)):
x(qs[i])
bs = measure_array(qs)
result("bools", collect_measurements(bs))
result("floats", array(1.0 / 2**i for i in range(10)))
result("ints", array(i for i in range(100)))
"""
)
llvm_file = compiled_guppy(
program_name="array_results",
guppy_source=guppy_source,
)
runner = build(llvm_file)
shots = list(
list(shot)
for shot in runner.run_shots(
Stim(), n_qubits=10, n_shots=20, parse_results=False, verbose=True
)
)
expected = [
("USER:BOOLARR:bools", [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
(
"USER:FLOATARR:floats",
[
1.0,
0.5,
0.25,
0.125,
0.0625,
0.03125,
0.015625,
0.0078125,
0.00390625,
0.001953125,
],
),
("USER:INTARR:ints", list(range(100))),
]
assert len(shots) == 20
for shot in shots:
assert shot == expected
def test_exit_unparsed(compiled_guppy):
"""
This test verifies the behaviour of exit(), which should stop the shot
and add the error message to the result stream, but should then resume
further shots.
"""
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, h, measure
from guppylang.std.builtins import result, exit
@guppy
def main() -> None:
q = qubit()
h(q)
outcome = measure(q).read()
if outcome:
exit("Postselection failed", 42)
result("c", outcome)
"""
)
llvm_file = compiled_guppy(
program_name="exit_unparsed",
guppy_source=guppy_source,
)
runner = build(llvm_file)
# some should have measurements of 0, some should have no measurements.
n_1 = 0
n_0 = 0
n_exit = 0
for shot in runner.run_shots(
Quest(),
n_qubits=1,
n_shots=100,
random_seed=0,
parse_results=False,
):
shot = list(shot)
if shot == [
("EXIT:INT:Postselection failed", 42),
]:
n_exit += 1
elif shot == [
("USER:BOOL:c", 1),
]:
n_1 += 1
elif shot == [
("USER:BOOL:c", 0),
]:
n_0 += 1
else:
raise ValueError(f"Unexpected shot result: {shot}")
# no shots should have provided a result of 1
assert n_1 == 0
# some shots should have provided a result of 0
assert n_0 > 0
# some shots should have exited
assert n_exit > 0
# we still have 100 shots total
assert n_0 + n_exit == 100
def test_panic_unparsed(compiled_guppy):
"""
This test verifies the behaviour of panic(), which should stop the shot
and should not allow any further shots to be performed. Unlike with
parse_results=True, the panic should be added into the results,
and the panic should not result in an exception.
"""
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, h, measure
from guppylang.std.builtins import result, panic
@guppy
def main() -> None:
q = qubit()
h(q)
outcome = measure(q).read()
if outcome:
panic("Postselection failed")
result("c", outcome)
"""
)
llvm_file = compiled_guppy(
program_name="panic_unparsed",
guppy_source=guppy_source,
)
runner = build(llvm_file)
shots, error = postprocess_unparsed_stream(
runner.run_shots(
Stim(),
n_qubits=1,
n_shots=100,
random_seed=1,
parse_results=False,
)
)
assert error is not None
assert isinstance(error, SelenePanicError)
assert error.code == 1001
assert error.message == "Postselection failed"
assert len(shots) == 3
assert shots[0] == [("USER:BOOL:c", 0)]
assert shots[1] == [("USER:BOOL:c", 0)]
assert shots[2] == [("EXIT:INT:Postselection failed", 1001)]
def test_infinite_loop_unparsed(compiled_guppy):
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, h, measure
from guppylang.std.builtins import result
@guppy
def main() -> None:
while True:
q0: qubit = qubit()
h(q0)
result("r", measure(q0).read())
"""
)
llvm_file = compiled_guppy(
program_name="infinite_loop",
guppy_source=guppy_source,
)
runner = build(llvm_file)
# give it no time to connect
shots, error = postprocess_unparsed_stream(
runner.run_shots(
Quest(random_seed=1234),
n_qubits=1,
n_shots=100,
timeout=datetime.timedelta(seconds=0),
n_processes=4,
parse_results=False,
)
)
assert error is not None
assert isinstance(error, SeleneStartupError)
assert "Timed out waiting for a client to connect for shot 0" in error.message
assert "Expired timers: 'overall'" in error.message
assert len(shots) == 0
# now give it some time to connect, but not enough to complete a shot (note: it is an infinite loop)
shots, error = postprocess_unparsed_stream(
runner.run_shots(
Quest(random_seed=1234),
n_qubits=1,
n_shots=100,
timeout=datetime.timedelta(seconds=1),
n_processes=4,
parse_results=False,
)
)
assert error is not None
assert isinstance(error, SeleneTimeoutError)
assert "Timed out waiting for shot results" in error.message
assert "Expired timers: 'overall'" in error.message
def test_memory_allocation_unparsed(compiled_guppy):
"""
Quest cannot physically allocate more qubits than can be addressed in memory,
regardless of RAM available. This test verifies that the process fails and that
the failure is reported correctly.
"""
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, x, measure_array, collect_measurements
from guppylang.std.builtins import result
@guppy
def main() -> None:
qs = array(qubit() for _ in range(70))
for i in range(len(qs)):
if i % 2 == 0:
x(qs[i])
bs = measure_array(qs)
result("bools", collect_measurements(bs))
"""
)
llvm_file = compiled_guppy(
program_name="memory_allocation",
guppy_source=guppy_source,
)
runner = build(llvm_file)
shots, error = postprocess_unparsed_stream(
runner.run_shots(
Quest(),
n_qubits=70,
n_shots=100,
parse_results=False,
)
)
assert isinstance(error, SeleneStartupError)
assert (
"It is impossible to describe more than 60 qubits in a statevector on a computer with a 64-bit address space."
in error.stderr
)
assert len(shots) == 0
def test_corrupted_plugin_unparsed(compiled_guppy):
"""
On the python side, we only check that plugin files exist.
They can still fail when we invoke the selene binary, so demand
that this is detected and reports back to the user in a
reasonable way.
"""
from selene_core import Runtime
class CorruptedPlugin(Runtime):
def __init__(self):
import tempfile
directory = Path(tempfile.mkdtemp())
dest = directory / "broken_plugin.so"
with open(dest, "w") as f:
f.write("this is not a shared object")
self.path = dest
def get_init_args(self) -> list[str]:
return []
@property
def library_file(self) -> Path:
return self.path
def __del__(self):
self.path.unlink(missing_ok=True)
guppy_source = dedent(
"""
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, h, measure
from guppylang.std.builtins import result
@guppy
def main() -> None:
q0: qubit = qubit()
h(q0)
result("c0", measure(q0).read())
"""
)
llvm_file = compiled_guppy(
program_name="corrupted_plugin",
guppy_source=guppy_source,
)
runner = build(llvm_file)
shots, error = postprocess_unparsed_stream(
runner.run_shots(
Quest(),
n_qubits=1,
n_shots=10,
runtime=CorruptedPlugin(),
parse_results=False,
)
)
assert len(shots) == 0
assert isinstance(error, SeleneStartupError)
assert "Failed to load runtime plugin" in error.stderr