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/control_values_test.py
CHANGED
|
@@ -19,7 +19,7 @@ import pytest
|
|
|
19
19
|
import cirq
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
def test_init_sum_of_products_raises():
|
|
22
|
+
def test_init_sum_of_products_raises() -> None:
|
|
23
23
|
# data shouldn't be empty.
|
|
24
24
|
with pytest.raises(ValueError):
|
|
25
25
|
_ = cirq.SumOfProducts([])
|
|
@@ -29,7 +29,7 @@ def test_init_sum_of_products_raises():
|
|
|
29
29
|
_ = cirq.SumOfProducts([[1], [1, 0]])
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
def test_init_product_of_sums():
|
|
32
|
+
def test_init_product_of_sums() -> None:
|
|
33
33
|
eq = cirq.testing.EqualsTester()
|
|
34
34
|
# 0. Trivial case of 1 control and 1 qubit.
|
|
35
35
|
eq.add_equality_group(cirq.ProductOfSums([1]), cirq.ProductOfSums(((1,),)))
|
|
@@ -59,7 +59,7 @@ def test_init_product_of_sums():
|
|
|
59
59
|
eq.add_equality_group([(1, 2), (0, 1)])
|
|
60
60
|
|
|
61
61
|
|
|
62
|
-
def test_init_sum_of_products():
|
|
62
|
+
def test_init_sum_of_products() -> None:
|
|
63
63
|
eq = cirq.testing.EqualsTester()
|
|
64
64
|
# 0. Trivial case of 1 control and 1 qubit
|
|
65
65
|
eq.add_equality_group(cirq.SumOfProducts([[1]]), cirq.SumOfProducts(((1,),)))
|
|
@@ -90,7 +90,7 @@ def test_init_sum_of_products():
|
|
|
90
90
|
eq.add_equality_group(cirq.SumOfProducts([(1, 0), (2, 0), (1, 1), (2, 1)]))
|
|
91
91
|
|
|
92
92
|
|
|
93
|
-
def test_equality_across_types():
|
|
93
|
+
def test_equality_across_types() -> None:
|
|
94
94
|
eq = cirq.testing.EqualsTester()
|
|
95
95
|
# Trivial case of 1 control and 1 qubit
|
|
96
96
|
eq.add_equality_group(
|
|
@@ -124,7 +124,7 @@ def test_equality_across_types():
|
|
|
124
124
|
eq.add_equality_group(cirq.ProductOfSums([(0, 1), (1, 0)])) # or control
|
|
125
125
|
|
|
126
126
|
|
|
127
|
-
def test_and_operation():
|
|
127
|
+
def test_and_operation() -> None:
|
|
128
128
|
eq = cirq.testing.EqualsTester()
|
|
129
129
|
|
|
130
130
|
eq.add_equality_group(
|
|
@@ -171,12 +171,12 @@ def test_and_operation():
|
|
|
171
171
|
[cirq.SumOfProducts([(0, 0), (1, 1)]), cirq.ProductOfSums([0, 1, 2]), cirq.SumOfProducts],
|
|
172
172
|
],
|
|
173
173
|
)
|
|
174
|
-
def test_and_operation_adds_qubits(cv1, cv2, expected_type):
|
|
174
|
+
def test_and_operation_adds_qubits(cv1, cv2, expected_type) -> None:
|
|
175
175
|
assert isinstance(cv1 & cv2, expected_type)
|
|
176
176
|
assert cirq.num_qubits(cv1 & cv2) == cirq.num_qubits(cv1) + cirq.num_qubits(cv2)
|
|
177
177
|
|
|
178
178
|
|
|
179
|
-
def test_or_operation():
|
|
179
|
+
def test_or_operation() -> None:
|
|
180
180
|
eq = cirq.testing.EqualsTester()
|
|
181
181
|
|
|
182
182
|
eq.add_equality_group(
|
|
@@ -214,7 +214,7 @@ def test_or_operation():
|
|
|
214
214
|
[cirq.SumOfProducts([[0], [1], [2]]), cirq.SumOfProducts([[1, 0]]), None],
|
|
215
215
|
],
|
|
216
216
|
)
|
|
217
|
-
def test_or_operation_acts_on_same_qubits(cv1, cv2, expected_type):
|
|
217
|
+
def test_or_operation_acts_on_same_qubits(cv1, cv2, expected_type) -> None:
|
|
218
218
|
if cirq.num_qubits(cv1) == cirq.num_qubits(cv2):
|
|
219
219
|
assert cirq.num_qubits(cv1 | cv2) == cirq.num_qubits(cv1)
|
|
220
220
|
assert expected_type is not None
|
|
@@ -226,17 +226,17 @@ def test_or_operation_acts_on_same_qubits(cv1, cv2, expected_type):
|
|
|
226
226
|
|
|
227
227
|
|
|
228
228
|
@pytest.mark.parametrize('data', [((1,),), ((0, 1), (1,)), [(0, 1), (1, 0)]])
|
|
229
|
-
def test_product_of_sums_repr(data):
|
|
229
|
+
def test_product_of_sums_repr(data) -> None:
|
|
230
230
|
cirq.testing.assert_equivalent_repr(cirq.ProductOfSums(data))
|
|
231
231
|
|
|
232
232
|
|
|
233
233
|
@pytest.mark.parametrize('data', [((1,),), ((0, 1),), ((0, 0), (0, 1), (1, 0))])
|
|
234
|
-
def test_sum_of_products_repr(data):
|
|
234
|
+
def test_sum_of_products_repr(data) -> None:
|
|
235
235
|
cirq.testing.assert_equivalent_repr(cirq.SumOfProducts(data))
|
|
236
236
|
cirq.testing.assert_equivalent_repr(cirq.SumOfProducts(data, name="CustomName"))
|
|
237
237
|
|
|
238
238
|
|
|
239
|
-
def test_sum_of_products_validate():
|
|
239
|
+
def test_sum_of_products_validate() -> None:
|
|
240
240
|
control_val = cirq.SumOfProducts(((1, 2), (0, 1)))
|
|
241
241
|
|
|
242
242
|
control_val.validate([2, 3])
|
|
@@ -250,7 +250,7 @@ def test_sum_of_products_validate():
|
|
|
250
250
|
|
|
251
251
|
|
|
252
252
|
@pytest.mark.parametrize('data', [((1,),), ((0, 1),), ((0, 0), (0, 1), (1, 0))])
|
|
253
|
-
def test_sum_of_products_num_qubits(data):
|
|
253
|
+
def test_sum_of_products_num_qubits(data) -> None:
|
|
254
254
|
assert cirq.num_qubits(cirq.SumOfProducts(data)) == len(data[0])
|
|
255
255
|
|
|
256
256
|
|
|
@@ -263,7 +263,7 @@ def test_sum_of_products_num_qubits(data):
|
|
|
263
263
|
[((1, 1, 1, 1),), True],
|
|
264
264
|
],
|
|
265
265
|
)
|
|
266
|
-
def test_sum_of_products_is_trivial(data, is_trivial):
|
|
266
|
+
def test_sum_of_products_is_trivial(data, is_trivial) -> None:
|
|
267
267
|
assert cirq.SumOfProducts(data).is_trivial == is_trivial
|
|
268
268
|
|
|
269
269
|
|
|
@@ -271,16 +271,16 @@ def test_sum_of_products_is_trivial(data, is_trivial):
|
|
|
271
271
|
'data, is_trivial',
|
|
272
272
|
[[((1,),), True], [((0, 1),), False], [([2], [1], [2]), False], [([1], [1], [1], [1]), True]],
|
|
273
273
|
)
|
|
274
|
-
def test_product_of_sum_is_trivial(data, is_trivial):
|
|
274
|
+
def test_product_of_sum_is_trivial(data, is_trivial) -> None:
|
|
275
275
|
assert cirq.ProductOfSums(data).is_trivial == is_trivial
|
|
276
276
|
|
|
277
277
|
|
|
278
|
-
def test_product_of_sums_str():
|
|
278
|
+
def test_product_of_sums_str() -> None:
|
|
279
279
|
c = cirq.ProductOfSums([(0, 1), 1, 0, (0, 2)])
|
|
280
280
|
assert str(c) == 'C01C1C0C02'
|
|
281
281
|
|
|
282
282
|
|
|
283
|
-
def test_sum_of_products_str():
|
|
283
|
+
def test_sum_of_products_str() -> None:
|
|
284
284
|
c = cirq.SumOfProducts(((1, 0), (0, 1)))
|
|
285
285
|
assert str(c) == 'C_01_10'
|
|
286
286
|
|
|
@@ -288,7 +288,7 @@ def test_sum_of_products_str():
|
|
|
288
288
|
assert str(c) == 'C_xor'
|
|
289
289
|
|
|
290
290
|
|
|
291
|
-
def test_control_values_diagrams():
|
|
291
|
+
def test_control_values_diagrams() -> None:
|
|
292
292
|
q = cirq.LineQubit.range(3)
|
|
293
293
|
ccx = cirq.X(q[0]).controlled_by(*q[1:])
|
|
294
294
|
ccx_sop = cirq.X(q[0]).controlled_by(*q[1:], control_values=cirq.SumOfProducts([[1, 1]]))
|
cirq/ops/controlled_gate_test.py
CHANGED
|
@@ -112,13 +112,13 @@ C_02_20H = cirq.ControlledGate(
|
|
|
112
112
|
C2Restricted = cirq.ControlledGate(RestrictedGate(), control_values=[2], control_qid_shape=(3,))
|
|
113
113
|
|
|
114
114
|
|
|
115
|
-
def test_init():
|
|
115
|
+
def test_init() -> None:
|
|
116
116
|
gate = cirq.ControlledGate(cirq.Z)
|
|
117
117
|
assert gate.sub_gate is cirq.Z
|
|
118
118
|
assert gate.num_qubits() == 2
|
|
119
119
|
|
|
120
120
|
|
|
121
|
-
def test_init2():
|
|
121
|
+
def test_init2() -> None:
|
|
122
122
|
with pytest.raises(ValueError, match=r'cirq\.num_qubits\(control_values\) != num_controls'):
|
|
123
123
|
cirq.ControlledGate(cirq.Z, num_controls=1, control_values=(1, 0))
|
|
124
124
|
with pytest.raises(ValueError, match=r'len\(control_qid_shape\) != num_controls'):
|
|
@@ -188,7 +188,7 @@ def test_init2():
|
|
|
188
188
|
assert cirq.qid_shape(gate) == (3, 3, 2)
|
|
189
189
|
|
|
190
190
|
|
|
191
|
-
def test_validate_args():
|
|
191
|
+
def test_validate_args() -> None:
|
|
192
192
|
a = cirq.NamedQubit('a')
|
|
193
193
|
b = cirq.NamedQubit('b')
|
|
194
194
|
c = cirq.NamedQubit('c')
|
|
@@ -234,7 +234,7 @@ def test_validate_args():
|
|
|
234
234
|
_ = C2C2H.on(q3, p3, a)
|
|
235
235
|
|
|
236
236
|
|
|
237
|
-
def test_eq():
|
|
237
|
+
def test_eq() -> None:
|
|
238
238
|
eq = cirq.testing.EqualsTester()
|
|
239
239
|
eq.add_equality_group(CY, cirq.ControlledGate(cirq.Y))
|
|
240
240
|
eq.add_equality_group(CCH)
|
|
@@ -272,7 +272,7 @@ def test_eq():
|
|
|
272
272
|
np.testing.assert_allclose(cirq.unitary(item), cirq.unitary(group[0]))
|
|
273
273
|
|
|
274
274
|
|
|
275
|
-
def test_control():
|
|
275
|
+
def test_control() -> None:
|
|
276
276
|
class G(cirq.testing.SingleQubitGate):
|
|
277
277
|
def _has_mixture_(self):
|
|
278
278
|
return True
|
|
@@ -301,7 +301,7 @@ def test_control():
|
|
|
301
301
|
eq.add_equality_group(
|
|
302
302
|
cirq.ControlledGate(g, num_controls=2),
|
|
303
303
|
g.controlled(control_values=[1, 1]),
|
|
304
|
-
g.controlled(control_qid_shape=
|
|
304
|
+
g.controlled(control_qid_shape=(2, 2)),
|
|
305
305
|
g.controlled(num_controls=2),
|
|
306
306
|
g.controlled().controlled(),
|
|
307
307
|
g.controlled(control_values=cirq.SumOfProducts([[1, 1]])),
|
|
@@ -314,14 +314,14 @@ def test_control():
|
|
|
314
314
|
)
|
|
315
315
|
eq.add_equality_group(g.controlled(control_values=[0]).controlled(control_values=[1]))
|
|
316
316
|
eq.add_equality_group(
|
|
317
|
-
cirq.ControlledGate(g, control_qid_shape=
|
|
318
|
-
g.controlled(control_qid_shape=
|
|
319
|
-
g.controlled(control_qid_shape=
|
|
317
|
+
cirq.ControlledGate(g, control_qid_shape=(4, 3)),
|
|
318
|
+
g.controlled(control_qid_shape=(4, 3)),
|
|
319
|
+
g.controlled(control_qid_shape=(3,)).controlled(control_qid_shape=(4,)),
|
|
320
320
|
)
|
|
321
|
-
eq.add_equality_group(g.controlled(control_qid_shape=
|
|
321
|
+
eq.add_equality_group(g.controlled(control_qid_shape=(4,)).controlled(control_qid_shape=(3,)))
|
|
322
322
|
|
|
323
323
|
|
|
324
|
-
def test_unitary():
|
|
324
|
+
def test_unitary() -> None:
|
|
325
325
|
cxa = cirq.ControlledGate(cirq.X ** sympy.Symbol('a'))
|
|
326
326
|
assert not cirq.has_unitary(cxa)
|
|
327
327
|
assert cirq.unitary(cxa, None) is None
|
|
@@ -426,7 +426,7 @@ def test_unitary():
|
|
|
426
426
|
(C0C_xorH, False),
|
|
427
427
|
],
|
|
428
428
|
)
|
|
429
|
-
def test_controlled_gate_is_consistent(gate: cirq.Gate, should_decompose_to_target):
|
|
429
|
+
def test_controlled_gate_is_consistent(gate: cirq.Gate, should_decompose_to_target) -> None:
|
|
430
430
|
_test_controlled_gate_is_consistent(gate, should_decompose_to_target)
|
|
431
431
|
|
|
432
432
|
|
|
@@ -466,7 +466,7 @@ def test_nontrivial_controlled_gate_is_consistent(
|
|
|
466
466
|
control_qid_shape: Sequence[int],
|
|
467
467
|
control_values: Any,
|
|
468
468
|
should_decompose_to_target: bool,
|
|
469
|
-
):
|
|
469
|
+
) -> None:
|
|
470
470
|
_test_controlled_gate_is_consistent(
|
|
471
471
|
gate, should_decompose_to_target, control_qid_shape, control_values
|
|
472
472
|
)
|
|
@@ -511,7 +511,7 @@ def _test_controlled_gate_is_consistent(
|
|
|
511
511
|
)
|
|
512
512
|
def test_controlled_gate_decomposition_uses_canonical_version(
|
|
513
513
|
sub_gate: cirq.Gate, expected_decomposition: list[cirq.Gate]
|
|
514
|
-
):
|
|
514
|
+
) -> None:
|
|
515
515
|
cgate = cirq.ControlledGate(sub_gate, num_controls=1)
|
|
516
516
|
qubits = cirq.LineQubit.range(1 + sub_gate.num_qubits())
|
|
517
517
|
dec = cirq.decompose_once(cgate.on(*qubits))
|
|
@@ -523,14 +523,14 @@ def test_controlled_gate_decomposition_uses_canonical_version(
|
|
|
523
523
|
)
|
|
524
524
|
def test_controlled_gate_full_decomposition(
|
|
525
525
|
sub_gate: cirq.Gate, expected_decomposition: list[cirq.Gate]
|
|
526
|
-
):
|
|
526
|
+
) -> None:
|
|
527
527
|
cgate = cirq.ControlledGate(sub_gate, num_controls=1)
|
|
528
528
|
qubits = cirq.LineQubit.range(1 + sub_gate.num_qubits())
|
|
529
529
|
dec = cirq.decompose(cgate.on(*qubits))
|
|
530
530
|
assert dec == [gate.on(*qubits) for gate in expected_decomposition]
|
|
531
531
|
|
|
532
532
|
|
|
533
|
-
def test_pow_inverse():
|
|
533
|
+
def test_pow_inverse() -> None:
|
|
534
534
|
assert cirq.inverse(CRestricted, None) is None
|
|
535
535
|
assert cirq.pow(CRestricted, 1.5, None) is None
|
|
536
536
|
assert cirq.pow(CY, 1.5) == cirq.ControlledGate(cirq.Y**1.5)
|
|
@@ -549,7 +549,7 @@ def test_pow_inverse():
|
|
|
549
549
|
assert cirq.inverse(C2Y) == C2Y**-1 == C2Y
|
|
550
550
|
|
|
551
551
|
|
|
552
|
-
def test_extrapolatable_effect():
|
|
552
|
+
def test_extrapolatable_effect() -> None:
|
|
553
553
|
a = cirq.NamedQubit('a')
|
|
554
554
|
b = cirq.NamedQubit('b')
|
|
555
555
|
|
|
@@ -560,7 +560,7 @@ def test_extrapolatable_effect():
|
|
|
560
560
|
assert cirq.ControlledGate(cirq.Z) ** 0.5 == cirq.ControlledGate(cirq.Z**0.5)
|
|
561
561
|
|
|
562
562
|
|
|
563
|
-
def test_reversible():
|
|
563
|
+
def test_reversible() -> None:
|
|
564
564
|
assert cirq.inverse(cirq.ControlledGate(cirq.S)) == cirq.ControlledGate(cirq.S**-1)
|
|
565
565
|
assert cirq.inverse(cirq.ControlledGate(cirq.S, num_controls=4)) == cirq.ControlledGate(
|
|
566
566
|
cirq.S**-1, num_controls=4
|
|
@@ -578,7 +578,7 @@ class UnphaseableGate(cirq.Gate):
|
|
|
578
578
|
|
|
579
579
|
|
|
580
580
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
581
|
-
def test_parameterizable(resolve_fn):
|
|
581
|
+
def test_parameterizable(resolve_fn) -> None:
|
|
582
582
|
a = sympy.Symbol('a')
|
|
583
583
|
cy = cirq.ControlledGate(cirq.Y)
|
|
584
584
|
cya = cirq.ControlledGate(cirq.YPowGate(exponent=a))
|
|
@@ -593,7 +593,7 @@ def test_parameterizable(resolve_fn):
|
|
|
593
593
|
resolve_fn(cchan, cirq.ParamResolver({'a': 0.1}))
|
|
594
594
|
|
|
595
595
|
|
|
596
|
-
def test_circuit_diagram_info():
|
|
596
|
+
def test_circuit_diagram_info() -> None:
|
|
597
597
|
assert cirq.circuit_diagram_info(CY) == cirq.CircuitDiagramInfo(
|
|
598
598
|
wire_symbols=('@', 'Y'), exponent=1
|
|
599
599
|
)
|
|
@@ -645,7 +645,7 @@ class MultiH(cirq.Gate):
|
|
|
645
645
|
return True
|
|
646
646
|
|
|
647
647
|
|
|
648
|
-
def test_circuit_diagram_product_of_sums():
|
|
648
|
+
def test_circuit_diagram_product_of_sums() -> None:
|
|
649
649
|
qubits = cirq.LineQubit.range(3)
|
|
650
650
|
c = cirq.Circuit()
|
|
651
651
|
c.append(cirq.ControlledGate(MultiH(2))(*qubits))
|
|
@@ -661,9 +661,9 @@ def test_circuit_diagram_product_of_sums():
|
|
|
661
661
|
""",
|
|
662
662
|
)
|
|
663
663
|
|
|
664
|
-
|
|
664
|
+
qids = cirq.LineQid.for_qid_shape((3, 3, 3, 2))
|
|
665
665
|
c = cirq.Circuit(
|
|
666
|
-
MultiH(1)(*
|
|
666
|
+
MultiH(1)(*qids[3:]).controlled_by(*qids[:3], control_values=[1, (0, 1), (2, 0)])
|
|
667
667
|
)
|
|
668
668
|
|
|
669
669
|
cirq.testing.assert_has_diagram(
|
|
@@ -680,7 +680,7 @@ def test_circuit_diagram_product_of_sums():
|
|
|
680
680
|
)
|
|
681
681
|
|
|
682
682
|
|
|
683
|
-
def test_circuit_diagram_sum_of_products():
|
|
683
|
+
def test_circuit_diagram_sum_of_products() -> None:
|
|
684
684
|
q = cirq.LineQubit.range(4)
|
|
685
685
|
c = cirq.Circuit(C_xorH.on(*q[:3]), C_01_10_11H.on(*q[:3]), C0C_xorH.on(*q))
|
|
686
686
|
cirq.testing.assert_has_diagram(
|
|
@@ -695,8 +695,8 @@ def test_circuit_diagram_sum_of_products():
|
|
|
695
695
|
3: ─────────────────────H───────
|
|
696
696
|
""",
|
|
697
697
|
)
|
|
698
|
-
|
|
699
|
-
c = cirq.Circuit(C_02_20H(*
|
|
698
|
+
qid = cirq.LineQid.for_qid_shape((2, 3, 2))
|
|
699
|
+
c = cirq.Circuit(C_02_20H(*qid))
|
|
700
700
|
cirq.testing.assert_has_diagram(
|
|
701
701
|
c,
|
|
702
702
|
"""
|
|
@@ -718,7 +718,7 @@ class MockGate(cirq.testing.TwoQubitGate):
|
|
|
718
718
|
return True
|
|
719
719
|
|
|
720
720
|
|
|
721
|
-
def test_uninformed_circuit_diagram_info():
|
|
721
|
+
def test_uninformed_circuit_diagram_info() -> None:
|
|
722
722
|
qbits = cirq.LineQubit.range(3)
|
|
723
723
|
mock_gate = MockGate()
|
|
724
724
|
cgate = cirq.ControlledGate(mock_gate)(*qbits)
|
|
@@ -731,7 +731,7 @@ def test_uninformed_circuit_diagram_info():
|
|
|
731
731
|
assert mock_gate.captured_diagram_args == args
|
|
732
732
|
|
|
733
733
|
|
|
734
|
-
def test_bounded_effect():
|
|
734
|
+
def test_bounded_effect() -> None:
|
|
735
735
|
assert cirq.trace_distance_bound(CY**0.001) < 0.01
|
|
736
736
|
assert cirq.approx_eq(cirq.trace_distance_bound(CCH), 1.0)
|
|
737
737
|
foo = sympy.Symbol('foo')
|
|
@@ -751,11 +751,11 @@ def test_bounded_effect():
|
|
|
751
751
|
C_02_20H,
|
|
752
752
|
],
|
|
753
753
|
)
|
|
754
|
-
def test_repr(gate):
|
|
754
|
+
def test_repr(gate) -> None:
|
|
755
755
|
cirq.testing.assert_equivalent_repr(gate)
|
|
756
756
|
|
|
757
757
|
|
|
758
|
-
def test_str():
|
|
758
|
+
def test_str() -> None:
|
|
759
759
|
assert str(cirq.ControlledGate(cirq.X)) == 'CX'
|
|
760
760
|
assert str(cirq.ControlledGate(cirq.Z)) == 'CZ'
|
|
761
761
|
assert str(cirq.ControlledGate(cirq.S)) == 'CS'
|
|
@@ -769,7 +769,7 @@ def test_str():
|
|
|
769
769
|
assert str(C2Restricted) == 'C2Restricted'
|
|
770
770
|
|
|
771
771
|
|
|
772
|
-
def test_controlled_mixture():
|
|
772
|
+
def test_controlled_mixture() -> None:
|
|
773
773
|
c_yes = cirq.ControlledGate(sub_gate=cirq.phase_flip(0.25), num_controls=1)
|
|
774
774
|
assert cirq.has_mixture(c_yes)
|
|
775
775
|
assert cirq.approx_eq(cirq.mixture(c_yes), [(0.75, np.eye(4)), (0.25, cirq.unitary(cirq.CZ))])
|
|
@@ -786,7 +786,9 @@ def test_controlled_mixture():
|
|
|
786
786
|
(4, -np.pi / 3, ((0,), (1,), (1,), (0,))),
|
|
787
787
|
],
|
|
788
788
|
)
|
|
789
|
-
def test_controlled_global_phase_matrix_gate_decomposes(
|
|
789
|
+
def test_controlled_global_phase_matrix_gate_decomposes(
|
|
790
|
+
num_controls, angle, control_values
|
|
791
|
+
) -> None:
|
|
790
792
|
all_qubits = cirq.LineQubit.range(num_controls)
|
|
791
793
|
control_values = cirq.ops.control_values.ProductOfSums(control_values)
|
|
792
794
|
control_qid_shape = (2,) * num_controls
|
cirq/ops/diagonal_gate_test.py
CHANGED
|
@@ -36,18 +36,18 @@ _candidate_angles: list[float] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41
|
|
|
36
36
|
)
|
|
37
37
|
),
|
|
38
38
|
)
|
|
39
|
-
def test_consistent_protocols(gate):
|
|
39
|
+
def test_consistent_protocols(gate) -> None:
|
|
40
40
|
cirq.testing.assert_implements_consistent_protocols(gate)
|
|
41
41
|
|
|
42
42
|
|
|
43
|
-
def test_property():
|
|
43
|
+
def test_property() -> None:
|
|
44
44
|
assert cirq.DiagonalGate([2, 3, 5, 7]).diag_angles_radians == (2, 3, 5, 7)
|
|
45
45
|
|
|
46
46
|
|
|
47
47
|
@pytest.mark.parametrize('n', [1, 2, 3, 4, 5, 6, 7, 8, 9])
|
|
48
|
-
def test_decomposition_unitary(n):
|
|
48
|
+
def test_decomposition_unitary(n) -> None:
|
|
49
49
|
diagonal_angles = np.random.randn(2**n)
|
|
50
|
-
diagonal_gate = cirq.DiagonalGate(diagonal_angles)
|
|
50
|
+
diagonal_gate = cirq.DiagonalGate(list(diagonal_angles))
|
|
51
51
|
decomposed_circ = cirq.Circuit(cirq.decompose(diagonal_gate(*cirq.LineQubit.range(n))))
|
|
52
52
|
|
|
53
53
|
expected_f = [np.exp(1j * angle) for angle in diagonal_angles]
|
|
@@ -59,7 +59,7 @@ def test_decomposition_unitary(n):
|
|
|
59
59
|
|
|
60
60
|
|
|
61
61
|
@pytest.mark.parametrize('n', [1, 2, 3, 4])
|
|
62
|
-
def test_diagonal_exponent(n):
|
|
62
|
+
def test_diagonal_exponent(n) -> None:
|
|
63
63
|
diagonal_angles = _candidate_angles[: 2**n]
|
|
64
64
|
diagonal_gate = cirq.DiagonalGate(diagonal_angles)
|
|
65
65
|
|
|
@@ -72,9 +72,9 @@ def test_diagonal_exponent(n):
|
|
|
72
72
|
|
|
73
73
|
|
|
74
74
|
@pytest.mark.parametrize('n', [1, 2, 3, 4])
|
|
75
|
-
def test_decomposition_diagonal_exponent(n):
|
|
75
|
+
def test_decomposition_diagonal_exponent(n) -> None:
|
|
76
76
|
diagonal_angles = np.random.randn(2**n)
|
|
77
|
-
diagonal_gate = cirq.DiagonalGate(diagonal_angles)
|
|
77
|
+
diagonal_gate = cirq.DiagonalGate(list(diagonal_angles))
|
|
78
78
|
sqrt_diagonal_gate = diagonal_gate**0.5
|
|
79
79
|
decomposed_circ = cirq.Circuit(cirq.decompose(sqrt_diagonal_gate(*cirq.LineQubit.range(n))))
|
|
80
80
|
|
|
@@ -85,7 +85,7 @@ def test_decomposition_diagonal_exponent(n):
|
|
|
85
85
|
|
|
86
86
|
|
|
87
87
|
@pytest.mark.parametrize('n', [1, 2, 3, 4])
|
|
88
|
-
def test_decomposition_with_parameterization(n):
|
|
88
|
+
def test_decomposition_with_parameterization(n) -> None:
|
|
89
89
|
angles = sympy.symbols([f'x_{i}' for i in range(2**n)])
|
|
90
90
|
exponent = sympy.Symbol('e')
|
|
91
91
|
diagonal_gate = cirq.DiagonalGate(angles) ** exponent
|
|
@@ -104,7 +104,7 @@ def test_decomposition_with_parameterization(n):
|
|
|
104
104
|
)
|
|
105
105
|
|
|
106
106
|
|
|
107
|
-
def test_diagram():
|
|
107
|
+
def test_diagram() -> None:
|
|
108
108
|
a, b, c, d = cirq.LineQubit.range(4)
|
|
109
109
|
|
|
110
110
|
diagonal_circuit = cirq.Circuit(cirq.DiagonalGate(_candidate_angles[:16])(a, b, c, d))
|
|
@@ -145,7 +145,7 @@ def test_diagram():
|
|
|
145
145
|
|
|
146
146
|
|
|
147
147
|
@pytest.mark.parametrize('n', [1, 2, 3, 4])
|
|
148
|
-
def test_unitary(n):
|
|
148
|
+
def test_unitary(n) -> None:
|
|
149
149
|
diagonal_angles = _candidate_angles[: 2**n]
|
|
150
150
|
assert cirq.has_unitary(cirq.DiagonalGate(diagonal_angles))
|
|
151
151
|
np.testing.assert_allclose(
|
|
@@ -156,7 +156,7 @@ def test_unitary(n):
|
|
|
156
156
|
|
|
157
157
|
|
|
158
158
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
159
|
-
def test_resolve(resolve_fn):
|
|
159
|
+
def test_resolve(resolve_fn) -> None:
|
|
160
160
|
diagonal_angles = [2, 3, 5, 7, 11, 13, 17, 19]
|
|
161
161
|
diagonal_gate = cirq.DiagonalGate(diagonal_angles[:6] + [sympy.Symbol('a'), sympy.Symbol('b')])
|
|
162
162
|
assert cirq.is_parameterized(diagonal_gate)
|
cirq/ops/eigen_gate_test.py
CHANGED
|
@@ -58,7 +58,7 @@ class ZGateDef(cirq.EigenGate, cirq.testing.SingleQubitGate):
|
|
|
58
58
|
return [(0, np.diag([1, 0])), (1, np.diag([0, 1]))]
|
|
59
59
|
|
|
60
60
|
|
|
61
|
-
def test_approximate_common_period():
|
|
61
|
+
def test_approximate_common_period() -> None:
|
|
62
62
|
from cirq.ops.eigen_gate import _approximate_common_period as f
|
|
63
63
|
|
|
64
64
|
assert f([]) is None
|
|
@@ -71,13 +71,19 @@ def test_approximate_common_period():
|
|
|
71
71
|
assert f([2.5]) == 2.5
|
|
72
72
|
assert f([1.5, 2]) == 6
|
|
73
73
|
assert f([2, 3]) == 6
|
|
74
|
-
|
|
75
|
-
assert
|
|
74
|
+
f_x = f([1 / 3, 2 / 3])
|
|
75
|
+
assert f_x is not None
|
|
76
|
+
assert abs(f_x - 2 / 3) < 1e-8
|
|
77
|
+
f_x = f([2 / 5, 3 / 5])
|
|
78
|
+
assert f_x is not None
|
|
79
|
+
assert abs(f_x - 6 / 5) < 1e-8
|
|
76
80
|
assert f([0.5, -0.5]) == 0.5
|
|
77
|
-
|
|
81
|
+
f_x = f([np.e])
|
|
82
|
+
assert f_x is not None
|
|
83
|
+
np.testing.assert_allclose(f_x, np.e, atol=1e-8)
|
|
78
84
|
|
|
79
85
|
|
|
80
|
-
def test_init():
|
|
86
|
+
def test_init() -> None:
|
|
81
87
|
assert CExpZinGate(1).exponent == 1
|
|
82
88
|
assert CExpZinGate(0.5).exponent == 0.5
|
|
83
89
|
assert CExpZinGate(4.5).exponent == 4.5
|
|
@@ -91,7 +97,7 @@ def test_init():
|
|
|
91
97
|
assert ZGateDef(exponent=0.5 + 0j).exponent == 0.5
|
|
92
98
|
|
|
93
99
|
|
|
94
|
-
def test_eq():
|
|
100
|
+
def test_eq() -> None:
|
|
95
101
|
eq = cirq.testing.EqualsTester()
|
|
96
102
|
eq.make_equality_group(lambda: CExpZinGate(quarter_turns=0.1))
|
|
97
103
|
eq.add_equality_group(CExpZinGate(0), CExpZinGate(4), CExpZinGate(-4))
|
|
@@ -166,7 +172,7 @@ def test_eq():
|
|
|
166
172
|
)
|
|
167
173
|
|
|
168
174
|
|
|
169
|
-
def test_approx_eq():
|
|
175
|
+
def test_approx_eq() -> None:
|
|
170
176
|
assert cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(1.5), atol=0.1)
|
|
171
177
|
assert cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(1.7), atol=0.3)
|
|
172
178
|
assert not cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(1.7), atol=0.1)
|
|
@@ -188,7 +194,7 @@ def test_approx_eq():
|
|
|
188
194
|
)
|
|
189
195
|
|
|
190
196
|
|
|
191
|
-
def test_approx_eq_periodic():
|
|
197
|
+
def test_approx_eq_periodic() -> None:
|
|
192
198
|
assert cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(5.5), atol=1e-9)
|
|
193
199
|
assert cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(9.5), atol=1e-9)
|
|
194
200
|
assert cirq.approx_eq(CExpZinGate(-2.5), CExpZinGate(1.5), atol=1e-9)
|
|
@@ -199,7 +205,7 @@ def test_approx_eq_periodic():
|
|
|
199
205
|
assert cirq.approx_eq(CExpZinGate(0), CExpZinGate(4 - 1e-10), atol=1e-9)
|
|
200
206
|
|
|
201
207
|
|
|
202
|
-
def test_period():
|
|
208
|
+
def test_period() -> None:
|
|
203
209
|
class Components(cirq.EigenGate, cirq.testing.TwoQubitGate):
|
|
204
210
|
def __init__(self, a, b, c, d):
|
|
205
211
|
super().__init__()
|
|
@@ -224,14 +230,16 @@ def test_period():
|
|
|
224
230
|
assert Components(1 / 3, 1 / 2, 1 / 5, 0)._period() == 60
|
|
225
231
|
assert Components(1 / 6, 1 / 2, 1 / 5, 0)._period() == 60
|
|
226
232
|
assert Components(np.e, np.pi, 0, 0)._period() is None
|
|
227
|
-
|
|
233
|
+
period = Components(np.e, np.e, 0, 0)._period()
|
|
234
|
+
assert period is not None
|
|
235
|
+
np.testing.assert_allclose(period, 2 / np.e)
|
|
228
236
|
assert Components(-0.5, 0, 0, 0)._period() == 4
|
|
229
237
|
assert Components(-0.5, 0.5, 0, 0)._period() == 4
|
|
230
238
|
assert Components(-0.5, 0.5, 0.5, 0.5)._period() == 4
|
|
231
239
|
assert Components(1, 1, -1, 1)._period() == 2
|
|
232
240
|
|
|
233
241
|
|
|
234
|
-
def test_pow():
|
|
242
|
+
def test_pow() -> None:
|
|
235
243
|
assert CExpZinGate(0.25) ** 2 == CExpZinGate(0.5)
|
|
236
244
|
assert CExpZinGate(0.25) ** -1 == CExpZinGate(-0.25)
|
|
237
245
|
assert CExpZinGate(0.25) ** 0 == CExpZinGate(0)
|
|
@@ -245,12 +253,12 @@ def test_pow():
|
|
|
245
253
|
assert ZGateDef(exponent=0.5) ** (1 + 0j) == ZGateDef(exponent=0.5)
|
|
246
254
|
|
|
247
255
|
|
|
248
|
-
def test_inverse():
|
|
256
|
+
def test_inverse() -> None:
|
|
249
257
|
assert cirq.inverse(CExpZinGate(0.25)) == CExpZinGate(-0.25)
|
|
250
258
|
assert cirq.inverse(CExpZinGate(sympy.Symbol('a'))) == CExpZinGate(-sympy.Symbol('a'))
|
|
251
259
|
|
|
252
260
|
|
|
253
|
-
def test_trace_distance_bound():
|
|
261
|
+
def test_trace_distance_bound() -> None:
|
|
254
262
|
assert cirq.trace_distance_bound(CExpZinGate(0.001)) < 0.01
|
|
255
263
|
assert cirq.trace_distance_bound(CExpZinGate(sympy.Symbol('a'))) == 1
|
|
256
264
|
assert cirq.approx_eq(cirq.trace_distance_bound(CExpZinGate(2)), 1)
|
|
@@ -266,7 +274,7 @@ def test_trace_distance_bound():
|
|
|
266
274
|
assert_has_consistent_trace_distance_bound(E() ** (numerator / 12))
|
|
267
275
|
|
|
268
276
|
|
|
269
|
-
def test_extrapolate():
|
|
277
|
+
def test_extrapolate() -> None:
|
|
270
278
|
h = CExpZinGate(2)
|
|
271
279
|
assert cirq.pow(h, 1.5) is not None
|
|
272
280
|
assert cirq.inverse(h, None) is not None
|
|
@@ -280,7 +288,7 @@ def test_extrapolate():
|
|
|
280
288
|
assert cirq.inverse(s) == CExpZinGate(-sympy.Symbol('a'))
|
|
281
289
|
|
|
282
290
|
|
|
283
|
-
def test_matrix():
|
|
291
|
+
def test_matrix() -> None:
|
|
284
292
|
for n in [1, 2, 3, 4, 0.0001, 3.9999]:
|
|
285
293
|
assert cirq.has_unitary(CExpZinGate(n))
|
|
286
294
|
|
|
@@ -326,11 +334,11 @@ def test_matrix():
|
|
|
326
334
|
)
|
|
327
335
|
|
|
328
336
|
|
|
329
|
-
def test_matrix_is_exact_for_quarter_turn():
|
|
337
|
+
def test_matrix_is_exact_for_quarter_turn() -> None:
|
|
330
338
|
np.testing.assert_equal(cirq.unitary(CExpZinGate(1)), np.diag([1, 1, 1j, -1j]))
|
|
331
339
|
|
|
332
340
|
|
|
333
|
-
def test_is_parameterized():
|
|
341
|
+
def test_is_parameterized() -> None:
|
|
334
342
|
assert not cirq.is_parameterized(CExpZinGate(0))
|
|
335
343
|
assert not cirq.is_parameterized(CExpZinGate(1))
|
|
336
344
|
assert not cirq.is_parameterized(CExpZinGate(3))
|
|
@@ -338,7 +346,7 @@ def test_is_parameterized():
|
|
|
338
346
|
|
|
339
347
|
|
|
340
348
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
341
|
-
def test_resolve_parameters(resolve_fn):
|
|
349
|
+
def test_resolve_parameters(resolve_fn) -> None:
|
|
342
350
|
assert resolve_fn(
|
|
343
351
|
CExpZinGate(sympy.Symbol('a')), cirq.ParamResolver({'a': 0.5})
|
|
344
352
|
) == CExpZinGate(0.5)
|
|
@@ -349,7 +357,7 @@ def test_resolve_parameters(resolve_fn):
|
|
|
349
357
|
resolve_fn(CExpZinGate(sympy.Symbol('a')), cirq.ParamResolver({'a': 0.5j}))
|
|
350
358
|
|
|
351
359
|
|
|
352
|
-
def test_diagram_period():
|
|
360
|
+
def test_diagram_period() -> None:
|
|
353
361
|
class ShiftyGate(cirq.EigenGate, cirq.testing.SingleQubitGate):
|
|
354
362
|
def _eigen_components(self) -> list[tuple[float, np.ndarray]]:
|
|
355
363
|
raise NotImplementedError()
|
|
@@ -419,5 +427,5 @@ class WeightedZPowGate(cirq.EigenGate, cirq.testing.SingleQubitGate):
|
|
|
419
427
|
(cirq.Z**2, cirq.Z ** (4 + 1e-10), True),
|
|
420
428
|
],
|
|
421
429
|
)
|
|
422
|
-
def test_equal_up_to_global_phase(gate1, gate2, eq_up_to_global_phase):
|
|
430
|
+
def test_equal_up_to_global_phase(gate1, gate2, eq_up_to_global_phase) -> None:
|
|
423
431
|
assert cirq.equal_up_to_global_phase(gate1, gate2) == eq_up_to_global_phase
|
cirq/ops/fourier_transform.py
CHANGED
|
@@ -212,6 +212,4 @@ def qft(*qubits: cirq.Qid, without_reverse: bool = False, inverse: bool = False)
|
|
|
212
212
|
A `cirq.Operation` applying the qft to the given qubits.
|
|
213
213
|
"""
|
|
214
214
|
result = QuantumFourierTransformGate(len(qubits), without_reverse=without_reverse).on(*qubits)
|
|
215
|
-
if inverse
|
|
216
|
-
result = cirq.inverse(result)
|
|
217
|
-
return result
|
|
215
|
+
return cirq.inverse(result) if inverse else result
|