cirq-core 1.6.0.dev20250702012506__py3-none-any.whl → 1.6.0.dev20250702211226__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 (46) 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/paulistring/pauli_string_measurement_with_readout_mitigation.py +7 -6
  34. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +6 -9
  35. cirq/contrib/qcircuit/qcircuit_diagram_info.py +1 -1
  36. cirq/contrib/qcircuit/qcircuit_diagram_info_test.py +2 -1
  37. cirq/contrib/qcircuit/qcircuit_pdf.py +1 -1
  38. cirq/contrib/quantum_volume/quantum_volume_test.py +17 -16
  39. cirq/contrib/quimb/density_matrix.py +1 -1
  40. cirq/contrib/quimb/density_matrix_test.py +7 -7
  41. cirq/devices/noise_model_test.py +16 -14
  42. {cirq_core-1.6.0.dev20250702012506.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/METADATA +1 -1
  43. {cirq_core-1.6.0.dev20250702012506.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/RECORD +46 -46
  44. {cirq_core-1.6.0.dev20250702012506.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/WHEEL +0 -0
  45. {cirq_core-1.6.0.dev20250702012506.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/licenses/LICENSE +0 -0
  46. {cirq_core-1.6.0.dev20250702012506.dist-info → cirq_core-1.6.0.dev20250702211226.dist-info}/top_level.txt +0 -0
@@ -22,7 +22,7 @@ import cirq
22
22
  import cirq.testing
23
23
 
24
24
 
25
- def test_validation():
25
+ def test_validation() -> None:
26
26
  a = cirq.NamedQubit('a')
27
27
  b = cirq.NamedQubit('b')
28
28
  c = cirq.NamedQubit('c')
@@ -44,7 +44,7 @@ def test_validation():
44
44
  _ = cirq.Moment([cirq.CZ(a, c), cirq.CZ(c, d)])
45
45
 
46
46
 
47
- def test_equality():
47
+ def test_equality() -> None:
48
48
  a = cirq.NamedQubit('a')
49
49
  b = cirq.NamedQubit('b')
50
50
  c = cirq.NamedQubit('c')
@@ -71,7 +71,7 @@ def test_equality():
71
71
  eq.make_equality_group(lambda: cirq.Moment([cirq.CZ(a, c), cirq.CZ(b, d)]))
72
72
 
73
73
 
74
- def test_approx_eq():
74
+ def test_approx_eq() -> None:
75
75
  a = cirq.NamedQubit('a')
76
76
  b = cirq.NamedQubit('b')
77
77
 
@@ -97,7 +97,7 @@ def test_approx_eq():
97
97
  )
98
98
 
99
99
 
100
- def test_operates_on_single_qubit():
100
+ def test_operates_on_single_qubit() -> None:
101
101
  a = cirq.NamedQubit('a')
102
102
  b = cirq.NamedQubit('b')
103
103
  c = cirq.NamedQubit('c')
@@ -121,7 +121,7 @@ def test_operates_on_single_qubit():
121
121
  assert not cirq.Moment([cirq.X(a), cirq.X(b)]).operates_on_single_qubit(c)
122
122
 
123
123
 
124
- def test_operates_on():
124
+ def test_operates_on() -> None:
125
125
  a = cirq.NamedQubit('a')
126
126
  b = cirq.NamedQubit('b')
127
127
  c = cirq.NamedQubit('c')
@@ -157,7 +157,7 @@ def test_operates_on():
157
157
  assert cirq.Moment([cirq.X(a), cirq.X(b)]).operates_on([a, b, c])
158
158
 
159
159
 
160
- def test_operation_at():
160
+ def test_operation_at() -> None:
161
161
  a = cirq.NamedQubit('a')
162
162
  b = cirq.NamedQubit('b')
163
163
  c = cirq.NamedQubit('c')
@@ -172,14 +172,14 @@ def test_operation_at():
172
172
  assert cirq.Moment([cirq.CZ(a, b), cirq.X(c)]).operation_at(a) == cirq.CZ(a, b)
173
173
 
174
174
 
175
- def test_from_ops():
175
+ def test_from_ops() -> None:
176
176
  a = cirq.NamedQubit('a')
177
177
  b = cirq.NamedQubit('b')
178
178
 
179
179
  assert cirq.Moment.from_ops(cirq.X(a), cirq.Y(b)) == cirq.Moment(cirq.X(a), cirq.Y(b))
180
180
 
181
181
 
182
- def test_with_operation():
182
+ def test_with_operation() -> None:
183
183
  a = cirq.NamedQubit('a')
184
184
  b = cirq.NamedQubit('b')
185
185
 
@@ -204,7 +204,7 @@ def test_with_operation():
204
204
  _ = cirq.Moment([cirq.X(a), cirq.X(b)]).with_operation(cirq.X(b))
205
205
 
206
206
 
207
- def test_with_operations():
207
+ def test_with_operations() -> None:
208
208
  a = cirq.NamedQubit('a')
209
209
  b = cirq.NamedQubit('b')
210
210
  c = cirq.NamedQubit('c')
@@ -238,7 +238,7 @@ def test_with_operations():
238
238
  _ = cirq.Moment([cirq.X(a), cirq.X(b)]).with_operations(cirq.X(b))
239
239
 
240
240
 
241
- def test_without_operations_touching():
241
+ def test_without_operations_touching() -> None:
242
242
  a = cirq.NamedQubit('a')
243
243
  b = cirq.NamedQubit('b')
244
244
  c = cirq.NamedQubit('c')
@@ -284,14 +284,14 @@ def test_without_operations_touching():
284
284
  )
285
285
 
286
286
 
287
- def test_is_parameterized():
287
+ def test_is_parameterized() -> None:
288
288
  a, b = cirq.LineQubit.range(2)
289
289
  moment = cirq.Moment(cirq.X(a) ** sympy.Symbol('v'), cirq.Y(b) ** sympy.Symbol('w'))
290
290
  assert cirq.is_parameterized(moment)
291
291
  assert not cirq.is_parameterized(cirq.Moment(cirq.X(a), cirq.Y(b)))
292
292
 
293
293
 
294
- def test_resolve_parameters():
294
+ def test_resolve_parameters() -> None:
295
295
  a, b = cirq.LineQubit.range(2)
296
296
  moment = cirq.Moment(cirq.X(a) ** sympy.Symbol('v'), cirq.Y(b) ** sympy.Symbol('w'))
297
297
  resolved_moment = cirq.resolve_parameters(moment, cirq.ParamResolver({'v': 0.1, 'w': 0.2}))
@@ -301,12 +301,13 @@ def test_resolve_parameters():
301
301
  resolved_moment = cirq.resolve_parameters(moment, {'pi': np.pi})
302
302
  assert resolved_moment == cirq.Moment(cirq.Rz(rads=np.pi).on(a))
303
303
  resolved_gate = resolved_moment.operations[0].gate
304
+ assert isinstance(resolved_gate, cirq.Rz)
304
305
  assert not isinstance(resolved_gate.exponent, sympy.Basic)
305
306
  assert isinstance(resolved_gate.exponent, float)
306
307
  assert not cirq.is_parameterized(resolved_moment)
307
308
 
308
309
 
309
- def test_resolve_parameters_no_change():
310
+ def test_resolve_parameters_no_change() -> None:
310
311
  a, b = cirq.LineQubit.range(2)
311
312
  moment = cirq.Moment(cirq.X(a), cirq.Y(b))
312
313
  resolved_moment = cirq.resolve_parameters(moment, cirq.ParamResolver({'v': 0.1, 'w': 0.2}))
@@ -317,14 +318,14 @@ def test_resolve_parameters_no_change():
317
318
  assert resolved_moment is moment
318
319
 
319
320
 
320
- def test_parameter_names():
321
+ def test_parameter_names() -> None:
321
322
  a, b = cirq.LineQubit.range(2)
322
323
  moment = cirq.Moment(cirq.X(a) ** sympy.Symbol('v'), cirq.Y(b) ** sympy.Symbol('w'))
323
324
  assert cirq.parameter_names(moment) == {'v', 'w'}
324
325
  assert cirq.parameter_names(cirq.Moment(cirq.X(a), cirq.Y(b))) == set()
325
326
 
326
327
 
327
- def test_with_measurement_keys():
328
+ def test_with_measurement_keys() -> None:
328
329
  a, b = cirq.LineQubit.range(2)
329
330
  m = cirq.Moment(cirq.measure(a, key='m1'), cirq.measure(b, key='m2'))
330
331
 
@@ -334,7 +335,7 @@ def test_with_measurement_keys():
334
335
  assert new_moment.operations[1] == cirq.measure(b, key='p2')
335
336
 
336
337
 
337
- def test_with_key_path():
338
+ def test_with_key_path() -> None:
338
339
  a, b = cirq.LineQubit.range(2)
339
340
  m = cirq.Moment(cirq.measure(a, key='m1'), cirq.measure(b, key='m2'))
340
341
 
@@ -348,7 +349,7 @@ def test_with_key_path():
348
349
  )
349
350
 
350
351
 
351
- def test_with_key_path_prefix():
352
+ def test_with_key_path_prefix() -> None:
352
353
  a, b, c = cirq.LineQubit.range(3)
353
354
  m = cirq.Moment(cirq.measure(a, key='m1'), cirq.measure(b, key='m2'), cirq.X(c))
354
355
  mb = cirq.with_key_path_prefix(m, ('b',))
@@ -358,7 +359,7 @@ def test_with_key_path_prefix():
358
359
  assert mab.operations[2] is m.operations[2]
359
360
 
360
361
 
361
- def test_copy():
362
+ def test_copy() -> None:
362
363
  a = cirq.NamedQubit('a')
363
364
  b = cirq.NamedQubit('b')
364
365
  original = cirq.Moment([cirq.CZ(a, b)])
@@ -367,7 +368,7 @@ def test_copy():
367
368
  assert id(original) != id(copy)
368
369
 
369
370
 
370
- def test_qubits():
371
+ def test_qubits() -> None:
371
372
  a = cirq.NamedQubit('a')
372
373
  b = cirq.NamedQubit('b')
373
374
 
@@ -376,7 +377,7 @@ def test_qubits():
376
377
  assert cirq.Moment([cirq.CZ(a, b)]).qubits == {a, b}
377
378
 
378
379
 
379
- def test_container_methods():
380
+ def test_container_methods() -> None:
380
381
  a = cirq.NamedQubit('a')
381
382
  b = cirq.NamedQubit('b')
382
383
  m = cirq.Moment([cirq.H(a), cirq.H(b)])
@@ -389,14 +390,14 @@ def test_container_methods():
389
390
  assert len(m) == 2
390
391
 
391
392
 
392
- def test_decompose():
393
+ def test_decompose() -> None:
393
394
  a = cirq.NamedQubit('a')
394
395
  b = cirq.NamedQubit('b')
395
396
  m = cirq.Moment(cirq.X(a), cirq.X(b))
396
397
  assert list(cirq.decompose(m)) == list(m.operations)
397
398
 
398
399
 
399
- def test_measurement_keys():
400
+ def test_measurement_keys() -> None:
400
401
  a = cirq.NamedQubit('a')
401
402
  b = cirq.NamedQubit('b')
402
403
  m = cirq.Moment(cirq.X(a), cirq.X(b))
@@ -409,7 +410,7 @@ def test_measurement_keys():
409
410
  assert cirq.is_measurement(m2)
410
411
 
411
412
 
412
- def test_measurement_key_objs_caching():
413
+ def test_measurement_key_objs_caching() -> None:
413
414
  q0, q1, q2, q3 = cirq.LineQubit.range(4)
414
415
  m = cirq.Moment(cirq.measure(q0, key='foo'))
415
416
  assert m._measurement_key_objs is None
@@ -432,7 +433,7 @@ def test_measurement_key_objs_caching():
432
433
  }
433
434
 
434
435
 
435
- def test_control_keys_caching():
436
+ def test_control_keys_caching() -> None:
436
437
  q0, q1, q2, q3 = cirq.LineQubit.range(4)
437
438
  m = cirq.Moment(cirq.X(q0).with_classical_controls('foo'))
438
439
  assert m._control_keys is None
@@ -454,13 +455,13 @@ def test_control_keys_caching():
454
455
  }
455
456
 
456
457
 
457
- def test_bool():
458
+ def test_bool() -> None:
458
459
  assert not cirq.Moment()
459
460
  a = cirq.NamedQubit('a')
460
461
  assert cirq.Moment([cirq.X(a)])
461
462
 
462
463
 
463
- def test_repr():
464
+ def test_repr() -> None:
464
465
  a = cirq.NamedQubit('a')
465
466
  b = cirq.NamedQubit('b')
466
467
 
@@ -469,14 +470,14 @@ def test_repr():
469
470
  cirq.testing.assert_equivalent_repr(cirq.Moment(cirq.X(a), cirq.Y(b)))
470
471
 
471
472
 
472
- def test_json_dict():
473
+ def test_json_dict() -> None:
473
474
  a = cirq.NamedQubit('a')
474
475
  b = cirq.NamedQubit('b')
475
476
  mom = cirq.Moment([cirq.CZ(a, b)])
476
477
  assert mom._json_dict_() == {'operations': (cirq.CZ(a, b),)}
477
478
 
478
479
 
479
- def test_inverse():
480
+ def test_inverse() -> None:
480
481
  a, b, c = cirq.LineQubit.range(3)
481
482
  m = cirq.Moment([cirq.S(a), cirq.CNOT(b, c)])
482
483
  assert m**1 is m
@@ -487,15 +488,15 @@ def test_inverse():
487
488
  assert cirq.inverse(cirq.Moment([cirq.measure(a)]), default=None) is None
488
489
 
489
490
 
490
- def test_immutable_moment():
491
+ def test_immutable_moment() -> None:
491
492
  with pytest.raises(AttributeError):
492
493
  q1, q2 = cirq.LineQubit.range(2)
493
494
  circuit = cirq.Circuit(cirq.X(q1))
494
495
  moment = circuit.moments[0]
495
- moment.operations += (cirq.Y(q2),)
496
+ moment.operations += (cirq.Y(q2),) # type: ignore[misc]
496
497
 
497
498
 
498
- def test_add():
499
+ def test_add() -> None:
499
500
  a, b, c = cirq.LineQubit.range(3)
500
501
  expected_circuit = cirq.Circuit([cirq.CNOT(a, b), cirq.X(a), cirq.Y(b)])
501
502
 
@@ -520,7 +521,7 @@ def test_add():
520
521
  assert m1 + [] is m1
521
522
 
522
523
 
523
- def test_sub():
524
+ def test_sub() -> None:
524
525
  a, b, c = cirq.LineQubit.range(3)
525
526
  m = cirq.Moment(cirq.X(a), cirq.Y(b))
526
527
  assert m - [] == m
@@ -539,7 +540,7 @@ def test_sub():
539
540
  assert m2 - cirq.Y(b) == cirq.Moment(cirq.X(a), cirq.Z(c))
540
541
 
541
542
 
542
- def test_op_tree():
543
+ def test_op_tree() -> None:
543
544
  eq = cirq.testing.EqualsTester()
544
545
  a, b = cirq.LineQubit.range(2)
545
546
 
@@ -552,7 +553,7 @@ def test_op_tree():
552
553
  eq.add_equality_group(cirq.Moment(cirq.X(a), cirq.Y(b)), cirq.Moment([cirq.X(a), cirq.Y(b)]))
553
554
 
554
555
 
555
- def test_indexes_by_qubit():
556
+ def test_indexes_by_qubit() -> None:
556
557
  a, b, c = cirq.LineQubit.range(3)
557
558
  moment = cirq.Moment([cirq.H(a), cirq.CNOT(b, c)])
558
559
 
@@ -561,7 +562,7 @@ def test_indexes_by_qubit():
561
562
  assert moment[c] == cirq.CNOT(b, c)
562
563
 
563
564
 
564
- def test_throws_when_indexed_by_unused_qubit():
565
+ def test_throws_when_indexed_by_unused_qubit() -> None:
565
566
  a, b = cirq.LineQubit.range(2)
566
567
  moment = cirq.Moment([cirq.H(a)])
567
568
 
@@ -569,7 +570,7 @@ def test_throws_when_indexed_by_unused_qubit():
569
570
  _ = moment[b]
570
571
 
571
572
 
572
- def test_indexes_by_list_of_qubits():
573
+ def test_indexes_by_list_of_qubits() -> None:
573
574
  q = cirq.LineQubit.range(4)
574
575
  moment = cirq.Moment([cirq.Z(q[0]), cirq.CNOT(q[1], q[2])])
575
576
 
@@ -584,7 +585,11 @@ def test_indexes_by_list_of_qubits():
584
585
  assert moment[q] == moment
585
586
 
586
587
 
587
- def test_moment_text_diagram():
588
+ def test_moment_text_diagram() -> None:
589
+ a: cirq.Qid
590
+ b: cirq.Qid
591
+ c: cirq.Qid
592
+ d: cirq.Qid
588
593
  a, b, c, d = cirq.GridQubit.rect(2, 2)
589
594
  m = cirq.Moment(cirq.CZ(a, b), cirq.CNOT(c, d))
590
595
  assert (
@@ -674,7 +679,7 @@ aa │
674
679
  )
675
680
 
676
681
 
677
- def test_text_diagram_does_not_depend_on_insertion_order():
682
+ def test_text_diagram_does_not_depend_on_insertion_order() -> None:
678
683
  q = cirq.LineQubit.range(4)
679
684
  ops = [cirq.CNOT(q[0], q[3]), cirq.CNOT(q[1], q[2])]
680
685
  m1, m2 = cirq.Moment(ops), cirq.Moment(ops[::-1])
@@ -682,7 +687,7 @@ def test_text_diagram_does_not_depend_on_insertion_order():
682
687
  assert str(m1) == str(m2)
683
688
 
684
689
 
685
- def test_commutes_moment_and_operation():
690
+ def test_commutes_moment_and_operation() -> None:
686
691
  a = cirq.NamedQubit('a')
687
692
  b = cirq.NamedQubit('b')
688
693
  c = cirq.NamedQubit('c')
@@ -712,7 +717,7 @@ def test_commutes_moment_and_operation():
712
717
  assert cirq.commutes(moment, cirq.XX(a, b))
713
718
 
714
719
 
715
- def test_commutes_moment_and_moment():
720
+ def test_commutes_moment_and_moment() -> None:
716
721
  a = cirq.NamedQubit('a')
717
722
  b = cirq.NamedQubit('b')
718
723
  c = cirq.NamedQubit('c')
@@ -733,7 +738,7 @@ def test_commutes_moment_and_moment():
733
738
  )
734
739
 
735
740
 
736
- def test_commutes_moment_with_controls():
741
+ def test_commutes_moment_with_controls() -> None:
737
742
  a, b = cirq.LineQubit.range(2)
738
743
  assert cirq.commutes(
739
744
  cirq.Moment(cirq.measure(a, key='k0')), cirq.Moment(cirq.X(b).with_classical_controls('k1'))
@@ -761,7 +766,7 @@ def test_commutes_moment_with_controls():
761
766
  )
762
767
 
763
768
 
764
- def test_commutes_moment_and_moment_comprehensive():
769
+ def test_commutes_moment_and_moment_comprehensive() -> None:
765
770
  a, b, c, d = cirq.LineQubit.range(4)
766
771
 
767
772
  # Basic Z⊗Z commuting with XX at different angles
@@ -790,7 +795,7 @@ def test_commutes_moment_and_moment_comprehensive():
790
795
  assert not cirq.commutes(m1, m2) # Z⊗Z⊗Z doesn't commute with XX⊗X
791
796
 
792
797
 
793
- def test_commutes_handles_non_unitary_operation():
798
+ def test_commutes_handles_non_unitary_operation() -> None:
794
799
  a = cirq.NamedQubit('a')
795
800
  op_damp_a = cirq.AmplitudeDampingChannel(gamma=0.1).on(a)
796
801
  assert cirq.commutes(cirq.Moment(cirq.X(a)), op_damp_a, default=None) is None
@@ -798,7 +803,7 @@ def test_commutes_handles_non_unitary_operation():
798
803
  assert cirq.commutes(cirq.Moment(op_damp_a), cirq.Moment(op_damp_a))
799
804
 
800
805
 
801
- def test_transform_qubits():
806
+ def test_transform_qubits() -> None:
802
807
  a, b = cirq.LineQubit.range(2)
803
808
  x, y = cirq.GridQubit.rect(2, 1, 10, 20)
804
809
 
@@ -806,12 +811,17 @@ def test_transform_qubits():
806
811
  modified = cirq.Moment([cirq.X(x), cirq.Y(y)])
807
812
 
808
813
  assert original.transform_qubits({a: x, b: y}) == modified
809
- assert original.transform_qubits(lambda q: cirq.GridQubit(10 + q.x, 20)) == modified
814
+ assert (
815
+ original.transform_qubits(
816
+ lambda q: cirq.GridQubit(10 + q.x, 20) # type: ignore[attr-defined]
817
+ )
818
+ == modified
819
+ )
810
820
  with pytest.raises(TypeError, match='must be a function or dict'):
811
- _ = original.transform_qubits('bad arg')
821
+ _ = original.transform_qubits('bad arg') # type: ignore[arg-type]
812
822
 
813
823
 
814
- def test_expand_to():
824
+ def test_expand_to() -> None:
815
825
  a, b = cirq.LineQubit.range(2)
816
826
  m1 = cirq.Moment(cirq.H(a))
817
827
  m2 = m1.expand_to({a})
@@ -826,7 +836,7 @@ def test_expand_to():
826
836
  _ = m1.expand_to({b})
827
837
 
828
838
 
829
- def test_kraus():
839
+ def test_kraus() -> None:
830
840
  I = np.eye(2)
831
841
  X = np.array([[0, 1], [1, 0]])
832
842
  Y = np.array([[0, -1j], [1j, 0]])
@@ -874,7 +884,7 @@ def test_kraus():
874
884
  assert np.allclose(k[3], np.sqrt(p * q) * np.kron(X, Z))
875
885
 
876
886
 
877
- def test_kraus_too_big():
887
+ def test_kraus_too_big() -> None:
878
888
  m = cirq.Moment(cirq.IdentityGate(11).on(*cirq.LineQubit.range(11)))
879
889
  assert not cirq.has_kraus(m)
880
890
  assert not m._has_superoperator_()
@@ -883,7 +893,7 @@ def test_kraus_too_big():
883
893
  assert cirq.kraus(m, default=None) is None
884
894
 
885
895
 
886
- def test_op_has_no_kraus():
896
+ def test_op_has_no_kraus() -> None:
887
897
  class EmptyGate(cirq.testing.SingleQubitGate):
888
898
  pass
889
899
 
@@ -895,7 +905,7 @@ def test_op_has_no_kraus():
895
905
  assert cirq.kraus(m, default=None) is None
896
906
 
897
907
 
898
- def test_superoperator():
908
+ def test_superoperator() -> None:
899
909
  cnot = cirq.unitary(cirq.CNOT)
900
910
 
901
911
  a, b = cirq.LineQubit.range(2)
@@ -126,7 +126,7 @@ class PointOptimizer:
126
126
  change should be made.
127
127
  """
128
128
 
129
- def optimize_circuit(self, circuit: cirq.Circuit):
129
+ def optimize_circuit(self, circuit: cirq.Circuit) -> None:
130
130
  frontier: dict[cirq.Qid, int] = defaultdict(lambda: 0)
131
131
  i = 0
132
132
  while i < len(circuit): # Note: circuit may mutate as we go.
@@ -14,7 +14,7 @@
14
14
 
15
15
  from __future__ import annotations
16
16
 
17
- from typing import Any, Callable, cast, Iterable, Mapping, NamedTuple, Sequence, TYPE_CHECKING
17
+ from typing import Any, Callable, cast, Iterable, Mapping, NamedTuple, Self, Sequence, TYPE_CHECKING
18
18
 
19
19
  import numpy as np
20
20
 
@@ -63,8 +63,8 @@ class TextDiagramDrawer:
63
63
  entries: Mapping[tuple[int, int], _DiagramText] | None = None,
64
64
  horizontal_lines: Iterable[_HorizontalLine] | None = None,
65
65
  vertical_lines: Iterable[_VerticalLine] | None = None,
66
- horizontal_padding: Mapping[int, int] | None = None,
67
- vertical_padding: Mapping[int, int] | None = None,
66
+ horizontal_padding: Mapping[int, float] | None = None,
67
+ vertical_padding: Mapping[int, float] | None = None,
68
68
  ) -> None:
69
69
  self.entries: dict[tuple[int, int], _DiagramText] = (
70
70
  dict() if entries is None else dict(entries)
@@ -95,7 +95,7 @@ class TextDiagramDrawer:
95
95
  def __bool__(self):
96
96
  return any(self._value_equality_values_())
97
97
 
98
- def write(self, x: int, y: int, text: str, transposed_text: str | None = None):
98
+ def write(self, x: int, y: int, text: str, transposed_text: str | None = None) -> None:
99
99
  """Adds text to the given location.
100
100
 
101
101
  Args:
@@ -130,7 +130,7 @@ class TextDiagramDrawer:
130
130
 
131
131
  def grid_line(
132
132
  self, x1: int, y1: int, x2: int, y2: int, emphasize: bool = False, doubled: bool = False
133
- ):
133
+ ) -> None:
134
134
  """Adds a vertical or horizontal line from (x1, y1) to (x2, y2).
135
135
 
136
136
  Horizontal line is selected on equality in the second coordinate and
@@ -313,7 +313,7 @@ class TextDiagramDrawer:
313
313
 
314
314
  return block_diagram.render()
315
315
 
316
- def copy(self):
316
+ def copy(self) -> Self:
317
317
  return self.__class__(
318
318
  entries=self.entries,
319
319
  vertical_lines=self.vertical_lines,
cirq/conftest.py CHANGED
@@ -14,18 +14,19 @@
14
14
 
15
15
  import inspect
16
16
  import os
17
+ from collections.abc import Iterator
17
18
 
18
19
  import matplotlib.pyplot as plt
19
20
  import pytest
20
21
 
21
22
 
22
- def pytest_configure(config):
23
+ def pytest_configure(config) -> None:
23
24
  # Use matplotlib agg backend which does not require a display.
24
25
  plt.switch_backend('agg')
25
26
  os.environ['CIRQ_TESTING'] = "true"
26
27
 
27
28
 
28
- def pytest_pyfunc_call(pyfuncitem):
29
+ def pytest_pyfunc_call(pyfuncitem) -> None:
29
30
  if inspect.iscoroutinefunction(pyfuncitem._obj):
30
31
  raise ValueError( # pragma: no cover
31
32
  f'{pyfuncitem._obj.__name__} is a bare async function. '
@@ -34,6 +35,6 @@ def pytest_pyfunc_call(pyfuncitem):
34
35
 
35
36
 
36
37
  @pytest.fixture
37
- def closefigures():
38
+ def closefigures() -> Iterator[None]:
38
39
  yield
39
40
  plt.close('all')
@@ -270,7 +270,7 @@ circuit_diagrams = {
270
270
  @pytest.mark.parametrize(
271
271
  'subgraph,part_size', itertools.product(cca.BipartiteGraphType, range(1, 5))
272
272
  )
273
- def test_circuit_diagrams(part_size, subgraph):
273
+ def test_circuit_diagrams(part_size, subgraph) -> None:
274
274
  qubits = cirq.LineQubit.range(2 * part_size)
275
275
  gate = cca.BipartiteSwapNetworkGate(subgraph, part_size)
276
276
  circuit = cirq.Circuit(gate(*qubits))
@@ -285,10 +285,10 @@ def test_circuit_diagrams(part_size, subgraph):
285
285
  cirq.testing.assert_has_diagram(circuit, diagram)
286
286
 
287
287
 
288
- def test_bad_args():
288
+ def test_bad_args() -> None:
289
289
  gate = cca.BipartiteSwapNetworkGate(cca.BipartiteGraphType.COMPLETE, 2)
290
290
  qubits = cirq.LineQubit.range(4)
291
- gate.subgraph = 'not a subgraph'
291
+ gate.subgraph = 'not a subgraph' # type: ignore[assignment]
292
292
  args = cirq.CircuitDiagramInfoArgs(
293
293
  known_qubits=None,
294
294
  known_qubit_count=None,
@@ -306,7 +306,7 @@ def test_bad_args():
306
306
  with pytest.raises(ValueError):
307
307
  gate._decompose_(qubits[:3])
308
308
 
309
- gate.subgraph = 'unimplemented subgraph'
309
+ gate.subgraph = 'unimplemented subgraph' # type: ignore[assignment]
310
310
  with pytest.raises(NotImplementedError):
311
311
  gate._decompose_(qubits)
312
312
 
@@ -315,7 +315,7 @@ def test_bad_args():
315
315
  gate._circuit_diagram_info_(args)
316
316
 
317
317
 
318
- def test_bipartite_swap_network_acquaintance_size():
318
+ def test_bipartite_swap_network_acquaintance_size() -> None:
319
319
  qubits = cirq.LineQubit.range(4)
320
320
  gate = cca.BipartiteSwapNetworkGate(cca.BipartiteGraphType.COMPLETE, 2)
321
321
  assert cca.get_acquaintance_size(gate(*qubits)) == 2
@@ -324,7 +324,7 @@ def test_bipartite_swap_network_acquaintance_size():
324
324
  @pytest.mark.parametrize(
325
325
  'subgraph,part_size', itertools.product(cca.BipartiteGraphType, range(1, 3))
326
326
  )
327
- def test_repr(subgraph, part_size):
327
+ def test_repr(subgraph, part_size) -> None:
328
328
  gate = cca.BipartiteSwapNetworkGate(subgraph, part_size)
329
329
  cirq.testing.assert_equivalent_repr(gate)
330
330
 
@@ -335,18 +335,20 @@ def test_repr(subgraph, part_size):
335
335
  @pytest.mark.parametrize(
336
336
  'subgraph,part_size', itertools.product(cca.BipartiteGraphType, range(1, 6))
337
337
  )
338
- def test_decomposition_permutation_consistency(part_size, subgraph):
338
+ def test_decomposition_permutation_consistency(part_size, subgraph) -> None:
339
339
  gate = cca.BipartiteSwapNetworkGate(subgraph, part_size)
340
340
  qubits = cirq.LineQubit.range(2 * part_size)
341
+ mapping: dict[cirq.Qid, int]
341
342
  mapping = {q: i for i, q in enumerate(qubits)}
342
343
  cca.update_mapping(mapping, gate._decompose_(qubits))
343
344
  permutation = gate.permutation()
344
345
  assert {qubits[i]: j for i, j in permutation.items()} == mapping
345
346
 
346
347
 
347
- def test_bad_number_of_qubits():
348
+ def test_bad_number_of_qubits() -> None:
348
349
  gate = cca.BipartiteSwapNetworkGate(cca.BipartiteGraphType.COMPLETE, 6)
349
350
  qubits = cirq.LineQubit.range(6)
351
+ mapping: dict[cirq.Qid, int]
350
352
  mapping = {q: i for i, q in enumerate(qubits)}
351
353
  with pytest.raises(ValueError, match='len'):
352
354
  cca.update_mapping(mapping, gate._decompose_(qubits))
@@ -20,7 +20,7 @@ import cirq
20
20
  import cirq.contrib.acquaintance as cca
21
21
 
22
22
 
23
- def test_acquaintance_device():
23
+ def test_acquaintance_device() -> None:
24
24
  with pytest.raises(ValueError):
25
25
  op = cirq.X(cirq.NamedQubit('q'))
26
26
  cca.UnconstrainedAcquaintanceDevice.validate_operation(op)
@@ -31,6 +31,6 @@ def test_acquaintance_device():
31
31
  cca.UnconstrainedAcquaintanceDevice.validate_operation(swap_network(*qubits))
32
32
 
33
33
 
34
- def test_not_operation():
34
+ def test_not_operation() -> None:
35
35
  with pytest.raises(TypeError):
36
- _ = cca.get_acquaintance_size(cirq.LineQubit(1))
36
+ _ = cca.get_acquaintance_size(cirq.LineQubit(1)) # type: ignore[arg-type]
@@ -16,7 +16,7 @@ from __future__ import annotations
16
16
 
17
17
  from itertools import combinations
18
18
  from string import ascii_lowercase
19
- from typing import Sequence
19
+ from typing import Any, Sequence
20
20
 
21
21
  import numpy as np
22
22
  import pytest
@@ -38,11 +38,12 @@ class ExampleGate(cirq.Gate):
38
38
  return self._wire_symbols
39
39
 
40
40
 
41
- def test_executor_explicit():
41
+ def test_executor_explicit() -> None:
42
42
  num_qubits = 8
43
43
  qubits = cirq.LineQubit.range(num_qubits)
44
44
  circuit = cca.complete_acquaintance_strategy(qubits, 2)
45
45
 
46
+ gates: dict[tuple[Any, ...], cirq.Gate]
46
47
  gates = {
47
48
  (i, j): ExampleGate([str(k) for k in ij])
48
49
  for ij in combinations(range(num_qubits), 2)
@@ -53,10 +54,11 @@ def test_executor_explicit():
53
54
  assert execution_strategy.device == cirq.UNCONSTRAINED_DEVICE
54
55
 
55
56
  with pytest.raises(ValueError):
56
- executor = cca.StrategyExecutorTransformer(None)
57
+ executor = cca.StrategyExecutorTransformer(None) # type: ignore[arg-type]
57
58
  executor = cca.StrategyExecutorTransformer(execution_strategy)
58
59
 
59
60
  with pytest.raises(NotImplementedError):
61
+ bad_gates: dict[tuple[Any, ...], cirq.Gate]
60
62
  bad_gates = {(0,): ExampleGate(['0']), (0, 1): ExampleGate(['0', '1'])}
61
63
  cca.GreedyExecutionStrategy(bad_gates, initial_mapping)
62
64
 
@@ -107,7 +109,7 @@ def random_diagonal_gates(
107
109
  )
108
110
  def test_executor_random(
109
111
  num_qubits: int, acquaintance_size: int, gates: dict[tuple[cirq.Qid, ...], cirq.Gate]
110
- ):
112
+ ) -> None:
111
113
  qubits = cirq.LineQubit.range(num_qubits)
112
114
  circuit = cca.complete_acquaintance_strategy(qubits, acquaintance_size)
113
115
 
@@ -126,7 +128,7 @@ def test_executor_random(
126
128
  np.testing.assert_allclose(actual=actual_unitary, desired=expected_unitary, verbose=True)
127
129
 
128
130
 
129
- def test_acquaintance_operation():
131
+ def test_acquaintance_operation() -> None:
130
132
  n = 5
131
133
  physical_qubits = tuple(cirq.LineQubit.range(n))
132
134
  logical_qubits = tuple(cirq.NamedQubit(s) for s in ascii_lowercase[:n])
@@ -134,7 +136,7 @@ def test_acquaintance_operation():
134
136
  with pytest.raises(ValueError):
135
137
  cca.AcquaintanceOperation(physical_qubits[:3], int_indices[:4])
136
138
  for logical_indices in (logical_qubits, int_indices):
137
- op = cca.AcquaintanceOperation(physical_qubits, logical_indices)
139
+ op = cca.AcquaintanceOperation(physical_qubits, logical_indices) # type: ignore[type-var]
138
140
  assert op.logical_indices == logical_indices
139
141
  assert op.qubits == physical_qubits
140
142
  wire_symbols = tuple(f'({i})' for i in logical_indices)
@@ -148,7 +148,7 @@ def acquaint_and_shift(
148
148
  acquaintance_size: int | None,
149
149
  swap_gate: cirq.Gate,
150
150
  mapping: dict[ops.Qid, int],
151
- ):
151
+ ) -> None:
152
152
  """Acquaints and shifts a pair of lists of qubits. The first part is
153
153
  acquainted with every qubit individually in the second part, and vice
154
154
  versa. Operations are grouped into several layers: