-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_qis.py
More file actions
287 lines (259 loc) · 8 KB
/
Copy pathtest_qis.py
File metadata and controls
287 lines (259 loc) · 8 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
import pytest
from pathlib import Path
import yaml
from selene_sim.event_hooks import CircuitExtractor, MetricStore, MultiEventHook
from selene_sim import Quest
from selene_sim.build import build
from selene_helios_qis_plugin import HeliosInterface
from selene_sol_qis_plugin import SolInterface
RESOURCE_DIR = Path(__file__).parent / "resources"
QIS_RESOURCE_DIR = RESOURCE_DIR / "qis"
@pytest.mark.parametrize(
"program_name",
[
"add_3_11",
],
)
def test_qis(snapshot, program_name: str):
filename = f"{program_name}-any.ll"
helios_file = QIS_RESOURCE_DIR / "helios" / filename
sol_file = QIS_RESOURCE_DIR / "sol" / filename
assert helios_file.exists()
assert sol_file.exists()
helios_build = build(helios_file, interface=HeliosInterface())
sol_build = build(sol_file, interface=SolInterface())
helios_results = helios_build.run_shots(
Quest(), n_qubits=10, n_shots=100, random_seed=1024
)
sol_results = sol_build.run_shots(
Quest(), n_qubits=10, n_shots=100, random_seed=1024
)
results = {
"helios": list(list(shot) for shot in helios_results),
"sol": list(list(shot) for shot in sol_results),
}
assert results["helios"] == results["sol"]
snapshot.assert_match(yaml.dump(results), f"{program_name}.yaml")
@pytest.mark.parametrize(
"program_name",
[
"add_3_11",
],
)
def test_qis_circuit_log(snapshot, program_name: str):
filename = f"{program_name}-any.ll"
helios_file = QIS_RESOURCE_DIR / "helios" / filename
sol_file = QIS_RESOURCE_DIR / "sol" / filename
assert helios_file.exists()
assert sol_file.exists()
helios_build = build(helios_file, interface=HeliosInterface())
sol_build = build(sol_file, interface=SolInterface())
helios_circuit_extractor = CircuitExtractor()
sol_circuit_extractor = CircuitExtractor()
helios_results = helios_build.run_shots(
Quest(),
n_qubits=10,
n_shots=1,
random_seed=1024,
event_hook=helios_circuit_extractor,
)
sol_results = sol_build.run_shots(
Quest(),
n_qubits=10,
n_shots=1,
random_seed=1024,
event_hook=sol_circuit_extractor,
)
# trigger shot consumption
list(list(shot) for shot in helios_results)
list(list(shot) for shot in sol_results)
circuits = {
"helios": repr(helios_circuit_extractor.shots[0].get_user_circuit()),
"sol": repr(sol_circuit_extractor.shots[0].get_user_circuit()),
}
snapshot.assert_match(yaml.dump(circuits), f"{program_name}_circuits.yaml")
def test_simulate_delay():
filename = "simulate_delay-any.ll"
helios_file = QIS_RESOURCE_DIR / "helios" / filename
assert helios_file.exists()
helios_build = build(helios_file, interface=HeliosInterface())
helios_circuit_extractor = CircuitExtractor()
metric_store = MetricStore()
hook = MultiEventHook([helios_circuit_extractor, metric_store])
helios_results = helios_build.run_shots(
Quest(),
n_qubits=2,
n_shots=1,
random_seed=1024,
event_hook=hook,
)
results = {
"helios": list(dict(shot) for shot in helios_results),
}
assert results["helios"][0]["qubit_0"] == 0
assert results["helios"][0]["qubit_1"] == 0
shot_metrics = metric_store.shots[0]
# the QIS example adds a simulated delay of 123,450,000 ns, so we check that the metric store reflects this
# in the total runtime (the default "simple" runtime doesn't add any time taken for operations)
assert shot_metrics["post_runtime"]["total_duration_ns"] == 1_234_500_000, (
f"Expected total_duration_ns to be 1,234,500,000 ns, but got {shot_metrics['post_runtime']['total_duration_ns']}"
)
optimiser_output = helios_circuit_extractor.shots[0].get_optimiser_output()
assert optimiser_output == [
{"op": "BatchStart", "start_time_ns": 0, "duration_ns": 0},
{"op": "Reset", "qubit": 0},
{"op": "BatchStart", "start_time_ns": 0, "duration_ns": 0},
{"op": "Reset", "qubit": 1},
{"op": "BatchStart", "start_time_ns": 0, "duration_ns": 0},
{"op": "FutureRead", "qubit": 0},
# simulated delay happens here, so we expect the next operations to be logged as starting at 1,234,500,000 ns
{"op": "BatchStart", "start_time_ns": 1234500000, "duration_ns": 0},
{"op": "FutureRead", "qubit": 1},
]
all_output = list(helios_circuit_extractor.shots[0])
all_output_serialised = []
for item in all_output:
if str(item.source) not in {"Source.USER", "Source.OPTIMISER"}:
continue
all_output_serialised.append(
{"source": str(item.source), "operation": item.operation.to_dict()}
)
assert all_output_serialised == [
{
"operation": {
"op": "QAlloc",
"qubit": 0,
},
"source": "Source.USER",
},
{
"operation": {
"op": "QAlloc",
"qubit": 1,
},
"source": "Source.USER",
},
{
"operation": {
"op": "Reset",
"qubit": 0,
},
"source": "Source.USER",
},
{
"operation": {
"duration_ns": 0,
"op": "BatchStart",
"start_time_ns": 0,
},
"source": "Source.OPTIMISER",
},
{
"operation": {
"op": "Reset",
"qubit": 0,
},
"source": "Source.OPTIMISER",
},
{
"operation": {
"op": "Reset",
"qubit": 1,
},
"source": "Source.USER",
},
{
"operation": {
"duration_ns": 0,
"op": "BatchStart",
"start_time_ns": 0,
},
"source": "Source.OPTIMISER",
},
{
"operation": {
"op": "Reset",
"qubit": 1,
},
"source": "Source.OPTIMISER",
},
{
"operation": {
"op": "MeasureRequest",
"qubit": 0,
},
"source": "Source.USER",
},
{
"operation": {
"duration_ns": 0,
"op": "BatchStart",
"start_time_ns": 0,
},
"source": "Source.OPTIMISER",
},
{
"operation": {
"op": "FutureRead",
"qubit": 0,
},
"source": "Source.OPTIMISER",
},
{
"operation": {
"op": "QFree",
"qubit": 0,
},
"source": "Source.USER",
},
{
"operation": {
"op": "FutureRead",
"qubit": 0,
},
"source": "Source.USER",
},
{
"operation": {
"duration_ns": 1234500000,
"op": "ClassicalDelay",
},
"source": "Source.USER",
},
{
"operation": {
"op": "MeasureRequest",
"qubit": 1,
},
"source": "Source.USER",
},
{
"operation": {
"duration_ns": 0,
"op": "BatchStart",
"start_time_ns": 1234500000,
},
"source": "Source.OPTIMISER",
},
{
"operation": {
"op": "FutureRead",
"qubit": 1,
},
"source": "Source.OPTIMISER",
},
{
"operation": {
"op": "QFree",
"qubit": 1,
},
"source": "Source.USER",
},
{
"operation": {
"op": "FutureRead",
"qubit": 1,
},
"source": "Source.USER",
},
]