cirq-core 1.7.0.dev20250818230756__py3-none-any.whl → 1.7.0.dev20250820205919__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/experiments/z_phase_calibration_test.py +8 -7
- cirq/interop/quirk/cells/arithmetic_cells.py +1 -1
- cirq/interop/quirk/cells/cell_test.py +5 -5
- cirq/interop/quirk/cells/composite_cell_test.py +5 -3
- cirq/interop/quirk/cells/control_cells.py +2 -2
- cirq/interop/quirk/cells/input_cells.py +3 -3
- cirq/interop/quirk/cells/input_rotation_cells.py +2 -2
- cirq/interop/quirk/cells/swap_cell.py +2 -2
- cirq/interop/quirk/cells/testing.py +1 -1
- cirq/interop/quirk/url_to_circuit.py +2 -2
- cirq/interop/quirk/url_to_circuit_test.py +11 -11
- cirq/linalg/decompositions.py +1 -1
- cirq/linalg/decompositions_test.py +52 -50
- cirq/linalg/diagonalize_test.py +2 -2
- cirq/linalg/predicates.py +1 -1
- cirq/linalg/predicates_test.py +24 -24
- cirq/linalg/transformations.py +6 -4
- cirq/neutral_atoms/neutral_atom_devices.py +1 -1
- cirq/ops/classically_controlled_operation.py +2 -2
- cirq/ops/clifford_gate.py +1 -1
- cirq/ops/common_channels_test.py +1 -1
- cirq/ops/common_gates.py +2 -2
- cirq/ops/controlled_operation.py +2 -2
- cirq/ops/controlled_operation_test.py +17 -16
- cirq/ops/dense_pauli_string_test.py +41 -37
- cirq/ops/eigen_gate_test.py +0 -7
- cirq/ops/gateset.py +3 -3
- cirq/ops/gateset_test.py +23 -23
- cirq/ops/linear_combinations.py +1 -1
- cirq/ops/linear_combinations_test.py +103 -89
- cirq/ops/matrix_gates_test.py +31 -31
- cirq/ops/measure_util_test.py +11 -10
- cirq/ops/measurement_gate_test.py +23 -22
- cirq/ops/pauli_gates.py +1 -1
- cirq/ops/pauli_measurement_gate_test.py +12 -12
- cirq/ops/pauli_string_phasor_test.py +45 -40
- cirq/ops/pauli_string_raw_types_test.py +5 -4
- cirq/ops/pauli_string_test.py +190 -118
- cirq/ops/permutation_gate.py +1 -1
- cirq/ops/raw_types.py +3 -4
- cirq/ops/raw_types_test.py +1 -1
- cirq/ops/three_qubit_gates.py +3 -3
- cirq/protocols/act_on_protocol.py +1 -1
- cirq/protocols/act_on_protocol_test.py +4 -4
- cirq/protocols/apply_channel_protocol_test.py +19 -18
- cirq/protocols/apply_mixture_protocol_test.py +20 -19
- cirq/protocols/apply_unitary_protocol_test.py +31 -23
- cirq/protocols/approximate_equality_protocol_test.py +22 -16
- cirq/protocols/circuit_diagram_info_protocol.py +4 -4
- cirq/protocols/commutes_protocol_test.py +4 -4
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/METADATA +1 -1
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/RECORD +57 -57
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/WHEEL +0 -0
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/top_level.txt +0 -0
cirq/ops/matrix_gates_test.py
CHANGED
|
@@ -28,7 +28,7 @@ QFT2 = np.array([[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j
|
|
|
28
28
|
PLUS_ONE = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]])
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
def test_single_qubit_init():
|
|
31
|
+
def test_single_qubit_init() -> None:
|
|
32
32
|
m = np.array([[1, 1j], [1j, 1]]) * np.sqrt(0.5)
|
|
33
33
|
x2 = cirq.MatrixGate(m)
|
|
34
34
|
assert cirq.has_unitary(x2)
|
|
@@ -50,7 +50,7 @@ def test_single_qubit_init():
|
|
|
50
50
|
cirq.MatrixGate(np.ones((2, 2, 2)))
|
|
51
51
|
|
|
52
52
|
|
|
53
|
-
def test_single_qubit_eq():
|
|
53
|
+
def test_single_qubit_eq() -> None:
|
|
54
54
|
eq = cirq.testing.EqualsTester()
|
|
55
55
|
eq.make_equality_group(lambda: cirq.MatrixGate(np.eye(2)))
|
|
56
56
|
eq.make_equality_group(lambda: cirq.MatrixGate(np.array([[0, 1], [1, 0]])))
|
|
@@ -59,14 +59,14 @@ def test_single_qubit_eq():
|
|
|
59
59
|
eq.add_equality_group(cirq.MatrixGate(PLUS_ONE, qid_shape=(3,)))
|
|
60
60
|
|
|
61
61
|
|
|
62
|
-
def test_single_qubit_trace_distance_bound():
|
|
62
|
+
def test_single_qubit_trace_distance_bound() -> None:
|
|
63
63
|
x = cirq.MatrixGate(np.array([[0, 1], [1, 0]]))
|
|
64
64
|
x2 = cirq.MatrixGate(np.array([[1, 1j], [1j, 1]]) * np.sqrt(0.5))
|
|
65
65
|
assert cirq.trace_distance_bound(x) >= 1
|
|
66
66
|
assert cirq.trace_distance_bound(x2) >= 0.5
|
|
67
67
|
|
|
68
68
|
|
|
69
|
-
def test_single_qubit_approx_eq():
|
|
69
|
+
def test_single_qubit_approx_eq() -> None:
|
|
70
70
|
x = cirq.MatrixGate(np.array([[0, 1], [1, 0]]))
|
|
71
71
|
i = cirq.MatrixGate(np.array([[1, 0], [0, 1]]))
|
|
72
72
|
i_ish = cirq.MatrixGate(np.array([[1, 0.000000000000001], [0, 1]]))
|
|
@@ -76,7 +76,7 @@ def test_single_qubit_approx_eq():
|
|
|
76
76
|
assert not cirq.approx_eq(i, '', atol=1e-9)
|
|
77
77
|
|
|
78
78
|
|
|
79
|
-
def test_single_qubit_extrapolate():
|
|
79
|
+
def test_single_qubit_extrapolate() -> None:
|
|
80
80
|
i = cirq.MatrixGate(np.eye(2))
|
|
81
81
|
x = cirq.MatrixGate(np.array([[0, 1], [1, 0]]))
|
|
82
82
|
x2 = cirq.MatrixGate(np.array([[1, 1j], [1j, 1]]) * (1 - 1j) / 2)
|
|
@@ -97,20 +97,20 @@ def test_single_qubit_extrapolate():
|
|
|
97
97
|
_ = x ** sympy.Symbol('a')
|
|
98
98
|
|
|
99
99
|
|
|
100
|
-
def test_two_qubit_init():
|
|
100
|
+
def test_two_qubit_init() -> None:
|
|
101
101
|
x2 = cirq.MatrixGate(QFT2)
|
|
102
102
|
assert cirq.has_unitary(x2)
|
|
103
103
|
assert np.all(cirq.unitary(x2) == QFT2)
|
|
104
104
|
|
|
105
105
|
|
|
106
|
-
def test_two_qubit_eq():
|
|
106
|
+
def test_two_qubit_eq() -> None:
|
|
107
107
|
eq = cirq.testing.EqualsTester()
|
|
108
108
|
eq.make_equality_group(lambda: cirq.MatrixGate(np.eye(4)))
|
|
109
109
|
eq.make_equality_group(lambda: cirq.MatrixGate(QFT2))
|
|
110
110
|
eq.make_equality_group(lambda: cirq.MatrixGate(HH))
|
|
111
111
|
|
|
112
112
|
|
|
113
|
-
def test_two_qubit_approx_eq():
|
|
113
|
+
def test_two_qubit_approx_eq() -> None:
|
|
114
114
|
f = cirq.MatrixGate(QFT2)
|
|
115
115
|
perturb = np.zeros(shape=QFT2.shape, dtype=np.float64)
|
|
116
116
|
perturb[1, 2] = 1e-8
|
|
@@ -123,7 +123,7 @@ def test_two_qubit_approx_eq():
|
|
|
123
123
|
assert not cirq.approx_eq(f, cirq.MatrixGate(HH), atol=1e-9)
|
|
124
124
|
|
|
125
125
|
|
|
126
|
-
def test_two_qubit_extrapolate():
|
|
126
|
+
def test_two_qubit_extrapolate() -> None:
|
|
127
127
|
cz2 = cirq.MatrixGate(np.diag([1, 1, 1, 1j]))
|
|
128
128
|
cz4 = cirq.MatrixGate(np.diag([1, 1, 1, (1 + 1j) * np.sqrt(0.5)]))
|
|
129
129
|
i = cirq.MatrixGate(np.eye(4))
|
|
@@ -135,7 +135,7 @@ def test_two_qubit_extrapolate():
|
|
|
135
135
|
_ = cz2 ** sympy.Symbol('a')
|
|
136
136
|
|
|
137
137
|
|
|
138
|
-
def test_single_qubit_diagram():
|
|
138
|
+
def test_single_qubit_diagram() -> None:
|
|
139
139
|
a = cirq.NamedQubit('a')
|
|
140
140
|
b = cirq.NamedQubit('b')
|
|
141
141
|
m = np.array([[1, 1j], [1j, 1]]) * np.sqrt(0.5)
|
|
@@ -169,11 +169,11 @@ a[ ]+ b
|
|
|
169
169
|
)
|
|
170
170
|
|
|
171
171
|
|
|
172
|
-
def test_two_qubit_diagram():
|
|
172
|
+
def test_two_qubit_diagram() -> None:
|
|
173
173
|
a = cirq.NamedQubit('a')
|
|
174
174
|
b = cirq.NamedQubit('b')
|
|
175
175
|
c = cirq.NamedQubit('c')
|
|
176
|
-
|
|
176
|
+
circuit = cirq.Circuit(
|
|
177
177
|
cirq.MatrixGate(cirq.unitary(cirq.CZ)).on(a, b),
|
|
178
178
|
cirq.MatrixGate(cirq.unitary(cirq.CZ)).on(c, a),
|
|
179
179
|
)
|
|
@@ -195,7 +195,7 @@ c: ────[──────────]+────│[0-9\.+\-j ]+│
|
|
|
195
195
|
[ ]+ │[0-9\.+\-j ]+│
|
|
196
196
|
[ ]+ └[ ]+┘
|
|
197
197
|
""".strip(),
|
|
198
|
-
|
|
198
|
+
circuit.to_text_diagram().strip(),
|
|
199
199
|
)
|
|
200
200
|
|
|
201
201
|
assert re.match(
|
|
@@ -217,11 +217,11 @@ a[ ]+ b c
|
|
|
217
217
|
│[ ]+ │ └[ ]+┘
|
|
218
218
|
│[ ]+ │ │
|
|
219
219
|
""".strip(),
|
|
220
|
-
|
|
220
|
+
circuit.to_text_diagram(transpose=True).strip(),
|
|
221
221
|
)
|
|
222
222
|
|
|
223
223
|
|
|
224
|
-
def test_named_single_qubit_diagram():
|
|
224
|
+
def test_named_single_qubit_diagram() -> None:
|
|
225
225
|
a = cirq.NamedQubit('a')
|
|
226
226
|
b = cirq.NamedQubit('b')
|
|
227
227
|
m = np.array([[1, 1j], [1j, 1]]) * np.sqrt(0.5)
|
|
@@ -245,11 +245,11 @@ Foo │
|
|
|
245
245
|
assert expected_vertical == c.to_text_diagram(transpose=True).strip()
|
|
246
246
|
|
|
247
247
|
|
|
248
|
-
def test_named_two_qubit_diagram():
|
|
248
|
+
def test_named_two_qubit_diagram() -> None:
|
|
249
249
|
a = cirq.NamedQubit('a')
|
|
250
250
|
b = cirq.NamedQubit('b')
|
|
251
251
|
c = cirq.NamedQubit('c')
|
|
252
|
-
|
|
252
|
+
circuit = cirq.Circuit(
|
|
253
253
|
cirq.MatrixGate(cirq.unitary(cirq.CZ), name='Foo').on(a, b),
|
|
254
254
|
cirq.MatrixGate(cirq.unitary(cirq.CZ), name='Bar').on(c, a),
|
|
255
255
|
)
|
|
@@ -261,7 +261,7 @@ b: ───Foo[2]───┼────────
|
|
|
261
261
|
│
|
|
262
262
|
c: ────────────Bar[1]───
|
|
263
263
|
""".strip()
|
|
264
|
-
assert expected_horizontal ==
|
|
264
|
+
assert expected_horizontal == circuit.to_text_diagram().strip()
|
|
265
265
|
|
|
266
266
|
expected_vertical = """
|
|
267
267
|
a b c
|
|
@@ -271,10 +271,10 @@ Foo[1]─Foo[2] │
|
|
|
271
271
|
Bar[2]─┼──────Bar[1]
|
|
272
272
|
│ │ │
|
|
273
273
|
""".strip()
|
|
274
|
-
assert expected_vertical ==
|
|
274
|
+
assert expected_vertical == circuit.to_text_diagram(transpose=True).strip()
|
|
275
275
|
|
|
276
276
|
|
|
277
|
-
def test_with_name():
|
|
277
|
+
def test_with_name() -> None:
|
|
278
278
|
gate = cirq.MatrixGate(cirq.unitary(cirq.Z**0.25))
|
|
279
279
|
T = gate.with_name('T')
|
|
280
280
|
S = (T**2).with_name('S')
|
|
@@ -284,13 +284,13 @@ def test_with_name():
|
|
|
284
284
|
np.testing.assert_allclose(cirq.unitary(S), cirq.unitary(T**2))
|
|
285
285
|
|
|
286
286
|
|
|
287
|
-
def test_str_executes():
|
|
287
|
+
def test_str_executes() -> None:
|
|
288
288
|
assert '1' in str(cirq.MatrixGate(np.eye(2)))
|
|
289
289
|
assert '0' in str(cirq.MatrixGate(np.eye(4)))
|
|
290
290
|
|
|
291
291
|
|
|
292
292
|
@pytest.mark.parametrize('n', [1, 2, 3, 4, 5])
|
|
293
|
-
def test_implements_consistent_protocols(n):
|
|
293
|
+
def test_implements_consistent_protocols(n) -> None:
|
|
294
294
|
u = cirq.testing.random_unitary(2**n)
|
|
295
295
|
g1 = cirq.MatrixGate(u)
|
|
296
296
|
cirq.testing.assert_implements_consistent_protocols(g1, ignoring_global_phase=True)
|
|
@@ -304,12 +304,12 @@ def test_implements_consistent_protocols(n):
|
|
|
304
304
|
cirq.testing.assert_decompose_ends_at_default_gateset(g2)
|
|
305
305
|
|
|
306
306
|
|
|
307
|
-
def test_repr():
|
|
307
|
+
def test_repr() -> None:
|
|
308
308
|
cirq.testing.assert_equivalent_repr(cirq.MatrixGate(cirq.testing.random_unitary(2)))
|
|
309
309
|
cirq.testing.assert_equivalent_repr(cirq.MatrixGate(cirq.testing.random_unitary(4)))
|
|
310
310
|
|
|
311
311
|
|
|
312
|
-
def test_matrix_gate_init_validation():
|
|
312
|
+
def test_matrix_gate_init_validation() -> None:
|
|
313
313
|
with pytest.raises(ValueError, match='square 2d numpy array'):
|
|
314
314
|
_ = cirq.MatrixGate(np.ones(shape=(1, 1, 1)))
|
|
315
315
|
with pytest.raises(ValueError, match='square 2d numpy array'):
|
|
@@ -322,7 +322,7 @@ def test_matrix_gate_init_validation():
|
|
|
322
322
|
_ = cirq.MatrixGate(np.eye(3), qid_shape=(4,))
|
|
323
323
|
|
|
324
324
|
|
|
325
|
-
def test_matrix_gate_eq():
|
|
325
|
+
def test_matrix_gate_eq() -> None:
|
|
326
326
|
eq = cirq.testing.EqualsTester()
|
|
327
327
|
eq.add_equality_group(cirq.MatrixGate(np.eye(1)))
|
|
328
328
|
eq.add_equality_group(cirq.MatrixGate(-np.eye(1)))
|
|
@@ -330,7 +330,7 @@ def test_matrix_gate_eq():
|
|
|
330
330
|
eq.add_equality_group(cirq.MatrixGate(np.diag([1, 1, 1, 1, 1, -1]), qid_shape=(3, 2)))
|
|
331
331
|
|
|
332
332
|
|
|
333
|
-
def test_matrix_gate_pow():
|
|
333
|
+
def test_matrix_gate_pow() -> None:
|
|
334
334
|
t = sympy.Symbol('t')
|
|
335
335
|
assert cirq.pow(cirq.MatrixGate(1j * np.eye(1)), t, default=None) is None
|
|
336
336
|
assert cirq.pow(cirq.MatrixGate(1j * np.eye(1)), 2) == cirq.MatrixGate(-np.eye(1))
|
|
@@ -339,7 +339,7 @@ def test_matrix_gate_pow():
|
|
|
339
339
|
assert m**3 == cirq.MatrixGate(np.diag([1, -1j, -1]), qid_shape=(3,))
|
|
340
340
|
|
|
341
341
|
|
|
342
|
-
def test_phase_by():
|
|
342
|
+
def test_phase_by() -> None:
|
|
343
343
|
# Single qubit case.
|
|
344
344
|
x = cirq.MatrixGate(cirq.unitary(cirq.X))
|
|
345
345
|
y = cirq.phase_by(x, 0.25, 0)
|
|
@@ -363,14 +363,14 @@ def test_phase_by():
|
|
|
363
363
|
_ = cirq.phase_by(m, 0.25, 0)
|
|
364
364
|
|
|
365
365
|
|
|
366
|
-
def test_protocols_and_repr():
|
|
366
|
+
def test_protocols_and_repr() -> None:
|
|
367
367
|
cirq.testing.assert_implements_consistent_protocols(cirq.MatrixGate(np.diag([1, 1j, 1, -1])))
|
|
368
368
|
cirq.testing.assert_implements_consistent_protocols(
|
|
369
369
|
cirq.MatrixGate(np.diag([1, 1j, -1]), qid_shape=(3,))
|
|
370
370
|
)
|
|
371
371
|
|
|
372
372
|
|
|
373
|
-
def test_matrixgate_unitary_tolerance():
|
|
373
|
+
def test_matrixgate_unitary_tolerance() -> None:
|
|
374
374
|
## non-unitary matrix
|
|
375
375
|
with pytest.raises(ValueError):
|
|
376
376
|
_ = cirq.MatrixGate(np.array([[1, 0], [0, -0.6]]), unitary_check_atol=0.5)
|
|
@@ -393,7 +393,7 @@ def test_matrixgate_unitary_tolerance():
|
|
|
393
393
|
_ = cirq.MatrixGate(np.array([[0.707, 0.707], [-0.707, 0.707]]), unitary_check_rtol=1e-10)
|
|
394
394
|
|
|
395
395
|
|
|
396
|
-
def test_matrixgate_name_serialization():
|
|
396
|
+
def test_matrixgate_name_serialization() -> None:
|
|
397
397
|
# https://github.com/quantumlib/Cirq/issues/5999
|
|
398
398
|
|
|
399
399
|
# Test name serialization
|
|
@@ -415,7 +415,7 @@ def test_matrixgate_name_serialization():
|
|
|
415
415
|
assert gate_after_serialization3._name == ''
|
|
416
416
|
|
|
417
417
|
|
|
418
|
-
def test_decompose_when_qubits_not_in_ascending_order():
|
|
418
|
+
def test_decompose_when_qubits_not_in_ascending_order() -> None:
|
|
419
419
|
# Previous code for preserving global phase would misorder qubits
|
|
420
420
|
q0, q1 = cirq.LineQubit.range(2)
|
|
421
421
|
circuit1 = cirq.Circuit()
|
cirq/ops/measure_util_test.py
CHANGED
|
@@ -20,7 +20,7 @@ import pytest
|
|
|
20
20
|
import cirq
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def test_measure_qubits():
|
|
23
|
+
def test_measure_qubits() -> None:
|
|
24
24
|
a = cirq.NamedQubit('a')
|
|
25
25
|
b = cirq.NamedQubit('b')
|
|
26
26
|
|
|
@@ -48,6 +48,7 @@ def test_measure_qubits():
|
|
|
48
48
|
assert cirq.measure(cirq.LineQid.for_qid_shape((1, 2, 3)), key='a') == cirq.MeasurementGate(
|
|
49
49
|
num_qubits=3, key='a', qid_shape=(1, 2, 3)
|
|
50
50
|
).on(*cirq.LineQid.for_qid_shape((1, 2, 3)))
|
|
51
|
+
cmap: dict[tuple[int, ...], np.ndarray]
|
|
51
52
|
cmap = {(0,): np.array([[0, 1], [1, 0]])}
|
|
52
53
|
assert cirq.measure(a, confusion_map=cmap) == cirq.MeasurementGate(
|
|
53
54
|
num_qubits=1, key='a', confusion_map=cmap
|
|
@@ -57,16 +58,16 @@ def test_measure_qubits():
|
|
|
57
58
|
_ = cirq.measure(np.array([1, 0]))
|
|
58
59
|
|
|
59
60
|
with pytest.raises(ValueError, match='Qid'):
|
|
60
|
-
_ = cirq.measure("bork")
|
|
61
|
+
_ = cirq.measure("bork") # type: ignore[arg-type]
|
|
61
62
|
|
|
62
63
|
with pytest.raises(ValueError, match='Qid'):
|
|
63
|
-
_ = cirq.measure([a, [b]])
|
|
64
|
+
_ = cirq.measure([a, [b]]) # type: ignore[list-item]
|
|
64
65
|
|
|
65
66
|
with pytest.raises(ValueError, match='Qid'):
|
|
66
|
-
_ = cirq.measure([a], [b])
|
|
67
|
+
_ = cirq.measure([a], [b]) # type: ignore
|
|
67
68
|
|
|
68
69
|
|
|
69
|
-
def test_measure_each():
|
|
70
|
+
def test_measure_each() -> None:
|
|
70
71
|
a = cirq.NamedQubit('a')
|
|
71
72
|
b = cirq.NamedQubit('b')
|
|
72
73
|
|
|
@@ -83,13 +84,13 @@ def test_measure_each():
|
|
|
83
84
|
cirq.measure(b.with_dimension(3)),
|
|
84
85
|
]
|
|
85
86
|
|
|
86
|
-
assert cirq.measure_each(a, b, key_func=lambda e: e.name + '!') == [
|
|
87
|
+
assert cirq.measure_each(a, b, key_func=lambda e: e.name + '!') == [ # type: ignore[attr-defined]
|
|
87
88
|
cirq.measure(a, key='a!'),
|
|
88
89
|
cirq.measure(b, key='b!'),
|
|
89
90
|
]
|
|
90
91
|
|
|
91
92
|
|
|
92
|
-
def test_measure_single_paulistring():
|
|
93
|
+
def test_measure_single_paulistring() -> None:
|
|
93
94
|
# Correct application
|
|
94
95
|
q = cirq.LineQubit.range(3)
|
|
95
96
|
ps = cirq.X(q[0]) * cirq.Y(q[1]) * cirq.Z(q[2])
|
|
@@ -109,14 +110,14 @@ def test_measure_single_paulistring():
|
|
|
109
110
|
|
|
110
111
|
# Wrong type
|
|
111
112
|
with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'):
|
|
112
|
-
_ = cirq.measure_single_paulistring(q)
|
|
113
|
+
_ = cirq.measure_single_paulistring(q) # type: ignore[arg-type]
|
|
113
114
|
|
|
114
115
|
# Coefficient != +1 or -1
|
|
115
116
|
with pytest.raises(ValueError, match='must have a coefficient'):
|
|
116
117
|
_ = cirq.measure_single_paulistring(-2 * ps)
|
|
117
118
|
|
|
118
119
|
|
|
119
|
-
def test_measure_paulistring_terms():
|
|
120
|
+
def test_measure_paulistring_terms() -> None:
|
|
120
121
|
# Correct application
|
|
121
122
|
q = cirq.LineQubit.range(3)
|
|
122
123
|
ps = cirq.X(q[0]) * cirq.Y(q[1]) * cirq.Z(q[2])
|
|
@@ -132,4 +133,4 @@ def test_measure_paulistring_terms():
|
|
|
132
133
|
|
|
133
134
|
# Wrong type
|
|
134
135
|
with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'):
|
|
135
|
-
_ = cirq.measure_paulistring_terms(q)
|
|
136
|
+
_ = cirq.measure_paulistring_terms(q) # type: ignore[arg-type]
|
|
@@ -26,14 +26,14 @@ import cirq
|
|
|
26
26
|
'key',
|
|
27
27
|
['q0_1_0', cirq.MeasurementKey(name='q0_1_0'), cirq.MeasurementKey(path=('a', 'b'), name='c')],
|
|
28
28
|
)
|
|
29
|
-
def test_eval_repr(key):
|
|
29
|
+
def test_eval_repr(key) -> None:
|
|
30
30
|
# Basic safeguard against repr-inequality.
|
|
31
31
|
op = cirq.GateOperation(gate=cirq.MeasurementGate(1, key), qubits=[cirq.GridQubit(0, 1)])
|
|
32
32
|
cirq.testing.assert_equivalent_repr(op)
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
@pytest.mark.parametrize('num_qubits', [1, 2, 4])
|
|
36
|
-
def test_measure_init(num_qubits):
|
|
36
|
+
def test_measure_init(num_qubits) -> None:
|
|
37
37
|
assert cirq.MeasurementGate(num_qubits, 'a').num_qubits() == num_qubits
|
|
38
38
|
assert cirq.MeasurementGate(num_qubits, key='a').key == 'a'
|
|
39
39
|
assert cirq.MeasurementGate(num_qubits, key='a').mkey == cirq.MeasurementKey('a')
|
|
@@ -43,11 +43,12 @@ def test_measure_init(num_qubits):
|
|
|
43
43
|
)
|
|
44
44
|
assert cirq.MeasurementGate(num_qubits, 'a', invert_mask=(True,)).invert_mask == (True,)
|
|
45
45
|
assert cirq.qid_shape(cirq.MeasurementGate(num_qubits, 'a')) == (2,) * num_qubits
|
|
46
|
+
cmap: dict[tuple[int, ...], np.ndarray]
|
|
46
47
|
cmap = {(0,): np.array([[0, 1], [1, 0]])}
|
|
47
48
|
assert cirq.MeasurementGate(num_qubits, 'a', confusion_map=cmap).confusion_map == cmap
|
|
48
49
|
|
|
49
50
|
|
|
50
|
-
def test_measure_init_num_qubit_agnostic():
|
|
51
|
+
def test_measure_init_num_qubit_agnostic() -> None:
|
|
51
52
|
assert cirq.qid_shape(cirq.MeasurementGate(3, 'a', qid_shape=(1, 2, 3))) == (1, 2, 3)
|
|
52
53
|
assert cirq.qid_shape(cirq.MeasurementGate(key='a', qid_shape=(1, 2, 3))) == (1, 2, 3)
|
|
53
54
|
with pytest.raises(ValueError, match='len.* >'):
|
|
@@ -55,24 +56,24 @@ def test_measure_init_num_qubit_agnostic():
|
|
|
55
56
|
with pytest.raises(ValueError, match='len.* !='):
|
|
56
57
|
cirq.MeasurementGate(5, 'a', qid_shape=(1, 2))
|
|
57
58
|
with pytest.raises(ValueError, match='valid string'):
|
|
58
|
-
cirq.MeasurementGate(2, qid_shape=(1, 2), key=None)
|
|
59
|
+
cirq.MeasurementGate(2, qid_shape=(1, 2), key=None) # type: ignore[arg-type]
|
|
59
60
|
with pytest.raises(ValueError, match='Confusion matrices have index out of bounds'):
|
|
60
61
|
cirq.MeasurementGate(1, 'a', confusion_map={(1,): np.array([[0, 1], [1, 0]])})
|
|
61
62
|
with pytest.raises(ValueError, match='Specify either'):
|
|
62
63
|
cirq.MeasurementGate()
|
|
63
64
|
|
|
64
65
|
|
|
65
|
-
def test_measurement_has_unitary_returns_false():
|
|
66
|
+
def test_measurement_has_unitary_returns_false() -> None:
|
|
66
67
|
gate = cirq.MeasurementGate(1, 'a')
|
|
67
68
|
assert not cirq.has_unitary(gate)
|
|
68
69
|
|
|
69
70
|
|
|
70
71
|
@pytest.mark.parametrize('num_qubits', [1, 2, 4])
|
|
71
|
-
def test_has_stabilizer_effect(num_qubits):
|
|
72
|
+
def test_has_stabilizer_effect(num_qubits) -> None:
|
|
72
73
|
assert cirq.has_stabilizer_effect(cirq.MeasurementGate(num_qubits, 'a'))
|
|
73
74
|
|
|
74
75
|
|
|
75
|
-
def test_measurement_eq():
|
|
76
|
+
def test_measurement_eq() -> None:
|
|
76
77
|
eq = cirq.testing.EqualsTester()
|
|
77
78
|
eq.make_equality_group(
|
|
78
79
|
lambda: cirq.MeasurementGate(1, 'a'),
|
|
@@ -93,7 +94,7 @@ def test_measurement_eq():
|
|
|
93
94
|
eq.add_equality_group(cirq.MeasurementGate(3, 'a', qid_shape=(1, 2, 3)))
|
|
94
95
|
|
|
95
96
|
|
|
96
|
-
def test_measurement_full_invert_mask():
|
|
97
|
+
def test_measurement_full_invert_mask() -> None:
|
|
97
98
|
assert cirq.MeasurementGate(1, 'a').full_invert_mask() == (False,)
|
|
98
99
|
assert cirq.MeasurementGate(2, 'a', invert_mask=(False, True)).full_invert_mask() == (
|
|
99
100
|
False,
|
|
@@ -112,7 +113,7 @@ def test_measurement_full_invert_mask():
|
|
|
112
113
|
cirq.MeasurementGate(2, 'a', invert_mask=(True, False), qid_shape=(2, 3)),
|
|
113
114
|
],
|
|
114
115
|
)
|
|
115
|
-
def test_measurement_with_key(use_protocol, gate):
|
|
116
|
+
def test_measurement_with_key(use_protocol, gate) -> None:
|
|
116
117
|
if use_protocol:
|
|
117
118
|
gate1 = cirq.with_measurement_key_mapping(gate, {'a': 'b'})
|
|
118
119
|
else:
|
|
@@ -136,7 +137,7 @@ def test_measurement_with_key(use_protocol, gate):
|
|
|
136
137
|
(3, (False, False), [0, 2], (True, False, True)),
|
|
137
138
|
],
|
|
138
139
|
)
|
|
139
|
-
def test_measurement_with_bits_flipped(num_qubits, mask, bits, flipped):
|
|
140
|
+
def test_measurement_with_bits_flipped(num_qubits, mask, bits, flipped) -> None:
|
|
140
141
|
gate = cirq.MeasurementGate(num_qubits, key='a', invert_mask=mask, qid_shape=(3,) * num_qubits)
|
|
141
142
|
|
|
142
143
|
gate1 = gate.with_bits_flipped(*bits)
|
|
@@ -150,7 +151,7 @@ def test_measurement_with_bits_flipped(num_qubits, mask, bits, flipped):
|
|
|
150
151
|
assert gate2.full_invert_mask() == gate.full_invert_mask()
|
|
151
152
|
|
|
152
153
|
|
|
153
|
-
def test_qudit_measure_qasm():
|
|
154
|
+
def test_qudit_measure_qasm() -> None:
|
|
154
155
|
assert (
|
|
155
156
|
cirq.qasm(
|
|
156
157
|
cirq.measure(cirq.LineQid(0, 3), key='a'),
|
|
@@ -161,7 +162,7 @@ def test_qudit_measure_qasm():
|
|
|
161
162
|
)
|
|
162
163
|
|
|
163
164
|
|
|
164
|
-
def test_confused_measure_qasm():
|
|
165
|
+
def test_confused_measure_qasm() -> None:
|
|
165
166
|
q0 = cirq.LineQubit(0)
|
|
166
167
|
assert (
|
|
167
168
|
cirq.qasm(
|
|
@@ -173,7 +174,7 @@ def test_confused_measure_qasm():
|
|
|
173
174
|
)
|
|
174
175
|
|
|
175
176
|
|
|
176
|
-
def test_measurement_gate_diagram():
|
|
177
|
+
def test_measurement_gate_diagram() -> None:
|
|
177
178
|
# Shows key.
|
|
178
179
|
assert cirq.circuit_diagram_info(
|
|
179
180
|
cirq.MeasurementGate(1, key='test')
|
|
@@ -245,7 +246,7 @@ b: ───M───────────
|
|
|
245
246
|
)
|
|
246
247
|
|
|
247
248
|
|
|
248
|
-
def test_measurement_channel():
|
|
249
|
+
def test_measurement_channel() -> None:
|
|
249
250
|
np.testing.assert_allclose(
|
|
250
251
|
cirq.kraus(cirq.MeasurementGate(1, 'a')),
|
|
251
252
|
(np.array([[1, 0], [0, 0]]), np.array([[0, 0], [0, 1]])),
|
|
@@ -282,7 +283,7 @@ def test_measurement_channel():
|
|
|
282
283
|
# yapf: enable
|
|
283
284
|
|
|
284
285
|
|
|
285
|
-
def test_measurement_qubit_count_vs_mask_length():
|
|
286
|
+
def test_measurement_qubit_count_vs_mask_length() -> None:
|
|
286
287
|
a = cirq.NamedQubit('a')
|
|
287
288
|
b = cirq.NamedQubit('b')
|
|
288
289
|
c = cirq.NamedQubit('c')
|
|
@@ -296,7 +297,7 @@ def test_measurement_qubit_count_vs_mask_length():
|
|
|
296
297
|
_ = cirq.MeasurementGate(num_qubits=3, key='a', invert_mask=(True, False, True)).on(a, b)
|
|
297
298
|
|
|
298
299
|
|
|
299
|
-
def test_consistent_protocols():
|
|
300
|
+
def test_consistent_protocols() -> None:
|
|
300
301
|
for n in range(1, 5):
|
|
301
302
|
gate = cirq.MeasurementGate(num_qubits=n, key='a')
|
|
302
303
|
cirq.testing.assert_implements_consistent_protocols(gate)
|
|
@@ -305,7 +306,7 @@ def test_consistent_protocols():
|
|
|
305
306
|
cirq.testing.assert_implements_consistent_protocols(gate)
|
|
306
307
|
|
|
307
308
|
|
|
308
|
-
def test_op_repr():
|
|
309
|
+
def test_op_repr() -> None:
|
|
309
310
|
a, b = cirq.LineQubit.range(2)
|
|
310
311
|
assert repr(cirq.measure(a)) == 'cirq.measure(cirq.LineQubit(0))'
|
|
311
312
|
assert repr(cirq.measure(a, b)) == ('cirq.measure(cirq.LineQubit(0), cirq.LineQubit(1))')
|
|
@@ -330,7 +331,7 @@ def test_op_repr():
|
|
|
330
331
|
)
|
|
331
332
|
|
|
332
333
|
|
|
333
|
-
def test_repr():
|
|
334
|
+
def test_repr() -> None:
|
|
334
335
|
gate = cirq.MeasurementGate(
|
|
335
336
|
3,
|
|
336
337
|
'a',
|
|
@@ -345,7 +346,7 @@ def test_repr():
|
|
|
345
346
|
)
|
|
346
347
|
|
|
347
348
|
|
|
348
|
-
def test_act_on_state_vector():
|
|
349
|
+
def test_act_on_state_vector() -> None:
|
|
349
350
|
a, b = [cirq.LineQubit(3), cirq.LineQubit(1)]
|
|
350
351
|
m = cirq.measure(
|
|
351
352
|
a, b, key='out', invert_mask=(True,), confusion_map={(1,): np.array([[0, 1], [1, 0]])}
|
|
@@ -392,7 +393,7 @@ def test_act_on_state_vector():
|
|
|
392
393
|
assert datastore.records[out] == [(0, 0), (0, 0)]
|
|
393
394
|
|
|
394
395
|
|
|
395
|
-
def test_act_on_clifford_tableau():
|
|
396
|
+
def test_act_on_clifford_tableau() -> None:
|
|
396
397
|
a, b = [cirq.LineQubit(3), cirq.LineQubit(1)]
|
|
397
398
|
m = cirq.measure(
|
|
398
399
|
a, b, key='out', invert_mask=(True,), confusion_map={(1,): np.array([[0, 1], [1, 0]])}
|
|
@@ -432,7 +433,7 @@ def test_act_on_clifford_tableau():
|
|
|
432
433
|
assert datastore.records[out] == [(0, 0), (0, 0)]
|
|
433
434
|
|
|
434
435
|
|
|
435
|
-
def test_act_on_stabilizer_ch_form():
|
|
436
|
+
def test_act_on_stabilizer_ch_form() -> None:
|
|
436
437
|
a, b = [cirq.LineQubit(3), cirq.LineQubit(1)]
|
|
437
438
|
m = cirq.measure(
|
|
438
439
|
a, b, key='out', invert_mask=(True,), confusion_map={(1,): np.array([[0, 1], [1, 0]])}
|
|
@@ -466,7 +467,7 @@ def test_act_on_stabilizer_ch_form():
|
|
|
466
467
|
assert datastore.records[out] == [(0, 0), (0, 0)]
|
|
467
468
|
|
|
468
469
|
|
|
469
|
-
def test_act_on_qutrit():
|
|
470
|
+
def test_act_on_qutrit() -> None:
|
|
470
471
|
a, b = [cirq.LineQid(3, dimension=3), cirq.LineQid(1, dimension=3)]
|
|
471
472
|
m = cirq.measure(
|
|
472
473
|
a,
|
cirq/ops/pauli_gates.py
CHANGED
|
@@ -56,7 +56,7 @@ class Pauli(raw_types.Gate, metaclass=abc.ABCMeta):
|
|
|
56
56
|
self._index = index
|
|
57
57
|
self._name = name
|
|
58
58
|
|
|
59
|
-
def num_qubits(self):
|
|
59
|
+
def num_qubits(self) -> int:
|
|
60
60
|
return 1
|
|
61
61
|
|
|
62
62
|
def _commutes_(self, other: Any, *, atol: float = 1e-8) -> bool | NotImplementedType | None:
|
|
@@ -23,7 +23,7 @@ import cirq
|
|
|
23
23
|
'key',
|
|
24
24
|
['q0_1_0', cirq.MeasurementKey(name='q0_1_0'), cirq.MeasurementKey(path=('a', 'b'), name='c')],
|
|
25
25
|
)
|
|
26
|
-
def test_eval_repr(key):
|
|
26
|
+
def test_eval_repr(key) -> None:
|
|
27
27
|
# Basic safeguard against repr-inequality.
|
|
28
28
|
op = cirq.GateOperation(
|
|
29
29
|
gate=cirq.PauliMeasurementGate([cirq.X, cirq.Y], key),
|
|
@@ -36,7 +36,7 @@ def test_eval_repr(key):
|
|
|
36
36
|
|
|
37
37
|
@pytest.mark.parametrize('observable', [[cirq.X], [cirq.Y, cirq.Z], cirq.DensePauliString('XYZ')])
|
|
38
38
|
@pytest.mark.parametrize('key', ['a', cirq.MeasurementKey('a')])
|
|
39
|
-
def test_init(observable, key):
|
|
39
|
+
def test_init(observable, key) -> None:
|
|
40
40
|
g = cirq.PauliMeasurementGate(observable, key)
|
|
41
41
|
assert g.num_qubits() == len(observable)
|
|
42
42
|
assert g.key == 'a'
|
|
@@ -45,12 +45,12 @@ def test_init(observable, key):
|
|
|
45
45
|
assert cirq.qid_shape(g) == (2,) * len(observable)
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
def test_measurement_has_unitary_returns_false():
|
|
48
|
+
def test_measurement_has_unitary_returns_false() -> None:
|
|
49
49
|
gate = cirq.PauliMeasurementGate([cirq.X], 'a')
|
|
50
50
|
assert not cirq.has_unitary(gate)
|
|
51
51
|
|
|
52
52
|
|
|
53
|
-
def test_measurement_eq():
|
|
53
|
+
def test_measurement_eq() -> None:
|
|
54
54
|
eq = cirq.testing.EqualsTester()
|
|
55
55
|
eq.make_equality_group(
|
|
56
56
|
lambda: cirq.PauliMeasurementGate([cirq.X, cirq.Y], 'a'),
|
|
@@ -76,7 +76,7 @@ def test_measurement_eq():
|
|
|
76
76
|
cirq.PauliMeasurementGate([cirq.X, cirq.Y, cirq.Z], 'a'),
|
|
77
77
|
],
|
|
78
78
|
)
|
|
79
|
-
def test_measurement_with_key(protocol, args, key, gate):
|
|
79
|
+
def test_measurement_with_key(protocol, args, key, gate) -> None:
|
|
80
80
|
if protocol:
|
|
81
81
|
gate_with_key = protocol(gate, args)
|
|
82
82
|
else:
|
|
@@ -92,7 +92,7 @@ def test_measurement_with_key(protocol, args, key, gate):
|
|
|
92
92
|
assert same_gate == gate
|
|
93
93
|
|
|
94
94
|
|
|
95
|
-
def test_measurement_gate_diagram():
|
|
95
|
+
def test_measurement_gate_diagram() -> None:
|
|
96
96
|
# Shows observable & key.
|
|
97
97
|
assert cirq.circuit_diagram_info(
|
|
98
98
|
cirq.PauliMeasurementGate([cirq.X], key='test')
|
|
@@ -129,14 +129,14 @@ b: ───M(Y)───────────
|
|
|
129
129
|
'key',
|
|
130
130
|
['q0_1_0', cirq.MeasurementKey(name='q0_1_0'), cirq.MeasurementKey(path=('a', 'b'), name='c')],
|
|
131
131
|
)
|
|
132
|
-
def test_consistent_protocols(observable, key):
|
|
132
|
+
def test_consistent_protocols(observable, key) -> None:
|
|
133
133
|
gate = cirq.PauliMeasurementGate(observable, key=key)
|
|
134
134
|
cirq.testing.assert_implements_consistent_protocols(gate)
|
|
135
135
|
assert cirq.is_measurement(gate)
|
|
136
136
|
assert cirq.measurement_key_name(gate) == str(key)
|
|
137
137
|
|
|
138
138
|
|
|
139
|
-
def test_op_repr():
|
|
139
|
+
def test_op_repr() -> None:
|
|
140
140
|
a, b, c = cirq.LineQubit.range(3)
|
|
141
141
|
ps = cirq.X(a) * cirq.Y(b) * cirq.Z(c)
|
|
142
142
|
assert (
|
|
@@ -152,12 +152,12 @@ def test_op_repr():
|
|
|
152
152
|
)
|
|
153
153
|
|
|
154
154
|
|
|
155
|
-
def test_bad_observable_raises():
|
|
155
|
+
def test_bad_observable_raises() -> None:
|
|
156
156
|
with pytest.raises(ValueError, match='Pauli observable .* is empty'):
|
|
157
157
|
_ = cirq.PauliMeasurementGate([])
|
|
158
158
|
|
|
159
159
|
with pytest.raises(ValueError, match=r'Pauli observable .* must be Iterable\[`cirq.Pauli`\]'):
|
|
160
|
-
_ = cirq.PauliMeasurementGate([cirq.I, cirq.X, cirq.Y])
|
|
160
|
+
_ = cirq.PauliMeasurementGate([cirq.I, cirq.X, cirq.Y]) # type: ignore[list-item]
|
|
161
161
|
|
|
162
162
|
with pytest.raises(ValueError, match=r'Pauli observable .* must be Iterable\[`cirq.Pauli`\]'):
|
|
163
163
|
_ = cirq.PauliMeasurementGate(cirq.DensePauliString('XYZI'))
|
|
@@ -166,7 +166,7 @@ def test_bad_observable_raises():
|
|
|
166
166
|
_ = cirq.PauliMeasurementGate(cirq.DensePauliString('XYZ', coefficient=1j))
|
|
167
167
|
|
|
168
168
|
|
|
169
|
-
def test_with_observable():
|
|
169
|
+
def test_with_observable() -> None:
|
|
170
170
|
o1 = [cirq.Z, cirq.Y, cirq.X]
|
|
171
171
|
o2 = [cirq.X, cirq.Y, cirq.Z]
|
|
172
172
|
g1 = cirq.PauliMeasurementGate(o1, key='a')
|
|
@@ -186,7 +186,7 @@ def test_with_observable():
|
|
|
186
186
|
(cirq.X**-0.5, cirq.DensePauliString("Y", coefficient=-1), 1),
|
|
187
187
|
],
|
|
188
188
|
)
|
|
189
|
-
def test_pauli_measurement_gate_samples(rot, obs, out):
|
|
189
|
+
def test_pauli_measurement_gate_samples(rot, obs, out) -> None:
|
|
190
190
|
q = cirq.NamedQubit("q")
|
|
191
191
|
c = cirq.Circuit(rot(q), cirq.PauliMeasurementGate(obs, key='out').on(q))
|
|
192
192
|
assert cirq.Simulator().sample(c)['out'][0] == out
|