-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_debug.py
More file actions
325 lines (291 loc) · 10.6 KB
/
test_debug.py
File metadata and controls
325 lines (291 loc) · 10.6 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
import pytest
from guppylang import guppy
from guppylang.std.builtins import array
from guppylang.std.debug import state_result
from guppylang.std.angles import pi
from guppylang.std.quantum import (
discard,
qubit,
discard_array,
x,
h,
cx,
measure,
ry,
rz,
rx,
)
from guppylang.std.qsystem.utils import get_current_shot
from hugr.qsystem.result import QsysResult
import numpy as np
from selene_sim.build import build
from selene_sim import Quest, Stim, QuantumReplay
@pytest.mark.parametrize("simulator_plugin", [Quest, Stim])
def test_initial_state(simulator_plugin):
@guppy
def main() -> None:
q0 = qubit()
state_result("initial_state", q0)
discard(q0)
runner = build(main.compile(), "initial_state")
got = runner.run(simulator_plugin(), n_qubits=1)
state = simulator_plugin.extract_states_dict(got)["initial_state"]
assert state.get_dirac_notation()[0].probability == 1
@pytest.mark.parametrize("simulator_plugin", [Quest, Stim])
def test_array_state(simulator_plugin):
@guppy
def main() -> None:
qs = array(qubit() for _ in range(2))
for i in range(2):
x(qs[i])
state_result("array_state", qs)
discard_array(qs)
runner = build(main.compile(), "array_state")
plugin = simulator_plugin()
shots = QsysResult(
runner.run_shots(
simulator=plugin,
n_qubits=2,
n_shots=2,
)
)
for shot in shots.results:
state = simulator_plugin.extract_states_dict(shot.entries)["array_state"]
assert state.get_density_matrix()[3][3] == 1
assert len(state.get_single_state()) == 4
@pytest.mark.parametrize("simulator_plugin", [Quest, Stim])
def test_array_subscript_state(simulator_plugin):
@guppy
def main() -> None:
qs = array(qubit() for _ in range(2))
for i in range(2):
x(qs[i])
state_result("array_state", qs[0])
discard_array(qs)
runner = build(main.compile(), "array_state")
plugin = simulator_plugin()
shots = QsysResult(
runner.run_shots(
simulator=plugin,
n_qubits=2,
n_shots=1,
)
)
for shot in shots.results:
state = simulator_plugin.extract_states_dict(shot.entries)["array_state"]
assert state.get_density_matrix()[1][1] == 1
assert state.get_state_vector_distribution()[0].probability == 1
@pytest.mark.parametrize("simulator_plugin", [Quest, Stim])
def test_qubit_ordering_state(simulator_plugin):
@guppy
def main() -> None:
qs = array(qubit() for _ in range(2))
x(qs[0])
# expected state is |10> so that qs[0] is the MSB
state_result("default", qs[0], qs[1])
# reversed order, expected state is |01>
state_result("reversed", qs[1], qs[0])
discard_array(qs)
runner = build(main.compile())
plugin = simulator_plugin()
shots = QsysResult(
runner.run_shots(
simulator=plugin,
n_qubits=2,
n_shots=1,
)
)
for shot in shots.results:
states = simulator_plugin.extract_states_dict(shot.entries)
state_default = states["default"]
state_reversed = states["reversed"]
assert state_default.get_single_state()[2] == 1 # expect |10>
assert state_reversed.get_single_state()[1] == 1 # expect |01>
@pytest.mark.parametrize("simulator_plugin", [Quest, Stim])
@pytest.mark.parametrize("first_measurement", [0, 1])
def test_quantum_replay_state(simulator_plugin, first_measurement):
@guppy
def main() -> None:
q0: qubit = qubit()
q1: qubit = qubit()
h(q0)
cx(q0, q1)
state_result("entangled_state", q0, q1)
result("c0", measure(q0))
state_result("post_measurement_state", q1)
result("c1", measure(q1))
runner = build(main.compile(), "quantum_replay_state")
underlying_simulator = simulator_plugin(random_seed=1234)
replay_simulator = QuantumReplay(
simulator=underlying_simulator,
resume_with_measurement=True,
measurements=[
[
first_measurement
], # first qubit measures as requested, simulator will handle the second
],
)
shots = QsysResult(
runner.run_shots(
simulator=replay_simulator,
n_qubits=2,
n_shots=1,
)
)
shot = shots.results[0]
shot_dict = dict(shot.entries)
shot_states = simulator_plugin.extract_states_dict(shot.entries)
# First measurement is correctly replayed
assert shot_dict["c0"] == first_measurement
# Second measurement is consistent with entanglement
assert shot_dict["c1"] == first_measurement
# Initial entangled state is correct
entangled = shot_states["entangled_state"].get_single_state()
# both should be (1/sqrt(2))[|00> + |11>]
np.testing.assert_allclose(entangled, np.array([1, 0, 0, 1]) / np.sqrt(2))
# Post-measurement states are correct
post_measurement = shot_states["post_measurement_state"].get_single_state()
# first shot measured 0 on q0, so q1 should be |0>
expected = np.array([1, 0]) if first_measurement == 0 else np.array([0, 1])
np.testing.assert_allclose(post_measurement, expected)
@pytest.mark.parametrize("gate_name", ["rx", "ry", "rz"])
def test_stim_gate_implementations_single_qubit(gate_name):
import random
random.seed(1234)
all_quarter_turns = [
i / 2 for i in range(-8, 9)
] # from -4pi to 4pi in pi/2 increments
params = [random.choice(all_quarter_turns) for _ in range(1000)]
gate = {"rx": rx, "ry": ry, "rz": rz}[gate_name]
@guppy
def main() -> None:
q0: qubit = qubit()
angle = comptime(params)[get_current_shot()]
gate(q0, pi * angle)
state_result("entangled_state", q0)
discard(q0)
runner = build(main.compile())
stim_shots = QsysResult(
runner.run_shots(
simulator=Stim(
angle_threshold=1e-2, # reduce threshold to catch more errors
),
n_qubits=2,
n_shots=len(params),
)
)
quest_shots = QsysResult(
runner.run_shots(
simulator=Quest(),
n_qubits=2,
n_shots=len(params),
)
)
for shot, (stim_shot, quest_shot) in enumerate(
zip(stim_shots.results, quest_shots.results)
):
stim_state = Stim.extract_states_dict(stim_shot.entries)["entangled_state"]
stim_statevector = stim_state.get_single_state()
quest_state = Quest.extract_states_dict(quest_shot.entries)["entangled_state"]
quest_statevector = quest_state.get_single_state()
print(f"Shot {shot}:")
print(f" gate: {gate_name}(pi * {params[shot]})")
print(" Stim state:", stim_statevector)
print(" Quest state:", quest_statevector)
print(" Stabilizers", stim_state.get_reduced_stabilizers())
np.testing.assert_allclose(stim_statevector, quest_statevector)
def test_stim_gate_implementations_single_qubit_triples():
import random
random.seed(1234)
all_quarter_turns = [
i / 2 for i in range(-8, 9)
] # from -4pi to 4pi in pi/2 increments
params = [[random.choice(all_quarter_turns) for _ in range(3)] for _ in range(1000)]
@guppy
def main() -> None:
q0: qubit = qubit()
angles = comptime(params)[get_current_shot()]
rx(q0, pi * angles[0])
ry(q0, pi * angles[1])
rz(q0, pi * angles[2])
state_result("entangled_state", q0)
discard(q0)
runner = build(main.compile())
stim_shots = QsysResult(
runner.run_shots(
simulator=Stim(
angle_threshold=1e-2, # reduce threshold to catch more errors
),
n_qubits=2,
n_shots=len(params),
)
)
quest_shots = QsysResult(
runner.run_shots(
simulator=Quest(),
n_qubits=2,
n_shots=len(params),
)
)
for shot, (stim_shot, quest_shot) in enumerate(
zip(stim_shots.results, quest_shots.results)
):
stim_state = Stim.extract_states_dict(stim_shot.entries)["entangled_state"]
stim_statevector = stim_state.get_single_state()
quest_state = Quest.extract_states_dict(quest_shot.entries)["entangled_state"]
quest_statevector = quest_state.get_single_state()
print(f"Shot {shot}:")
print(
f" gate: rx(pi * {params[shot][0]}); ry(pi * {params[shot][1]}); rz(pi * {params[shot][2]})"
)
print(" Stim state:", stim_statevector)
print(" Quest state:", quest_statevector)
print(" Stabilizers", stim_state.get_reduced_stabilizers())
np.testing.assert_allclose(stim_statevector, quest_statevector)
@pytest.mark.parametrize("simulator_plugin", [Quest, Stim])
@pytest.mark.parametrize("cleanup_param", [True, False, None])
def test_state_cleanup(simulator_plugin, cleanup_param):
@guppy
def main() -> None:
qs = array(qubit() for _ in range(2))
for i in range(2):
x(qs[i])
state_result("array_state", qs[0])
discard_array(qs)
runner = build(main.compile(), "array_state")
plugin = simulator_plugin()
shots = QsysResult(
runner.run_shots(
simulator=plugin,
n_qubits=2,
n_shots=1,
)
)
run_directory = runner.runs
individual_runs = list(run_directory.iterdir())
assert len(individual_runs) == 1, "Expected only one run directory"
individual_run = individual_runs[0]
run_artifact_dir = individual_run / "artifacts"
assert run_artifact_dir.exists(), "Expected artifacts directory to exist"
state_files_pre_parse = list(run_artifact_dir.glob("*.state"))
assert len(state_files_pre_parse) == 1
for shot in shots.results:
state_dict = (
simulator_plugin.extract_states_dict(shot.entries)
if cleanup_param is None
else simulator_plugin.extract_states_dict(
shot.entries, cleanup=cleanup_param
)
)
state = state_dict["array_state"]
assert state.get_density_matrix()[1][1] == 1
assert state.get_state_vector_distribution()[0].probability == 1
state_files_post_parse = list(run_artifact_dir.glob("*.state"))
if cleanup_param in [True, None]:
assert len(state_files_post_parse) == 0, (
"Expected no state files after cleanup"
)
else:
assert state_files_post_parse == state_files_pre_parse, (
"Expected artifacts to remain unchanged if cleanup is False"
)