cirq-core 1.7.0.dev20250818230756__py3-none-any.whl → 1.7.0.dev20250820205919__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cirq-core might be problematic. Click here for more details.

Files changed (57) hide show
  1. cirq/_version.py +1 -1
  2. cirq/_version_test.py +1 -1
  3. cirq/experiments/z_phase_calibration_test.py +8 -7
  4. cirq/interop/quirk/cells/arithmetic_cells.py +1 -1
  5. cirq/interop/quirk/cells/cell_test.py +5 -5
  6. cirq/interop/quirk/cells/composite_cell_test.py +5 -3
  7. cirq/interop/quirk/cells/control_cells.py +2 -2
  8. cirq/interop/quirk/cells/input_cells.py +3 -3
  9. cirq/interop/quirk/cells/input_rotation_cells.py +2 -2
  10. cirq/interop/quirk/cells/swap_cell.py +2 -2
  11. cirq/interop/quirk/cells/testing.py +1 -1
  12. cirq/interop/quirk/url_to_circuit.py +2 -2
  13. cirq/interop/quirk/url_to_circuit_test.py +11 -11
  14. cirq/linalg/decompositions.py +1 -1
  15. cirq/linalg/decompositions_test.py +52 -50
  16. cirq/linalg/diagonalize_test.py +2 -2
  17. cirq/linalg/predicates.py +1 -1
  18. cirq/linalg/predicates_test.py +24 -24
  19. cirq/linalg/transformations.py +6 -4
  20. cirq/neutral_atoms/neutral_atom_devices.py +1 -1
  21. cirq/ops/classically_controlled_operation.py +2 -2
  22. cirq/ops/clifford_gate.py +1 -1
  23. cirq/ops/common_channels_test.py +1 -1
  24. cirq/ops/common_gates.py +2 -2
  25. cirq/ops/controlled_operation.py +2 -2
  26. cirq/ops/controlled_operation_test.py +17 -16
  27. cirq/ops/dense_pauli_string_test.py +41 -37
  28. cirq/ops/eigen_gate_test.py +0 -7
  29. cirq/ops/gateset.py +3 -3
  30. cirq/ops/gateset_test.py +23 -23
  31. cirq/ops/linear_combinations.py +1 -1
  32. cirq/ops/linear_combinations_test.py +103 -89
  33. cirq/ops/matrix_gates_test.py +31 -31
  34. cirq/ops/measure_util_test.py +11 -10
  35. cirq/ops/measurement_gate_test.py +23 -22
  36. cirq/ops/pauli_gates.py +1 -1
  37. cirq/ops/pauli_measurement_gate_test.py +12 -12
  38. cirq/ops/pauli_string_phasor_test.py +45 -40
  39. cirq/ops/pauli_string_raw_types_test.py +5 -4
  40. cirq/ops/pauli_string_test.py +190 -118
  41. cirq/ops/permutation_gate.py +1 -1
  42. cirq/ops/raw_types.py +3 -4
  43. cirq/ops/raw_types_test.py +1 -1
  44. cirq/ops/three_qubit_gates.py +3 -3
  45. cirq/protocols/act_on_protocol.py +1 -1
  46. cirq/protocols/act_on_protocol_test.py +4 -4
  47. cirq/protocols/apply_channel_protocol_test.py +19 -18
  48. cirq/protocols/apply_mixture_protocol_test.py +20 -19
  49. cirq/protocols/apply_unitary_protocol_test.py +31 -23
  50. cirq/protocols/approximate_equality_protocol_test.py +22 -16
  51. cirq/protocols/circuit_diagram_info_protocol.py +4 -4
  52. cirq/protocols/commutes_protocol_test.py +4 -4
  53. {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/METADATA +1 -1
  54. {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/RECORD +57 -57
  55. {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/WHEEL +0 -0
  56. {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/licenses/LICENSE +0 -0
  57. {cirq_core-1.7.0.dev20250818230756.dist-info → cirq_core-1.7.0.dev20250820205919.dist-info}/top_level.txt +0 -0
@@ -35,19 +35,19 @@ CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
35
35
  CZ = np.diag([1, 1, 1, -1])
36
36
 
37
37
 
38
- def assert_kronecker_factorization_within_tolerance(matrix, g, f1, f2):
38
+ def assert_kronecker_factorization_within_tolerance(matrix, g, f1, f2) -> None:
39
39
  restored = g * cirq.linalg.combinators.kron(f1, f2)
40
40
  assert not np.any(np.isnan(restored)), "NaN in kronecker product."
41
41
  assert np.allclose(restored, matrix), "Can't factor kronecker product."
42
42
 
43
43
 
44
- def assert_magic_su2_within_tolerance(mat, a, b):
44
+ def assert_magic_su2_within_tolerance(mat, a, b) -> None:
45
45
  recon = cirq.linalg.combinators.dot(MAGIC_CONJ_T, cirq.linalg.combinators.kron(a, b), MAGIC)
46
46
  assert np.allclose(recon, mat), "Failed to decompose within tolerance."
47
47
 
48
48
 
49
49
  @pytest.mark.parametrize('matrix', [X, cirq.kron(X, X), cirq.kron(X, Y), cirq.kron(X, np.eye(2))])
50
- def test_map_eigenvalues_identity(matrix):
50
+ def test_map_eigenvalues_identity(matrix) -> None:
51
51
  identity_mapped = cirq.map_eigenvalues(matrix, lambda e: e)
52
52
  assert np.allclose(matrix, identity_mapped)
53
53
 
@@ -63,7 +63,7 @@ def test_map_eigenvalues_identity(matrix):
63
63
  [X, 0.5, np.array([[1j, 1], [1, 1j]]) * (1 - 1j) / 2],
64
64
  ],
65
65
  )
66
- def test_map_eigenvalues_raise(matrix, exponent, desired):
66
+ def test_map_eigenvalues_raise(matrix, exponent, desired) -> None:
67
67
  exp_mapped = cirq.map_eigenvalues(matrix, lambda e: complex(e) ** exponent)
68
68
  assert np.allclose(desired, exp_mapped)
69
69
 
@@ -88,7 +88,7 @@ def _random_unitary_with_close_eigenvalues():
88
88
  _random_unitary_with_close_eigenvalues(),
89
89
  ],
90
90
  )
91
- def test_unitary_eig(matrix):
91
+ def test_unitary_eig(matrix) -> None:
92
92
  # np.linalg.eig(matrix) won't work for the perturbed matrix
93
93
  d, vecs = unitary_eig(matrix)
94
94
 
@@ -96,7 +96,7 @@ def test_unitary_eig(matrix):
96
96
  np.testing.assert_allclose(matrix, vecs @ np.diag(d) @ vecs.conj().T, atol=1e-14)
97
97
 
98
98
 
99
- def test_non_unitary_eig():
99
+ def test_non_unitary_eig() -> None:
100
100
  with pytest.raises(Exception):
101
101
  unitary_eig(np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2], [3, 4, 5, 6]]))
102
102
 
@@ -118,7 +118,7 @@ def test_non_unitary_eig():
118
118
  ]
119
119
  + [(cirq.testing.random_unitary(2), cirq.testing.random_unitary(2)) for _ in range(10)],
120
120
  )
121
- def test_kron_factor(f1, f2):
121
+ def test_kron_factor(f1, f2) -> None:
122
122
  p = cirq.kron(f1, f2)
123
123
  g, g1, g2 = cirq.kron_factor_4x4_to_2x2s(p)
124
124
  assert abs(np.linalg.det(g1) - 1) < 0.00001
@@ -134,7 +134,7 @@ def test_kron_factor(f1, f2):
134
134
  for _ in range(10)
135
135
  ],
136
136
  )
137
- def test_kron_factor_special_unitaries(f1, f2):
137
+ def test_kron_factor_special_unitaries(f1, f2) -> None:
138
138
  p = cirq.kron(f1, f2)
139
139
  g, g1, g2 = cirq.kron_factor_4x4_to_2x2s(p)
140
140
  assert np.allclose(cirq.kron(g1, g2), p)
@@ -144,7 +144,7 @@ def test_kron_factor_special_unitaries(f1, f2):
144
144
  assert_kronecker_factorization_within_tolerance(p, g, g1, g2)
145
145
 
146
146
 
147
- def test_kron_factor_invalid_input():
147
+ def test_kron_factor_invalid_input() -> None:
148
148
  mats = [
149
149
  cirq.kron_with_controls(cirq.CONTROL_TAG, X),
150
150
  np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 2, 3, 4]]),
@@ -167,7 +167,7 @@ def recompose_so4(a: np.ndarray, b: np.ndarray) -> np.ndarray:
167
167
 
168
168
 
169
169
  @pytest.mark.parametrize('m', [cirq.testing.random_special_orthogonal(4) for _ in range(10)])
170
- def test_so4_to_magic_su2s(m):
170
+ def test_so4_to_magic_su2s(m) -> None:
171
171
  a, b = cirq.so4_to_magic_su2s(m)
172
172
  m2 = recompose_so4(a, b)
173
173
  assert_magic_su2_within_tolerance(m2, a, b)
@@ -181,7 +181,7 @@ def test_so4_to_magic_su2s(m):
181
181
  for _ in range(10)
182
182
  ],
183
183
  )
184
- def test_so4_to_magic_su2s_known_factors(a, b):
184
+ def test_so4_to_magic_su2s_known_factors(a, b) -> None:
185
185
  m = recompose_so4(a, b)
186
186
  a2, b2 = cirq.so4_to_magic_su2s(m)
187
187
  m2 = recompose_so4(a2, b2)
@@ -206,7 +206,7 @@ def test_so4_to_magic_su2s_known_factors(a, b):
206
206
  np.diag([1, 1, 1, -1]),
207
207
  ],
208
208
  )
209
- def test_so4_to_magic_su2s_fail(mat):
209
+ def test_so4_to_magic_su2s_fail(mat) -> None:
210
210
  with pytest.raises(ValueError):
211
211
  _ = cirq.so4_to_magic_su2s(mat)
212
212
 
@@ -214,7 +214,7 @@ def test_so4_to_magic_su2s_fail(mat):
214
214
  @pytest.mark.parametrize(
215
215
  'x,y,z', [[(random.random() * 2 - 1) * np.pi * 2 for _ in range(3)] for _ in range(10)]
216
216
  )
217
- def test_kak_canonicalize_vector(x, y, z):
217
+ def test_kak_canonicalize_vector(x, y, z) -> None:
218
218
  i = np.eye(2)
219
219
  m = cirq.unitary(
220
220
  cirq.KakDecomposition(
@@ -243,12 +243,12 @@ def test_kak_canonicalize_vector(x, y, z):
243
243
  assert np.allclose(m, m2)
244
244
 
245
245
 
246
- def test_kak_vector_empty():
246
+ def test_kak_vector_empty() -> None:
247
247
  assert len(cirq.kak_vector([])) == 0
248
248
 
249
249
 
250
250
  @pytest.mark.usefixtures('closefigures')
251
- def test_kak_plot_empty():
251
+ def test_kak_plot_empty() -> None:
252
252
  cirq.scatter_plot_normalized_kak_interaction_coefficients([])
253
253
 
254
254
 
@@ -257,21 +257,21 @@ def test_kak_plot_empty():
257
257
  [np.eye(4), SWAP, SWAP * 1j, CZ, CNOT, SWAP @ CZ]
258
258
  + [cirq.testing.random_unitary(4) for _ in range(10)],
259
259
  )
260
- def test_kak_decomposition(target):
260
+ def test_kak_decomposition(target) -> None:
261
261
  kak = cirq.kak_decomposition(target)
262
262
  np.testing.assert_allclose(cirq.unitary(kak), target, atol=1e-8)
263
263
 
264
264
 
265
- def test_kak_decomposition_unitary_object():
265
+ def test_kak_decomposition_unitary_object() -> None:
266
266
  op = cirq.ISWAP(*cirq.LineQubit.range(2)) ** 0.5
267
267
  kak = cirq.kak_decomposition(op)
268
268
  np.testing.assert_allclose(cirq.unitary(kak), cirq.unitary(op), atol=1e-8)
269
269
  assert cirq.kak_decomposition(kak) is kak
270
270
 
271
271
 
272
- def test_kak_decomposition_invalid_object():
272
+ def test_kak_decomposition_invalid_object() -> None:
273
273
  with pytest.raises(TypeError, match='unitary effect'):
274
- _ = cirq.kak_decomposition('test')
274
+ _ = cirq.kak_decomposition('test') # type: ignore[arg-type]
275
275
 
276
276
  with pytest.raises(ValueError, match='4x4 unitary matrix'):
277
277
  _ = cirq.kak_decomposition(np.eye(3))
@@ -289,7 +289,7 @@ def test_kak_decomposition_invalid_object():
289
289
  np.testing.assert_allclose(cirq.unitary(nil), np.eye(4), atol=1e-8)
290
290
 
291
291
 
292
- def test_kak_decomposition_eq():
292
+ def test_kak_decomposition_eq() -> None:
293
293
  eq = cirq.testing.EqualsTester()
294
294
 
295
295
  eq.make_equality_group(
@@ -339,7 +339,7 @@ def test_kak_decomposition_eq():
339
339
  )
340
340
 
341
341
 
342
- def test_kak_repr():
342
+ def test_kak_repr() -> None:
343
343
  cirq.testing.assert_equivalent_repr(
344
344
  cirq.KakDecomposition(
345
345
  global_phase=1j,
@@ -374,7 +374,7 @@ cirq.KakDecomposition(
374
374
  )
375
375
 
376
376
 
377
- def test_kak_str():
377
+ def test_kak_str() -> None:
378
378
  v = cirq.KakDecomposition(
379
379
  interaction_coefficients=(0.3 * np.pi / 4, 0.2 * np.pi / 4, 0.1 * np.pi / 4),
380
380
  single_qubit_operations_before=(cirq.unitary(cirq.I), cirq.unitary(cirq.X)),
@@ -391,7 +391,7 @@ def test_kak_str():
391
391
  )
392
392
 
393
393
 
394
- def test_axis_angle_decomposition_eq():
394
+ def test_axis_angle_decomposition_eq() -> None:
395
395
  eq = cirq.testing.EqualsTester()
396
396
 
397
397
  eq.make_equality_group(
@@ -402,13 +402,13 @@ def test_axis_angle_decomposition_eq():
402
402
  eq.add_equality_group(cirq.AxisAngleDecomposition(angle=1, axis=(0.8, 0.6, 0), global_phase=1))
403
403
 
404
404
 
405
- def test_axis_angle_decomposition_repr():
405
+ def test_axis_angle_decomposition_repr() -> None:
406
406
  cirq.testing.assert_equivalent_repr(
407
407
  cirq.AxisAngleDecomposition(angle=1, axis=(0, 0.6, 0.8), global_phase=-1)
408
408
  )
409
409
 
410
410
 
411
- def test_axis_angle_decomposition_str():
411
+ def test_axis_angle_decomposition_str() -> None:
412
412
  assert str(cirq.axis_angle(cirq.unitary(cirq.X))) == '1*π around X'
413
413
  assert str(cirq.axis_angle(cirq.unitary(cirq.Y))) == '1*π around Y'
414
414
  assert str(cirq.axis_angle(cirq.unitary(cirq.Z))) == '1*π around Z'
@@ -424,14 +424,14 @@ def test_axis_angle_decomposition_str():
424
424
  )
425
425
 
426
426
 
427
- def test_axis_angle_decomposition_unitary():
427
+ def test_axis_angle_decomposition_unitary() -> None:
428
428
  u = cirq.testing.random_unitary(2)
429
429
  u = cirq.unitary(cirq.T)
430
430
  a = cirq.axis_angle(u)
431
431
  np.testing.assert_allclose(u, cirq.unitary(a), atol=1e-8)
432
432
 
433
433
 
434
- def test_axis_angle():
434
+ def test_axis_angle() -> None:
435
435
  assert cirq.approx_eq(
436
436
  cirq.axis_angle(cirq.unitary(cirq.ry(1e-10))),
437
437
  cirq.AxisAngleDecomposition(angle=0, axis=(1, 0, 0), global_phase=1),
@@ -492,7 +492,7 @@ def test_axis_angle():
492
492
  )
493
493
 
494
494
 
495
- def test_axis_angle_canonicalize():
495
+ def test_axis_angle_canonicalize() -> None:
496
496
  a = cirq.AxisAngleDecomposition(
497
497
  angle=np.pi * 2.3, axis=(1, 0, 0), global_phase=1j
498
498
  ).canonicalize()
@@ -522,7 +522,7 @@ def test_axis_angle_canonicalize():
522
522
  assert np.isclose(a.angle, -np.pi + 0.01)
523
523
 
524
524
 
525
- def test_axis_angle_canonicalize_approx_equal():
525
+ def test_axis_angle_canonicalize_approx_equal() -> None:
526
526
  a1 = cirq.AxisAngleDecomposition(angle=np.pi, axis=(1, 0, 0), global_phase=1)
527
527
  a2 = cirq.AxisAngleDecomposition(angle=-np.pi, axis=(1, 0, 0), global_phase=-1)
528
528
  b1 = cirq.AxisAngleDecomposition(angle=np.pi, axis=(1, 0, 0), global_phase=-1)
@@ -530,7 +530,7 @@ def test_axis_angle_canonicalize_approx_equal():
530
530
  assert not cirq.approx_eq(a1, b1, atol=1e-8)
531
531
 
532
532
 
533
- def test_axis_angle_init():
533
+ def test_axis_angle_init() -> None:
534
534
  a = cirq.AxisAngleDecomposition(angle=1, axis=(0, 1, 0), global_phase=1j)
535
535
  assert a.angle == 1
536
536
  assert a.axis == (0, 1, 0)
@@ -541,14 +541,14 @@ def test_axis_angle_init():
541
541
 
542
542
 
543
543
  @pytest.mark.usefixtures('closefigures')
544
- def test_scatter_plot_normalized_kak_interaction_coefficients():
544
+ def test_scatter_plot_normalized_kak_interaction_coefficients() -> None:
545
545
  a, b = cirq.LineQubit.range(2)
546
- data = [
546
+ data = (
547
547
  cirq.kak_decomposition(cirq.unitary(cirq.CZ)),
548
548
  cirq.unitary(cirq.CZ),
549
549
  cirq.CZ,
550
550
  cirq.Circuit(cirq.H(a), cirq.CNOT(a, b)),
551
- ]
551
+ )
552
552
  ax = cirq.scatter_plot_normalized_kak_interaction_coefficients(data)
553
553
  assert ax is not None
554
554
  ax2 = cirq.scatter_plot_normalized_kak_interaction_coefficients(
@@ -645,33 +645,35 @@ def _local_invariants_from_kak(vector: np.ndarray) -> np.ndarray:
645
645
  _random_unitaries, _kak_vecs = _random_two_qubit_unitaries(100, random_state=11)
646
646
 
647
647
 
648
- def test_kak_vector_matches_vectorized():
648
+ def test_kak_vector_matches_vectorized() -> None:
649
649
  actual = cirq.kak_vector(_random_unitaries)
650
650
  expected = np.array([cirq.kak_vector(u) for u in _random_unitaries])
651
651
  np.testing.assert_almost_equal(actual, expected)
652
652
 
653
653
 
654
- def test_kak_vector_local_invariants_random_input():
654
+ def test_kak_vector_local_invariants_random_input() -> None:
655
655
  actual = _local_invariants_from_kak(cirq.kak_vector(_random_unitaries))
656
656
  expected = _local_invariants_from_kak(_kak_vecs)
657
657
 
658
658
  np.testing.assert_almost_equal(actual, expected)
659
659
 
660
660
 
661
- def test_kak_vector_on_weyl_chamber_face():
661
+ def test_kak_vector_on_weyl_chamber_face() -> None:
662
662
  # unitaries with KAK vectors from I to ISWAP
663
663
  theta_swap = np.linspace(0, np.pi / 4, 10)
664
664
  k_vecs = np.zeros((10, 3))
665
665
  k_vecs[:, (0, 1)] = theta_swap[:, np.newaxis]
666
666
 
667
- kwargs = dict(
668
- global_phase=1j,
669
- single_qubit_operations_before=(X, Y),
670
- single_qubit_operations_after=(Z, 1j * X),
671
- )
672
667
  unitaries = np.array(
673
668
  [
674
- cirq.unitary(cirq.KakDecomposition(interaction_coefficients=(t, t, 0), **kwargs))
669
+ cirq.unitary(
670
+ cirq.KakDecomposition(
671
+ interaction_coefficients=(t, t, 0),
672
+ global_phase=1j,
673
+ single_qubit_operations_before=(X, Y),
674
+ single_qubit_operations_after=(Z, 1j * X),
675
+ )
676
+ )
675
677
  for t in theta_swap
676
678
  ]
677
679
  )
@@ -692,7 +694,7 @@ def test_kak_vector_on_weyl_chamber_face():
692
694
  (np.kron(X, X), (0, 0, 0)),
693
695
  ),
694
696
  )
695
- def test_kak_vector_weyl_chamber_vertices(unitary, expected):
697
+ def test_kak_vector_weyl_chamber_vertices(unitary, expected) -> None:
696
698
  actual = cirq.kak_vector(unitary)
697
699
  np.testing.assert_almost_equal(actual, expected)
698
700
 
@@ -701,17 +703,17 @@ cases = [np.eye(3), SWAP.reshape((2, 8)), SWAP.ravel()]
701
703
 
702
704
 
703
705
  @pytest.mark.parametrize('bad_input', cases)
704
- def test_kak_vector_wrong_matrix_shape(bad_input):
706
+ def test_kak_vector_wrong_matrix_shape(bad_input) -> None:
705
707
  with pytest.raises(ValueError, match='to have shape'):
706
708
  cirq.kak_vector(bad_input)
707
709
 
708
710
 
709
- def test_kak_vector_negative_atol():
711
+ def test_kak_vector_negative_atol() -> None:
710
712
  with pytest.raises(ValueError, match='must be positive'):
711
713
  cirq.kak_vector(np.eye(4), atol=-1.0)
712
714
 
713
715
 
714
- def test_kak_vector_input_not_unitary():
716
+ def test_kak_vector_input_not_unitary() -> None:
715
717
  with pytest.raises(ValueError, match='must correspond to'):
716
718
  cirq.kak_vector(np.zeros((4, 4)))
717
719
 
@@ -728,7 +730,7 @@ def test_kak_vector_input_not_unitary():
728
730
  cirq.unitary(cirq.CZ),
729
731
  ],
730
732
  )
731
- def test_kak_decompose(unitary: np.ndarray):
733
+ def test_kak_decompose(unitary: np.ndarray) -> None:
732
734
  kak = cirq.kak_decomposition(unitary)
733
735
  circuit = cirq.Circuit(kak._decompose_(cirq.LineQubit.range(2)))
734
736
  np.testing.assert_allclose(cirq.unitary(circuit), unitary, atol=1e-6)
@@ -737,7 +739,7 @@ def test_kak_decompose(unitary: np.ndarray):
737
739
 
738
740
 
739
741
  @cirq.testing.retry_once_with_later_random_values
740
- def test_num_two_qubit_gates_required():
742
+ def test_num_two_qubit_gates_required() -> None:
741
743
  for i in range(4):
742
744
  assert (
743
745
  cirq.num_cnots_required(
@@ -749,7 +751,7 @@ def test_num_two_qubit_gates_required():
749
751
  assert cirq.num_cnots_required(np.eye(4)) == 0
750
752
 
751
753
 
752
- def test_num_two_qubit_gates_required_invalid():
754
+ def test_num_two_qubit_gates_required_invalid() -> None:
753
755
  with pytest.raises(ValueError, match="(4,4)"):
754
756
  cirq.num_cnots_required(np.array([[1]]))
755
757
 
@@ -772,7 +774,7 @@ def test_num_two_qubit_gates_required_invalid():
772
774
  ),
773
775
  ],
774
776
  )
775
- def test_extract_right_diag(u):
777
+ def test_extract_right_diag(u) -> None:
776
778
  assert cirq.num_cnots_required(u) == 3
777
779
  diag = cirq.linalg.extract_right_diag(u)
778
780
  assert cirq.is_diagonal(diag)
@@ -67,7 +67,7 @@ def _get_assert_diagonalized_by_str(m, p, d):
67
67
  )
68
68
 
69
69
 
70
- def assert_diagonalized_by(m, p, atol: float = 1e-8):
70
+ def assert_diagonalized_by(m, p, atol: float = 1e-8) -> None:
71
71
  d = p.T.dot(m).dot(p)
72
72
 
73
73
  assert cirq.is_orthogonal(p) and cirq.is_diagonal(
@@ -82,7 +82,7 @@ def _get_assert_bidiagonalized_by_str(m, p, q, d):
82
82
  )
83
83
 
84
84
 
85
- def assert_bidiagonalized_by(m, p, q, rtol: float = 1e-5, atol: float = 1e-8):
85
+ def assert_bidiagonalized_by(m, p, q, rtol: float = 1e-5, atol: float = 1e-8) -> None:
86
86
  d = p.dot(m).dot(q)
87
87
 
88
88
  assert (
cirq/linalg/predicates.py CHANGED
@@ -160,7 +160,7 @@ def is_normal(matrix: np.ndarray, *, rtol: float = 1e-5, atol: float = 1e-8) ->
160
160
  return matrix_commutes(matrix, matrix.T.conj(), rtol=rtol, atol=atol)
161
161
 
162
162
 
163
- def is_cptp(*, kraus_ops: Sequence[np.ndarray], rtol: float = 1e-5, atol: float = 1e-8):
163
+ def is_cptp(*, kraus_ops: Sequence[np.ndarray], rtol: float = 1e-5, atol: float = 1e-8) -> bool:
164
164
  """Determines if a channel is completely positive trace preserving (CPTP).
165
165
 
166
166
  A channel composed of Kraus operators K[0:n] is a CPTP map if the sum of
@@ -23,7 +23,7 @@ import cirq
23
23
  from cirq.linalg import matrix_commutes
24
24
 
25
25
 
26
- def test_is_diagonal():
26
+ def test_is_diagonal() -> None:
27
27
  assert cirq.is_diagonal(np.empty((0, 0)))
28
28
  assert cirq.is_diagonal(np.empty((1, 0)))
29
29
  assert cirq.is_diagonal(np.empty((0, 1)))
@@ -48,7 +48,7 @@ def test_is_diagonal():
48
48
  assert cirq.is_diagonal(np.array([[1, 1e-11], [1e-10, 1]]))
49
49
 
50
50
 
51
- def test_is_diagonal_tolerance():
51
+ def test_is_diagonal_tolerance() -> None:
52
52
  atol = 0.5
53
53
 
54
54
  # Pays attention to specified tolerance.
@@ -60,7 +60,7 @@ def test_is_diagonal_tolerance():
60
60
  assert not cirq.is_diagonal(np.array([[1, 0.5], [-0.6, 1]]), atol=atol)
61
61
 
62
62
 
63
- def test_is_hermitian():
63
+ def test_is_hermitian() -> None:
64
64
  assert cirq.is_hermitian(np.empty((0, 0)))
65
65
  assert not cirq.is_hermitian(np.empty((1, 0)))
66
66
  assert not cirq.is_hermitian(np.empty((0, 1)))
@@ -87,7 +87,7 @@ def test_is_hermitian():
87
87
  assert cirq.is_hermitian(np.array([[1, 1j + 1e-11], [-1j, 1 + 1j * 1e-9]]))
88
88
 
89
89
 
90
- def test_is_hermitian_tolerance():
90
+ def test_is_hermitian_tolerance() -> None:
91
91
  atol = 0.5
92
92
 
93
93
  # Pays attention to specified tolerance.
@@ -102,7 +102,7 @@ def test_is_hermitian_tolerance():
102
102
  assert not cirq.is_hermitian(np.array([[1, 0, 0.6], [0, 1, 0], [0, 0, 1]]), atol=atol)
103
103
 
104
104
 
105
- def test_is_unitary():
105
+ def test_is_unitary() -> None:
106
106
  assert not cirq.is_unitary(np.empty((0,)))
107
107
  assert cirq.is_unitary(np.empty((0, 0)))
108
108
  assert not cirq.is_unitary(np.empty((1, 0)))
@@ -133,7 +133,7 @@ def test_is_unitary():
133
133
  assert cirq.is_unitary(np.array([[1, 1j + 1e-11], [1j, 1 + 1j * 1e-9]]) * np.sqrt(0.5))
134
134
 
135
135
 
136
- def test_is_unitary_tolerance():
136
+ def test_is_unitary_tolerance() -> None:
137
137
  atol = 0.5
138
138
 
139
139
  # Pays attention to specified tolerance.
@@ -145,7 +145,7 @@ def test_is_unitary_tolerance():
145
145
  assert not cirq.is_unitary(np.array([[1.2, 0, 0], [0, 1.3, 0], [0, 0, 1.2]]), atol=atol)
146
146
 
147
147
 
148
- def test_is_orthogonal():
148
+ def test_is_orthogonal() -> None:
149
149
  assert cirq.is_orthogonal(np.empty((0, 0)))
150
150
  assert not cirq.is_orthogonal(np.empty((1, 0)))
151
151
  assert not cirq.is_orthogonal(np.empty((0, 1)))
@@ -173,7 +173,7 @@ def test_is_orthogonal():
173
173
  assert cirq.is_orthogonal(np.array([[1, 1e-11], [0, 1 + 1e-11]]))
174
174
 
175
175
 
176
- def test_is_orthogonal_tolerance():
176
+ def test_is_orthogonal_tolerance() -> None:
177
177
  atol = 0.5
178
178
 
179
179
  # Pays attention to specified tolerance.
@@ -185,7 +185,7 @@ def test_is_orthogonal_tolerance():
185
185
  assert not cirq.is_orthogonal(np.array([[1.2, 0, 0], [0, 1.3, 0], [0, 0, 1.2]]), atol=atol)
186
186
 
187
187
 
188
- def test_is_special_orthogonal():
188
+ def test_is_special_orthogonal() -> None:
189
189
  assert cirq.is_special_orthogonal(np.empty((0, 0)))
190
190
  assert not cirq.is_special_orthogonal(np.empty((1, 0)))
191
191
  assert not cirq.is_special_orthogonal(np.empty((0, 1)))
@@ -215,7 +215,7 @@ def test_is_special_orthogonal():
215
215
  assert cirq.is_special_orthogonal(np.array([[1, 1e-11], [0, 1 + 1e-11]]))
216
216
 
217
217
 
218
- def test_is_special_orthogonal_tolerance():
218
+ def test_is_special_orthogonal_tolerance() -> None:
219
219
  atol = 0.5
220
220
 
221
221
  # Pays attention to specified tolerance.
@@ -234,7 +234,7 @@ def test_is_special_orthogonal_tolerance():
234
234
  )
235
235
 
236
236
 
237
- def test_is_special_unitary():
237
+ def test_is_special_unitary() -> None:
238
238
  assert cirq.is_special_unitary(np.empty((0, 0)))
239
239
  assert not cirq.is_special_unitary(np.empty((1, 0)))
240
240
  assert not cirq.is_special_unitary(np.empty((0, 1)))
@@ -260,7 +260,7 @@ def test_is_special_unitary():
260
260
  assert cirq.is_special_unitary(np.array([[1, 1j + 1e-11], [1j, 1 + 1j * 1e-9]]) * np.sqrt(0.5))
261
261
 
262
262
 
263
- def test_is_special_unitary_tolerance():
263
+ def test_is_special_unitary_tolerance() -> None:
264
264
  atol = 0.5
265
265
 
266
266
  # Pays attention to specified tolerance.
@@ -277,7 +277,7 @@ def test_is_special_unitary_tolerance():
277
277
  )
278
278
 
279
279
 
280
- def test_is_normal():
280
+ def test_is_normal() -> None:
281
281
  assert cirq.is_normal(np.array([[1]]))
282
282
  assert cirq.is_normal(np.array([[3j]]))
283
283
  assert cirq.is_normal(cirq.testing.random_density_matrix(4))
@@ -286,7 +286,7 @@ def test_is_normal():
286
286
  assert not cirq.is_normal(np.zeros((1, 0)))
287
287
 
288
288
 
289
- def test_is_normal_tolerance():
289
+ def test_is_normal_tolerance() -> None:
290
290
  atol = 0.25
291
291
 
292
292
  # Pays attention to specified tolerance.
@@ -298,7 +298,7 @@ def test_is_normal_tolerance():
298
298
  assert not cirq.is_normal(np.array([[0, 0.5, 0], [0, 0, 0.6], [0, 0, 0]]), atol=atol)
299
299
 
300
300
 
301
- def test_is_cptp():
301
+ def test_is_cptp() -> None:
302
302
  rt2 = np.sqrt(0.5)
303
303
  # Amplitude damping with gamma=0.5.
304
304
  assert cirq.is_cptp(kraus_ops=[np.array([[1, 0], [0, rt2]]), np.array([[0, rt2], [0, 0]])])
@@ -325,15 +325,15 @@ def test_is_cptp():
325
325
  # Makes 4 2x2 kraus ops.
326
326
  one_qubit_u = cirq.testing.random_unitary(8)
327
327
  one_qubit_kraus = np.reshape(one_qubit_u[:, :2], (-1, 2, 2))
328
- assert cirq.is_cptp(kraus_ops=one_qubit_kraus)
328
+ assert cirq.is_cptp(kraus_ops=one_qubit_kraus) # type: ignore[arg-type]
329
329
 
330
330
  # Makes 16 4x4 kraus ops.
331
331
  two_qubit_u = cirq.testing.random_unitary(64)
332
332
  two_qubit_kraus = np.reshape(two_qubit_u[:, :4], (-1, 4, 4))
333
- assert cirq.is_cptp(kraus_ops=two_qubit_kraus)
333
+ assert cirq.is_cptp(kraus_ops=two_qubit_kraus) # type: ignore[arg-type]
334
334
 
335
335
 
336
- def test_is_cptp_tolerance():
336
+ def test_is_cptp_tolerance() -> None:
337
337
  rt2_ish = np.sqrt(0.5) - 0.01
338
338
  atol = 0.25
339
339
  # Moderately-incorrect amplitude damping with gamma=0.5.
@@ -345,7 +345,7 @@ def test_is_cptp_tolerance():
345
345
  )
346
346
 
347
347
 
348
- def test_commutes():
348
+ def test_commutes() -> None:
349
349
  assert matrix_commutes(np.empty((0, 0)), np.empty((0, 0)))
350
350
  assert not matrix_commutes(np.empty((1, 0)), np.empty((0, 1)))
351
351
  assert not matrix_commutes(np.empty((0, 1)), np.empty((1, 0)))
@@ -372,7 +372,7 @@ def test_commutes():
372
372
  assert matrix_commutes(xx, np.diag([1, -1, -1, 1 + 1e-9]))
373
373
 
374
374
 
375
- def test_commutes_tolerance():
375
+ def test_commutes_tolerance() -> None:
376
376
  atol = 0.5
377
377
 
378
378
  x = np.array([[0, 1], [1, 0]])
@@ -383,7 +383,7 @@ def test_commutes_tolerance():
383
383
  assert not matrix_commutes(x, x + z * 0.5, atol=atol)
384
384
 
385
385
 
386
- def test_allclose_up_to_global_phase():
386
+ def test_allclose_up_to_global_phase() -> None:
387
387
  assert cirq.allclose_up_to_global_phase(np.array([1]), np.array([1j]))
388
388
 
389
389
  assert not cirq.allclose_up_to_global_phase(np.array([[[1]]]), np.array([1]))
@@ -402,7 +402,7 @@ def test_allclose_up_to_global_phase():
402
402
  assert not cirq.allclose_up_to_global_phase(np.array([[1]]), np.array([[2]]))
403
403
 
404
404
 
405
- def test_binary_sub_tensor_slice():
405
+ def test_binary_sub_tensor_slice() -> None:
406
406
  a = slice(None)
407
407
  e = Ellipsis
408
408
 
@@ -437,7 +437,7 @@ def test_binary_sub_tensor_slice():
437
437
  assert cirq.slice_for_qubits_equal_to([2], 0b0, num_qubits=3) == (a, a, 0)
438
438
 
439
439
 
440
- def test_binary_sub_tensor_slice_big_endian():
440
+ def test_binary_sub_tensor_slice_big_endian() -> None:
441
441
  a = slice(None)
442
442
  e = Ellipsis
443
443
  sfqet = cirq.slice_for_qubits_equal_to
@@ -473,7 +473,7 @@ def test_binary_sub_tensor_slice_big_endian():
473
473
  assert sfqet([2], big_endian_qureg_value=0b0, num_qubits=3) == (a, a, 0)
474
474
 
475
475
 
476
- def test_qudit_sub_tensor_slice():
476
+ def test_qudit_sub_tensor_slice() -> None:
477
477
  a = slice(None)
478
478
  sfqet = cirq.slice_for_qubits_equal_to
479
479
 
@@ -34,7 +34,7 @@ from cirq.linalg import predicates
34
34
  RaiseValueErrorIfNotProvided: np.ndarray = np.array([])
35
35
 
36
36
 
37
- def reflection_matrix_pow(reflection_matrix: np.ndarray, exponent: float):
37
+ def reflection_matrix_pow(reflection_matrix: np.ndarray, exponent: float) -> np.ndarray:
38
38
  """Raises a matrix with two opposing eigenvalues to a power.
39
39
 
40
40
  Args:
@@ -720,7 +720,7 @@ def factor_density_matrix(
720
720
  return extracted, remainder
721
721
 
722
722
 
723
- def transpose_state_vector_to_axis_order(t: np.ndarray, axes: Sequence[int]):
723
+ def transpose_state_vector_to_axis_order(t: np.ndarray, axes: Sequence[int]) -> np.ndarray:
724
724
  """Transposes the axes of a state vector to a specified order.
725
725
 
726
726
  Args:
@@ -733,7 +733,7 @@ def transpose_state_vector_to_axis_order(t: np.ndarray, axes: Sequence[int]):
733
733
  return np.moveaxis(t, axes, range(len(axes)))
734
734
 
735
735
 
736
- def transpose_density_matrix_to_axis_order(t: np.ndarray, axes: Sequence[int]):
736
+ def transpose_density_matrix_to_axis_order(t: np.ndarray, axes: Sequence[int]) -> np.ndarray:
737
737
  """Transposes the axes of a density matrix to a specified order.
738
738
 
739
739
  Args:
@@ -780,7 +780,9 @@ def _index_from_coordinates(s: Sequence[int], volume: Sequence[int]) -> int:
780
780
  return np.dot(s, volume)
781
781
 
782
782
 
783
- def transpose_flattened_array(t: np.ndarray, shape: Sequence[int], axes: Sequence[int]):
783
+ def transpose_flattened_array(
784
+ t: np.ndarray, shape: Sequence[int], axes: Sequence[int]
785
+ ) -> np.ndarray:
784
786
  """Transposes a flattened array.
785
787
 
786
788
  Equivalent to np.transpose(t.reshape(shape), axes).reshape((-1,)).
@@ -17,7 +17,7 @@ from __future__ import annotations
17
17
  from cirq import ops
18
18
 
19
19
 
20
- def neutral_atom_gateset(max_parallel_z=None, max_parallel_xy=None):
20
+ def neutral_atom_gateset(max_parallel_z=None, max_parallel_xy=None) -> ops.Gateset:
21
21
  return ops.Gateset(
22
22
  ops.AnyIntegerPowerGateFamily(ops.CNotPowGate),
23
23
  ops.AnyIntegerPowerGateFamily(ops.CCNotPowGate),
@@ -117,10 +117,10 @@ class ClassicallyControlledOperation(raw_types.Operation):
117
117
  return self._sub_operation.without_classical_controls()
118
118
 
119
119
  @property
120
- def qubits(self):
120
+ def qubits(self) -> tuple[cirq.Qid, ...]:
121
121
  return self._sub_operation.qubits
122
122
 
123
- def with_qubits(self, *new_qubits):
123
+ def with_qubits(self, *new_qubits) -> cirq.Operation:
124
124
  return self._sub_operation.with_qubits(*new_qubits).with_classical_controls(
125
125
  *self._conditions
126
126
  )
cirq/ops/clifford_gate.py CHANGED
@@ -374,7 +374,7 @@ class CliffordGate(raw_types.Gate, CommonCliffordGates):
374
374
  object.__setattr__(self, '_clifford_tableau', _clifford_tableau.copy())
375
375
 
376
376
  @property
377
- def clifford_tableau(self):
377
+ def clifford_tableau(self) -> qis.CliffordTableau:
378
378
  return self._clifford_tableau
379
379
 
380
380
  def _json_dict_(self) -> dict[str, Any]:
@@ -51,7 +51,7 @@ round_to_2_prec = cirq.CircuitDiagramInfoArgs(
51
51
  )
52
52
 
53
53
 
54
- def assert_mixtures_equal(actual, expected):
54
+ def assert_mixtures_equal(actual, expected) -> None:
55
55
  """Assert equal for tuple of mixed scalar and array types."""
56
56
  for a, e in zip(actual, expected):
57
57
  np.testing.assert_almost_equal(a[0], e[0])