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
|
@@ -22,7 +22,7 @@ from cirq.contrib.qasm_import import circuit_from_qasm
|
|
|
22
22
|
from cirq.testing import consistent_qasm as cq
|
|
23
23
|
|
|
24
24
|
|
|
25
|
-
def test_consistency_with_qasm_output_and_qiskit():
|
|
25
|
+
def test_consistency_with_qasm_output_and_qiskit() -> None:
|
|
26
26
|
qubits = [cirq.NamedQubit(f'q_{i}') for i in range(4)]
|
|
27
27
|
a, b, c, d = qubits
|
|
28
28
|
circuit1 = cirq.Circuit(
|
|
@@ -35,9 +35,9 @@ def test_consistency_with_qasm_output_and_qiskit():
|
|
|
35
35
|
cirq.H.on(d),
|
|
36
36
|
cirq.S.on(a),
|
|
37
37
|
cirq.T.on(b),
|
|
38
|
-
cirq.S.on(c)
|
|
39
|
-
cirq.T.on(d)
|
|
40
|
-
cirq.X.on(d)
|
|
38
|
+
(cirq.S**-1).on(c),
|
|
39
|
+
(cirq.T**-1).on(d),
|
|
40
|
+
(cirq.X**0.125).on(d),
|
|
41
41
|
cirq.TOFFOLI.on(a, b, c),
|
|
42
42
|
cirq.CSWAP.on(d, a, b),
|
|
43
43
|
cirq.SWAP.on(c, d),
|
|
@@ -21,7 +21,7 @@ import cirq
|
|
|
21
21
|
from cirq.contrib.quirk.export_to_quirk import circuit_to_quirk_url
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
def assert_links_to(circuit: cirq.Circuit, expected: str, **kwargs):
|
|
24
|
+
def assert_links_to(circuit: cirq.Circuit, expected: str, **kwargs) -> None:
|
|
25
25
|
actual = circuit_to_quirk_url(circuit, **kwargs)
|
|
26
26
|
actual = actual.replace('\n', '').replace(' ', '').strip()
|
|
27
27
|
expected = expected.replace('],[', '],\n[').strip()
|
|
@@ -29,7 +29,7 @@ def assert_links_to(circuit: cirq.Circuit, expected: str, **kwargs):
|
|
|
29
29
|
assert actual == expected
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
def test_x_z_same_col():
|
|
32
|
+
def test_x_z_same_col() -> None:
|
|
33
33
|
a = cirq.NamedQubit('a')
|
|
34
34
|
b = cirq.NamedQubit('b')
|
|
35
35
|
circuit = cirq.Circuit(cirq.X(a), cirq.Z(b))
|
|
@@ -46,7 +46,7 @@ def test_x_z_same_col():
|
|
|
46
46
|
)
|
|
47
47
|
|
|
48
48
|
|
|
49
|
-
def test_x_cnot_split_cols():
|
|
49
|
+
def test_x_cnot_split_cols() -> None:
|
|
50
50
|
a = cirq.NamedQubit('a')
|
|
51
51
|
b = cirq.NamedQubit('b')
|
|
52
52
|
c = cirq.NamedQubit('c')
|
|
@@ -60,7 +60,7 @@ def test_x_cnot_split_cols():
|
|
|
60
60
|
)
|
|
61
61
|
|
|
62
62
|
|
|
63
|
-
def test_cz_cnot_split_cols():
|
|
63
|
+
def test_cz_cnot_split_cols() -> None:
|
|
64
64
|
a = cirq.NamedQubit('a')
|
|
65
65
|
b = cirq.NamedQubit('b')
|
|
66
66
|
c = cirq.NamedQubit('c')
|
|
@@ -74,7 +74,7 @@ def test_cz_cnot_split_cols():
|
|
|
74
74
|
)
|
|
75
75
|
|
|
76
76
|
|
|
77
|
-
def test_various_known_gate_types():
|
|
77
|
+
def test_various_known_gate_types() -> None:
|
|
78
78
|
a = cirq.NamedQubit('a')
|
|
79
79
|
b = cirq.NamedQubit('b')
|
|
80
80
|
circuit = cirq.Circuit(
|
|
@@ -118,7 +118,7 @@ def test_various_known_gate_types():
|
|
|
118
118
|
)
|
|
119
119
|
|
|
120
120
|
|
|
121
|
-
def test_parameterized_gates():
|
|
121
|
+
def test_parameterized_gates() -> None:
|
|
122
122
|
a = cirq.LineQubit(0)
|
|
123
123
|
s = sympy.Symbol('s')
|
|
124
124
|
t = sympy.Symbol('t')
|
|
@@ -184,7 +184,7 @@ class MysteryGate(cirq.testing.SingleQubitGate):
|
|
|
184
184
|
return True
|
|
185
185
|
|
|
186
186
|
|
|
187
|
-
def test_various_unknown_gate_types():
|
|
187
|
+
def test_various_unknown_gate_types() -> None:
|
|
188
188
|
a = cirq.NamedQubit('a')
|
|
189
189
|
b = cirq.NamedQubit('b')
|
|
190
190
|
circuit = cirq.Circuit(
|
|
@@ -227,7 +227,7 @@ def test_various_unknown_gate_types():
|
|
|
227
227
|
)
|
|
228
228
|
|
|
229
229
|
|
|
230
|
-
def test_formulaic_exponent_export():
|
|
230
|
+
def test_formulaic_exponent_export() -> None:
|
|
231
231
|
a = cirq.LineQubit(0)
|
|
232
232
|
t = sympy.Symbol('t')
|
|
233
233
|
assert_links_to(
|
|
@@ -243,7 +243,7 @@ def test_formulaic_exponent_export():
|
|
|
243
243
|
)
|
|
244
244
|
|
|
245
245
|
|
|
246
|
-
def test_formulaic_rotation_xyz_export():
|
|
246
|
+
def test_formulaic_rotation_xyz_export() -> None:
|
|
247
247
|
a = cirq.LineQubit(0)
|
|
248
248
|
t = sympy.Symbol('t')
|
|
249
249
|
assert_links_to(
|
|
@@ -264,9 +264,9 @@ def test_formulaic_rotation_xyz_export():
|
|
|
264
264
|
_ = circuit_to_quirk_url(cirq.Circuit(cirq.rx(sympy.FallingFactorial(t, t)).on(a)))
|
|
265
265
|
|
|
266
266
|
|
|
267
|
-
def test_unrecognized_single_qubit_gate_with_matrix():
|
|
267
|
+
def test_unrecognized_single_qubit_gate_with_matrix() -> None:
|
|
268
268
|
a = cirq.NamedQubit('a')
|
|
269
|
-
circuit = cirq.Circuit(cirq.PhasedXPowGate(phase_exponent=0).on(a)
|
|
269
|
+
circuit = cirq.Circuit(cirq.PhasedXPowGate(exponent=0.2731, phase_exponent=0).on(a))
|
|
270
270
|
assert_links_to(
|
|
271
271
|
circuit,
|
|
272
272
|
"""
|
|
@@ -281,7 +281,7 @@ def test_unrecognized_single_qubit_gate_with_matrix():
|
|
|
281
281
|
)
|
|
282
282
|
|
|
283
283
|
|
|
284
|
-
def test_unknown_gate():
|
|
284
|
+
def test_unknown_gate() -> None:
|
|
285
285
|
class UnknownGate(cirq.testing.SingleQubitGate):
|
|
286
286
|
pass
|
|
287
287
|
|
|
@@ -301,7 +301,7 @@ def test_unknown_gate():
|
|
|
301
301
|
)
|
|
302
302
|
|
|
303
303
|
|
|
304
|
-
def test_controlled_gate():
|
|
304
|
+
def test_controlled_gate() -> None:
|
|
305
305
|
a, b, c, d = cirq.LineQubit.range(4)
|
|
306
306
|
circuit = cirq.Circuit(cirq.ControlledGate(cirq.ControlledGate(cirq.CZ)).on(a, d, c, b))
|
|
307
307
|
assert_links_to(
|
|
@@ -336,7 +336,7 @@ def test_controlled_gate():
|
|
|
336
336
|
)
|
|
337
337
|
|
|
338
338
|
|
|
339
|
-
def test_toffoli():
|
|
339
|
+
def test_toffoli() -> None:
|
|
340
340
|
a, b, c, d = cirq.LineQubit.range(4)
|
|
341
341
|
|
|
342
342
|
# Raw.
|
|
@@ -374,7 +374,7 @@ def test_toffoli():
|
|
|
374
374
|
)
|
|
375
375
|
|
|
376
376
|
|
|
377
|
-
def test_fredkin():
|
|
377
|
+
def test_fredkin() -> None:
|
|
378
378
|
a, b, c = cirq.LineQubit.range(3)
|
|
379
379
|
circuit = cirq.Circuit(cirq.FREDKIN(a, b, c))
|
|
380
380
|
assert_links_to(
|
|
@@ -400,7 +400,7 @@ def test_fredkin():
|
|
|
400
400
|
)
|
|
401
401
|
|
|
402
402
|
|
|
403
|
-
def test_ccz():
|
|
403
|
+
def test_ccz() -> None:
|
|
404
404
|
a, b, c, d = cirq.LineQubit.range(4)
|
|
405
405
|
|
|
406
406
|
# Raw.
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
from __future__ import annotations
|
|
16
16
|
|
|
17
17
|
import dataclasses
|
|
18
|
-
from typing import Any, Sequence, TYPE_CHECKING
|
|
18
|
+
from typing import Any, Mapping, Sequence, TYPE_CHECKING
|
|
19
19
|
|
|
20
20
|
from cirq import devices
|
|
21
21
|
from cirq.devices import noise_utils
|
|
@@ -44,7 +44,7 @@ class InsertionNoiseModel(devices.NoiseModel):
|
|
|
44
44
|
with PHYSICAL_GATE_TAG.
|
|
45
45
|
"""
|
|
46
46
|
|
|
47
|
-
ops_added:
|
|
47
|
+
ops_added: Mapping[noise_utils.OpIdentifier, cirq.Operation] = dataclasses.field(
|
|
48
48
|
default_factory=dict
|
|
49
49
|
)
|
|
50
50
|
prepend: bool = False
|
|
@@ -21,8 +21,10 @@ import cirq
|
|
|
21
21
|
import cirq.testing
|
|
22
22
|
from cirq import linalg
|
|
23
23
|
|
|
24
|
+
_DEFAULT_ARRAY = np.asarray([1])
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
|
|
27
|
+
def test_reflection_matrix_pow_consistent_results() -> None:
|
|
26
28
|
x = np.array([[0, 1], [1, 0]])
|
|
27
29
|
sqrt_x = cirq.reflection_matrix_pow(x, 0.5)
|
|
28
30
|
np.testing.assert_allclose(np.dot(sqrt_x, sqrt_x), x, atol=1e-10)
|
|
@@ -42,7 +44,7 @@ def test_reflection_matrix_pow_consistent_results():
|
|
|
42
44
|
np.testing.assert_allclose(np.dot(sqrt_yh, sqrt_yh), yh, atol=1e-10)
|
|
43
45
|
|
|
44
46
|
|
|
45
|
-
def test_reflection_matrix_sign_preference_under_perturbation():
|
|
47
|
+
def test_reflection_matrix_sign_preference_under_perturbation() -> None:
|
|
46
48
|
x = np.array([[0, 1], [1, 0]])
|
|
47
49
|
sqrt_x = np.array([[1, -1j], [-1j, 1]]) * (1 + 1j) / 2
|
|
48
50
|
np.testing.assert_allclose(cirq.reflection_matrix_pow(x, 0.5), sqrt_x, atol=1e-8)
|
|
@@ -57,7 +59,7 @@ def test_reflection_matrix_sign_preference_under_perturbation():
|
|
|
57
59
|
np.testing.assert_allclose(sqrt_px, expected_sqrt_px, atol=1e-10)
|
|
58
60
|
|
|
59
61
|
|
|
60
|
-
def test_match_global_phase():
|
|
62
|
+
def test_match_global_phase() -> None:
|
|
61
63
|
a = np.array([[5, 4], [3, -2]])
|
|
62
64
|
b = np.array([[0.000001, 0], [0, 1j]])
|
|
63
65
|
c, d = cirq.match_global_phase(a, b)
|
|
@@ -65,7 +67,7 @@ def test_match_global_phase():
|
|
|
65
67
|
np.testing.assert_allclose(d, b * -1j, atol=1e-10)
|
|
66
68
|
|
|
67
69
|
|
|
68
|
-
def test_match_global_phase_incompatible_shape():
|
|
70
|
+
def test_match_global_phase_incompatible_shape() -> None:
|
|
69
71
|
a = np.array([1])
|
|
70
72
|
b = np.array([1, 2])
|
|
71
73
|
c, d = cirq.match_global_phase(a, b)
|
|
@@ -87,7 +89,7 @@ def test_match_global_phase_incompatible_shape():
|
|
|
87
89
|
assert np.allclose(d, b)
|
|
88
90
|
|
|
89
91
|
|
|
90
|
-
def test_match_global_phase_zeros():
|
|
92
|
+
def test_match_global_phase_zeros() -> None:
|
|
91
93
|
z = np.array([[0, 0], [0, 0]])
|
|
92
94
|
b = np.array([[1, 1], [1, 1]])
|
|
93
95
|
|
|
@@ -104,7 +106,7 @@ def test_match_global_phase_zeros():
|
|
|
104
106
|
np.testing.assert_allclose(z, z4, atol=1e-10)
|
|
105
107
|
|
|
106
108
|
|
|
107
|
-
def test_match_global_no_float_error_when_axis_aligned():
|
|
109
|
+
def test_match_global_no_float_error_when_axis_aligned() -> None:
|
|
108
110
|
a = np.array([[1, 1.1], [-1.3, np.pi]])
|
|
109
111
|
a2, _ = cirq.match_global_phase(a, a)
|
|
110
112
|
a3, _ = cirq.match_global_phase(a * 1j, a * 1j)
|
|
@@ -117,7 +119,7 @@ def test_match_global_no_float_error_when_axis_aligned():
|
|
|
117
119
|
assert np.all(a5 == a)
|
|
118
120
|
|
|
119
121
|
|
|
120
|
-
def test_targeted_left_multiply_matches_kron_then_dot():
|
|
122
|
+
def test_targeted_left_multiply_matches_kron_then_dot() -> None:
|
|
121
123
|
t = np.array([1, 2, 3, 4, 5, 6, 7, 8])
|
|
122
124
|
m = np.array([[2, 3], [5, 7]])
|
|
123
125
|
i = np.eye(2)
|
|
@@ -161,7 +163,7 @@ def test_targeted_left_multiply_matches_kron_then_dot():
|
|
|
161
163
|
cirq.targeted_left_multiply(left_matrix=m, right_target=common, out=m, target_axes=[2])
|
|
162
164
|
|
|
163
165
|
|
|
164
|
-
def test_targeted_left_multiply_reorders_matrices():
|
|
166
|
+
def test_targeted_left_multiply_reorders_matrices() -> None:
|
|
165
167
|
t = np.eye(4).reshape((2, 2, 2, 2))
|
|
166
168
|
m = np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0]).reshape((2, 2, 2, 2))
|
|
167
169
|
|
|
@@ -176,7 +178,7 @@ def test_targeted_left_multiply_reorders_matrices():
|
|
|
176
178
|
)
|
|
177
179
|
|
|
178
180
|
|
|
179
|
-
def test_targeted_left_multiply_out():
|
|
181
|
+
def test_targeted_left_multiply_out() -> None:
|
|
180
182
|
left = np.array([[2, 3], [5, 7]])
|
|
181
183
|
right = np.array([1, -1])
|
|
182
184
|
out = np.zeros(2)
|
|
@@ -188,7 +190,7 @@ def test_targeted_left_multiply_out():
|
|
|
188
190
|
np.testing.assert_allclose(result, np.array([-1, -2]), atol=1e-8)
|
|
189
191
|
|
|
190
192
|
|
|
191
|
-
def test_targeted_conjugate_simple():
|
|
193
|
+
def test_targeted_conjugate_simple() -> None:
|
|
192
194
|
a = np.array([[0, 1j], [0, 0]])
|
|
193
195
|
# yapf: disable
|
|
194
196
|
b = np.reshape(
|
|
@@ -212,7 +214,7 @@ def test_targeted_conjugate_simple():
|
|
|
212
214
|
np.testing.assert_almost_equal(result, expected)
|
|
213
215
|
|
|
214
216
|
|
|
215
|
-
def test_targeted_conjugate():
|
|
217
|
+
def test_targeted_conjugate() -> None:
|
|
216
218
|
a = np.reshape([0, 1, 2j, 3j], (2, 2))
|
|
217
219
|
b = np.reshape(np.arange(16), (2,) * 4)
|
|
218
220
|
result = cirq.targeted_conjugate_about(a, b, [0])
|
|
@@ -224,7 +226,7 @@ def test_targeted_conjugate():
|
|
|
224
226
|
np.testing.assert_almost_equal(result, expected)
|
|
225
227
|
|
|
226
228
|
|
|
227
|
-
def test_targeted_conjugate_multiple_indices():
|
|
229
|
+
def test_targeted_conjugate_multiple_indices() -> None:
|
|
228
230
|
a = np.reshape(np.arange(16) + 1j, (2, 2, 2, 2))
|
|
229
231
|
b = np.reshape(np.arange(16), (2,) * 4)
|
|
230
232
|
result = cirq.targeted_conjugate_about(a, b, [0, 1])
|
|
@@ -232,7 +234,7 @@ def test_targeted_conjugate_multiple_indices():
|
|
|
232
234
|
np.testing.assert_almost_equal(result, expected)
|
|
233
235
|
|
|
234
236
|
|
|
235
|
-
def test_targeted_conjugate_multiple_indices_flip_order():
|
|
237
|
+
def test_targeted_conjugate_multiple_indices_flip_order() -> None:
|
|
236
238
|
a = np.reshape(np.arange(16) + 1j, (2, 2, 2, 2))
|
|
237
239
|
b = np.reshape(np.arange(16), (2,) * 4)
|
|
238
240
|
result = cirq.targeted_conjugate_about(a, b, [1, 0], [3, 2])
|
|
@@ -240,7 +242,7 @@ def test_targeted_conjugate_multiple_indices_flip_order():
|
|
|
240
242
|
np.testing.assert_almost_equal(result, expected)
|
|
241
243
|
|
|
242
244
|
|
|
243
|
-
def test_targeted_conjugate_out():
|
|
245
|
+
def test_targeted_conjugate_out() -> None:
|
|
244
246
|
a = np.reshape([0, 1, 2j, 3j], (2, 2))
|
|
245
247
|
b = np.reshape(np.arange(16), (2,) * 4)
|
|
246
248
|
buffer = np.empty((2,) * 4, dtype=a.dtype)
|
|
@@ -251,7 +253,7 @@ def test_targeted_conjugate_out():
|
|
|
251
253
|
np.testing.assert_almost_equal(result, expected)
|
|
252
254
|
|
|
253
255
|
|
|
254
|
-
def test_apply_matrix_to_slices():
|
|
256
|
+
def test_apply_matrix_to_slices() -> None:
|
|
255
257
|
# Output is input.
|
|
256
258
|
with pytest.raises(ValueError, match='out'):
|
|
257
259
|
target = np.eye(2)
|
|
@@ -303,7 +305,7 @@ def test_apply_matrix_to_slices():
|
|
|
303
305
|
np.testing.assert_allclose(actual, np.array([1, 13, 31, 4]))
|
|
304
306
|
|
|
305
307
|
|
|
306
|
-
def test_partial_trace():
|
|
308
|
+
def test_partial_trace() -> None:
|
|
307
309
|
a = np.reshape(np.arange(4), (2, 2))
|
|
308
310
|
b = np.reshape(np.arange(9) + 4, (3, 3))
|
|
309
311
|
c = np.reshape(np.arange(16) + 13, (4, 4))
|
|
@@ -337,21 +339,21 @@ def test_partial_trace():
|
|
|
337
339
|
)
|
|
338
340
|
|
|
339
341
|
|
|
340
|
-
def test_partial_trace_non_kron():
|
|
342
|
+
def test_partial_trace_non_kron() -> None:
|
|
341
343
|
tensor = np.zeros((2, 2, 2, 2))
|
|
342
344
|
tensor[0, 0, 0, 0] = 1
|
|
343
345
|
tensor[1, 1, 1, 1] = 4
|
|
344
346
|
np.testing.assert_almost_equal(cirq.partial_trace(tensor, [0]), np.array([[1, 0], [0, 4]]))
|
|
345
347
|
|
|
346
348
|
|
|
347
|
-
def test_partial_trace_invalid_inputs():
|
|
349
|
+
def test_partial_trace_invalid_inputs() -> None:
|
|
348
350
|
with pytest.raises(ValueError, match='2, 3, 2, 2'):
|
|
349
351
|
cirq.partial_trace(np.reshape(np.arange(2 * 3 * 2 * 2), (2, 3, 2, 2)), [1])
|
|
350
352
|
with pytest.raises(ValueError, match='2'):
|
|
351
353
|
cirq.partial_trace(np.reshape(np.arange(2 * 2 * 2 * 2), (2,) * 4), [2])
|
|
352
354
|
|
|
353
355
|
|
|
354
|
-
def test_sub_state_vector():
|
|
356
|
+
def test_sub_state_vector() -> None:
|
|
355
357
|
a = np.arange(4) / np.linalg.norm(np.arange(4))
|
|
356
358
|
b = (np.arange(8) + 3) / np.linalg.norm(np.arange(8) + 3)
|
|
357
359
|
c = (np.arange(16) + 1) / np.linalg.norm(np.arange(16) + 1)
|
|
@@ -384,40 +386,66 @@ def test_sub_state_vector():
|
|
|
384
386
|
)
|
|
385
387
|
|
|
386
388
|
# Reject factoring for very tight tolerance.
|
|
387
|
-
assert
|
|
388
|
-
|
|
389
|
-
|
|
389
|
+
assert (
|
|
390
|
+
cirq.sub_state_vector(state, [0, 1], default=_DEFAULT_ARRAY, atol=1e-16) is _DEFAULT_ARRAY
|
|
391
|
+
)
|
|
392
|
+
assert (
|
|
393
|
+
cirq.sub_state_vector(state, [2, 3, 4], default=_DEFAULT_ARRAY, atol=1e-16)
|
|
394
|
+
is _DEFAULT_ARRAY
|
|
395
|
+
)
|
|
396
|
+
assert (
|
|
397
|
+
cirq.sub_state_vector(state, [5, 6, 7, 8], default=_DEFAULT_ARRAY, atol=1e-16)
|
|
398
|
+
is _DEFAULT_ARRAY
|
|
399
|
+
)
|
|
390
400
|
|
|
391
401
|
# Permit invalid factoring for loose tolerance.
|
|
392
402
|
for q1 in range(9):
|
|
393
|
-
assert
|
|
403
|
+
assert (
|
|
404
|
+
cirq.sub_state_vector(state, [q1], default=_DEFAULT_ARRAY, atol=1) is not _DEFAULT_ARRAY
|
|
405
|
+
)
|
|
394
406
|
|
|
395
407
|
|
|
396
|
-
def test_sub_state_vector_bad_subset():
|
|
408
|
+
def test_sub_state_vector_bad_subset() -> None:
|
|
397
409
|
a = cirq.testing.random_superposition(4)
|
|
398
410
|
b = cirq.testing.random_superposition(8)
|
|
399
411
|
state = np.kron(a, b).reshape((2, 2, 2, 2, 2))
|
|
400
412
|
|
|
401
413
|
for q1 in range(5):
|
|
402
|
-
assert
|
|
414
|
+
assert (
|
|
415
|
+
cirq.sub_state_vector(state, [q1], default=_DEFAULT_ARRAY, atol=1e-8) is _DEFAULT_ARRAY
|
|
416
|
+
)
|
|
403
417
|
for q1 in range(2):
|
|
404
418
|
for q2 in range(2, 5):
|
|
405
|
-
assert
|
|
419
|
+
assert (
|
|
420
|
+
cirq.sub_state_vector(state, [q1, q2], default=_DEFAULT_ARRAY, atol=1e-8)
|
|
421
|
+
is _DEFAULT_ARRAY
|
|
422
|
+
)
|
|
406
423
|
for q3 in range(2, 5):
|
|
407
|
-
assert
|
|
424
|
+
assert (
|
|
425
|
+
cirq.sub_state_vector(state, [0, 1, q3], default=_DEFAULT_ARRAY, atol=1e-8)
|
|
426
|
+
is _DEFAULT_ARRAY
|
|
427
|
+
)
|
|
408
428
|
for q4 in range(2):
|
|
409
|
-
assert
|
|
429
|
+
assert (
|
|
430
|
+
cirq.sub_state_vector(state, [2, 3, 4, q4], default=_DEFAULT_ARRAY, atol=1e-8)
|
|
431
|
+
is _DEFAULT_ARRAY
|
|
432
|
+
)
|
|
410
433
|
|
|
411
434
|
|
|
412
|
-
def test_sub_state_vector_non_kron():
|
|
435
|
+
def test_sub_state_vector_non_kron() -> None:
|
|
413
436
|
a = np.array([1, 0, 0, 0, 0, 0, 0, 1]) / np.sqrt(2)
|
|
414
437
|
b = np.array([1, 1]) / np.sqrt(2)
|
|
415
438
|
state = np.kron(a, b).reshape((2, 2, 2, 2))
|
|
416
439
|
|
|
417
440
|
for q1 in [0, 1, 2]:
|
|
418
|
-
assert
|
|
441
|
+
assert (
|
|
442
|
+
cirq.sub_state_vector(state, [q1], default=_DEFAULT_ARRAY, atol=1e-8) is _DEFAULT_ARRAY
|
|
443
|
+
)
|
|
419
444
|
for q1 in [0, 1, 2]:
|
|
420
|
-
assert
|
|
445
|
+
assert (
|
|
446
|
+
cirq.sub_state_vector(state, [q1, 3], default=_DEFAULT_ARRAY, atol=1e-8)
|
|
447
|
+
is _DEFAULT_ARRAY
|
|
448
|
+
)
|
|
421
449
|
|
|
422
450
|
with pytest.raises(ValueError, match='factored'):
|
|
423
451
|
_ = cirq.sub_state_vector(a, [0], atol=1e-8)
|
|
@@ -425,7 +453,7 @@ def test_sub_state_vector_non_kron():
|
|
|
425
453
|
assert cirq.equal_up_to_global_phase(cirq.sub_state_vector(state, [3]), b, atol=1e-8)
|
|
426
454
|
|
|
427
455
|
|
|
428
|
-
def test_sub_state_vector_invalid_inputs():
|
|
456
|
+
def test_sub_state_vector_invalid_inputs() -> None:
|
|
429
457
|
|
|
430
458
|
# State cannot be expressed as a separable pure state.
|
|
431
459
|
with pytest.raises(ValueError, match='7'):
|
|
@@ -452,7 +480,7 @@ def test_sub_state_vector_invalid_inputs():
|
|
|
452
480
|
cirq.sub_state_vector(state, [0, 1, 2], atol=1e-8)
|
|
453
481
|
|
|
454
482
|
|
|
455
|
-
def test_partial_trace_of_state_vector_as_mixture_invalid_input():
|
|
483
|
+
def test_partial_trace_of_state_vector_as_mixture_invalid_input() -> None:
|
|
456
484
|
|
|
457
485
|
with pytest.raises(ValueError, match='7'):
|
|
458
486
|
cirq.partial_trace_of_state_vector_as_mixture(np.arange(7), [1, 2], atol=1e-8)
|
|
@@ -471,7 +499,7 @@ def test_partial_trace_of_state_vector_as_mixture_invalid_input():
|
|
|
471
499
|
cirq.partial_trace_of_state_vector_as_mixture(state, [0, 1, 2], atol=1e-8)
|
|
472
500
|
|
|
473
501
|
|
|
474
|
-
def mixtures_equal(m1, m2, atol=1e-7):
|
|
502
|
+
def mixtures_equal(m1, m2, atol=1e-7) -> bool:
|
|
475
503
|
for (p1, v1), (p2, v2) in zip(m1, m2):
|
|
476
504
|
if not (
|
|
477
505
|
cirq.approx_eq(p1, p2, atol=atol) and cirq.equal_up_to_global_phase(v1, v2, atol=atol)
|
|
@@ -480,7 +508,7 @@ def mixtures_equal(m1, m2, atol=1e-7):
|
|
|
480
508
|
return True
|
|
481
509
|
|
|
482
510
|
|
|
483
|
-
def test_partial_trace_of_state_vector_as_mixture_pure_result():
|
|
511
|
+
def test_partial_trace_of_state_vector_as_mixture_pure_result() -> None:
|
|
484
512
|
a = cirq.testing.random_superposition(4)
|
|
485
513
|
b = cirq.testing.random_superposition(8)
|
|
486
514
|
c = cirq.testing.random_superposition(16)
|
|
@@ -534,7 +562,7 @@ def test_partial_trace_of_state_vector_as_mixture_pure_result():
|
|
|
534
562
|
)
|
|
535
563
|
|
|
536
564
|
|
|
537
|
-
def test_partial_trace_of_state_vector_as_mixture_pure_result_qudits():
|
|
565
|
+
def test_partial_trace_of_state_vector_as_mixture_pure_result_qudits() -> None:
|
|
538
566
|
a = cirq.testing.random_superposition(2)
|
|
539
567
|
b = cirq.testing.random_superposition(3)
|
|
540
568
|
c = cirq.testing.random_superposition(4)
|
|
@@ -563,7 +591,7 @@ def test_partial_trace_of_state_vector_as_mixture_pure_result_qudits():
|
|
|
563
591
|
)
|
|
564
592
|
|
|
565
593
|
|
|
566
|
-
def test_partial_trace_of_state_vector_as_mixture_mixed_result():
|
|
594
|
+
def test_partial_trace_of_state_vector_as_mixture_mixed_result() -> None:
|
|
567
595
|
state = np.array([1, 0, 0, 1]) / np.sqrt(2)
|
|
568
596
|
truth = ((0.5, np.array([1, 0])), (0.5, np.array([0, 1])))
|
|
569
597
|
for q1 in [0, 1]:
|
|
@@ -591,7 +619,7 @@ def test_partial_trace_of_state_vector_as_mixture_mixed_result():
|
|
|
591
619
|
assert mixtures_equal(mixture, truth)
|
|
592
620
|
|
|
593
621
|
|
|
594
|
-
def test_partial_trace_of_state_vector_as_mixture_mixed_result_qudits():
|
|
622
|
+
def test_partial_trace_of_state_vector_as_mixture_mixed_result_qudits() -> None:
|
|
595
623
|
state = np.array([[1, 0, 0], [0, 0, 0], [0, 0, 1]]) / np.sqrt(2)
|
|
596
624
|
truth = ((0.5, np.array([1, 0, 0])), (0.5, np.array([0, 0, 1])))
|
|
597
625
|
for q1 in [0, 1]:
|
|
@@ -599,14 +627,14 @@ def test_partial_trace_of_state_vector_as_mixture_mixed_result_qudits():
|
|
|
599
627
|
assert mixtures_equal(mixture, truth)
|
|
600
628
|
|
|
601
629
|
|
|
602
|
-
def test_to_special():
|
|
630
|
+
def test_to_special() -> None:
|
|
603
631
|
u = cirq.testing.random_unitary(4)
|
|
604
632
|
su = cirq.to_special(u)
|
|
605
633
|
assert not cirq.is_special_unitary(u)
|
|
606
634
|
assert cirq.is_special_unitary(su)
|
|
607
635
|
|
|
608
636
|
|
|
609
|
-
def test_default_tolerance():
|
|
637
|
+
def test_default_tolerance() -> None:
|
|
610
638
|
a, b = cirq.LineQubit.range(2)
|
|
611
639
|
final_state_vector = (
|
|
612
640
|
cirq.Simulator()
|
|
@@ -620,7 +648,7 @@ def test_default_tolerance():
|
|
|
620
648
|
|
|
621
649
|
@pytest.mark.parametrize('state_1', [0, 1])
|
|
622
650
|
@pytest.mark.parametrize('state_2', [0, 1])
|
|
623
|
-
def test_factor_state_vector(state_1: int, state_2: int):
|
|
651
|
+
def test_factor_state_vector(state_1: int, state_2: int) -> None:
|
|
624
652
|
# Kron two state vectors and apply a phase. Factoring should produce the expected results.
|
|
625
653
|
n = 12
|
|
626
654
|
for i in range(n):
|
|
@@ -638,7 +666,7 @@ def test_factor_state_vector(state_1: int, state_2: int):
|
|
|
638
666
|
|
|
639
667
|
|
|
640
668
|
@pytest.mark.parametrize('num_dimensions', [*range(1, 7)])
|
|
641
|
-
def test_transpose_flattened_array(num_dimensions):
|
|
669
|
+
def test_transpose_flattened_array(num_dimensions) -> None:
|
|
642
670
|
np.random.seed(0)
|
|
643
671
|
for _ in range(10):
|
|
644
672
|
shape = np.random.randint(1, 5, (num_dimensions,)).tolist()
|
|
@@ -658,7 +686,7 @@ def test_can_numpy_support_shape(shape: tuple[int, ...], result: bool) -> None:
|
|
|
658
686
|
|
|
659
687
|
|
|
660
688
|
@pytest.mark.parametrize('coeff', [1, 1j, -1, -1j, 1j**0.5, 1j**0.3])
|
|
661
|
-
def test_phase_delta(coeff):
|
|
689
|
+
def test_phase_delta(coeff) -> None:
|
|
662
690
|
u1 = cirq.testing.random_unitary(4)
|
|
663
691
|
u2 = u1 * coeff
|
|
664
692
|
np.testing.assert_almost_equal(linalg.phase_delta(u1, u2), coeff)
|
|
@@ -56,7 +56,7 @@ import cirq.ops.boolean_hamiltonian as bh
|
|
|
56
56
|
'(x2 | x1) ^ x0',
|
|
57
57
|
],
|
|
58
58
|
)
|
|
59
|
-
def test_circuit(boolean_str):
|
|
59
|
+
def test_circuit(boolean_str) -> None:
|
|
60
60
|
boolean_expr = sympy_parser.parse_expr(boolean_str)
|
|
61
61
|
var_names = cirq.parameter_names(boolean_expr)
|
|
62
62
|
qubits = [cirq.NamedQubit(name) for name in var_names]
|
|
@@ -76,7 +76,7 @@ def test_circuit(boolean_str):
|
|
|
76
76
|
circuit.append(cirq.H.on_each(*qubits))
|
|
77
77
|
|
|
78
78
|
hamiltonian_gate = cirq.BooleanHamiltonianGate(
|
|
79
|
-
|
|
79
|
+
[q.name for q in qubits], [boolean_str], 0.1 * math.pi
|
|
80
80
|
)
|
|
81
81
|
|
|
82
82
|
assert hamiltonian_gate.num_qubits() == n
|
|
@@ -90,7 +90,7 @@ def test_circuit(boolean_str):
|
|
|
90
90
|
np.testing.assert_array_equal(actual, expected)
|
|
91
91
|
|
|
92
92
|
|
|
93
|
-
def test_gate_with_custom_names():
|
|
93
|
+
def test_gate_with_custom_names() -> None:
|
|
94
94
|
q0, q1, q2, q3 = cirq.LineQubit.range(4)
|
|
95
95
|
gate = cirq.BooleanHamiltonianGate(['a', 'b'], ['a'], 0.1)
|
|
96
96
|
assert cirq.decompose(gate.on(q0, q1)) == [cirq.Rz(rads=-0.05).on(q0)]
|
|
@@ -104,7 +104,7 @@ def test_gate_with_custom_names():
|
|
|
104
104
|
gate.on(q0, cirq.LineQid(1, 3))
|
|
105
105
|
|
|
106
106
|
|
|
107
|
-
def test_gate_consistent():
|
|
107
|
+
def test_gate_consistent() -> None:
|
|
108
108
|
gate = cirq.BooleanHamiltonianGate(['a', 'b'], ['a'], 0.1)
|
|
109
109
|
op = gate.on(*cirq.LineQubit.range(2))
|
|
110
110
|
cirq.testing.assert_implements_consistent_protocols(gate)
|
|
@@ -119,7 +119,7 @@ def test_gate_consistent():
|
|
|
119
119
|
(3, [(), (0,), (0, 1), (1,), (1, 2), (0, 1, 2), (0, 2), (2,)]),
|
|
120
120
|
],
|
|
121
121
|
)
|
|
122
|
-
def test_gray_code_sorting(n_bits, expected_hs):
|
|
122
|
+
def test_gray_code_sorting(n_bits, expected_hs) -> None:
|
|
123
123
|
hs_template = []
|
|
124
124
|
for x in range(2**n_bits):
|
|
125
125
|
h = []
|
|
@@ -142,7 +142,7 @@ def test_gray_code_sorting(n_bits, expected_hs):
|
|
|
142
142
|
@pytest.mark.parametrize(
|
|
143
143
|
'seq_a,seq_b,expected', [((), (), 0), ((), (0,), -1), ((0,), (), 1), ((0,), (0,), 0)]
|
|
144
144
|
)
|
|
145
|
-
def test_gray_code_comparison(seq_a, seq_b, expected):
|
|
145
|
+
def test_gray_code_comparison(seq_a, seq_b, expected) -> None:
|
|
146
146
|
assert bh._gray_code_comparator(seq_a, seq_b) == expected
|
|
147
147
|
|
|
148
148
|
|
|
@@ -173,7 +173,7 @@ def test_gray_code_comparison(seq_a, seq_b, expected):
|
|
|
173
173
|
)
|
|
174
174
|
def test_simplify_commuting_cnots(
|
|
175
175
|
input_cnots, input_flip_control_and_target, expected_simplified, expected_output_cnots
|
|
176
|
-
):
|
|
176
|
+
) -> None:
|
|
177
177
|
actual_simplified, actual_output_cnots = bh._simplify_commuting_cnots(
|
|
178
178
|
input_cnots, input_flip_control_and_target
|
|
179
179
|
)
|
|
@@ -206,7 +206,7 @@ def test_simplify_commuting_cnots(
|
|
|
206
206
|
)
|
|
207
207
|
def test_simplify_cnots_triplets(
|
|
208
208
|
input_cnots, input_flip_control_and_target, expected_simplified, expected_output_cnots
|
|
209
|
-
):
|
|
209
|
+
) -> None:
|
|
210
210
|
actual_simplified, actual_output_cnots = bh._simplify_cnots_triplets(
|
|
211
211
|
input_cnots, input_flip_control_and_target
|
|
212
212
|
)
|