-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_determinism.py
More file actions
117 lines (97 loc) · 3.06 KB
/
test_determinism.py
File metadata and controls
117 lines (97 loc) · 3.06 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
import yaml
from guppylang.decorator import guppy
from guppylang.std.quantum import qubit, h, measure_array
from guppylang.std.builtins import result, array
from guppylang.std.qsystem.random import RNG
from guppylang.std.qsystem.utils import get_current_shot
from selene_sim.build import build
from selene_sim import Stim, DepolarizingErrorModel
def test_repetition(snapshot):
@guppy
def main() -> None:
qubits = array(qubit() for _ in range(5))
for i in range(len(qubits)):
h(qubits[i])
bits = measure_array(qubits)
result("a", bits[0])
result("b", bits[1])
result("c", bits[2])
result("d", bits[3])
result("e", bits[4])
result("shot", get_current_shot())
rng = RNG(get_current_shot())
result("random_int", rng.random_int())
result("random_float", rng.random_float())
rng.discard()
runner = build(main.compile(), "no_results")
error_model = DepolarizingErrorModel(
random_seed=194678,
p_init=0.01,
p_meas=0.01,
p_1q=0.01,
p_2q=0.01,
)
simulator = Stim(random_seed=561278)
full_results = list(
dict(a)
for a in runner.run_shots(
simulator=simulator, error_model=error_model, n_qubits=5, n_shots=1000
)
)
# ensure results agree with the snapshot
snapshot.assert_match(yaml.dump(full_results), "full_results")
full_results_again = list(
dict(a)
for a in runner.run_shots(
simulator=simulator, error_model=error_model, n_qubits=5, n_shots=1000
)
)
assert full_results == full_results_again
offset_results = list(
dict(a)
for a in runner.run_shots(
simulator=simulator,
error_model=error_model,
n_qubits=5,
n_shots=600, # note: only running 600 shots
shot_offset=400, # note: offsetting by 400
)
)
assert full_results[400:] == offset_results
results_ending_in_5 = list(
dict(a)
for a in runner.run_shots(
simulator=simulator,
error_model=error_model,
n_qubits=5,
n_shots=100,
shot_offset=5,
shot_increment=10, # note: incrementing by 10
)
)
assert full_results[5::10] == results_ending_in_5
last_result = dict(
runner.run(
simulator=simulator,
error_model=error_model,
n_qubits=5,
shot_offset=999,
)
)
assert full_results[-1] == last_result
# now try some multi-process runs
results_ending_in_5_but_with_5_processes = list(
dict(a)
for a in runner.run_shots(
simulator=simulator,
error_model=error_model,
n_qubits=5,
n_shots=100,
shot_offset=5,
shot_increment=10, # note: incrementing by 10
n_processes=5,
)
)
assert results_ending_in_5_but_with_5_processes == results_ending_in_5
if __name__ == "__main__":
test_repetition()