cirq-core 1.6.0.dev20250529194600__py3-none-any.whl → 1.6.0.dev20250530203656__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cirq-core might be problematic. Click here for more details.
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/circuits/_bucket_priority_queue_test.py +20 -20
- cirq/circuits/circuit_operation_test.py +58 -56
- cirq/circuits/frozen_circuit_test.py +6 -5
- cirq/circuits/qasm_output_test.py +22 -22
- cirq/circuits/text_diagram_drawer_test.py +17 -17
- cirq/contrib/custom_simulators/custom_state_simulator_test.py +11 -10
- cirq/contrib/qasm_import/_lexer_test.py +47 -13
- cirq/contrib/qasm_import/_parser_test.py +5 -5
- cirq/contrib/qasm_import/qasm_test.py +4 -4
- cirq/contrib/quirk/export_to_quirk_test.py +16 -16
- cirq/devices/insertion_noise_model.py +2 -2
- cirq/linalg/transformations_test.py +71 -43
- cirq/ops/boolean_hamiltonian_test.py +8 -8
- cirq/ops/classically_controlled_operation_test.py +51 -51
- cirq/ops/clifford_gate_test.py +58 -54
- cirq/ops/common_gate_families_test.py +9 -7
- cirq/ops/common_gates_test.py +81 -81
- cirq/ops/control_values_test.py +17 -17
- cirq/ops/controlled_gate_test.py +34 -32
- cirq/ops/diagonal_gate_test.py +11 -11
- cirq/ops/eigen_gate_test.py +28 -20
- cirq/ops/fourier_transform.py +1 -3
- cirq/ops/fourier_transform_test.py +13 -12
- cirq/ops/gate_operation_test.py +43 -42
- cirq/ops/global_phase_op_test.py +22 -20
- cirq/ops/identity_test.py +21 -20
- cirq/ops/kraus_channel_test.py +11 -11
- cirq/ops/named_qubit_test.py +13 -13
- cirq/ops/op_tree_test.py +19 -19
- cirq/ops/phased_x_gate_test.py +16 -16
- cirq/ops/phased_x_z_gate_test.py +14 -11
- cirq/ops/projector_test.py +16 -16
- cirq/ops/qubit_order_test.py +10 -10
- cirq/ops/raw_types.py +2 -2
- cirq/ops/raw_types_test.py +51 -49
- cirq/ops/wait_gate.py +1 -1
- cirq/ops/wait_gate_test.py +6 -6
- cirq/protocols/qasm.py +2 -2
- cirq/qis/entropy_test.py +1 -1
- cirq/testing/consistent_protocols.py +1 -3
- cirq/testing/consistent_protocols_test.py +3 -3
- cirq/value/digits.py +5 -0
- cirq/value/duration.py +1 -1
- {cirq_core-1.6.0.dev20250529194600.dist-info → cirq_core-1.6.0.dev20250530203656.dist-info}/METADATA +1 -1
- {cirq_core-1.6.0.dev20250529194600.dist-info → cirq_core-1.6.0.dev20250530203656.dist-info}/RECORD +50 -50
- {cirq_core-1.6.0.dev20250529194600.dist-info → cirq_core-1.6.0.dev20250530203656.dist-info}/WHEEL +0 -0
- {cirq_core-1.6.0.dev20250529194600.dist-info → cirq_core-1.6.0.dev20250530203656.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.6.0.dev20250529194600.dist-info → cirq_core-1.6.0.dev20250530203656.dist-info}/top_level.txt +0 -0
|
@@ -29,12 +29,12 @@ def _make_qubits(n):
|
|
|
29
29
|
return [cirq.NamedQubit(f'q{i}') for i in range(n)]
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
def test_u_gate_repr():
|
|
32
|
+
def test_u_gate_repr() -> None:
|
|
33
33
|
gate = QasmUGate(0.1, 0.2, 0.3)
|
|
34
34
|
assert repr(gate) == 'cirq.circuits.qasm_output.QasmUGate(theta=0.1, phi=0.2, lmda=0.3)'
|
|
35
35
|
|
|
36
36
|
|
|
37
|
-
def test_u_gate_eq():
|
|
37
|
+
def test_u_gate_eq() -> None:
|
|
38
38
|
gate = QasmUGate(0.1, 0.2, 0.3)
|
|
39
39
|
gate2 = QasmUGate(0.1, 0.2, 0.3)
|
|
40
40
|
cirq.approx_eq(gate, gate2, atol=1e-16)
|
|
@@ -43,13 +43,13 @@ def test_u_gate_eq():
|
|
|
43
43
|
cirq.approx_eq(gate4, gate3, atol=1e-16)
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
def test_qasm_two_qubit_gate_repr():
|
|
46
|
+
def test_qasm_two_qubit_gate_repr() -> None:
|
|
47
47
|
cirq.testing.assert_equivalent_repr(
|
|
48
48
|
QasmTwoQubitGate.from_matrix(cirq.testing.random_unitary(4))
|
|
49
49
|
)
|
|
50
50
|
|
|
51
51
|
|
|
52
|
-
def test_qasm_u_qubit_gate_unitary():
|
|
52
|
+
def test_qasm_u_qubit_gate_unitary() -> None:
|
|
53
53
|
u = cirq.testing.random_unitary(2)
|
|
54
54
|
g = QasmUGate.from_matrix(u)
|
|
55
55
|
cirq.testing.assert_allclose_up_to_global_phase(cirq.unitary(g), u, atol=1e-7)
|
|
@@ -62,13 +62,13 @@ def test_qasm_u_qubit_gate_unitary():
|
|
|
62
62
|
cirq.testing.assert_implements_consistent_protocols(g)
|
|
63
63
|
|
|
64
64
|
|
|
65
|
-
def test_qasm_two_qubit_gate_unitary():
|
|
65
|
+
def test_qasm_two_qubit_gate_unitary() -> None:
|
|
66
66
|
u = cirq.testing.random_unitary(4)
|
|
67
67
|
g = QasmTwoQubitGate.from_matrix(u)
|
|
68
68
|
np.testing.assert_allclose(cirq.unitary(g), u)
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
def test_empty_circuit_one_qubit():
|
|
71
|
+
def test_empty_circuit_one_qubit() -> None:
|
|
72
72
|
(q0,) = _make_qubits(1)
|
|
73
73
|
output = cirq.QasmOutput((), (q0,))
|
|
74
74
|
assert (
|
|
@@ -83,7 +83,7 @@ qreg q[1];
|
|
|
83
83
|
)
|
|
84
84
|
|
|
85
85
|
|
|
86
|
-
def test_empty_circuit_no_qubits():
|
|
86
|
+
def test_empty_circuit_no_qubits() -> None:
|
|
87
87
|
output = cirq.QasmOutput((), ())
|
|
88
88
|
assert (
|
|
89
89
|
str(output)
|
|
@@ -96,7 +96,7 @@ include "qelib1.inc";
|
|
|
96
96
|
)
|
|
97
97
|
|
|
98
98
|
|
|
99
|
-
def test_header():
|
|
99
|
+
def test_header() -> None:
|
|
100
100
|
(q0,) = _make_qubits(1)
|
|
101
101
|
output = cirq.QasmOutput(
|
|
102
102
|
(),
|
|
@@ -143,7 +143,7 @@ qreg q[1];
|
|
|
143
143
|
)
|
|
144
144
|
|
|
145
145
|
|
|
146
|
-
def test_single_gate_no_parameter():
|
|
146
|
+
def test_single_gate_no_parameter() -> None:
|
|
147
147
|
(q0,) = _make_qubits(1)
|
|
148
148
|
output = cirq.QasmOutput((cirq.X(q0),), (q0,))
|
|
149
149
|
assert (
|
|
@@ -161,7 +161,7 @@ x q[0];
|
|
|
161
161
|
)
|
|
162
162
|
|
|
163
163
|
|
|
164
|
-
def test_single_gate_with_parameter():
|
|
164
|
+
def test_single_gate_with_parameter() -> None:
|
|
165
165
|
(q0,) = _make_qubits(1)
|
|
166
166
|
output = cirq.QasmOutput((cirq.X(q0) ** 0.25,), (q0,))
|
|
167
167
|
assert (
|
|
@@ -179,7 +179,7 @@ rx(pi*0.25) q[0];
|
|
|
179
179
|
)
|
|
180
180
|
|
|
181
181
|
|
|
182
|
-
def test_h_gate_with_parameter():
|
|
182
|
+
def test_h_gate_with_parameter() -> None:
|
|
183
183
|
(q0,) = _make_qubits(1)
|
|
184
184
|
output = cirq.QasmOutput((cirq.H(q0) ** 0.25,), (q0,))
|
|
185
185
|
assert (
|
|
@@ -200,7 +200,7 @@ ry(pi*-0.25) q[0];
|
|
|
200
200
|
)
|
|
201
201
|
|
|
202
202
|
|
|
203
|
-
def test_qasm_global_pahse():
|
|
203
|
+
def test_qasm_global_pahse() -> None:
|
|
204
204
|
output = cirq.QasmOutput((cirq.global_phase_operation(np.exp(1j * 5))), ())
|
|
205
205
|
assert (
|
|
206
206
|
str(output)
|
|
@@ -213,7 +213,7 @@ include "qelib1.inc";
|
|
|
213
213
|
)
|
|
214
214
|
|
|
215
215
|
|
|
216
|
-
def test_precision():
|
|
216
|
+
def test_precision() -> None:
|
|
217
217
|
(q0,) = _make_qubits(1)
|
|
218
218
|
output = cirq.QasmOutput((cirq.X(q0) ** 0.1234567,), (q0,), precision=3)
|
|
219
219
|
assert (
|
|
@@ -231,14 +231,14 @@ rx(pi*0.123) q[0];
|
|
|
231
231
|
)
|
|
232
232
|
|
|
233
233
|
|
|
234
|
-
def test_version():
|
|
234
|
+
def test_version() -> None:
|
|
235
235
|
(q0,) = _make_qubits(1)
|
|
236
236
|
with pytest.raises(ValueError):
|
|
237
237
|
output = cirq.QasmOutput((), (q0,), version='4.0')
|
|
238
238
|
_ = str(output)
|
|
239
239
|
|
|
240
240
|
|
|
241
|
-
def test_save_to_file(tmpdir):
|
|
241
|
+
def test_save_to_file(tmpdir) -> None:
|
|
242
242
|
file_path = os.path.join(tmpdir, 'test.qasm')
|
|
243
243
|
(q0,) = _make_qubits(1)
|
|
244
244
|
output = cirq.QasmOutput((), (q0,))
|
|
@@ -257,7 +257,7 @@ qreg q[1];
|
|
|
257
257
|
)
|
|
258
258
|
|
|
259
259
|
|
|
260
|
-
def test_unsupported_operation():
|
|
260
|
+
def test_unsupported_operation() -> None:
|
|
261
261
|
(q0,) = _make_qubits(1)
|
|
262
262
|
|
|
263
263
|
class UnsupportedOperation(cirq.Operation):
|
|
@@ -269,7 +269,7 @@ def test_unsupported_operation():
|
|
|
269
269
|
_ = str(output)
|
|
270
270
|
|
|
271
271
|
|
|
272
|
-
def _all_operations(q0, q1, q2, q3, q4, include_measurements=True):
|
|
272
|
+
def _all_operations(q0, q1, q2, q3, q4, *, include_measurements=True):
|
|
273
273
|
class ExampleOperation(cirq.Operation):
|
|
274
274
|
qubits = (q0,)
|
|
275
275
|
with_qubits = NotImplemented
|
|
@@ -357,7 +357,7 @@ def _all_operations(q0, q1, q2, q3, q4, include_measurements=True):
|
|
|
357
357
|
)
|
|
358
358
|
|
|
359
359
|
|
|
360
|
-
def test_output_unitary_same_as_qiskit():
|
|
360
|
+
def test_output_unitary_same_as_qiskit() -> None:
|
|
361
361
|
qubits = tuple(_make_qubits(5))
|
|
362
362
|
operations = _all_operations(*qubits, include_measurements=False)
|
|
363
363
|
output = cirq.QasmOutput(operations, qubits, header='Generated from Cirq', precision=10)
|
|
@@ -368,7 +368,7 @@ def test_output_unitary_same_as_qiskit():
|
|
|
368
368
|
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(text, cirq_unitary)
|
|
369
369
|
|
|
370
370
|
|
|
371
|
-
def test_fails_on_big_unknowns():
|
|
371
|
+
def test_fails_on_big_unknowns() -> None:
|
|
372
372
|
class UnrecognizedGate(cirq.testing.ThreeQubitGate):
|
|
373
373
|
pass
|
|
374
374
|
|
|
@@ -377,7 +377,7 @@ def test_fails_on_big_unknowns():
|
|
|
377
377
|
_ = c.to_qasm()
|
|
378
378
|
|
|
379
379
|
|
|
380
|
-
def test_output_format():
|
|
380
|
+
def test_output_format() -> None:
|
|
381
381
|
def filter_unpredictable_numbers(text):
|
|
382
382
|
return re.sub(r'u3\(.+\)', r'u3(<not-repeatable>)', text)
|
|
383
383
|
|
|
@@ -571,7 +571,7 @@ x q[0];
|
|
|
571
571
|
)
|
|
572
572
|
|
|
573
573
|
|
|
574
|
-
def test_reset():
|
|
574
|
+
def test_reset() -> None:
|
|
575
575
|
a, b = cirq.LineQubit.range(2)
|
|
576
576
|
c = cirq.Circuit(cirq.H(a), cirq.CNOT(a, b), cirq.reset(a), cirq.reset(b))
|
|
577
577
|
output = cirq.QasmOutput(
|
|
@@ -601,7 +601,7 @@ reset q[1];
|
|
|
601
601
|
)
|
|
602
602
|
|
|
603
603
|
|
|
604
|
-
def test_different_sized_registers():
|
|
604
|
+
def test_different_sized_registers() -> None:
|
|
605
605
|
qubits = cirq.LineQubit.range(2)
|
|
606
606
|
c = cirq.Circuit(cirq.measure(qubits[0], key='c'), cirq.measure(qubits, key='c'))
|
|
607
607
|
output = cirq.QasmOutput(
|
|
@@ -59,7 +59,7 @@ def assert_has_rendering(actual: TextDiagramDrawer, desired: str, **kwargs) -> N
|
|
|
59
59
|
)
|
|
60
60
|
|
|
61
61
|
|
|
62
|
-
def test_draw_entries_and_lines_with_options():
|
|
62
|
+
def test_draw_entries_and_lines_with_options() -> None:
|
|
63
63
|
d = TextDiagramDrawer()
|
|
64
64
|
d.write(0, 0, '!')
|
|
65
65
|
d.write(6, 2, 'span')
|
|
@@ -132,7 +132,7 @@ def test_draw_entries_and_lines_with_options():
|
|
|
132
132
|
)
|
|
133
133
|
|
|
134
134
|
|
|
135
|
-
def test_draw_entries_and_lines_with_emphasize():
|
|
135
|
+
def test_draw_entries_and_lines_with_emphasize() -> None:
|
|
136
136
|
d = TextDiagramDrawer()
|
|
137
137
|
d.write(0, 0, '!')
|
|
138
138
|
d.write(6, 2, 'span')
|
|
@@ -161,27 +161,27 @@ def test_draw_entries_and_lines_with_emphasize():
|
|
|
161
161
|
)
|
|
162
162
|
|
|
163
163
|
|
|
164
|
-
def test_line_detects_horizontal():
|
|
164
|
+
def test_line_detects_horizontal() -> None:
|
|
165
165
|
d = TextDiagramDrawer()
|
|
166
166
|
with mock.patch.object(d, 'vertical_line') as vertical_line:
|
|
167
167
|
d.grid_line(1, 2, 1, 5, True)
|
|
168
168
|
vertical_line.assert_called_once_with(1, 2, 5, True, False)
|
|
169
169
|
|
|
170
170
|
|
|
171
|
-
def test_line_detects_vertical():
|
|
171
|
+
def test_line_detects_vertical() -> None:
|
|
172
172
|
d = TextDiagramDrawer()
|
|
173
173
|
with mock.patch.object(d, 'horizontal_line') as horizontal_line:
|
|
174
174
|
d.grid_line(2, 1, 5, 1, True)
|
|
175
175
|
horizontal_line.assert_called_once_with(1, 2, 5, True, False)
|
|
176
176
|
|
|
177
177
|
|
|
178
|
-
def test_line_fails_when_not_aligned():
|
|
178
|
+
def test_line_fails_when_not_aligned() -> None:
|
|
179
179
|
d = TextDiagramDrawer()
|
|
180
180
|
with pytest.raises(ValueError):
|
|
181
181
|
d.grid_line(1, 2, 3, 4)
|
|
182
182
|
|
|
183
183
|
|
|
184
|
-
def test_multiline_entries():
|
|
184
|
+
def test_multiline_entries() -> None:
|
|
185
185
|
d = TextDiagramDrawer()
|
|
186
186
|
d.write(0, 0, 'hello\nthere')
|
|
187
187
|
d.write(0, 1, 'next')
|
|
@@ -225,7 +225,7 @@ short │ │ │
|
|
|
225
225
|
)
|
|
226
226
|
|
|
227
227
|
|
|
228
|
-
def test_drawer_copy():
|
|
228
|
+
def test_drawer_copy() -> None:
|
|
229
229
|
orig_entries = {(0, 0): _DiagramText('entry', '')}
|
|
230
230
|
orig_vertical_lines = [_VerticalLine(1, 1, 3, True, False)]
|
|
231
231
|
orig_horizontal_lines = [_HorizontalLine(0, 0, 3, False, False)]
|
|
@@ -238,9 +238,9 @@ def test_drawer_copy():
|
|
|
238
238
|
'vertical_padding': orig_vertical_padding,
|
|
239
239
|
'horizontal_padding': orig_horizontal_padding,
|
|
240
240
|
}
|
|
241
|
-
orig_drawer = TextDiagramDrawer(**kwargs)
|
|
241
|
+
orig_drawer = TextDiagramDrawer(**kwargs) # type: ignore[arg-type]
|
|
242
242
|
|
|
243
|
-
same_drawer = TextDiagramDrawer(**kwargs)
|
|
243
|
+
same_drawer = TextDiagramDrawer(**kwargs) # type: ignore[arg-type]
|
|
244
244
|
assert orig_drawer == same_drawer
|
|
245
245
|
|
|
246
246
|
copy_drawer = orig_drawer.copy()
|
|
@@ -266,7 +266,7 @@ def test_drawer_copy():
|
|
|
266
266
|
assert copy_drawer != orig_drawer
|
|
267
267
|
|
|
268
268
|
|
|
269
|
-
def test_drawer_stack():
|
|
269
|
+
def test_drawer_stack() -> None:
|
|
270
270
|
d = TextDiagramDrawer()
|
|
271
271
|
d.write(0, 0, 'A')
|
|
272
272
|
d.write(1, 0, 'B')
|
|
@@ -336,7 +336,7 @@ AB D
|
|
|
336
336
|
with pytest.raises(ValueError):
|
|
337
337
|
TextDiagramDrawer.vstack((d, dd))
|
|
338
338
|
|
|
339
|
-
vstacked = TextDiagramDrawer.vstack((dd, d), padding_resolver=max)
|
|
339
|
+
vstacked = TextDiagramDrawer.vstack((dd, d), padding_resolver=max) # type: ignore[arg-type]
|
|
340
340
|
expected = """
|
|
341
341
|
D
|
|
342
342
|
|
|
@@ -350,7 +350,7 @@ A B
|
|
|
350
350
|
""".strip()
|
|
351
351
|
assert_has_rendering(vstacked, expected)
|
|
352
352
|
|
|
353
|
-
hstacked = TextDiagramDrawer.hstack((d, dd), padding_resolver=max)
|
|
353
|
+
hstacked = TextDiagramDrawer.hstack((d, dd), padding_resolver=max) # type: ignore[arg-type]
|
|
354
354
|
expected = """
|
|
355
355
|
AB D
|
|
356
356
|
|
|
@@ -360,7 +360,7 @@ AB D
|
|
|
360
360
|
""".strip()
|
|
361
361
|
assert_has_rendering(hstacked, expected)
|
|
362
362
|
|
|
363
|
-
vstacked_min = TextDiagramDrawer.vstack((dd, d), padding_resolver=min)
|
|
363
|
+
vstacked_min = TextDiagramDrawer.vstack((dd, d), padding_resolver=min) # type: ignore[arg-type]
|
|
364
364
|
expected = """
|
|
365
365
|
D
|
|
366
366
|
|
|
@@ -374,7 +374,7 @@ AB
|
|
|
374
374
|
""".strip()
|
|
375
375
|
assert_has_rendering(vstacked_min, expected)
|
|
376
376
|
|
|
377
|
-
hstacked_min = TextDiagramDrawer.hstack((d, dd), padding_resolver=min)
|
|
377
|
+
hstacked_min = TextDiagramDrawer.hstack((d, dd), padding_resolver=min) # type: ignore[arg-type]
|
|
378
378
|
expected = """
|
|
379
379
|
AB D
|
|
380
380
|
|
|
@@ -383,7 +383,7 @@ AB D
|
|
|
383
383
|
assert_has_rendering(hstacked_min, expected)
|
|
384
384
|
|
|
385
385
|
|
|
386
|
-
def test_drawer_eq():
|
|
386
|
+
def test_drawer_eq() -> None:
|
|
387
387
|
assert TextDiagramDrawer().__eq__(23) == NotImplemented
|
|
388
388
|
|
|
389
389
|
eq = ct.EqualsTester()
|
|
@@ -407,7 +407,7 @@ def test_drawer_eq():
|
|
|
407
407
|
eq.add_equality_group(dd)
|
|
408
408
|
|
|
409
409
|
|
|
410
|
-
def test_drawer_superimposed():
|
|
410
|
+
def test_drawer_superimposed() -> None:
|
|
411
411
|
empty_drawer = TextDiagramDrawer()
|
|
412
412
|
assert not empty_drawer
|
|
413
413
|
drawer_with_something = TextDiagramDrawer()
|
|
@@ -418,7 +418,7 @@ def test_drawer_superimposed():
|
|
|
418
418
|
assert not empty_drawer
|
|
419
419
|
|
|
420
420
|
|
|
421
|
-
def test_pick_charset():
|
|
421
|
+
def test_pick_charset() -> None:
|
|
422
422
|
assert pick_charset(use_unicode=False, emphasize=False, doubled=False) == ASCII_BOX_CHARS
|
|
423
423
|
assert pick_charset(use_unicode=False, emphasize=False, doubled=True) == ASCII_BOX_CHARS
|
|
424
424
|
assert pick_charset(use_unicode=False, emphasize=True, doubled=False) == ASCII_BOX_CHARS
|
|
@@ -64,26 +64,27 @@ def create_test_circuit():
|
|
|
64
64
|
)
|
|
65
65
|
|
|
66
66
|
|
|
67
|
-
def test_basis_state_simulator():
|
|
67
|
+
def test_basis_state_simulator() -> None:
|
|
68
68
|
sim = CustomStateSimulator(ComputationalBasisSimState)
|
|
69
69
|
circuit = create_test_circuit()
|
|
70
70
|
r = sim.simulate(circuit)
|
|
71
71
|
assert r.measurements == {'a': np.array([1]), 'b': np.array([2])}
|
|
72
|
-
assert r._final_simulator_state._state.basis == [2, 2]
|
|
72
|
+
assert r._final_simulator_state._state.basis == [2, 2] # type: ignore[attr-defined]
|
|
73
73
|
|
|
74
74
|
|
|
75
|
-
def test_built_in_states():
|
|
75
|
+
def test_built_in_states() -> None:
|
|
76
76
|
# Verify this works for the built-in states too, you just lose the custom step/trial results.
|
|
77
77
|
sim = CustomStateSimulator(cirq.StateVectorSimulationState)
|
|
78
78
|
circuit = create_test_circuit()
|
|
79
79
|
r = sim.simulate(circuit)
|
|
80
80
|
assert r.measurements == {'a': np.array([1]), 'b': np.array([2])}
|
|
81
81
|
assert np.allclose(
|
|
82
|
-
r._final_simulator_state._state._state_vector,
|
|
82
|
+
r._final_simulator_state._state._state_vector, # type: ignore[attr-defined]
|
|
83
|
+
[[0, 0, 0], [0, 0, 0], [0, 0, 1]],
|
|
83
84
|
)
|
|
84
85
|
|
|
85
86
|
|
|
86
|
-
def test_product_state_mode_built_in_state():
|
|
87
|
+
def test_product_state_mode_built_in_state() -> None:
|
|
87
88
|
sim = CustomStateSimulator(cirq.StateVectorSimulationState, split_untangled_states=True)
|
|
88
89
|
circuit = create_test_circuit()
|
|
89
90
|
r = sim.simulate(circuit)
|
|
@@ -99,16 +100,16 @@ def test_product_state_mode_built_in_state():
|
|
|
99
100
|
)
|
|
100
101
|
|
|
101
102
|
|
|
102
|
-
def test_noise():
|
|
103
|
+
def test_noise() -> None:
|
|
103
104
|
x = cirq.XPowGate(dimension=3)
|
|
104
105
|
sim = CustomStateSimulator(ComputationalBasisSimState, noise=x**2)
|
|
105
106
|
circuit = create_test_circuit()
|
|
106
107
|
r = sim.simulate(circuit)
|
|
107
108
|
assert r.measurements == {'a': np.array([2]), 'b': np.array([2])}
|
|
108
|
-
assert r._final_simulator_state._state.basis == [1, 2]
|
|
109
|
+
assert r._final_simulator_state._state.basis == [1, 2] # type: ignore[attr-defined]
|
|
109
110
|
|
|
110
111
|
|
|
111
|
-
def test_run():
|
|
112
|
+
def test_run() -> None:
|
|
112
113
|
sim = CustomStateSimulator(ComputationalBasisSimState)
|
|
113
114
|
circuit = create_test_circuit()
|
|
114
115
|
r = sim.run(circuit)
|
|
@@ -116,7 +117,7 @@ def test_run():
|
|
|
116
117
|
assert np.allclose(r.records['b'], np.array([[1], [2]]))
|
|
117
118
|
|
|
118
119
|
|
|
119
|
-
def test_parameterized_repetitions():
|
|
120
|
+
def test_parameterized_repetitions() -> None:
|
|
120
121
|
q = cirq.LineQid(0, dimension=5)
|
|
121
122
|
x = cirq.XPowGate(dimension=5)
|
|
122
123
|
circuit = cirq.Circuit(
|
|
@@ -182,7 +183,7 @@ class ComputationalBasisSimProductState(cirq.SimulationState[ComputationalBasisP
|
|
|
182
183
|
return True
|
|
183
184
|
|
|
184
185
|
|
|
185
|
-
def test_product_state_mode():
|
|
186
|
+
def test_product_state_mode() -> None:
|
|
186
187
|
sim = CustomStateSimulator(ComputationalBasisSimProductState, split_untangled_states=True)
|
|
187
188
|
circuit = create_test_circuit()
|
|
188
189
|
r = sim.simulate(circuit)
|
|
@@ -20,14 +20,14 @@ from cirq.contrib.qasm_import import QasmException
|
|
|
20
20
|
from cirq.contrib.qasm_import._lexer import QasmLexer
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def test_empty_circuit():
|
|
23
|
+
def test_empty_circuit() -> None:
|
|
24
24
|
lexer = QasmLexer()
|
|
25
25
|
lexer.input("")
|
|
26
26
|
assert lexer.token() is None
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
@pytest.mark.parametrize('number', ["00000", "03", "3", "0045", "21"])
|
|
30
|
-
def test_natural_numbers(number: str):
|
|
30
|
+
def test_natural_numbers(number: str) -> None:
|
|
31
31
|
lexer = QasmLexer()
|
|
32
32
|
lexer.input(number)
|
|
33
33
|
token = lexer.token()
|
|
@@ -36,7 +36,7 @@ def test_natural_numbers(number: str):
|
|
|
36
36
|
assert token.value == int(number)
|
|
37
37
|
|
|
38
38
|
|
|
39
|
-
def test_supported_format():
|
|
39
|
+
def test_supported_format() -> None:
|
|
40
40
|
lexer = QasmLexer()
|
|
41
41
|
lexer.input("OPENQASM 2.0;")
|
|
42
42
|
token = lexer.token()
|
|
@@ -45,7 +45,7 @@ def test_supported_format():
|
|
|
45
45
|
assert token.value == '2.0'
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
def test_qelib_inc():
|
|
48
|
+
def test_qelib_inc() -> None:
|
|
49
49
|
lexer = QasmLexer()
|
|
50
50
|
lexer.input('include "qelib1.inc";')
|
|
51
51
|
token = lexer.token()
|
|
@@ -54,26 +54,31 @@ def test_qelib_inc():
|
|
|
54
54
|
assert token.value == 'include "qelib1.inc";'
|
|
55
55
|
|
|
56
56
|
|
|
57
|
-
def test_measurement():
|
|
57
|
+
def test_measurement() -> None:
|
|
58
58
|
lexer = QasmLexer()
|
|
59
59
|
lexer.input("measure q -> c;")
|
|
60
60
|
token = lexer.token()
|
|
61
|
+
assert token is not None
|
|
61
62
|
assert token.type == "MEASURE"
|
|
62
63
|
assert token.value == 'measure'
|
|
63
64
|
|
|
64
65
|
token = lexer.token()
|
|
66
|
+
assert token is not None
|
|
65
67
|
assert token.type == "ID"
|
|
66
68
|
assert token.value == 'q'
|
|
67
69
|
|
|
68
70
|
token = lexer.token()
|
|
71
|
+
assert token is not None
|
|
69
72
|
assert token.type == "ARROW"
|
|
70
73
|
assert token.value == '->'
|
|
71
74
|
|
|
72
75
|
token = lexer.token()
|
|
76
|
+
assert token is not None
|
|
73
77
|
assert token.type == "ID"
|
|
74
78
|
assert token.value == 'c'
|
|
75
79
|
|
|
76
80
|
token = lexer.token()
|
|
81
|
+
assert token is not None
|
|
77
82
|
assert token.type == ";"
|
|
78
83
|
assert token.value == ';'
|
|
79
84
|
|
|
@@ -81,7 +86,7 @@ def test_measurement():
|
|
|
81
86
|
@pytest.mark.parametrize(
|
|
82
87
|
'identifier', ['b', 'CX', 'abc', 'aXY03', 'a_valid_name_with_02_digits_and_underscores']
|
|
83
88
|
)
|
|
84
|
-
def test_valid_ids(identifier: str):
|
|
89
|
+
def test_valid_ids(identifier: str) -> None:
|
|
85
90
|
lexer = QasmLexer()
|
|
86
91
|
lexer.input(identifier)
|
|
87
92
|
token = lexer.token()
|
|
@@ -95,7 +100,7 @@ def test_valid_ids(identifier: str):
|
|
|
95
100
|
'number',
|
|
96
101
|
['1e2', '1e0', '3.', '4.e10', '.333', '1.0', '0.1', '2.0e-05', '1.2E+05', '123123.2132312'],
|
|
97
102
|
)
|
|
98
|
-
def test_numbers(number: str):
|
|
103
|
+
def test_numbers(number: str) -> None:
|
|
99
104
|
lexer = QasmLexer()
|
|
100
105
|
lexer.input(number)
|
|
101
106
|
token = lexer.token()
|
|
@@ -106,146 +111,175 @@ def test_numbers(number: str):
|
|
|
106
111
|
|
|
107
112
|
|
|
108
113
|
@pytest.mark.parametrize('token', QasmLexer.reserved.keys())
|
|
109
|
-
def test_keywords(token):
|
|
114
|
+
def test_keywords(token) -> None:
|
|
110
115
|
lexer = QasmLexer()
|
|
111
116
|
identifier = f'{token} {token}'
|
|
112
117
|
lexer.input(identifier)
|
|
113
118
|
t = lexer.token()
|
|
119
|
+
assert t is not None
|
|
114
120
|
assert t.type == QasmLexer.reserved[token]
|
|
115
121
|
assert t.value == token
|
|
116
122
|
t2 = lexer.token()
|
|
123
|
+
assert t2 is not None
|
|
117
124
|
assert t2.type == QasmLexer.reserved[token]
|
|
118
125
|
assert t2.value == token
|
|
119
126
|
|
|
120
127
|
|
|
121
128
|
@pytest.mark.parametrize('token', QasmLexer.reserved.keys())
|
|
122
129
|
@pytest.mark.parametrize('separator', ['', '_'])
|
|
123
|
-
def test_identifier_starts_or_ends_with_keyword(token, separator):
|
|
130
|
+
def test_identifier_starts_or_ends_with_keyword(token, separator) -> None:
|
|
124
131
|
lexer = QasmLexer()
|
|
125
132
|
identifier = f'{token}{separator}{token}'
|
|
126
133
|
lexer.input(identifier)
|
|
127
134
|
t = lexer.token()
|
|
135
|
+
assert t is not None
|
|
128
136
|
assert t.type == "ID"
|
|
129
137
|
assert t.value == identifier
|
|
130
138
|
|
|
131
139
|
|
|
132
|
-
def test_qreg():
|
|
140
|
+
def test_qreg() -> None:
|
|
133
141
|
lexer = QasmLexer()
|
|
134
142
|
lexer.input('qreg [5];')
|
|
135
143
|
token = lexer.token()
|
|
144
|
+
assert token is not None
|
|
136
145
|
assert token.type == "QREG"
|
|
137
146
|
assert token.value == "qreg"
|
|
138
147
|
|
|
139
148
|
token = lexer.token()
|
|
149
|
+
assert token is not None
|
|
140
150
|
assert token.type == "["
|
|
141
151
|
assert token.value == "["
|
|
142
152
|
|
|
143
153
|
token = lexer.token()
|
|
154
|
+
assert token is not None
|
|
144
155
|
assert token.type == "NATURAL_NUMBER"
|
|
145
156
|
assert token.value == 5
|
|
146
157
|
|
|
147
158
|
token = lexer.token()
|
|
159
|
+
assert token is not None
|
|
148
160
|
assert token.type == "]"
|
|
149
161
|
assert token.value == "]"
|
|
150
162
|
|
|
151
163
|
token = lexer.token()
|
|
164
|
+
assert token is not None
|
|
152
165
|
assert token.type == ";"
|
|
153
166
|
assert token.value == ";"
|
|
154
167
|
|
|
155
168
|
|
|
156
|
-
def test_creg():
|
|
169
|
+
def test_creg() -> None:
|
|
157
170
|
lexer = QasmLexer()
|
|
158
171
|
lexer.input('creg [8];')
|
|
159
172
|
token = lexer.token()
|
|
173
|
+
assert token is not None
|
|
160
174
|
assert token.type == "CREG"
|
|
161
175
|
assert token.value == "creg"
|
|
162
176
|
|
|
163
177
|
token = lexer.token()
|
|
178
|
+
assert token is not None
|
|
164
179
|
assert token.type == "["
|
|
165
180
|
assert token.value == "["
|
|
166
181
|
|
|
167
182
|
token = lexer.token()
|
|
183
|
+
assert token is not None
|
|
168
184
|
assert token.type == "NATURAL_NUMBER"
|
|
169
185
|
assert token.value == 8
|
|
170
186
|
|
|
171
187
|
token = lexer.token()
|
|
188
|
+
assert token is not None
|
|
172
189
|
assert token.type == "]"
|
|
173
190
|
assert token.value == "]"
|
|
174
191
|
|
|
175
192
|
token = lexer.token()
|
|
193
|
+
assert token is not None
|
|
176
194
|
assert token.type == ";"
|
|
177
195
|
assert token.value == ";"
|
|
178
196
|
|
|
179
197
|
|
|
180
|
-
def test_custom_gate():
|
|
198
|
+
def test_custom_gate() -> None:
|
|
181
199
|
lexer = QasmLexer()
|
|
182
200
|
lexer.input('gate name(param1,param2) q1, q2 {X(q1)}')
|
|
183
201
|
token = lexer.token()
|
|
202
|
+
assert token is not None
|
|
184
203
|
assert token.type == "GATE"
|
|
185
204
|
assert token.value == "gate"
|
|
186
205
|
|
|
187
206
|
token = lexer.token()
|
|
207
|
+
assert token is not None
|
|
188
208
|
assert token.type == "ID"
|
|
189
209
|
assert token.value == "name"
|
|
190
210
|
|
|
191
211
|
token = lexer.token()
|
|
212
|
+
assert token is not None
|
|
192
213
|
assert token.type == "("
|
|
193
214
|
assert token.value == "("
|
|
194
215
|
|
|
195
216
|
token = lexer.token()
|
|
217
|
+
assert token is not None
|
|
196
218
|
assert token.type == "ID"
|
|
197
219
|
assert token.value == "param1"
|
|
198
220
|
|
|
199
221
|
token = lexer.token()
|
|
222
|
+
assert token is not None
|
|
200
223
|
assert token.type == ","
|
|
201
224
|
assert token.value == ","
|
|
202
225
|
|
|
203
226
|
token = lexer.token()
|
|
227
|
+
assert token is not None
|
|
204
228
|
assert token.type == "ID"
|
|
205
229
|
assert token.value == "param2"
|
|
206
230
|
|
|
207
231
|
token = lexer.token()
|
|
232
|
+
assert token is not None
|
|
208
233
|
assert token.type == ")"
|
|
209
234
|
assert token.value == ")"
|
|
210
235
|
|
|
211
236
|
token = lexer.token()
|
|
237
|
+
assert token is not None
|
|
212
238
|
assert token.type == "ID"
|
|
213
239
|
assert token.value == "q1"
|
|
214
240
|
|
|
215
241
|
token = lexer.token()
|
|
242
|
+
assert token is not None
|
|
216
243
|
assert token.type == ","
|
|
217
244
|
assert token.value == ","
|
|
218
245
|
|
|
219
246
|
token = lexer.token()
|
|
247
|
+
assert token is not None
|
|
220
248
|
assert token.type == "ID"
|
|
221
249
|
assert token.value == "q2"
|
|
222
250
|
|
|
223
251
|
token = lexer.token()
|
|
252
|
+
assert token is not None
|
|
224
253
|
assert token.type == "{"
|
|
225
254
|
assert token.value == "{"
|
|
226
255
|
|
|
227
256
|
token = lexer.token()
|
|
257
|
+
assert token is not None
|
|
228
258
|
assert token.type == "ID"
|
|
229
259
|
assert token.value == "X"
|
|
230
260
|
|
|
231
261
|
token = lexer.token()
|
|
262
|
+
assert token is not None
|
|
232
263
|
assert token.type == "("
|
|
233
264
|
assert token.value == "("
|
|
234
265
|
|
|
235
266
|
token = lexer.token()
|
|
267
|
+
assert token is not None
|
|
236
268
|
assert token.type == "ID"
|
|
237
269
|
assert token.value == "q1"
|
|
238
270
|
|
|
239
271
|
token = lexer.token()
|
|
272
|
+
assert token is not None
|
|
240
273
|
assert token.type == ")"
|
|
241
274
|
assert token.value == ")"
|
|
242
275
|
|
|
243
276
|
token = lexer.token()
|
|
277
|
+
assert token is not None
|
|
244
278
|
assert token.type == "}"
|
|
245
279
|
assert token.value == "}"
|
|
246
280
|
|
|
247
281
|
|
|
248
|
-
def test_error():
|
|
282
|
+
def test_error() -> None:
|
|
249
283
|
lexer = QasmLexer()
|
|
250
284
|
lexer.input('θ')
|
|
251
285
|
|
|
@@ -1516,7 +1516,7 @@ def test_nested_custom_gate_has_keyword_in_name() -> None:
|
|
|
1516
1516
|
assert parsed_qasm.circuit == expected
|
|
1517
1517
|
|
|
1518
1518
|
|
|
1519
|
-
def test_rzz_gate():
|
|
1519
|
+
def test_rzz_gate() -> None:
|
|
1520
1520
|
qasm = """
|
|
1521
1521
|
OPENQASM 2.0;
|
|
1522
1522
|
include "qelib1.inc";
|
|
@@ -1539,7 +1539,7 @@ def test_rzz_gate():
|
|
|
1539
1539
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1540
1540
|
|
|
1541
1541
|
|
|
1542
|
-
def test_rxx_gate():
|
|
1542
|
+
def test_rxx_gate() -> None:
|
|
1543
1543
|
qasm = """
|
|
1544
1544
|
OPENQASM 2.0;
|
|
1545
1545
|
include "qelib1.inc";
|
|
@@ -1562,7 +1562,7 @@ def test_rxx_gate():
|
|
|
1562
1562
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1563
1563
|
|
|
1564
1564
|
|
|
1565
|
-
def test_ryy_gate():
|
|
1565
|
+
def test_ryy_gate() -> None:
|
|
1566
1566
|
qasm = """
|
|
1567
1567
|
OPENQASM 2.0;
|
|
1568
1568
|
include "qelib1.inc";
|
|
@@ -1585,7 +1585,7 @@ def test_ryy_gate():
|
|
|
1585
1585
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1586
1586
|
|
|
1587
1587
|
|
|
1588
|
-
def test_crx_gate():
|
|
1588
|
+
def test_crx_gate() -> None:
|
|
1589
1589
|
qasm = """
|
|
1590
1590
|
OPENQASM 2.0;
|
|
1591
1591
|
include "qelib1.inc";
|
|
@@ -1608,7 +1608,7 @@ def test_crx_gate():
|
|
|
1608
1608
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1609
1609
|
|
|
1610
1610
|
|
|
1611
|
-
def test_iswap_gate():
|
|
1611
|
+
def test_iswap_gate() -> None:
|
|
1612
1612
|
qasm = """
|
|
1613
1613
|
OPENQASM 2.0;
|
|
1614
1614
|
include "qelib1.inc";
|