cirq-core 1.6.0.dev20250702182429__py3-none-any.whl → 1.6.0.dev20250704055727__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 (60) hide show
  1. cirq/_version.py +1 -1
  2. cirq/_version_test.py +1 -1
  3. cirq/circuits/circuit.py +4 -2
  4. cirq/circuits/circuit_operation.py +2 -2
  5. cirq/circuits/circuit_test.py +157 -148
  6. cirq/circuits/moment_test.py +61 -51
  7. cirq/circuits/optimization_pass.py +1 -1
  8. cirq/circuits/text_diagram_drawer.py +6 -6
  9. cirq/conftest.py +4 -3
  10. cirq/contrib/acquaintance/bipartite_test.py +10 -8
  11. cirq/contrib/acquaintance/devices_test.py +3 -3
  12. cirq/contrib/acquaintance/executor_test.py +8 -6
  13. cirq/contrib/acquaintance/gates.py +1 -1
  14. cirq/contrib/acquaintance/gates_test.py +18 -16
  15. cirq/contrib/acquaintance/inspection_utils.py +7 -3
  16. cirq/contrib/acquaintance/permutation_test.py +24 -17
  17. cirq/contrib/acquaintance/shift_swap_network_test.py +7 -6
  18. cirq/contrib/acquaintance/strategies/quartic_paired_test.py +8 -6
  19. cirq/contrib/bayesian_network/bayesian_network_gate.py +2 -2
  20. cirq/contrib/bayesian_network/bayesian_network_gate_test.py +10 -8
  21. cirq/contrib/circuitdag/circuit_dag.py +2 -2
  22. cirq/contrib/circuitdag/circuit_dag_test.py +15 -15
  23. cirq/contrib/custom_simulators/custom_state_simulator_test.py +7 -3
  24. cirq/contrib/graph_device/graph_device.py +6 -5
  25. cirq/contrib/graph_device/graph_device_test.py +16 -14
  26. cirq/contrib/graph_device/hypergraph.py +2 -2
  27. cirq/contrib/graph_device/hypergraph_test.py +11 -11
  28. cirq/contrib/graph_device/uniform_graph_device_test.py +9 -3
  29. cirq/contrib/hacks/disable_validation.py +4 -1
  30. cirq/contrib/json.py +2 -2
  31. cirq/contrib/noise_models/noise_models.py +5 -5
  32. cirq/contrib/noise_models/noise_models_test.py +4 -0
  33. cirq/contrib/qcircuit/qcircuit_diagram_info.py +1 -1
  34. cirq/contrib/qcircuit/qcircuit_diagram_info_test.py +2 -1
  35. cirq/contrib/qcircuit/qcircuit_pdf.py +1 -1
  36. cirq/contrib/quantum_volume/quantum_volume_test.py +17 -16
  37. cirq/contrib/quimb/density_matrix.py +1 -1
  38. cirq/contrib/quimb/density_matrix_test.py +7 -7
  39. cirq/devices/noise_model_test.py +16 -14
  40. cirq/transformers/align.py +2 -2
  41. cirq/transformers/dynamical_decoupling.py +4 -4
  42. cirq/transformers/dynamical_decoupling_test.py +1 -1
  43. cirq/transformers/expand_composite_test.py +1 -1
  44. cirq/transformers/measurement_transformers.py +1 -1
  45. cirq/transformers/merge_single_qubit_gates.py +3 -3
  46. cirq/transformers/merge_single_qubit_gates_test.py +2 -2
  47. cirq/transformers/noise_adding.py +2 -2
  48. cirq/transformers/optimize_for_target_gateset.py +3 -3
  49. cirq/transformers/qubit_management_transformers.py +2 -2
  50. cirq/transformers/randomized_measurements.py +7 -7
  51. cirq/transformers/stratify.py +1 -1
  52. cirq/transformers/symbolize.py +1 -1
  53. cirq/transformers/tag_transformers.py +3 -3
  54. cirq/transformers/transformer_api.py +2 -2
  55. cirq/transformers/transformer_primitives.py +6 -6
  56. {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250704055727.dist-info}/METADATA +1 -1
  57. {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250704055727.dist-info}/RECORD +60 -60
  58. {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250704055727.dist-info}/WHEEL +0 -0
  59. {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250704055727.dist-info}/licenses/LICENSE +0 -0
  60. {cirq_core-1.6.0.dev20250702182429.dist-info → cirq_core-1.6.0.dev20250704055727.dist-info}/top_level.txt +0 -0
@@ -27,7 +27,7 @@ import cirq.contrib.acquaintance as cca
27
27
  import cirq.testing as ct
28
28
 
29
29
 
30
- def test_acquaintance_gate_repr():
30
+ def test_acquaintance_gate_repr() -> None:
31
31
  assert (
32
32
  repr(cca.AcquaintanceOpportunityGate(2))
33
33
  == 'cirq.contrib.acquaintance.AcquaintanceOpportunityGate'
@@ -35,7 +35,7 @@ def test_acquaintance_gate_repr():
35
35
  )
36
36
 
37
37
 
38
- def test_acquaintance_gate_text_diagram_info():
38
+ def test_acquaintance_gate_text_diagram_info() -> None:
39
39
  qubits = [cirq.NamedQubit(s) for s in 'xyz']
40
40
  circuit = cirq.Circuit([cirq.Moment([cca.acquaint(*qubits)])])
41
41
  actual_text_diagram = circuit.to_text_diagram().strip()
@@ -49,11 +49,11 @@ z: ───█───
49
49
  assert actual_text_diagram == expected_text_diagram
50
50
 
51
51
 
52
- def test_acquaintance_gate_unknown_qubit_count():
52
+ def test_acquaintance_gate_unknown_qubit_count() -> None:
53
53
  assert cirq.circuit_diagram_info(cca.acquaint, default=None) is None
54
54
 
55
55
 
56
- def test_swap_network_gate():
56
+ def test_swap_network_gate() -> None:
57
57
  qubits = tuple(cirq.NamedQubit(s) for s in alphabet)
58
58
 
59
59
  acquaintance_size = 3
@@ -125,7 +125,7 @@ f: ───╱1╲─────────╱1╲─────────
125
125
  @pytest.mark.parametrize(
126
126
  'part_lens', [tuple(randint(1, 3) for _ in range(randint(2, 10))) for _ in range(3)]
127
127
  )
128
- def test_acquaint_part_pairs(part_lens):
128
+ def test_acquaint_part_pairs(part_lens) -> None:
129
129
  parts = []
130
130
  n_qubits = 0
131
131
  for part_len in part_lens:
@@ -154,13 +154,13 @@ acquaintance_sizes += tuple(range(5))
154
154
  )
155
155
  ),
156
156
  )
157
- def test_swap_network_gate_permutation(part_lens, acquaintance_size):
157
+ def test_swap_network_gate_permutation(part_lens, acquaintance_size) -> None:
158
158
  n_qubits = sum(part_lens)
159
159
  swap_network_gate = cca.SwapNetworkGate(part_lens, acquaintance_size)
160
160
  cca.testing.assert_permutation_decomposition_equivalence(swap_network_gate, n_qubits)
161
161
 
162
162
 
163
- def test_swap_network_gate_from_ops():
163
+ def test_swap_network_gate_from_ops() -> None:
164
164
  n_qubits = 10
165
165
  qubits = cirq.LineQubit.range(n_qubits)
166
166
  part_lens = (1, 2, 1, 3, 3)
@@ -198,7 +198,7 @@ def test_swap_network_gate_from_ops():
198
198
  cirq.testing.assert_has_diagram(circuit, expected_diagram)
199
199
 
200
200
 
201
- def test_swap_network_decomposition():
201
+ def test_swap_network_decomposition() -> None:
202
202
  qubits = cirq.LineQubit.range(8)
203
203
  swap_network_gate = cca.SwapNetworkGate((4, 4), 5)
204
204
  operations = cirq.decompose_once_with_qubits(swap_network_gate, qubits)
@@ -223,7 +223,7 @@ def test_swap_network_decomposition():
223
223
  ct.assert_has_diagram(circuit, expected_text_diagram)
224
224
 
225
225
 
226
- def test_swap_network_init_error():
226
+ def test_swap_network_init_error() -> None:
227
227
  with pytest.raises(ValueError):
228
228
  cca.SwapNetworkGate(())
229
229
  with pytest.raises(ValueError):
@@ -237,7 +237,7 @@ part_lens_and_acquaintance_sizes = [
237
237
 
238
238
 
239
239
  @pytest.mark.parametrize('part_lens, acquaintance_size', part_lens_and_acquaintance_sizes)
240
- def test_swap_network_permutation(part_lens, acquaintance_size):
240
+ def test_swap_network_permutation(part_lens, acquaintance_size) -> None:
241
241
  n_qubits = sum(part_lens)
242
242
  gate = cca.SwapNetworkGate(part_lens, acquaintance_size)
243
243
 
@@ -246,7 +246,7 @@ def test_swap_network_permutation(part_lens, acquaintance_size):
246
246
 
247
247
 
248
248
  @pytest.mark.parametrize('part_lens, acquaintance_size', part_lens_and_acquaintance_sizes)
249
- def test_swap_network_repr(part_lens, acquaintance_size):
249
+ def test_swap_network_repr(part_lens, acquaintance_size) -> None:
250
250
  gate = cca.SwapNetworkGate(part_lens, acquaintance_size)
251
251
  ct.assert_equivalent_repr(gate)
252
252
 
@@ -260,16 +260,18 @@ class OtherOperation(cirq.Operation):
260
260
  return self._qubits
261
261
 
262
262
  def with_qubits(self, *new_qubits: cirq.Qid) -> OtherOperation:
263
- return type(self)(self._qubits)
263
+ return type(self)(new_qubits)
264
264
 
265
265
  def __eq__(self, other):
266
266
  return isinstance(other, type(self)) and self.qubits == other.qubits
267
267
 
268
268
 
269
- def test_get_acquaintance_size():
269
+ def test_get_acquaintance_size() -> None:
270
+ qubits: Sequence[cirq.Qid]
270
271
  qubits = cirq.LineQubit.range(8)
272
+ op: cirq.Operation
271
273
  op = OtherOperation(qubits)
272
- assert op.with_qubits(qubits) == op
274
+ assert op.with_qubits(*qubits) == op
273
275
  assert cca.get_acquaintance_size(op) == 0
274
276
 
275
277
  for s, _ in enumerate(qubits):
@@ -301,7 +303,7 @@ def test_get_acquaintance_size():
301
303
  assert cca.get_acquaintance_size(op) == 0
302
304
 
303
305
 
304
- def test_operations_to_part_lens():
306
+ def test_operations_to_part_lens() -> None:
305
307
  qubits = cirq.LineQubit.range(6)
306
308
  ops = [cirq.CZ(*qubits[1:3]), cirq.XX(*qubits[3:5])]
307
309
  part_lens = cca.gates.operations_to_part_lens(qubits, ops)
@@ -319,7 +321,7 @@ def test_operations_to_part_lens():
319
321
  @pytest.mark.parametrize(
320
322
  'part_len_sets', [set(tuple(randint(1, 5) for _ in range(randint(2, 7))) for _ in range(5))]
321
323
  )
322
- def test_swap_network_gate_equality(part_len_sets):
324
+ def test_swap_network_gate_equality(part_len_sets) -> None:
323
325
  acquaintance_sizes = [None, 0, 1, 2, 3]
324
326
  swap_gates = [cirq.SWAP, cirq.CNOT]
325
327
  equals_tester = ct.EqualsTester()
@@ -51,7 +51,9 @@ class LogicalAnnotator(ExecutionStrategy):
51
51
  yield AcquaintanceOperation(qubits, indices)
52
52
 
53
53
 
54
- def get_acquaintance_dag(strategy: cirq.Circuit, initial_mapping: LogicalMapping):
54
+ def get_acquaintance_dag(
55
+ strategy: cirq.Circuit, initial_mapping: LogicalMapping
56
+ ) -> circuitdag.CircuitDag:
55
57
  strategy = strategy.copy()
56
58
  expose_acquaintance_gates(strategy)
57
59
  LogicalAnnotator(initial_mapping)(strategy)
@@ -66,9 +68,11 @@ def get_acquaintance_dag(strategy: cirq.Circuit, initial_mapping: LogicalMapping
66
68
 
67
69
  def get_logical_acquaintance_opportunities(
68
70
  strategy: cirq.Circuit, initial_mapping: LogicalMapping
69
- ) -> set[frozenset[LogicalIndex]]:
71
+ ) -> set[frozenset[int]] | set[frozenset[cirq.Qid]]:
70
72
  acquaintance_dag = get_acquaintance_dag(strategy, initial_mapping)
71
73
  logical_acquaintance_opportunities = set()
72
74
  for op in acquaintance_dag.all_operations():
73
- logical_acquaintance_opportunities.add(frozenset(op.logical_indices))
75
+ logical_acquaintance_opportunities.add(
76
+ frozenset(op.logical_indices) # type: ignore[attr-defined]
77
+ )
74
78
  return logical_acquaintance_opportunities
@@ -15,6 +15,7 @@
15
15
  from __future__ import annotations
16
16
 
17
17
  import random
18
+ from typing import Iterator
18
19
 
19
20
  import pytest
20
21
 
@@ -23,7 +24,7 @@ import cirq.contrib.acquaintance as cca
23
24
  import cirq.testing as ct
24
25
 
25
26
 
26
- def test_swap_permutation_gate():
27
+ def test_swap_permutation_gate() -> None:
27
28
  no_decomp = lambda op: (isinstance(op, cirq.GateOperation) and op.gate == cirq.SWAP)
28
29
  a, b = cirq.NamedQubit('a'), cirq.NamedQubit('b')
29
30
  gate = cca.SwapPermutationGate()
@@ -42,7 +43,7 @@ def test_swap_permutation_gate():
42
43
  cirq.commutes(gate, cirq.CCZ)
43
44
 
44
45
 
45
- def test_validate_permutation_errors():
46
+ def test_validate_permutation_errors() -> None:
46
47
  validate_permutation = cca.PermutationGate.validate_permutation
47
48
  validate_permutation({})
48
49
 
@@ -59,7 +60,7 @@ def test_validate_permutation_errors():
59
60
  assert cirq.circuit_diagram_info(gate, default=None) is None
60
61
 
61
62
 
62
- def test_diagram():
63
+ def test_diagram() -> None:
63
64
  gate = cca.SwapPermutationGate()
64
65
  a, b = cirq.NamedQubit('a'), cirq.NamedQubit('b')
65
66
  circuit = cirq.Circuit([gate(a, b)])
@@ -72,17 +73,19 @@ b: ───1↦0───
72
73
  assert actual_text_diagram == expected_text_diagram
73
74
 
74
75
 
75
- def test_update_mapping():
76
+ def test_update_mapping() -> None:
76
77
  gate = cca.SwapPermutationGate()
77
78
  a, b, c = (cirq.NamedQubit(s) for s in 'abc')
79
+ mapping: dict[cirq.Qid, int]
78
80
  mapping = {s: i for i, s in enumerate((a, b, c))}
79
81
  ops = [gate(a, b), gate(b, c)]
80
82
  cca.update_mapping(mapping, ops)
81
83
  assert mapping == {a: 1, b: 2, c: 0}
82
84
 
83
85
 
84
- def test_get_logical_operations():
86
+ def test_get_logical_operations() -> None:
85
87
  a, b, c, d = qubits = cirq.LineQubit.range(4)
88
+ mapping: dict[cirq.Qid, cirq.Qid]
86
89
  mapping = dict(zip(qubits, qubits))
87
90
  operations = [
88
91
  cirq.ZZ(a, b),
@@ -106,7 +109,7 @@ def test_get_logical_operations():
106
109
  for n_elements in (random.randint(5, 20) for _ in range(20))
107
110
  ),
108
111
  )
109
- def test_linear_permutation_gate(n_elements, n_permuted):
112
+ def test_linear_permutation_gate(n_elements, n_permuted) -> None:
110
113
  qubits = cirq.LineQubit.range(n_elements)
111
114
  elements = tuple(range(n_elements))
112
115
  elements_to_permute = random.sample(elements, n_permuted)
@@ -116,16 +119,18 @@ def test_linear_permutation_gate(n_elements, n_permuted):
116
119
  gate = cca.LinearPermutationGate(n_elements, permutation)
117
120
  ct.assert_equivalent_repr(gate)
118
121
  assert gate.permutation() == permutation
122
+ mapping: dict[cirq.Qid, int]
119
123
  mapping = dict(zip(qubits, elements))
120
124
  for swap in cirq.flatten_op_tree(cirq.decompose_once_with_qubits(gate, qubits)):
121
125
  assert isinstance(swap, cirq.GateOperation)
126
+ assert isinstance(swap.gate, cca.PermutationGate)
122
127
  swap.gate.update_mapping(mapping, swap.qubits)
123
128
  for i in range(n_elements):
124
129
  p = permutation.get(elements[i], i)
125
130
  assert mapping.get(qubits[p], elements[i]) == i
126
131
 
127
132
 
128
- def random_equal_permutations(n_perms, n_items, prob):
133
+ def random_equal_permutations(n_perms, n_items, prob) -> list[dict[int, int]]:
129
134
  indices_to_permute = [i for i in range(n_items) if random.random() <= prob]
130
135
  permuted_indices = random.sample(indices_to_permute, len(indices_to_permute))
131
136
  base_permutation = dict(zip(indices_to_permute, permuted_indices))
@@ -138,7 +143,9 @@ def random_equal_permutations(n_perms, n_items, prob):
138
143
  return permutations
139
144
 
140
145
 
141
- def random_permutation_equality_groups(n_groups, n_perms_per_group, n_items, prob):
146
+ def random_permutation_equality_groups(
147
+ n_groups, n_perms_per_group, n_items, prob
148
+ ) -> Iterator[list[dict[int, int]]]:
142
149
  fingerprints = set()
143
150
  for _ in range(n_groups):
144
151
  perms = random_equal_permutations(n_perms_per_group, n_items, prob)
@@ -150,7 +157,7 @@ def random_permutation_equality_groups(n_groups, n_perms_per_group, n_items, pro
150
157
 
151
158
 
152
159
  @pytest.mark.parametrize('permutation_sets', [random_permutation_equality_groups(5, 3, 10, 0.5)])
153
- def test_linear_permutation_gate_equality(permutation_sets):
160
+ def test_linear_permutation_gate_equality(permutation_sets) -> None:
154
161
  swap_gates = [cirq.SWAP, cirq.CNOT]
155
162
  equals_tester = ct.EqualsTester()
156
163
  for swap_gate in swap_gates:
@@ -163,7 +170,7 @@ def test_linear_permutation_gate_equality(permutation_sets):
163
170
  )
164
171
 
165
172
 
166
- def test_linear_permutation_gate_pow_not_implemented():
173
+ def test_linear_permutation_gate_pow_not_implemented() -> None:
167
174
  permutation_gate = cca.LinearPermutationGate(3, {0: 1, 1: 2, 2: 0})
168
175
 
169
176
  assert permutation_gate.__pow__(0) is NotImplemented
@@ -183,7 +190,7 @@ def test_linear_permutation_gate_pow_not_implemented():
183
190
  (4, {0: 3, 1: 2, 2: 1, 3: 0}),
184
191
  ],
185
192
  )
186
- def test_linear_permutation_gate_pow_identity(num_qubits, permutation):
193
+ def test_linear_permutation_gate_pow_identity(num_qubits, permutation) -> None:
187
194
  permutation_gate = cca.LinearPermutationGate(num_qubits, permutation)
188
195
 
189
196
  assert permutation_gate**1 == permutation_gate
@@ -199,7 +206,7 @@ def test_linear_permutation_gate_pow_identity(num_qubits, permutation):
199
206
  (4, {0: 3, 1: 2, 2: 1, 3: 0}, {0: 3, 1: 2, 2: 1, 3: 0}),
200
207
  ],
201
208
  )
202
- def test_linear_permutation_gate_pow_inverse(num_qubits, permutation, inverse):
209
+ def test_linear_permutation_gate_pow_inverse(num_qubits, permutation, inverse) -> None:
203
210
  permutation_gate = cca.LinearPermutationGate(num_qubits, permutation)
204
211
  inverse_gate = cca.LinearPermutationGate(num_qubits, inverse)
205
212
 
@@ -207,7 +214,7 @@ def test_linear_permutation_gate_pow_inverse(num_qubits, permutation, inverse):
207
214
  assert cirq.inverse(permutation_gate) == inverse_gate
208
215
 
209
216
 
210
- def test_display_mapping():
217
+ def test_display_mapping() -> None:
211
218
  indices = [4, 2, 0, 1, 3]
212
219
  qubits = cirq.LineQubit.range(len(indices))
213
220
  circuit = cca.complete_acquaintance_strategy(qubits, 2)
@@ -238,7 +245,7 @@ def test_display_mapping():
238
245
  for _ in range(4)
239
246
  ],
240
247
  )
241
- def test_return_to_initial_mapping(circuit):
248
+ def test_return_to_initial_mapping(circuit) -> None:
242
249
  qubits = sorted(circuit.all_qubits())
243
250
  cca.return_to_initial_mapping(circuit)
244
251
  initial_mapping = {q: i for i, q in enumerate(qubits)}
@@ -247,7 +254,7 @@ def test_return_to_initial_mapping(circuit):
247
254
  assert mapping == initial_mapping
248
255
 
249
256
 
250
- def test_uses_consistent_swap_gate():
257
+ def test_uses_consistent_swap_gate() -> None:
251
258
  a, b = cirq.LineQubit.range(2)
252
259
  circuit = cirq.Circuit([cca.SwapPermutationGate()(a, b), cca.SwapPermutationGate()(a, b)])
253
260
  assert cca.uses_consistent_swap_gate(circuit, cirq.SWAP)
@@ -264,7 +271,7 @@ def test_uses_consistent_swap_gate():
264
271
  assert not cca.uses_consistent_swap_gate(circuit, cirq.CZ)
265
272
 
266
273
 
267
- def test_swap_gate_eq():
274
+ def test_swap_gate_eq() -> None:
268
275
  assert cca.SwapPermutationGate() == cca.SwapPermutationGate(cirq.SWAP)
269
276
  assert cca.SwapPermutationGate() != cca.SwapPermutationGate(cirq.CZ)
270
277
  assert cca.SwapPermutationGate(cirq.CZ) == cca.SwapPermutationGate(cirq.CZ)
@@ -278,5 +285,5 @@ def test_swap_gate_eq():
278
285
  cca.SwapPermutationGate(cirq.CZ),
279
286
  ],
280
287
  )
281
- def test_swap_gate_repr(gate):
288
+ def test_swap_gate_repr(gate) -> None:
282
289
  cirq.testing.assert_equivalent_repr(gate)
@@ -23,7 +23,7 @@ import cirq
23
23
  import cirq.contrib.acquaintance as cca
24
24
 
25
25
 
26
- def random_part_lens(max_n_parts, max_part_size):
26
+ def random_part_lens(max_n_parts, max_part_size) -> tuple[int, ...]:
27
27
  return tuple(random.randint(1, max_part_size) for _ in range(random.randint(1, max_n_parts)))
28
28
 
29
29
 
@@ -31,7 +31,7 @@ def random_part_lens(max_n_parts, max_part_size):
31
31
  'left_part_lens,right_part_lens',
32
32
  [tuple(random_part_lens(7, 2) for _ in ('left', 'right')) for _ in range(5)],
33
33
  )
34
- def test_shift_swap_network_gate_acquaintance_opps(left_part_lens, right_part_lens):
34
+ def test_shift_swap_network_gate_acquaintance_opps(left_part_lens, right_part_lens) -> None:
35
35
 
36
36
  gate = cca.ShiftSwapNetworkGate(left_part_lens, right_part_lens)
37
37
  n_qubits = gate.qubit_count()
@@ -45,6 +45,7 @@ def test_shift_swap_network_gate_acquaintance_opps(left_part_lens, right_part_le
45
45
  # expected opps
46
46
  i = 0
47
47
  sides = ('left', 'right')
48
+ parts: dict[str, list[set[int]]]
48
49
  parts = {side: [] for side in sides}
49
50
  for side, part_lens in zip(sides, (left_part_lens, right_part_lens)):
50
51
  for part_len in part_lens:
@@ -205,7 +206,7 @@ circuit_diagrams = {
205
206
 
206
207
 
207
208
  @pytest.mark.parametrize('left_part_lens,right_part_lens', set(key[1:] for key in circuit_diagrams))
208
- def test_shift_swap_network_gate_diagrams(left_part_lens, right_part_lens):
209
+ def test_shift_swap_network_gate_diagrams(left_part_lens, right_part_lens) -> None:
209
210
 
210
211
  gate = cca.ShiftSwapNetworkGate(left_part_lens, right_part_lens)
211
212
  n_qubits = gate.qubit_count()
@@ -220,7 +221,7 @@ def test_shift_swap_network_gate_diagrams(left_part_lens, right_part_lens):
220
221
  cirq.testing.assert_has_diagram(circuit, diagram)
221
222
 
222
223
 
223
- def test_shift_swap_network_gate_bad_part_lens():
224
+ def test_shift_swap_network_gate_bad_part_lens() -> None:
224
225
  with pytest.raises(ValueError):
225
226
  cca.ShiftSwapNetworkGate((0, 1, 1), (2, 2))
226
227
  with pytest.raises(ValueError):
@@ -235,7 +236,7 @@ def test_shift_swap_network_gate_bad_part_lens():
235
236
  'left_part_lens,right_part_lens',
236
237
  [tuple(random_part_lens(2, 2) for _ in ('left', 'right')) for _ in range(5)],
237
238
  )
238
- def test_shift_swap_network_gate_repr(left_part_lens, right_part_lens):
239
+ def test_shift_swap_network_gate_repr(left_part_lens, right_part_lens) -> None:
239
240
  gate = cca.ShiftSwapNetworkGate(left_part_lens, right_part_lens)
240
241
  cirq.testing.assert_equivalent_repr(gate)
241
242
 
@@ -247,7 +248,7 @@ def test_shift_swap_network_gate_repr(left_part_lens, right_part_lens):
247
248
  'left_part_lens,right_part_lens',
248
249
  [tuple(random_part_lens(2, 2) for _ in ('left', 'right')) for _ in range(5)],
249
250
  )
250
- def test_shift_swap_network_gate_permutation(left_part_lens, right_part_lens):
251
+ def test_shift_swap_network_gate_permutation(left_part_lens, right_part_lens) -> None:
251
252
  gate = cca.ShiftSwapNetworkGate(left_part_lens, right_part_lens)
252
253
  n_qubits = gate.qubit_count()
253
254
  cca.testing.assert_permutation_decomposition_equivalence(gate, n_qubits)
@@ -16,6 +16,7 @@ from __future__ import annotations
16
16
 
17
17
  import itertools
18
18
  import random
19
+ from typing import Iterable
19
20
 
20
21
  import pytest
21
22
 
@@ -23,27 +24,28 @@ import cirq
23
24
  import cirq.contrib.acquaintance as cca
24
25
 
25
26
 
26
- def test_bad_qubit_pairs():
27
+ def test_bad_qubit_pairs() -> None:
27
28
  a, b, c, d, e = cirq.LineQubit.range(5)
28
29
  bad_qubit_pairs = [(a, b), (c, d), (e,)]
29
30
  with pytest.raises(ValueError):
30
31
  cca.strategies.quartic_paired.qubit_pairs_to_qubit_order(bad_qubit_pairs)
31
32
 
32
33
 
33
- def random_index_pairs(n_pairs: int):
34
+ def random_index_pairs(n_pairs: int) -> tuple[tuple[int, int], ...]:
34
35
  indices = list(range(2 * n_pairs))
35
36
  random.shuffle(indices)
36
- return tuple(indices[2 * i : 2 * (i + 1)] for i in range(n_pairs))
37
+ return tuple(zip(indices[0::2], indices[1::2]))
37
38
 
38
39
 
39
40
  @pytest.mark.parametrize(
40
41
  'index_pairs', [random_index_pairs(n_pairs) for n_pairs in range(2, 7) for _ in range(2)]
41
42
  )
42
- def test_quartic_paired_acquaintances(index_pairs):
43
+ def test_quartic_paired_acquaintances(index_pairs) -> None:
43
44
  n_pairs = len(index_pairs)
44
- qubit_pairs = tuple(tuple(cirq.LineQubit(x) for x in index_pair) for index_pair in index_pairs)
45
+ qubit_pairs: Iterable[tuple[cirq.Qid, cirq.Qid]]
46
+ qubit_pairs = tuple((cirq.LineQubit(x), cirq.LineQubit(y)) for x, y in index_pairs)
45
47
  strategy, qubits = cca.quartic_paired_acquaintance_strategy(qubit_pairs)
46
- initial_mapping = {q: q.x for q in qubits}
48
+ initial_mapping = {q: q.x for q in qubits} # type: ignore[attr-defined]
47
49
  opps = cca.get_logical_acquaintance_opportunities(strategy, initial_mapping)
48
50
  assert set(len(opp) for opp in opps) == set([2, 4])
49
51
  quadratic_opps = set(opp for opp in opps if len(opp) == 2)
@@ -97,7 +97,7 @@ class BayesianNetworkGate(raw_types.Gate):
97
97
  def __init__(
98
98
  self,
99
99
  init_probs: list[tuple[str, float | None]],
100
- arc_probs: list[tuple[str, tuple[str], list[float]]],
100
+ arc_probs: list[tuple[str, tuple[str, ...], list[float]]],
101
101
  ):
102
102
  """Builds a BayesianNetworkGate.
103
103
 
@@ -195,7 +195,7 @@ class BayesianNetworkGate(raw_types.Gate):
195
195
  list[tuple[str, float | None]], [(param, init_prob) for param, init_prob in init_probs]
196
196
  )
197
197
  converted_cond_probs = cast(
198
- list[tuple[str, tuple[str], list[float]]],
198
+ list[tuple[str, tuple[str, ...], list[float]]],
199
199
  [(target, tuple(params), cond_probs) for target, params, cond_probs in arc_probs],
200
200
  )
201
201
  return cls(converted_init_probs, converted_cond_probs)
@@ -21,14 +21,14 @@ import cirq
21
21
  import cirq.contrib.bayesian_network as ccb
22
22
 
23
23
 
24
- def test_basic_properties():
24
+ def test_basic_properties() -> None:
25
25
  gate = ccb.BayesianNetworkGate([('q0', None), ('q1', None), ('q2', None)], [])
26
26
 
27
27
  assert gate._has_unitary_()
28
28
  assert gate._qid_shape_() == (2, 2, 2)
29
29
 
30
30
 
31
- def test_incorrect_constructor():
31
+ def test_incorrect_constructor() -> None:
32
32
  # Success building.
33
33
  ccb.BayesianNetworkGate([('q0', 0.0), ('q1', None)], [('q1', ('q0',), [0.0, 0.0])])
34
34
 
@@ -37,7 +37,9 @@ def test_incorrect_constructor():
37
37
 
38
38
  # This is an easy mistake where the tuple for q0 doesn't have the comma at the end.
39
39
  with pytest.raises(ValueError, match='Conditional prob params must be a tuple.'):
40
- ccb.BayesianNetworkGate([('q0', 0.0), ('q1', None)], [('q1', ('q0'), [0.0, 0.0])])
40
+ ccb.BayesianNetworkGate(
41
+ [('q0', 0.0), ('q1', None)], [('q1', ('q0'), [0.0, 0.0])] # type: ignore
42
+ )
41
43
 
42
44
  with pytest.raises(ValueError, match='Incorrect number of conditional probs.'):
43
45
  ccb.BayesianNetworkGate([('q0', 0.0), ('q1', None)], [('q1', ('q0',), [0.0])])
@@ -46,7 +48,7 @@ def test_incorrect_constructor():
46
48
  ccb.BayesianNetworkGate([('q0', 0.0), ('q1', None)], [('q1', ('q0',), [2016.0913, 0.0])])
47
49
 
48
50
 
49
- def test_repr():
51
+ def test_repr() -> None:
50
52
  gate = ccb.BayesianNetworkGate([('q0', 0.0), ('q1', None)], [('q1', ('q0',), [0.0, 0.0])])
51
53
 
52
54
  assert repr(gate) == (
@@ -56,7 +58,7 @@ def test_repr():
56
58
 
57
59
 
58
60
  @pytest.mark.parametrize('input_prob', [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
59
- def test_prob_encoding(input_prob):
61
+ def test_prob_encoding(input_prob) -> None:
60
62
  q = cirq.NamedQubit('q')
61
63
  gate = ccb.BayesianNetworkGate([('q', input_prob)], [])
62
64
  circuit = cirq.Circuit(gate.on(q))
@@ -82,7 +84,7 @@ def test_prob_encoding(input_prob):
82
84
  ],
83
85
  )
84
86
  @pytest.mark.parametrize('decompose', [True, False])
85
- def test_initial_probs(p0, p1, p2, expected_probs, decompose):
87
+ def test_initial_probs(p0, p1, p2, expected_probs, decompose) -> None:
86
88
  q0, q1, q2 = cirq.LineQubit.range(3)
87
89
  gate = ccb.BayesianNetworkGate([('q0', p0), ('q1', p1), ('q2', p2)], [])
88
90
  if decompose:
@@ -102,7 +104,7 @@ def test_initial_probs(p0, p1, p2, expected_probs, decompose):
102
104
  [(0.0, 0.0, 0.1), (0.0, 1.0, 0.2), (1.0, 0.0, 0.3), (1.0, 1.0, 0.4)],
103
105
  )
104
106
  @pytest.mark.parametrize('decompose', [True, False])
105
- def test_arc_probs(input_prob_q0, input_prob_q1, expected_prob_q2, decompose):
107
+ def test_arc_probs(input_prob_q0, input_prob_q1, expected_prob_q2, decompose) -> None:
106
108
  q0, q1, q2 = cirq.LineQubit.range(3)
107
109
  gate = ccb.BayesianNetworkGate(
108
110
  [('q0', input_prob_q0), ('q1', input_prob_q1), ('q2', None)],
@@ -122,7 +124,7 @@ def test_arc_probs(input_prob_q0, input_prob_q1, expected_prob_q2, decompose):
122
124
  np.testing.assert_almost_equal(actual_prob_q2_is_one, expected_prob_q2, decimal=4)
123
125
 
124
126
 
125
- def test_repro_figure_10_of_paper():
127
+ def test_repro_figure_10_of_paper() -> None:
126
128
  # We try to create the network of figure 10 and check that the probabilities are the same as
127
129
  # the ones in table 10 of https://arxiv.org/abs/2004.14803.
128
130
  ir = cirq.NamedQubit('q4_IR')
@@ -44,7 +44,7 @@ class Unique(Generic[T]):
44
44
  def __repr__(self) -> str:
45
45
  return f'cirq.contrib.Unique({id(self)}, {self.val!r})'
46
46
 
47
- def __lt__(self, other):
47
+ def __lt__(self, other) -> bool:
48
48
  if not isinstance(other, type(self)):
49
49
  return NotImplemented
50
50
  return id(self) < id(other)
@@ -176,7 +176,7 @@ class CircuitDag(networkx.DiGraph):
176
176
  def all_operations(self) -> Iterator[cirq.Operation]:
177
177
  return (node.val for node in self.ordered_nodes())
178
178
 
179
- def all_qubits(self):
179
+ def all_qubits(self) -> frozenset[cirq.Qid]:
180
180
  return frozenset(q for node in self.nodes for q in node.val.qubits)
181
181
 
182
182
  def to_circuit(self) -> cirq.Circuit:
@@ -27,7 +27,7 @@ class FakeDevice(cirq.Device):
27
27
  pass
28
28
 
29
29
 
30
- def test_wrapper_eq():
30
+ def test_wrapper_eq() -> None:
31
31
  q0, q1 = cirq.LineQubit.range(2)
32
32
  eq = cirq.testing.EqualsTester()
33
33
  eq.add_equality_group(cirq.contrib.CircuitDag.make_node(cirq.X(q0)))
@@ -36,7 +36,7 @@ def test_wrapper_eq():
36
36
  eq.add_equality_group(cirq.contrib.CircuitDag.make_node(cirq.X(q1)))
37
37
 
38
38
 
39
- def test_wrapper_cmp():
39
+ def test_wrapper_cmp() -> None:
40
40
  u0 = cirq.contrib.Unique(0)
41
41
  u1 = cirq.contrib.Unique(1)
42
42
  # The ordering of Unique instances is unpredictable
@@ -51,14 +51,14 @@ def test_wrapper_cmp():
51
51
  assert u1 >= u0
52
52
 
53
53
 
54
- def test_wrapper_cmp_failure():
54
+ def test_wrapper_cmp_failure() -> None:
55
55
  with pytest.raises(TypeError):
56
56
  _ = object() < cirq.contrib.Unique(1)
57
57
  with pytest.raises(TypeError):
58
58
  _ = cirq.contrib.Unique(1) < object()
59
59
 
60
60
 
61
- def test_wrapper_repr():
61
+ def test_wrapper_repr() -> None:
62
62
  q0 = cirq.LineQubit(0)
63
63
 
64
64
  node = cirq.contrib.CircuitDag.make_node(cirq.X(q0))
@@ -66,14 +66,14 @@ def test_wrapper_repr():
66
66
  assert repr(node) == expected
67
67
 
68
68
 
69
- def test_init():
69
+ def test_init() -> None:
70
70
  dag = cirq.contrib.CircuitDag()
71
71
  assert networkx.dag.is_directed_acyclic_graph(dag)
72
72
  assert list(dag.nodes()) == []
73
73
  assert list(dag.edges()) == []
74
74
 
75
75
 
76
- def test_append():
76
+ def test_append() -> None:
77
77
  q0 = cirq.LineQubit(0)
78
78
  dag = cirq.contrib.CircuitDag()
79
79
  dag.append(cirq.X(q0))
@@ -83,7 +83,7 @@ def test_append():
83
83
  assert [(n1.val, n2.val) for n1, n2 in dag.edges()] == [(cirq.X(q0), cirq.Y(q0))]
84
84
 
85
85
 
86
- def test_two_identical_ops():
86
+ def test_two_identical_ops() -> None:
87
87
  q0 = cirq.LineQubit(0)
88
88
  dag = cirq.contrib.CircuitDag()
89
89
  dag.append(cirq.X(q0))
@@ -98,7 +98,7 @@ def test_two_identical_ops():
98
98
  }
99
99
 
100
100
 
101
- def test_from_ops():
101
+ def test_from_ops() -> None:
102
102
  q0 = cirq.LineQubit(0)
103
103
  dag = cirq.contrib.CircuitDag.from_ops(cirq.X(q0), cirq.Y(q0))
104
104
  assert networkx.dag.is_directed_acyclic_graph(dag)
@@ -106,7 +106,7 @@ def test_from_ops():
106
106
  assert [(n1.val, n2.val) for n1, n2 in dag.edges()] == [(cirq.X(q0), cirq.Y(q0))]
107
107
 
108
108
 
109
- def test_from_circuit():
109
+ def test_from_circuit() -> None:
110
110
  q0 = cirq.LineQubit(0)
111
111
  circuit = cirq.Circuit(cirq.X(q0), cirq.Y(q0))
112
112
  dag = cirq.contrib.CircuitDag.from_circuit(circuit)
@@ -116,14 +116,14 @@ def test_from_circuit():
116
116
  assert sorted(circuit.all_qubits()) == sorted(dag.all_qubits())
117
117
 
118
118
 
119
- def test_to_empty_circuit():
119
+ def test_to_empty_circuit() -> None:
120
120
  circuit = cirq.Circuit()
121
121
  dag = cirq.contrib.CircuitDag.from_circuit(circuit)
122
122
  assert networkx.dag.is_directed_acyclic_graph(dag)
123
123
  assert circuit == dag.to_circuit()
124
124
 
125
125
 
126
- def test_to_circuit():
126
+ def test_to_circuit() -> None:
127
127
  q0 = cirq.LineQubit(0)
128
128
  circuit = cirq.Circuit(cirq.X(q0), cirq.Y(q0))
129
129
  dag = cirq.contrib.CircuitDag.from_circuit(circuit)
@@ -137,7 +137,7 @@ def test_to_circuit():
137
137
  )
138
138
 
139
139
 
140
- def test_equality():
140
+ def test_equality() -> None:
141
141
  q0, q1 = cirq.LineQubit.range(2)
142
142
  circuit1 = cirq.Circuit(
143
143
  cirq.X(q0), cirq.Y(q0), cirq.Z(q1), cirq.CZ(q0, q1), cirq.X(q1), cirq.Y(q1), cirq.Z(q0)
@@ -167,7 +167,7 @@ def test_equality():
167
167
  eq.add_equality_group(cirq.contrib.CircuitDag.from_circuit(circuit4))
168
168
 
169
169
 
170
- def test_larger_circuit():
170
+ def test_larger_circuit() -> None:
171
171
  q0, q1, q2, q3 = [
172
172
  cirq.GridQubit(0, 5),
173
173
  cirq.GridQubit(1, 5),
@@ -213,7 +213,7 @@ def test_larger_circuit():
213
213
 
214
214
 
215
215
  @pytest.mark.parametrize('circuit', [cirq.testing.random_circuit(10, 10, 0.5) for _ in range(3)])
216
- def test_is_maximalist(circuit):
216
+ def test_is_maximalist(circuit) -> None:
217
217
  dag = cirq.contrib.CircuitDag.from_circuit(circuit)
218
218
  transitive_closure = networkx.dag.transitive_closure(dag)
219
219
  assert cirq.contrib.CircuitDag(incoming_graph_data=transitive_closure) == dag
@@ -232,7 +232,7 @@ def _get_circuits_and_is_blockers():
232
232
 
233
233
 
234
234
  @pytest.mark.parametrize('circuit, is_blocker', _get_circuits_and_is_blockers())
235
- def test_findall_nodes_until_blocked(circuit, is_blocker):
235
+ def test_findall_nodes_until_blocked(circuit, is_blocker) -> None:
236
236
  dag = cirq.contrib.CircuitDag.from_circuit(circuit)
237
237
  all_nodes = list(dag.ordered_nodes())
238
238
  found_nodes = list(dag.findall_nodes_until_blocked(is_blocker))