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
|
@@ -21,7 +21,7 @@ import sympy
|
|
|
21
21
|
import cirq
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
def test_phase_gradient():
|
|
24
|
+
def test_phase_gradient() -> None:
|
|
25
25
|
np.testing.assert_allclose(
|
|
26
26
|
cirq.unitary(cirq.PhaseGradientGate(num_qubits=2, exponent=1)), np.diag([1, 1j, -1, -1j])
|
|
27
27
|
)
|
|
@@ -33,7 +33,7 @@ def test_phase_gradient():
|
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
36
|
-
def test_phase_gradient_symbolic(resolve_fn):
|
|
36
|
+
def test_phase_gradient_symbolic(resolve_fn) -> None:
|
|
37
37
|
a = cirq.PhaseGradientGate(num_qubits=2, exponent=0.5)
|
|
38
38
|
b = cirq.PhaseGradientGate(num_qubits=2, exponent=sympy.Symbol('t'))
|
|
39
39
|
assert not cirq.is_parameterized(a)
|
|
@@ -45,22 +45,22 @@ def test_phase_gradient_symbolic(resolve_fn):
|
|
|
45
45
|
assert resolve_fn(b, {'t': 0.25}) == cirq.PhaseGradientGate(num_qubits=2, exponent=0.25)
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
def test_str():
|
|
48
|
+
def test_str() -> None:
|
|
49
49
|
assert str(cirq.PhaseGradientGate(num_qubits=2, exponent=0.5)) == 'Grad[2]^0.5'
|
|
50
50
|
assert str(cirq.PhaseGradientGate(num_qubits=2, exponent=1)) == 'Grad[2]'
|
|
51
51
|
|
|
52
52
|
|
|
53
|
-
def test_phase_gradient_gate_repr():
|
|
53
|
+
def test_phase_gradient_gate_repr() -> None:
|
|
54
54
|
a = cirq.PhaseGradientGate(num_qubits=2, exponent=0.5)
|
|
55
55
|
cirq.testing.assert_equivalent_repr(a)
|
|
56
56
|
|
|
57
57
|
|
|
58
|
-
def test_quantum_fourier_transform_gate_repr():
|
|
58
|
+
def test_quantum_fourier_transform_gate_repr() -> None:
|
|
59
59
|
b = cirq.QuantumFourierTransformGate(num_qubits=2, without_reverse=False)
|
|
60
60
|
cirq.testing.assert_equivalent_repr(b)
|
|
61
61
|
|
|
62
62
|
|
|
63
|
-
def test_pow():
|
|
63
|
+
def test_pow() -> None:
|
|
64
64
|
a = cirq.PhaseGradientGate(num_qubits=2, exponent=0.5)
|
|
65
65
|
assert a**0.5 == cirq.PhaseGradientGate(num_qubits=2, exponent=0.25)
|
|
66
66
|
assert a ** sympy.Symbol('t') == cirq.PhaseGradientGate(
|
|
@@ -68,7 +68,7 @@ def test_pow():
|
|
|
68
68
|
)
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
def test_qft():
|
|
71
|
+
def test_qft() -> None:
|
|
72
72
|
# fmt: off
|
|
73
73
|
np.testing.assert_allclose(
|
|
74
74
|
cirq.unitary(cirq.qft(*cirq.LineQubit.range(2))),
|
|
@@ -105,9 +105,10 @@ def test_qft():
|
|
|
105
105
|
atol=1e-8,
|
|
106
106
|
)
|
|
107
107
|
|
|
108
|
+
arr = np.array([[1, 1, 1, 1], [1, -1j, -1, 1j], [1, -1, 1, -1], [1, 1j, -1, -1j]]) / 2
|
|
108
109
|
np.testing.assert_allclose(
|
|
109
|
-
cirq.unitary(cirq.qft(*cirq.LineQubit.range(2)) ** -1),
|
|
110
|
-
|
|
110
|
+
cirq.unitary(cirq.qft(*cirq.LineQubit.range(2)) ** -1), # type: ignore[operator]
|
|
111
|
+
arr, # type: ignore[arg-type]
|
|
111
112
|
atol=1e-8,
|
|
112
113
|
)
|
|
113
114
|
|
|
@@ -118,15 +119,15 @@ def test_qft():
|
|
|
118
119
|
)
|
|
119
120
|
|
|
120
121
|
|
|
121
|
-
def test_inverse():
|
|
122
|
+
def test_inverse() -> None:
|
|
122
123
|
a, b, c = cirq.LineQubit.range(3)
|
|
123
|
-
assert cirq.qft(a, b, c, inverse=True) == cirq.qft(a, b, c) ** -1
|
|
124
|
+
assert cirq.qft(a, b, c, inverse=True) == cirq.qft(a, b, c) ** -1 # type: ignore[operator]
|
|
124
125
|
assert cirq.qft(a, b, c, inverse=True, without_reverse=True) == cirq.inverse(
|
|
125
126
|
cirq.qft(a, b, c, without_reverse=True)
|
|
126
127
|
)
|
|
127
128
|
|
|
128
129
|
|
|
129
|
-
def test_circuit_diagram():
|
|
130
|
+
def test_circuit_diagram() -> None:
|
|
130
131
|
cirq.testing.assert_has_diagram(
|
|
131
132
|
cirq.Circuit(cirq.decompose_once(cirq.qft(*cirq.LineQubit.range(4)))),
|
|
132
133
|
"""
|
cirq/ops/gate_operation_test.py
CHANGED
|
@@ -16,6 +16,7 @@ from __future__ import annotations
|
|
|
16
16
|
|
|
17
17
|
import collections.abc
|
|
18
18
|
import pathlib
|
|
19
|
+
from typing import cast
|
|
19
20
|
|
|
20
21
|
import numpy as np
|
|
21
22
|
import pytest
|
|
@@ -25,7 +26,7 @@ import cirq
|
|
|
25
26
|
import cirq.testing
|
|
26
27
|
|
|
27
28
|
|
|
28
|
-
def test_gate_operation_init():
|
|
29
|
+
def test_gate_operation_init() -> None:
|
|
29
30
|
q = cirq.NamedQubit('q')
|
|
30
31
|
g = cirq.testing.SingleQubitGate()
|
|
31
32
|
v = cirq.GateOperation(g, (q,))
|
|
@@ -33,14 +34,14 @@ def test_gate_operation_init():
|
|
|
33
34
|
assert v.qubits == (q,)
|
|
34
35
|
|
|
35
36
|
|
|
36
|
-
def test_invalid_gate_operation():
|
|
37
|
+
def test_invalid_gate_operation() -> None:
|
|
37
38
|
three_qubit_gate = cirq.testing.ThreeQubitGate()
|
|
38
39
|
single_qubit = [cirq.GridQubit(0, 0)]
|
|
39
40
|
with pytest.raises(ValueError, match="number of qubits"):
|
|
40
41
|
cirq.GateOperation(three_qubit_gate, single_qubit)
|
|
41
42
|
|
|
42
43
|
|
|
43
|
-
def test_immutable():
|
|
44
|
+
def test_immutable() -> None:
|
|
44
45
|
a, b = cirq.LineQubit.range(2)
|
|
45
46
|
op = cirq.X(a)
|
|
46
47
|
|
|
@@ -60,7 +61,7 @@ def test_immutable():
|
|
|
60
61
|
op.qubits = [b]
|
|
61
62
|
|
|
62
63
|
|
|
63
|
-
def test_gate_operation_eq():
|
|
64
|
+
def test_gate_operation_eq() -> None:
|
|
64
65
|
g1 = cirq.testing.SingleQubitGate()
|
|
65
66
|
g2 = cirq.testing.SingleQubitGate()
|
|
66
67
|
g3 = cirq.testing.TwoQubitGate()
|
|
@@ -105,7 +106,7 @@ def test_gate_operation_eq():
|
|
|
105
106
|
eq.add_equality_group(p(b0, a1, a0, b1, c0))
|
|
106
107
|
|
|
107
108
|
|
|
108
|
-
def test_gate_operation_approx_eq():
|
|
109
|
+
def test_gate_operation_approx_eq() -> None:
|
|
109
110
|
a = [cirq.NamedQubit('r1')]
|
|
110
111
|
b = [cirq.NamedQubit('r2')]
|
|
111
112
|
|
|
@@ -131,7 +132,7 @@ def test_gate_operation_approx_eq():
|
|
|
131
132
|
)
|
|
132
133
|
|
|
133
134
|
|
|
134
|
-
def test_gate_operation_qid_shape():
|
|
135
|
+
def test_gate_operation_qid_shape() -> None:
|
|
135
136
|
class ShapeGate(cirq.Gate):
|
|
136
137
|
def _qid_shape_(self):
|
|
137
138
|
return (1, 2, 3, 4)
|
|
@@ -141,7 +142,7 @@ def test_gate_operation_qid_shape():
|
|
|
141
142
|
assert cirq.num_qubits(op) == 4
|
|
142
143
|
|
|
143
144
|
|
|
144
|
-
def test_gate_operation_num_qubits():
|
|
145
|
+
def test_gate_operation_num_qubits() -> None:
|
|
145
146
|
class NumQubitsGate(cirq.Gate):
|
|
146
147
|
def _num_qubits_(self):
|
|
147
148
|
return 4
|
|
@@ -151,25 +152,25 @@ def test_gate_operation_num_qubits():
|
|
|
151
152
|
assert cirq.num_qubits(op) == 4
|
|
152
153
|
|
|
153
154
|
|
|
154
|
-
def test_gate_operation_pow():
|
|
155
|
+
def test_gate_operation_pow() -> None:
|
|
155
156
|
Y = cirq.Y
|
|
156
157
|
q = cirq.NamedQubit('q')
|
|
157
158
|
assert (Y**0.5)(q) == Y(q) ** 0.5
|
|
158
159
|
|
|
159
160
|
|
|
160
|
-
def test_with_qubits_and_transform_qubits():
|
|
161
|
+
def test_with_qubits_and_transform_qubits() -> None:
|
|
161
162
|
g = cirq.testing.ThreeQubitGate()
|
|
162
163
|
g = cirq.testing.ThreeQubitGate()
|
|
163
164
|
op = cirq.GateOperation(g, cirq.LineQubit.range(3))
|
|
164
165
|
assert op.with_qubits(*cirq.LineQubit.range(3, 0, -1)) == cirq.GateOperation(
|
|
165
166
|
g, cirq.LineQubit.range(3, 0, -1)
|
|
166
167
|
)
|
|
167
|
-
assert op.transform_qubits(
|
|
168
|
-
|
|
169
|
-
)
|
|
168
|
+
assert op.transform_qubits(
|
|
169
|
+
lambda e: cirq.LineQubit(-cast(cirq.LineQubit, e).x)
|
|
170
|
+
) == cirq.GateOperation(g, [cirq.LineQubit(0), cirq.LineQubit(-1), cirq.LineQubit(-2)])
|
|
170
171
|
|
|
171
172
|
|
|
172
|
-
def test_extrapolate():
|
|
173
|
+
def test_extrapolate() -> None:
|
|
173
174
|
q = cirq.NamedQubit('q')
|
|
174
175
|
|
|
175
176
|
# If the gate isn't extrapolatable, you get a type error.
|
|
@@ -182,7 +183,7 @@ def test_extrapolate():
|
|
|
182
183
|
assert (cirq.Y**0.5).on(q) == cirq.Y(q) ** 0.5
|
|
183
184
|
|
|
184
185
|
|
|
185
|
-
def test_inverse():
|
|
186
|
+
def test_inverse() -> None:
|
|
186
187
|
q = cirq.NamedQubit('q')
|
|
187
188
|
|
|
188
189
|
# If the gate isn't reversible, you get a type error.
|
|
@@ -194,7 +195,7 @@ def test_inverse():
|
|
|
194
195
|
assert cirq.inverse(cirq.S).on(q) == cirq.inverse(cirq.S.on(q))
|
|
195
196
|
|
|
196
197
|
|
|
197
|
-
def test_text_diagrammable():
|
|
198
|
+
def test_text_diagrammable() -> None:
|
|
198
199
|
q = cirq.NamedQubit('q')
|
|
199
200
|
|
|
200
201
|
# If the gate isn't diagrammable, you get a type error.
|
|
@@ -208,7 +209,7 @@ def test_text_diagrammable():
|
|
|
208
209
|
assert actual == expected
|
|
209
210
|
|
|
210
211
|
|
|
211
|
-
def test_bounded_effect():
|
|
212
|
+
def test_bounded_effect() -> None:
|
|
212
213
|
q = cirq.NamedQubit('q')
|
|
213
214
|
|
|
214
215
|
# If the gate isn't bounded, you get a type error.
|
|
@@ -220,7 +221,7 @@ def test_bounded_effect():
|
|
|
220
221
|
|
|
221
222
|
|
|
222
223
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
223
|
-
def test_parameterizable_effect(resolve_fn):
|
|
224
|
+
def test_parameterizable_effect(resolve_fn) -> None:
|
|
224
225
|
q = cirq.NamedQubit('q')
|
|
225
226
|
r = cirq.ParamResolver({'a': 0.5})
|
|
226
227
|
|
|
@@ -231,7 +232,7 @@ def test_parameterizable_effect(resolve_fn):
|
|
|
231
232
|
assert op2 == cirq.S.on(q)
|
|
232
233
|
|
|
233
234
|
|
|
234
|
-
def test_pauli_expansion():
|
|
235
|
+
def test_pauli_expansion() -> None:
|
|
235
236
|
a = cirq.NamedQubit('a')
|
|
236
237
|
b = cirq.NamedQubit('b')
|
|
237
238
|
|
|
@@ -253,7 +254,7 @@ def test_pauli_expansion():
|
|
|
253
254
|
assert cirq.pauli_expansion(Yes().on(a)) == cirq.LinearDict({'X': 0.5})
|
|
254
255
|
|
|
255
256
|
|
|
256
|
-
def test_unitary():
|
|
257
|
+
def test_unitary() -> None:
|
|
257
258
|
a = cirq.NamedQubit('a')
|
|
258
259
|
b = cirq.NamedQubit('b')
|
|
259
260
|
|
|
@@ -263,7 +264,7 @@ def test_unitary():
|
|
|
263
264
|
np.testing.assert_allclose(cirq.unitary(cirq.CNOT(a, b)), cirq.unitary(cirq.CNOT), atol=1e-8)
|
|
264
265
|
|
|
265
266
|
|
|
266
|
-
def test_channel():
|
|
267
|
+
def test_channel() -> None:
|
|
267
268
|
a = cirq.NamedQubit('a')
|
|
268
269
|
op = cirq.bit_flip(0.5).on(a)
|
|
269
270
|
np.testing.assert_allclose(cirq.kraus(op), cirq.kraus(op.gate))
|
|
@@ -273,19 +274,19 @@ def test_channel():
|
|
|
273
274
|
assert not cirq.has_kraus(cirq.testing.SingleQubitGate()(a))
|
|
274
275
|
|
|
275
276
|
|
|
276
|
-
def test_measurement_key():
|
|
277
|
+
def test_measurement_key() -> None:
|
|
277
278
|
a = cirq.NamedQubit('a')
|
|
278
279
|
assert cirq.measurement_key_name(cirq.measure(a, key='lock')) == 'lock'
|
|
279
280
|
|
|
280
281
|
|
|
281
|
-
def assert_mixtures_equal(actual, expected):
|
|
282
|
+
def assert_mixtures_equal(actual, expected) -> None:
|
|
282
283
|
"""Assert equal for tuple of mixed scalar and array types."""
|
|
283
284
|
for a, e in zip(actual, expected):
|
|
284
285
|
np.testing.assert_almost_equal(a[0], e[0])
|
|
285
286
|
np.testing.assert_almost_equal(a[1], e[1])
|
|
286
287
|
|
|
287
288
|
|
|
288
|
-
def test_mixture():
|
|
289
|
+
def test_mixture() -> None:
|
|
289
290
|
a = cirq.NamedQubit('a')
|
|
290
291
|
op = cirq.bit_flip(0.5).on(a)
|
|
291
292
|
assert_mixtures_equal(cirq.mixture(op), cirq.mixture(op.gate))
|
|
@@ -298,7 +299,7 @@ def test_mixture():
|
|
|
298
299
|
np.testing.assert_allclose(m[0][1], cirq.unitary(cirq.X))
|
|
299
300
|
|
|
300
301
|
|
|
301
|
-
def test_repr():
|
|
302
|
+
def test_repr() -> None:
|
|
302
303
|
a, b = cirq.LineQubit.range(2)
|
|
303
304
|
assert (
|
|
304
305
|
repr(cirq.GateOperation(cirq.CZ, (a, b))) == 'cirq.CZ(cirq.LineQubit(0), cirq.LineQubit(1))'
|
|
@@ -327,7 +328,7 @@ def test_repr():
|
|
|
327
328
|
(cirq.Z ** sympy.Symbol('e'), cirq.Z ** sympy.Symbol('f'), False),
|
|
328
329
|
],
|
|
329
330
|
)
|
|
330
|
-
def test_equal_up_to_global_phase_on_gates(gate1, gate2, eq_up_to_global_phase):
|
|
331
|
+
def test_equal_up_to_global_phase_on_gates(gate1, gate2, eq_up_to_global_phase) -> None:
|
|
331
332
|
num_qubits1, num_qubits2 = (cirq.num_qubits(g) for g in (gate1, gate2))
|
|
332
333
|
qubits = cirq.LineQubit.range(max(num_qubits1, num_qubits2) + 1)
|
|
333
334
|
op1, op2 = gate1(*qubits[:num_qubits1]), gate2(*qubits[:num_qubits2])
|
|
@@ -336,12 +337,12 @@ def test_equal_up_to_global_phase_on_gates(gate1, gate2, eq_up_to_global_phase):
|
|
|
336
337
|
assert not cirq.equal_up_to_global_phase(op1, op2_on_diff_qubits)
|
|
337
338
|
|
|
338
339
|
|
|
339
|
-
def test_equal_up_to_global_phase_on_diff_types():
|
|
340
|
+
def test_equal_up_to_global_phase_on_diff_types() -> None:
|
|
340
341
|
op = cirq.X(cirq.LineQubit(0))
|
|
341
342
|
assert not cirq.equal_up_to_global_phase(op, 3)
|
|
342
343
|
|
|
343
344
|
|
|
344
|
-
def test_gate_on_operation_besides_gate_operation():
|
|
345
|
+
def test_gate_on_operation_besides_gate_operation() -> None:
|
|
345
346
|
a, b = cirq.LineQubit.range(2)
|
|
346
347
|
|
|
347
348
|
op = -1j * cirq.X(a) * cirq.Y(b)
|
|
@@ -350,7 +351,7 @@ def test_gate_on_operation_besides_gate_operation():
|
|
|
350
351
|
assert not isinstance(op.gate, cirq.XPowGate)
|
|
351
352
|
|
|
352
353
|
|
|
353
|
-
def test_mul():
|
|
354
|
+
def test_mul() -> None:
|
|
354
355
|
class GateRMul(cirq.Gate):
|
|
355
356
|
def num_qubits(self) -> int:
|
|
356
357
|
return 1
|
|
@@ -376,29 +377,29 @@ def test_mul():
|
|
|
376
377
|
# Delegates right multiplication.
|
|
377
378
|
q = cirq.LineQubit(0)
|
|
378
379
|
r = GateRMul().on(q)
|
|
379
|
-
assert 2 * r == 3
|
|
380
|
+
assert 2 * r == 3 # type: ignore[operator]
|
|
380
381
|
with pytest.raises(TypeError):
|
|
381
|
-
_ = r * 2
|
|
382
|
+
_ = r * 2 # type: ignore[operator]
|
|
382
383
|
|
|
383
384
|
# Delegates left multiplication.
|
|
384
385
|
m = GateMul().on(q)
|
|
385
|
-
assert m * 2 == 5
|
|
386
|
+
assert m * 2 == 5 # type: ignore[operator]
|
|
386
387
|
with pytest.raises(TypeError):
|
|
387
|
-
_ = 2 * m
|
|
388
|
+
_ = 2 * m # type: ignore[operator]
|
|
388
389
|
|
|
389
390
|
# Handles the symmetric type case correctly.
|
|
390
|
-
assert m * m == 6
|
|
391
|
-
assert r * r == 4
|
|
391
|
+
assert m * m == 6 # type: ignore[operator]
|
|
392
|
+
assert r * r == 4 # type: ignore[operator]
|
|
392
393
|
|
|
393
394
|
|
|
394
|
-
def test_with_gate():
|
|
395
|
+
def test_with_gate() -> None:
|
|
395
396
|
g1 = cirq.GateOperation(cirq.X, cirq.LineQubit.range(1))
|
|
396
397
|
g2 = cirq.GateOperation(cirq.Y, cirq.LineQubit.range(1))
|
|
397
398
|
assert g1.with_gate(cirq.X) is g1
|
|
398
399
|
assert g1.with_gate(cirq.Y) == g2
|
|
399
400
|
|
|
400
401
|
|
|
401
|
-
def test_with_measurement_key_mapping():
|
|
402
|
+
def test_with_measurement_key_mapping() -> None:
|
|
402
403
|
a = cirq.LineQubit(0)
|
|
403
404
|
op = cirq.measure(a, key='m')
|
|
404
405
|
|
|
@@ -407,7 +408,7 @@ def test_with_measurement_key_mapping():
|
|
|
407
408
|
assert cirq.with_measurement_key_mapping(op, {'x': 'k'}) is op
|
|
408
409
|
|
|
409
410
|
|
|
410
|
-
def test_with_key_path():
|
|
411
|
+
def test_with_key_path() -> None:
|
|
411
412
|
a = cirq.LineQubit(0)
|
|
412
413
|
op = cirq.measure(a, key='m')
|
|
413
414
|
|
|
@@ -420,7 +421,7 @@ def test_with_key_path():
|
|
|
420
421
|
assert cirq.with_key_path(cirq.X(a), ('a', 'b')) is NotImplemented
|
|
421
422
|
|
|
422
423
|
|
|
423
|
-
def test_with_key_path_prefix():
|
|
424
|
+
def test_with_key_path_prefix() -> None:
|
|
424
425
|
a = cirq.LineQubit(0)
|
|
425
426
|
op = cirq.measure(a, key='m')
|
|
426
427
|
remap_op = cirq.with_key_path_prefix(op, ('a', 'b'))
|
|
@@ -430,14 +431,14 @@ def test_with_key_path_prefix():
|
|
|
430
431
|
assert cirq.with_key_path_prefix(cirq.X(a), ('a', 'b')) is NotImplemented
|
|
431
432
|
|
|
432
433
|
|
|
433
|
-
def test_cannot_remap_non_measurement_gate():
|
|
434
|
+
def test_cannot_remap_non_measurement_gate() -> None:
|
|
434
435
|
a = cirq.LineQubit(0)
|
|
435
436
|
op = cirq.X(a)
|
|
436
437
|
|
|
437
438
|
assert cirq.with_measurement_key_mapping(op, {'m': 'k'}) is NotImplemented
|
|
438
439
|
|
|
439
440
|
|
|
440
|
-
def test_is_parameterized():
|
|
441
|
+
def test_is_parameterized() -> None:
|
|
441
442
|
class No1(cirq.testing.SingleQubitGate):
|
|
442
443
|
def num_qubits(self) -> int:
|
|
443
444
|
return 1
|
|
@@ -463,7 +464,7 @@ def test_is_parameterized():
|
|
|
463
464
|
assert cirq.is_parameterized(Yes().on(q))
|
|
464
465
|
|
|
465
466
|
|
|
466
|
-
def test_group_interchangeable_qubits_creates_tuples_with_unique_keys():
|
|
467
|
+
def test_group_interchangeable_qubits_creates_tuples_with_unique_keys() -> None:
|
|
467
468
|
class MyGate(cirq.Gate, cirq.InterchangeableQubitsGate):
|
|
468
469
|
def __init__(self, num_qubits) -> None:
|
|
469
470
|
self._num_qubits = num_qubits
|
|
@@ -484,7 +485,7 @@ def test_group_interchangeable_qubits_creates_tuples_with_unique_keys():
|
|
|
484
485
|
)
|
|
485
486
|
|
|
486
487
|
|
|
487
|
-
def test_gate_to_operation_to_gate_round_trips():
|
|
488
|
+
def test_gate_to_operation_to_gate_round_trips() -> None:
|
|
488
489
|
def all_subclasses(cls):
|
|
489
490
|
return set(cls.__subclasses__()).union(
|
|
490
491
|
[s for c in cls.__subclasses__() for s in all_subclasses(c)]
|
cirq/ops/global_phase_op_test.py
CHANGED
|
@@ -21,9 +21,11 @@ import sympy
|
|
|
21
21
|
import cirq
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
def test_init():
|
|
24
|
+
def test_init() -> None:
|
|
25
25
|
op = cirq.global_phase_operation(1j)
|
|
26
|
-
|
|
26
|
+
gate = op.gate
|
|
27
|
+
assert isinstance(gate, cirq.GlobalPhaseGate)
|
|
28
|
+
assert gate.coefficient == 1j
|
|
27
29
|
assert op.qubits == ()
|
|
28
30
|
assert op.with_qubits() == op
|
|
29
31
|
assert cirq.has_stabilizer_effect(op)
|
|
@@ -34,7 +36,7 @@ def test_init():
|
|
|
34
36
|
_ = cirq.global_phase_operation(1j).with_qubits(cirq.LineQubit(0))
|
|
35
37
|
|
|
36
38
|
|
|
37
|
-
def test_protocols():
|
|
39
|
+
def test_protocols() -> None:
|
|
38
40
|
for p in [1, 1j, -1, sympy.Symbol('s')]:
|
|
39
41
|
cirq.testing.assert_implements_consistent_protocols(cirq.global_phase_operation(p))
|
|
40
42
|
|
|
@@ -44,7 +46,7 @@ def test_protocols():
|
|
|
44
46
|
|
|
45
47
|
|
|
46
48
|
@pytest.mark.parametrize('phase', [1, 1j, -1])
|
|
47
|
-
def test_act_on_tableau(phase):
|
|
49
|
+
def test_act_on_tableau(phase) -> None:
|
|
48
50
|
original_tableau = cirq.CliffordTableau(0)
|
|
49
51
|
args = cirq.CliffordTableauSimulationState(original_tableau.copy(), np.random.RandomState())
|
|
50
52
|
cirq.act_on(cirq.global_phase_operation(phase), args, allow_decompose=False)
|
|
@@ -52,7 +54,7 @@ def test_act_on_tableau(phase):
|
|
|
52
54
|
|
|
53
55
|
|
|
54
56
|
@pytest.mark.parametrize('phase', [1, 1j, -1])
|
|
55
|
-
def test_act_on_ch_form(phase):
|
|
57
|
+
def test_act_on_ch_form(phase) -> None:
|
|
56
58
|
state = cirq.StabilizerStateChForm(0)
|
|
57
59
|
args = cirq.StabilizerChFormSimulationState(
|
|
58
60
|
qubits=[], prng=np.random.RandomState(), initial_state=state
|
|
@@ -61,16 +63,16 @@ def test_act_on_ch_form(phase):
|
|
|
61
63
|
assert state.state_vector() == [[phase]]
|
|
62
64
|
|
|
63
65
|
|
|
64
|
-
def test_str():
|
|
66
|
+
def test_str() -> None:
|
|
65
67
|
assert str(cirq.global_phase_operation(1j)) == '1j'
|
|
66
68
|
|
|
67
69
|
|
|
68
|
-
def test_repr():
|
|
70
|
+
def test_repr() -> None:
|
|
69
71
|
op = cirq.global_phase_operation(1j)
|
|
70
72
|
cirq.testing.assert_equivalent_repr(op)
|
|
71
73
|
|
|
72
74
|
|
|
73
|
-
def test_diagram():
|
|
75
|
+
def test_diagram() -> None:
|
|
74
76
|
a, b = cirq.LineQubit.range(2)
|
|
75
77
|
x, y = cirq.LineQubit.range(10, 12)
|
|
76
78
|
|
|
@@ -202,7 +204,7 @@ global phase: -0.5π
|
|
|
202
204
|
)
|
|
203
205
|
|
|
204
206
|
|
|
205
|
-
def test_gate_init():
|
|
207
|
+
def test_gate_init() -> None:
|
|
206
208
|
gate = cirq.GlobalPhaseGate(1j)
|
|
207
209
|
assert gate.coefficient == 1j
|
|
208
210
|
assert isinstance(gate.on(), cirq.GateOperation)
|
|
@@ -215,7 +217,7 @@ def test_gate_init():
|
|
|
215
217
|
_ = gate.on(cirq.LineQubit(0))
|
|
216
218
|
|
|
217
219
|
|
|
218
|
-
def test_gate_protocols():
|
|
220
|
+
def test_gate_protocols() -> None:
|
|
219
221
|
for p in [1, 1j, -1]:
|
|
220
222
|
cirq.testing.assert_implements_consistent_protocols(cirq.GlobalPhaseGate(p))
|
|
221
223
|
|
|
@@ -223,7 +225,7 @@ def test_gate_protocols():
|
|
|
223
225
|
|
|
224
226
|
|
|
225
227
|
@pytest.mark.parametrize('phase', [1, 1j, -1])
|
|
226
|
-
def test_gate_act_on_tableau(phase):
|
|
228
|
+
def test_gate_act_on_tableau(phase) -> None:
|
|
227
229
|
original_tableau = cirq.CliffordTableau(0)
|
|
228
230
|
args = cirq.CliffordTableauSimulationState(original_tableau.copy(), np.random.RandomState())
|
|
229
231
|
cirq.act_on(cirq.GlobalPhaseGate(phase), args, qubits=(), allow_decompose=False)
|
|
@@ -231,7 +233,7 @@ def test_gate_act_on_tableau(phase):
|
|
|
231
233
|
|
|
232
234
|
|
|
233
235
|
@pytest.mark.parametrize('phase', [1, 1j, -1])
|
|
234
|
-
def test_gate_act_on_ch_form(phase):
|
|
236
|
+
def test_gate_act_on_ch_form(phase) -> None:
|
|
235
237
|
state = cirq.StabilizerStateChForm(0)
|
|
236
238
|
args = cirq.StabilizerChFormSimulationState(
|
|
237
239
|
qubits=[], prng=np.random.RandomState(), initial_state=state
|
|
@@ -240,25 +242,25 @@ def test_gate_act_on_ch_form(phase):
|
|
|
240
242
|
assert state.state_vector() == [[phase]]
|
|
241
243
|
|
|
242
244
|
|
|
243
|
-
def test_gate_str():
|
|
245
|
+
def test_gate_str() -> None:
|
|
244
246
|
assert str(cirq.GlobalPhaseGate(1j)) == '1j'
|
|
245
247
|
|
|
246
248
|
|
|
247
|
-
def test_gate_repr():
|
|
249
|
+
def test_gate_repr() -> None:
|
|
248
250
|
gate = cirq.GlobalPhaseGate(1j)
|
|
249
251
|
cirq.testing.assert_equivalent_repr(gate)
|
|
250
252
|
|
|
251
253
|
|
|
252
|
-
def test_gate_op_repr():
|
|
254
|
+
def test_gate_op_repr() -> None:
|
|
253
255
|
gate = cirq.GlobalPhaseGate(1j)
|
|
254
256
|
cirq.testing.assert_equivalent_repr(gate.on())
|
|
255
257
|
|
|
256
258
|
|
|
257
|
-
def test_gate_global_phase_op_json_dict():
|
|
259
|
+
def test_gate_global_phase_op_json_dict() -> None:
|
|
258
260
|
assert cirq.GlobalPhaseGate(-1j)._json_dict_() == {'coefficient': -1j}
|
|
259
261
|
|
|
260
262
|
|
|
261
|
-
def test_parameterization():
|
|
263
|
+
def test_parameterization() -> None:
|
|
262
264
|
t = sympy.Symbol('t')
|
|
263
265
|
gpt = cirq.GlobalPhaseGate(coefficient=t)
|
|
264
266
|
assert cirq.is_parameterized(gpt)
|
|
@@ -269,14 +271,14 @@ def test_parameterization():
|
|
|
269
271
|
|
|
270
272
|
|
|
271
273
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
272
|
-
def test_resolve(resolve_fn):
|
|
274
|
+
def test_resolve(resolve_fn) -> None:
|
|
273
275
|
t = sympy.Symbol('t')
|
|
274
276
|
gpt = cirq.GlobalPhaseGate(coefficient=t)
|
|
275
277
|
assert resolve_fn(gpt, {'t': -1}) == cirq.GlobalPhaseGate(coefficient=-1)
|
|
276
278
|
|
|
277
279
|
|
|
278
280
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
279
|
-
def test_resolve_error(resolve_fn):
|
|
281
|
+
def test_resolve_error(resolve_fn) -> None:
|
|
280
282
|
t = sympy.Symbol('t')
|
|
281
283
|
gpt = cirq.GlobalPhaseGate(coefficient=t)
|
|
282
284
|
with pytest.raises(ValueError, match='Coefficient is not unitary'):
|
|
@@ -286,7 +288,7 @@ def test_resolve_error(resolve_fn):
|
|
|
286
288
|
@pytest.mark.parametrize(
|
|
287
289
|
'coeff, exp', [(-1, 1), (1j, 0.5), (-1j, -0.5), (1 / np.sqrt(2) * (1 + 1j), 0.25)]
|
|
288
290
|
)
|
|
289
|
-
def test_global_phase_gate_controlled(coeff, exp):
|
|
291
|
+
def test_global_phase_gate_controlled(coeff, exp) -> None:
|
|
290
292
|
g = cirq.GlobalPhaseGate(coeff)
|
|
291
293
|
op = cirq.global_phase_operation(coeff)
|
|
292
294
|
q = cirq.LineQubit.range(3)
|