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
cirq/ops/common_gates_test.py
CHANGED
|
@@ -29,23 +29,23 @@ QFT2 = np.array([[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j
|
|
|
29
29
|
@pytest.mark.parametrize(
|
|
30
30
|
'eigen_gate_type', [cirq.CZPowGate, cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate]
|
|
31
31
|
)
|
|
32
|
-
def test_phase_insensitive_eigen_gates_consistent_protocols(eigen_gate_type):
|
|
32
|
+
def test_phase_insensitive_eigen_gates_consistent_protocols(eigen_gate_type) -> None:
|
|
33
33
|
cirq.testing.assert_eigengate_implements_consistent_protocols(eigen_gate_type)
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
@pytest.mark.parametrize('eigen_gate_type', [cirq.CNotPowGate, cirq.HPowGate])
|
|
37
|
-
def test_phase_sensitive_eigen_gates_consistent_protocols(eigen_gate_type):
|
|
37
|
+
def test_phase_sensitive_eigen_gates_consistent_protocols(eigen_gate_type) -> None:
|
|
38
38
|
cirq.testing.assert_eigengate_implements_consistent_protocols(eigen_gate_type)
|
|
39
39
|
|
|
40
40
|
|
|
41
|
-
def test_cz_init():
|
|
41
|
+
def test_cz_init() -> None:
|
|
42
42
|
assert cirq.CZPowGate(exponent=0.5).exponent == 0.5
|
|
43
43
|
assert cirq.CZPowGate(exponent=5).exponent == 5
|
|
44
44
|
assert (cirq.CZ**0.5).exponent == 0.5
|
|
45
45
|
|
|
46
46
|
|
|
47
47
|
@pytest.mark.parametrize('theta,pi', [(0.4, np.pi), (sympy.Symbol("theta"), sympy.pi)])
|
|
48
|
-
def test_transformations(theta, pi):
|
|
48
|
+
def test_transformations(theta, pi) -> None:
|
|
49
49
|
initialRx = cirq.rx(theta)
|
|
50
50
|
expectedPowx = cirq.X ** (theta / pi)
|
|
51
51
|
receivedPowx = initialRx.with_canonical_global_phase()
|
|
@@ -66,19 +66,19 @@ def test_transformations(theta, pi):
|
|
|
66
66
|
assert backToRz == initialRz
|
|
67
67
|
|
|
68
68
|
|
|
69
|
-
def test_cz_str():
|
|
69
|
+
def test_cz_str() -> None:
|
|
70
70
|
assert str(cirq.CZ) == 'CZ'
|
|
71
71
|
assert str(cirq.CZ**0.5) == 'CZ**0.5'
|
|
72
72
|
assert str(cirq.CZ**-0.25) == 'CZ**-0.25'
|
|
73
73
|
|
|
74
74
|
|
|
75
|
-
def test_cz_repr():
|
|
75
|
+
def test_cz_repr() -> None:
|
|
76
76
|
assert repr(cirq.CZ) == 'cirq.CZ'
|
|
77
77
|
assert repr(cirq.CZ**0.5) == '(cirq.CZ**0.5)'
|
|
78
78
|
assert repr(cirq.CZ**-0.25) == '(cirq.CZ**-0.25)'
|
|
79
79
|
|
|
80
80
|
|
|
81
|
-
def test_cz_unitary():
|
|
81
|
+
def test_cz_unitary() -> None:
|
|
82
82
|
assert np.allclose(
|
|
83
83
|
cirq.unitary(cirq.CZ), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]])
|
|
84
84
|
)
|
|
@@ -98,7 +98,7 @@ def test_cz_unitary():
|
|
|
98
98
|
)
|
|
99
99
|
|
|
100
100
|
|
|
101
|
-
def test_z_init():
|
|
101
|
+
def test_z_init() -> None:
|
|
102
102
|
z = cirq.ZPowGate(exponent=5)
|
|
103
103
|
assert z.exponent == 5
|
|
104
104
|
|
|
@@ -121,7 +121,7 @@ def test_z_init():
|
|
|
121
121
|
(cirq.CXPowGate(exponent=0.5), cirq.CCXPowGate(exponent=0.5), cirq.XPowGate(exponent=0.5)),
|
|
122
122
|
],
|
|
123
123
|
)
|
|
124
|
-
def test_specialized_control(input_gate, specialized_output, base_gate):
|
|
124
|
+
def test_specialized_control(input_gate, specialized_output, base_gate) -> None:
|
|
125
125
|
# Single qubit control on the input gate gives the specialized output
|
|
126
126
|
assert input_gate.controlled() == specialized_output
|
|
127
127
|
assert input_gate.controlled(num_controls=1) == specialized_output
|
|
@@ -181,7 +181,7 @@ def test_specialized_control(input_gate, specialized_output, base_gate):
|
|
|
181
181
|
(cirq.XPowGate(exponent=0.5), cirq.CCXPowGate(exponent=0.5)),
|
|
182
182
|
],
|
|
183
183
|
)
|
|
184
|
-
def test_specialized_control_two_step(input_gate, specialized_output):
|
|
184
|
+
def test_specialized_control_two_step(input_gate, specialized_output) -> None:
|
|
185
185
|
# Two-qubit control on the input gate gives the specialized output
|
|
186
186
|
assert input_gate.controlled().controlled() == specialized_output
|
|
187
187
|
assert input_gate.controlled(num_controls=2) == specialized_output
|
|
@@ -203,7 +203,7 @@ def test_specialized_control_two_step(input_gate, specialized_output):
|
|
|
203
203
|
(cirq.CXPowGate(global_shift=-0.5, exponent=0.5), cirq.CCXPowGate),
|
|
204
204
|
],
|
|
205
205
|
)
|
|
206
|
-
def test_no_specialized_control_for_global_shift_non_zero(gate, specialized_type):
|
|
206
|
+
def test_no_specialized_control_for_global_shift_non_zero(gate, specialized_type) -> None:
|
|
207
207
|
assert not isinstance(gate.controlled(), specialized_type)
|
|
208
208
|
|
|
209
209
|
|
|
@@ -229,11 +229,11 @@ def test_no_specialized_control_for_global_shift_non_zero(gate, specialized_type
|
|
|
229
229
|
),
|
|
230
230
|
],
|
|
231
231
|
)
|
|
232
|
-
def test_global_phase_controlled_gate(gate, matrix):
|
|
232
|
+
def test_global_phase_controlled_gate(gate, matrix) -> None:
|
|
233
233
|
np.testing.assert_equal(cirq.unitary(gate.controlled()), matrix)
|
|
234
234
|
|
|
235
235
|
|
|
236
|
-
def test_rot_gates_eq():
|
|
236
|
+
def test_rot_gates_eq() -> None:
|
|
237
237
|
eq = cirq.testing.EqualsTester()
|
|
238
238
|
gates = [
|
|
239
239
|
lambda p: cirq.CZ**p,
|
|
@@ -263,14 +263,14 @@ def test_rot_gates_eq():
|
|
|
263
263
|
eq.add_equality_group(cirq.CZPowGate(), cirq.CZPowGate(exponent=1), cirq.CZ)
|
|
264
264
|
|
|
265
265
|
|
|
266
|
-
def test_z_unitary():
|
|
266
|
+
def test_z_unitary() -> None:
|
|
267
267
|
assert np.allclose(cirq.unitary(cirq.Z), np.array([[1, 0], [0, -1]]))
|
|
268
268
|
assert np.allclose(cirq.unitary(cirq.Z**0.5), np.array([[1, 0], [0, 1j]]))
|
|
269
269
|
assert np.allclose(cirq.unitary(cirq.Z**0), np.array([[1, 0], [0, 1]]))
|
|
270
270
|
assert np.allclose(cirq.unitary(cirq.Z**-0.5), np.array([[1, 0], [0, -1j]]))
|
|
271
271
|
|
|
272
272
|
|
|
273
|
-
def test_y_unitary():
|
|
273
|
+
def test_y_unitary() -> None:
|
|
274
274
|
assert np.allclose(cirq.unitary(cirq.Y), np.array([[0, -1j], [1j, 0]]))
|
|
275
275
|
|
|
276
276
|
assert np.allclose(
|
|
@@ -284,7 +284,7 @@ def test_y_unitary():
|
|
|
284
284
|
)
|
|
285
285
|
|
|
286
286
|
|
|
287
|
-
def test_x_unitary():
|
|
287
|
+
def test_x_unitary() -> None:
|
|
288
288
|
assert np.allclose(cirq.unitary(cirq.X), np.array([[0, 1], [1, 0]]))
|
|
289
289
|
|
|
290
290
|
assert np.allclose(
|
|
@@ -298,28 +298,28 @@ def test_x_unitary():
|
|
|
298
298
|
)
|
|
299
299
|
|
|
300
300
|
|
|
301
|
-
def test_h_unitary():
|
|
301
|
+
def test_h_unitary() -> None:
|
|
302
302
|
sqrt = cirq.unitary(cirq.H**0.5)
|
|
303
303
|
m = np.dot(sqrt, sqrt)
|
|
304
304
|
assert np.allclose(m, cirq.unitary(cirq.H), atol=1e-8)
|
|
305
305
|
|
|
306
306
|
|
|
307
|
-
def test_h_init():
|
|
307
|
+
def test_h_init() -> None:
|
|
308
308
|
h = cirq.HPowGate(exponent=0.5)
|
|
309
309
|
assert h.exponent == 0.5
|
|
310
310
|
|
|
311
311
|
|
|
312
|
-
def test_h_str():
|
|
312
|
+
def test_h_str() -> None:
|
|
313
313
|
assert str(cirq.H) == 'H'
|
|
314
314
|
assert str(cirq.H**0.5) == 'H**0.5'
|
|
315
315
|
|
|
316
316
|
|
|
317
|
-
def test_phase_exponent():
|
|
317
|
+
def test_phase_exponent() -> None:
|
|
318
318
|
assert cirq.XPowGate(exponent=0.5).phase_exponent == 0.0
|
|
319
319
|
assert cirq.YPowGate(exponent=0.5).phase_exponent == 0.5
|
|
320
320
|
|
|
321
321
|
|
|
322
|
-
def test_x_act_on_tableau():
|
|
322
|
+
def test_x_act_on_tableau() -> None:
|
|
323
323
|
with pytest.raises(TypeError, match="Failed to act"):
|
|
324
324
|
cirq.act_on(cirq.X, ExampleSimulationState(), qubits=())
|
|
325
325
|
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
|
|
@@ -368,7 +368,7 @@ class MinusOnePhaseGate(cirq.testing.SingleQubitGate):
|
|
|
368
368
|
return np.array([[-1, 0], [0, -1]])
|
|
369
369
|
|
|
370
370
|
|
|
371
|
-
def test_y_act_on_tableau():
|
|
371
|
+
def test_y_act_on_tableau() -> None:
|
|
372
372
|
with pytest.raises(TypeError, match="Failed to act"):
|
|
373
373
|
cirq.act_on(cirq.Y, ExampleSimulationState(), qubits=())
|
|
374
374
|
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
|
|
@@ -406,7 +406,7 @@ def test_y_act_on_tableau():
|
|
|
406
406
|
cirq.act_on(cirq.Y**foo, state, [cirq.LineQubit(1)])
|
|
407
407
|
|
|
408
408
|
|
|
409
|
-
def test_z_h_act_on_tableau():
|
|
409
|
+
def test_z_h_act_on_tableau() -> None:
|
|
410
410
|
with pytest.raises(TypeError, match="Failed to act"):
|
|
411
411
|
cirq.act_on(cirq.Z, ExampleSimulationState(), qubits=())
|
|
412
412
|
with pytest.raises(TypeError, match="Failed to act"):
|
|
@@ -459,7 +459,7 @@ def test_z_h_act_on_tableau():
|
|
|
459
459
|
cirq.act_on(cirq.H**1.5, state, [cirq.LineQubit(1)])
|
|
460
460
|
|
|
461
461
|
|
|
462
|
-
def test_cx_act_on_tableau():
|
|
462
|
+
def test_cx_act_on_tableau() -> None:
|
|
463
463
|
with pytest.raises(TypeError, match="Failed to act"):
|
|
464
464
|
cirq.act_on(cirq.CX, ExampleSimulationState(), qubits=())
|
|
465
465
|
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
|
|
@@ -503,7 +503,7 @@ def test_cx_act_on_tableau():
|
|
|
503
503
|
cirq.act_on(cirq.CX**1.5, state, cirq.LineQubit.range(2))
|
|
504
504
|
|
|
505
505
|
|
|
506
|
-
def test_cz_act_on_tableau():
|
|
506
|
+
def test_cz_act_on_tableau() -> None:
|
|
507
507
|
with pytest.raises(TypeError, match="Failed to act"):
|
|
508
508
|
cirq.act_on(cirq.CZ, ExampleSimulationState(), qubits=())
|
|
509
509
|
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
|
|
@@ -547,7 +547,7 @@ def test_cz_act_on_tableau():
|
|
|
547
547
|
cirq.act_on(cirq.CZ**1.5, state, cirq.LineQubit.range(2))
|
|
548
548
|
|
|
549
549
|
|
|
550
|
-
def test_cz_act_on_equivalent_to_h_cx_h_tableau():
|
|
550
|
+
def test_cz_act_on_equivalent_to_h_cx_h_tableau() -> None:
|
|
551
551
|
state1 = cirq.CliffordTableauSimulationState(
|
|
552
552
|
tableau=cirq.CliffordTableau(num_qubits=2),
|
|
553
553
|
qubits=cirq.LineQubit.range(2),
|
|
@@ -607,7 +607,7 @@ foo = sympy.Symbol('foo')
|
|
|
607
607
|
([cirq.CZ, MinusOnePhaseGate()], 'Original'),
|
|
608
608
|
],
|
|
609
609
|
)
|
|
610
|
-
def test_act_on_ch_form(input_gate_sequence, outcome):
|
|
610
|
+
def test_act_on_ch_form(input_gate_sequence, outcome) -> None:
|
|
611
611
|
original_state = cirq.StabilizerStateChForm(num_qubits=5, initial_state=31)
|
|
612
612
|
num_qubits = cirq.num_qubits(input_gate_sequence[0])
|
|
613
613
|
if num_qubits == 1:
|
|
@@ -669,13 +669,13 @@ def test_act_on_ch_form(input_gate_sequence, outcome):
|
|
|
669
669
|
(cirq.CZ**0.5, False),
|
|
670
670
|
],
|
|
671
671
|
)
|
|
672
|
-
def test_act_on_consistency(input_gate, assert_implemented):
|
|
672
|
+
def test_act_on_consistency(input_gate, assert_implemented) -> None:
|
|
673
673
|
cirq.testing.assert_all_implemented_act_on_effects_match_unitary(
|
|
674
674
|
input_gate, assert_implemented, assert_implemented
|
|
675
675
|
)
|
|
676
676
|
|
|
677
677
|
|
|
678
|
-
def test_runtime_types_of_rot_gates():
|
|
678
|
+
def test_runtime_types_of_rot_gates() -> None:
|
|
679
679
|
for gate_type in [
|
|
680
680
|
lambda p: cirq.CZPowGate(exponent=p),
|
|
681
681
|
lambda p: cirq.XPowGate(exponent=p),
|
|
@@ -693,7 +693,7 @@ def test_runtime_types_of_rot_gates():
|
|
|
693
693
|
assert cirq.inverse(c) == gate_type(-0.5)
|
|
694
694
|
|
|
695
695
|
|
|
696
|
-
def test_interchangeable_qubit_eq():
|
|
696
|
+
def test_interchangeable_qubit_eq() -> None:
|
|
697
697
|
a = cirq.NamedQubit('a')
|
|
698
698
|
b = cirq.NamedQubit('b')
|
|
699
699
|
c = cirq.NamedQubit('c')
|
|
@@ -707,7 +707,7 @@ def test_interchangeable_qubit_eq():
|
|
|
707
707
|
eq.add_equality_group(cirq.CNOT(a, c))
|
|
708
708
|
|
|
709
709
|
|
|
710
|
-
def test_identity_multiplication():
|
|
710
|
+
def test_identity_multiplication() -> None:
|
|
711
711
|
a, b, c = cirq.LineQubit.range(3)
|
|
712
712
|
assert cirq.I(a) * cirq.CX(a, b) == cirq.CX(a, b)
|
|
713
713
|
assert cirq.CX(a, b) * cirq.I(a) == cirq.CX(a, b)
|
|
@@ -720,7 +720,7 @@ def test_identity_multiplication():
|
|
|
720
720
|
assert cirq.I(c) * cirq.T(b) ** 0.25 == cirq.T(b) ** 0.25
|
|
721
721
|
|
|
722
722
|
|
|
723
|
-
def test_text_diagrams():
|
|
723
|
+
def test_text_diagrams() -> None:
|
|
724
724
|
a = cirq.NamedQubit('a')
|
|
725
725
|
b = cirq.NamedQubit('b')
|
|
726
726
|
circuit = cirq.Circuit(
|
|
@@ -760,7 +760,7 @@ b: -----------------------------@---X---@---X^0.5---@-------------------I---@^t-
|
|
|
760
760
|
)
|
|
761
761
|
|
|
762
762
|
|
|
763
|
-
def test_cnot_unitary():
|
|
763
|
+
def test_cnot_unitary() -> None:
|
|
764
764
|
np.testing.assert_almost_equal(
|
|
765
765
|
cirq.unitary(cirq.CNOT**0.5),
|
|
766
766
|
np.array(
|
|
@@ -774,13 +774,13 @@ def test_cnot_unitary():
|
|
|
774
774
|
)
|
|
775
775
|
|
|
776
776
|
|
|
777
|
-
def test_cnot_decompose():
|
|
777
|
+
def test_cnot_decompose() -> None:
|
|
778
778
|
a = cirq.NamedQubit('a')
|
|
779
779
|
b = cirq.NamedQubit('b')
|
|
780
780
|
assert cirq.decompose_once(cirq.CNOT(a, b) ** sympy.Symbol('x')) is not None
|
|
781
781
|
|
|
782
782
|
|
|
783
|
-
def test_repr():
|
|
783
|
+
def test_repr() -> None:
|
|
784
784
|
assert repr(cirq.X) == 'cirq.X'
|
|
785
785
|
assert repr(cirq.X**0.5) == '(cirq.X**0.5)'
|
|
786
786
|
|
|
@@ -814,7 +814,7 @@ def test_repr():
|
|
|
814
814
|
assert repr(cirq.CZ**0.2) == '(cirq.CZ**0.2)'
|
|
815
815
|
|
|
816
816
|
|
|
817
|
-
def test_str():
|
|
817
|
+
def test_str() -> None:
|
|
818
818
|
assert str(cirq.X) == 'X'
|
|
819
819
|
assert str(cirq.X**0.5) == 'X**0.5'
|
|
820
820
|
assert str(cirq.rx(np.pi)) == 'Rx(π)'
|
|
@@ -850,7 +850,7 @@ def test_str():
|
|
|
850
850
|
assert str(cirq.cphase(np.pi / 2)) == 'CZ**0.5'
|
|
851
851
|
|
|
852
852
|
|
|
853
|
-
def test_rx_unitary():
|
|
853
|
+
def test_rx_unitary() -> None:
|
|
854
854
|
s = np.sqrt(0.5)
|
|
855
855
|
np.testing.assert_allclose(
|
|
856
856
|
cirq.unitary(cirq.rx(np.pi / 2)), np.array([[s, -s * 1j], [-s * 1j, s]])
|
|
@@ -869,7 +869,7 @@ def test_rx_unitary():
|
|
|
869
869
|
np.testing.assert_allclose(cirq.unitary(cirq.rx(-np.pi)), np.array([[0, 1j], [1j, 0]]))
|
|
870
870
|
|
|
871
871
|
|
|
872
|
-
def test_ry_unitary():
|
|
872
|
+
def test_ry_unitary() -> None:
|
|
873
873
|
s = np.sqrt(0.5)
|
|
874
874
|
np.testing.assert_allclose(cirq.unitary(cirq.ry(np.pi / 2)), np.array([[s, -s], [s, s]]))
|
|
875
875
|
|
|
@@ -884,7 +884,7 @@ def test_ry_unitary():
|
|
|
884
884
|
np.testing.assert_allclose(cirq.unitary(cirq.ry(-np.pi)), np.array([[0, 1], [-1, 0]]))
|
|
885
885
|
|
|
886
886
|
|
|
887
|
-
def test_rz_unitary():
|
|
887
|
+
def test_rz_unitary() -> None:
|
|
888
888
|
s = np.sqrt(0.5)
|
|
889
889
|
np.testing.assert_allclose(
|
|
890
890
|
cirq.unitary(cirq.rz(np.pi / 2)), np.array([[s - s * 1j, 0], [0, s + s * 1j]])
|
|
@@ -907,17 +907,17 @@ def test_rz_unitary():
|
|
|
907
907
|
'angle_rads, expected_unitary',
|
|
908
908
|
[(0, np.eye(4)), (1, np.diag([1, 1, 1, np.exp(1j)])), (np.pi / 2, np.diag([1, 1, 1, 1j]))],
|
|
909
909
|
)
|
|
910
|
-
def test_cphase_unitary(angle_rads, expected_unitary):
|
|
910
|
+
def test_cphase_unitary(angle_rads, expected_unitary) -> None:
|
|
911
911
|
np.testing.assert_allclose(cirq.unitary(cirq.cphase(angle_rads)), expected_unitary)
|
|
912
912
|
|
|
913
913
|
|
|
914
|
-
def test_parameterized_cphase():
|
|
914
|
+
def test_parameterized_cphase() -> None:
|
|
915
915
|
assert cirq.cphase(sympy.pi) == cirq.CZ
|
|
916
916
|
assert cirq.cphase(sympy.pi / 2) == cirq.CZ**0.5
|
|
917
917
|
|
|
918
918
|
|
|
919
919
|
@pytest.mark.parametrize('gate', [cirq.X, cirq.Y, cirq.Z])
|
|
920
|
-
def test_x_y_z_stabilizer(gate):
|
|
920
|
+
def test_x_y_z_stabilizer(gate) -> None:
|
|
921
921
|
assert cirq.has_stabilizer_effect(gate)
|
|
922
922
|
assert cirq.has_stabilizer_effect(gate**0.5)
|
|
923
923
|
assert cirq.has_stabilizer_effect(gate**0)
|
|
@@ -928,7 +928,7 @@ def test_x_y_z_stabilizer(gate):
|
|
|
928
928
|
assert not cirq.has_stabilizer_effect(gate**foo)
|
|
929
929
|
|
|
930
930
|
|
|
931
|
-
def test_h_stabilizer():
|
|
931
|
+
def test_h_stabilizer() -> None:
|
|
932
932
|
gate = cirq.H
|
|
933
933
|
assert cirq.has_stabilizer_effect(gate)
|
|
934
934
|
assert not cirq.has_stabilizer_effect(gate**0.5)
|
|
@@ -941,7 +941,7 @@ def test_h_stabilizer():
|
|
|
941
941
|
|
|
942
942
|
|
|
943
943
|
@pytest.mark.parametrize('gate', [cirq.CX, cirq.CZ])
|
|
944
|
-
def test_cx_cz_stabilizer(gate):
|
|
944
|
+
def test_cx_cz_stabilizer(gate) -> None:
|
|
945
945
|
assert cirq.has_stabilizer_effect(gate)
|
|
946
946
|
assert not cirq.has_stabilizer_effect(gate**0.5)
|
|
947
947
|
assert cirq.has_stabilizer_effect(gate**0)
|
|
@@ -952,13 +952,13 @@ def test_cx_cz_stabilizer(gate):
|
|
|
952
952
|
assert not cirq.has_stabilizer_effect(gate**foo)
|
|
953
953
|
|
|
954
954
|
|
|
955
|
-
def test_phase_by_xy():
|
|
955
|
+
def test_phase_by_xy() -> None:
|
|
956
956
|
assert cirq.phase_by(cirq.X, 0.25, 0) == cirq.Y
|
|
957
957
|
assert cirq.phase_by(cirq.X**0.5, 0.25, 0) == cirq.Y**0.5
|
|
958
958
|
assert cirq.phase_by(cirq.X**-0.5, 0.25, 0) == cirq.Y**-0.5
|
|
959
959
|
|
|
960
960
|
|
|
961
|
-
def test_ixyz_circuit_diagram():
|
|
961
|
+
def test_ixyz_circuit_diagram() -> None:
|
|
962
962
|
q = cirq.NamedQubit('q')
|
|
963
963
|
ix = cirq.XPowGate(exponent=1, global_shift=0.5)
|
|
964
964
|
iy = cirq.YPowGate(exponent=1, global_shift=0.5)
|
|
@@ -1003,7 +1003,7 @@ q: ───Z───Z───Z───S───S───
|
|
|
1003
1003
|
(sympy.Symbol("theta"), sympy.Symbol("exp")),
|
|
1004
1004
|
],
|
|
1005
1005
|
)
|
|
1006
|
-
def test_rxyz_exponent(theta, exp):
|
|
1006
|
+
def test_rxyz_exponent(theta, exp) -> None:
|
|
1007
1007
|
def resolve(gate):
|
|
1008
1008
|
return cirq.resolve_parameters(gate, {'theta': np.pi / 4}, {'exp': 1 / 4})
|
|
1009
1009
|
|
|
@@ -1012,7 +1012,7 @@ def test_rxyz_exponent(theta, exp):
|
|
|
1012
1012
|
assert resolve(cirq.Rz(rads=theta) ** exp) == resolve(cirq.Rz(rads=theta * exp))
|
|
1013
1013
|
|
|
1014
1014
|
|
|
1015
|
-
def test_rxyz_circuit_diagram():
|
|
1015
|
+
def test_rxyz_circuit_diagram() -> None:
|
|
1016
1016
|
q = cirq.NamedQubit('q')
|
|
1017
1017
|
|
|
1018
1018
|
cirq.testing.assert_has_diagram(
|
|
@@ -1069,7 +1069,7 @@ q: ───Rz(π)───Rz(-π)───Rz(-π)───Rz(0.5π)───Rz(
|
|
|
1069
1069
|
)
|
|
1070
1070
|
|
|
1071
1071
|
|
|
1072
|
-
def test_trace_distance():
|
|
1072
|
+
def test_trace_distance() -> None:
|
|
1073
1073
|
foo = sympy.Symbol('foo')
|
|
1074
1074
|
sx = cirq.X**foo
|
|
1075
1075
|
sy = cirq.Y**foo
|
|
@@ -1094,13 +1094,13 @@ def test_trace_distance():
|
|
|
1094
1094
|
assert cirq.approx_eq(cirq.trace_distance_bound(cirq.CZ ** (1 / 9)), np.sin(np.pi / 18))
|
|
1095
1095
|
|
|
1096
1096
|
|
|
1097
|
-
def test_commutes():
|
|
1097
|
+
def test_commutes() -> None:
|
|
1098
1098
|
assert cirq.commutes(cirq.ZPowGate(exponent=sympy.Symbol('t')), cirq.Z)
|
|
1099
1099
|
assert cirq.commutes(cirq.Z, cirq.Z(cirq.LineQubit(0)), default=None) is None
|
|
1100
1100
|
assert cirq.commutes(cirq.Z**0.1, cirq.XPowGate(exponent=0))
|
|
1101
1101
|
|
|
1102
1102
|
|
|
1103
|
-
def test_approx_eq():
|
|
1103
|
+
def test_approx_eq() -> None:
|
|
1104
1104
|
assert cirq.approx_eq(cirq.Z**0.1, cirq.Z**0.2, atol=0.3)
|
|
1105
1105
|
assert not cirq.approx_eq(cirq.Z**0.1, cirq.Z**0.2, atol=0.05)
|
|
1106
1106
|
assert cirq.approx_eq(cirq.Y**0.1, cirq.Y**0.2, atol=0.3)
|
|
@@ -1109,15 +1109,15 @@ def test_approx_eq():
|
|
|
1109
1109
|
assert not cirq.approx_eq(cirq.X**0.1, cirq.X**0.2, atol=0.05)
|
|
1110
1110
|
|
|
1111
1111
|
|
|
1112
|
-
def test_xpow_dim_3():
|
|
1112
|
+
def test_xpow_dim_3() -> None:
|
|
1113
1113
|
x = cirq.XPowGate(dimension=3)
|
|
1114
1114
|
assert cirq.X != x
|
|
1115
1115
|
# fmt: off
|
|
1116
|
-
expected = [
|
|
1116
|
+
expected = np.asarray([
|
|
1117
1117
|
[0, 0, 1],
|
|
1118
1118
|
[1, 0, 0],
|
|
1119
1119
|
[0, 1, 0],
|
|
1120
|
-
]
|
|
1120
|
+
])
|
|
1121
1121
|
# fmt: on
|
|
1122
1122
|
assert np.allclose(cirq.unitary(x), expected)
|
|
1123
1123
|
|
|
@@ -1125,28 +1125,28 @@ def test_xpow_dim_3():
|
|
|
1125
1125
|
circuit = cirq.Circuit([x(cirq.LineQid(0, 3)) ** 0.5] * 6)
|
|
1126
1126
|
svs = [step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit)]
|
|
1127
1127
|
# fmt: off
|
|
1128
|
-
expected = [
|
|
1128
|
+
expected = np.asarray([
|
|
1129
1129
|
[0.67, 0.67, 0.33],
|
|
1130
1130
|
[0.0, 1.0, 0.0],
|
|
1131
1131
|
[0.33, 0.67, 0.67],
|
|
1132
1132
|
[0.0, 0.0, 1.0],
|
|
1133
1133
|
[0.67, 0.33, 0.67],
|
|
1134
1134
|
[1.0, 0.0, 0.0],
|
|
1135
|
-
]
|
|
1135
|
+
])
|
|
1136
1136
|
# fmt: on
|
|
1137
1137
|
assert np.allclose(np.abs(svs), expected, atol=1e-2)
|
|
1138
1138
|
|
|
1139
1139
|
|
|
1140
|
-
def test_xpow_dim_4():
|
|
1140
|
+
def test_xpow_dim_4() -> None:
|
|
1141
1141
|
x = cirq.XPowGate(dimension=4)
|
|
1142
1142
|
assert cirq.X != x
|
|
1143
1143
|
# fmt: off
|
|
1144
|
-
expected = [
|
|
1144
|
+
expected = np.asarray([
|
|
1145
1145
|
[0, 0, 0, 1],
|
|
1146
1146
|
[1, 0, 0, 0],
|
|
1147
1147
|
[0, 1, 0, 0],
|
|
1148
1148
|
[0, 0, 1, 0],
|
|
1149
|
-
]
|
|
1149
|
+
])
|
|
1150
1150
|
# fmt: on
|
|
1151
1151
|
assert np.allclose(cirq.unitary(x), expected)
|
|
1152
1152
|
|
|
@@ -1154,7 +1154,7 @@ def test_xpow_dim_4():
|
|
|
1154
1154
|
circuit = cirq.Circuit([x(cirq.LineQid(0, 4)) ** 0.5] * 8)
|
|
1155
1155
|
svs = [step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit)]
|
|
1156
1156
|
# fmt: off
|
|
1157
|
-
expected = [
|
|
1157
|
+
expected = np.asarray([
|
|
1158
1158
|
[0.65, 0.65, 0.27, 0.27],
|
|
1159
1159
|
[0.0, 1.0, 0.0, 0.0],
|
|
1160
1160
|
[0.27, 0.65, 0.65, 0.27],
|
|
@@ -1163,22 +1163,22 @@ def test_xpow_dim_4():
|
|
|
1163
1163
|
[0.0, 0.0, 0.0, 1.0],
|
|
1164
1164
|
[0.65, 0.27, 0.27, 0.65],
|
|
1165
1165
|
[1.0, 0.0, 0.0, 0.0],
|
|
1166
|
-
]
|
|
1166
|
+
])
|
|
1167
1167
|
# fmt: on
|
|
1168
1168
|
assert np.allclose(np.abs(svs), expected, atol=1e-2)
|
|
1169
1169
|
|
|
1170
1170
|
|
|
1171
|
-
def test_zpow_dim_3():
|
|
1171
|
+
def test_zpow_dim_3() -> None:
|
|
1172
1172
|
L = np.exp(2 * np.pi * 1j / 3)
|
|
1173
1173
|
L2 = L**2
|
|
1174
1174
|
z = cirq.ZPowGate(dimension=3)
|
|
1175
1175
|
assert cirq.Z != z
|
|
1176
1176
|
# fmt: off
|
|
1177
|
-
expected = [
|
|
1177
|
+
expected = np.asarray([
|
|
1178
1178
|
[1, 0, 0],
|
|
1179
1179
|
[0, L, 0],
|
|
1180
1180
|
[0, 0, L2],
|
|
1181
|
-
]
|
|
1181
|
+
])
|
|
1182
1182
|
# fmt: on
|
|
1183
1183
|
assert np.allclose(cirq.unitary(z), expected)
|
|
1184
1184
|
|
|
@@ -1187,21 +1187,21 @@ def test_zpow_dim_3():
|
|
|
1187
1187
|
svs = [
|
|
1188
1188
|
step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=0)
|
|
1189
1189
|
]
|
|
1190
|
-
expected = [[1, 0, 0]] * 6
|
|
1190
|
+
expected = np.asarray([[1, 0, 0]] * 6)
|
|
1191
1191
|
assert np.allclose((svs), expected)
|
|
1192
1192
|
|
|
1193
1193
|
svs = [
|
|
1194
1194
|
step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=1)
|
|
1195
1195
|
]
|
|
1196
1196
|
# fmt: off
|
|
1197
|
-
expected = [
|
|
1197
|
+
expected = np.asarray([
|
|
1198
1198
|
[0, L**0.5, 0],
|
|
1199
1199
|
[0, L**1.0, 0],
|
|
1200
1200
|
[0, L**1.5, 0],
|
|
1201
1201
|
[0, L**2.0, 0],
|
|
1202
1202
|
[0, L**2.5, 0],
|
|
1203
1203
|
[0, 1, 0],
|
|
1204
|
-
]
|
|
1204
|
+
])
|
|
1205
1205
|
# fmt: on
|
|
1206
1206
|
assert np.allclose((svs), expected)
|
|
1207
1207
|
|
|
@@ -1209,28 +1209,28 @@ def test_zpow_dim_3():
|
|
|
1209
1209
|
step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=2)
|
|
1210
1210
|
]
|
|
1211
1211
|
# fmt: off
|
|
1212
|
-
expected = [
|
|
1212
|
+
expected = np.asarray([
|
|
1213
1213
|
[0, 0, L],
|
|
1214
1214
|
[0, 0, L2],
|
|
1215
1215
|
[0, 0, 1],
|
|
1216
1216
|
[0, 0, L],
|
|
1217
1217
|
[0, 0, L2],
|
|
1218
1218
|
[0, 0, 1],
|
|
1219
|
-
]
|
|
1219
|
+
])
|
|
1220
1220
|
# fmt: on
|
|
1221
1221
|
assert np.allclose((svs), expected)
|
|
1222
1222
|
|
|
1223
1223
|
|
|
1224
|
-
def test_zpow_dim_4():
|
|
1224
|
+
def test_zpow_dim_4() -> None:
|
|
1225
1225
|
z = cirq.ZPowGate(dimension=4)
|
|
1226
1226
|
assert cirq.Z != z
|
|
1227
1227
|
# fmt: off
|
|
1228
|
-
expected = [
|
|
1228
|
+
expected = np.asarray([
|
|
1229
1229
|
[1, 0, 0, 0],
|
|
1230
1230
|
[0, 1j, 0, 0],
|
|
1231
1231
|
[0, 0, -1, 0],
|
|
1232
1232
|
[0, 0, 0, -1j],
|
|
1233
|
-
]
|
|
1233
|
+
])
|
|
1234
1234
|
# fmt: on
|
|
1235
1235
|
assert np.allclose(cirq.unitary(z), expected)
|
|
1236
1236
|
|
|
@@ -1239,14 +1239,14 @@ def test_zpow_dim_4():
|
|
|
1239
1239
|
svs = [
|
|
1240
1240
|
step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=0)
|
|
1241
1241
|
]
|
|
1242
|
-
expected = [[1, 0, 0, 0]] * 8
|
|
1242
|
+
expected = np.asarray([[1, 0, 0, 0]] * 8)
|
|
1243
1243
|
assert np.allclose((svs), expected)
|
|
1244
1244
|
|
|
1245
1245
|
svs = [
|
|
1246
1246
|
step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=1)
|
|
1247
1247
|
]
|
|
1248
1248
|
# fmt: off
|
|
1249
|
-
expected = [
|
|
1249
|
+
expected = np.asarray([
|
|
1250
1250
|
[0, 1j**0.5, 0, 0],
|
|
1251
1251
|
[0, 1j**1.0, 0, 0],
|
|
1252
1252
|
[0, 1j**1.5, 0, 0],
|
|
@@ -1255,7 +1255,7 @@ def test_zpow_dim_4():
|
|
|
1255
1255
|
[0, 1j**3.0, 0, 0],
|
|
1256
1256
|
[0, 1j**3.5, 0, 0],
|
|
1257
1257
|
[0, 1, 0, 0],
|
|
1258
|
-
]
|
|
1258
|
+
])
|
|
1259
1259
|
# fmt: on
|
|
1260
1260
|
assert np.allclose(svs, expected)
|
|
1261
1261
|
|
|
@@ -1263,7 +1263,7 @@ def test_zpow_dim_4():
|
|
|
1263
1263
|
step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=2)
|
|
1264
1264
|
]
|
|
1265
1265
|
# fmt: off
|
|
1266
|
-
expected = [
|
|
1266
|
+
expected = np.asarray([
|
|
1267
1267
|
[0, 0, 1j, 0],
|
|
1268
1268
|
[0, 0, -1, 0],
|
|
1269
1269
|
[0, 0, -1j, 0],
|
|
@@ -1272,7 +1272,7 @@ def test_zpow_dim_4():
|
|
|
1272
1272
|
[0, 0, -1, 0],
|
|
1273
1273
|
[0, 0, -1j, 0],
|
|
1274
1274
|
[0, 0, 1, 0],
|
|
1275
|
-
]
|
|
1275
|
+
])
|
|
1276
1276
|
# fmt: on
|
|
1277
1277
|
assert np.allclose(svs, expected)
|
|
1278
1278
|
|
|
@@ -1280,7 +1280,7 @@ def test_zpow_dim_4():
|
|
|
1280
1280
|
step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=3)
|
|
1281
1281
|
]
|
|
1282
1282
|
# fmt: off
|
|
1283
|
-
expected = [
|
|
1283
|
+
expected = np.asarray([
|
|
1284
1284
|
[0, 0, 0, 1j**1.5],
|
|
1285
1285
|
[0, 0, 0, 1j**3],
|
|
1286
1286
|
[0, 0, 0, 1j**0.5],
|
|
@@ -1289,12 +1289,12 @@ def test_zpow_dim_4():
|
|
|
1289
1289
|
[0, 0, 0, 1j**1],
|
|
1290
1290
|
[0, 0, 0, 1j**2.5],
|
|
1291
1291
|
[0, 0, 0, 1],
|
|
1292
|
-
]
|
|
1292
|
+
])
|
|
1293
1293
|
# fmt: on
|
|
1294
1294
|
assert np.allclose(svs, expected)
|
|
1295
1295
|
|
|
1296
1296
|
|
|
1297
|
-
def test_wrong_dims():
|
|
1297
|
+
def test_wrong_dims() -> None:
|
|
1298
1298
|
x3 = cirq.XPowGate(dimension=3)
|
|
1299
1299
|
with pytest.raises(ValueError, match='Wrong shape'):
|
|
1300
1300
|
_ = x3.on(cirq.LineQubit(0))
|
|
@@ -1316,7 +1316,7 @@ def test_wrong_dims():
|
|
|
1316
1316
|
|
|
1317
1317
|
@pytest.mark.parametrize('gate_type', [cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate])
|
|
1318
1318
|
@pytest.mark.parametrize('exponent', [sympy.Symbol('s'), sympy.Symbol('s') * 2])
|
|
1319
|
-
def test_parameterized_pauli_expansion(gate_type, exponent):
|
|
1319
|
+
def test_parameterized_pauli_expansion(gate_type, exponent) -> None:
|
|
1320
1320
|
gate = gate_type(exponent=exponent)
|
|
1321
1321
|
pauli = cirq.pauli_expansion(gate)
|
|
1322
1322
|
gate_resolved = cirq.resolve_parameters(gate, {'s': 0.5})
|