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
@@ -16,6 +16,7 @@ from __future__ import annotations
16
16
 
17
17
  import itertools
18
18
  import math
19
+ from typing import Iterator
19
20
 
20
21
  import numpy as np
21
22
  import pytest
@@ -25,11 +26,11 @@ import cirq
25
26
  import cirq.testing
26
27
 
27
28
 
28
- def _make_qubits(n):
29
+ def _make_qubits(n) -> list[cirq.NamedQubit]:
29
30
  return [cirq.NamedQubit(f'q{i}') for i in range(n)]
30
31
 
31
32
 
32
- def _sample_qubit_pauli_maps():
33
+ def _sample_qubit_pauli_maps() -> Iterator[dict[cirq.NamedQubit, cirq.Pauli]]:
33
34
  """All combinations of having a Pauli or nothing on 3 qubits.
34
35
  Yields 64 qubit pauli maps
35
36
  """
@@ -39,7 +40,7 @@ def _sample_qubit_pauli_maps():
39
40
  yield {qubit: pauli for qubit, pauli in zip(qubits, paulis) if pauli is not None}
40
41
 
41
42
 
42
- def _small_sample_qubit_pauli_maps():
43
+ def _small_sample_qubit_pauli_maps() -> Iterator[dict[cirq.NamedQubit, cirq.Pauli]]:
43
44
  """A few representative samples of qubit maps.
44
45
 
45
46
  Only tests 10 combinations of Paulis to speed up testing.
@@ -63,7 +64,7 @@ def assert_conjugation(
63
64
  op: cirq.Operation,
64
65
  expected: cirq.PauliString | None = None,
65
66
  force_checking_unitary=True,
66
- ):
67
+ ) -> None:
67
68
  """Verifies that conjugating `input_ps` by `op` results in `expected`.
68
69
 
69
70
  Also ensures that the unitary representation of the Pauli string is
@@ -96,7 +97,7 @@ def assert_conjugation(
96
97
 
97
98
  def assert_conjugation_multi_ops(
98
99
  input_ps: cirq.PauliString, ops: list[cirq.Operation], expected: cirq.PauliString | None = None
99
- ):
100
+ ) -> None:
100
101
  conjugation = input_ps.conjugated_by(ops)
101
102
  if expected is not None:
102
103
  assert conjugation == expected
@@ -108,7 +109,7 @@ def assert_conjugation_multi_ops(
108
109
  assert conjugation == conj_in_order
109
110
 
110
111
 
111
- def test_eq_ne_hash():
112
+ def test_eq_ne_hash() -> None:
112
113
  q0, q1, q2 = _make_qubits(3)
113
114
  eq = cirq.testing.EqualsTester()
114
115
  eq.make_equality_group(
@@ -126,7 +127,7 @@ def test_eq_ne_hash():
126
127
  eq.add_equality_group(cirq.PauliString(qubit_pauli_map={q: p0, q2: p1}, coefficient=+1))
127
128
 
128
129
 
129
- def test_equal_up_to_coefficient():
130
+ def test_equal_up_to_coefficient() -> None:
130
131
  (q0,) = _make_qubits(1)
131
132
  assert cirq.PauliString({}, +1).equal_up_to_coefficient(cirq.PauliString({}, +1))
132
133
  assert cirq.PauliString({}, -1).equal_up_to_coefficient(cirq.PauliString({}, -1))
@@ -161,8 +162,9 @@ def test_equal_up_to_coefficient():
161
162
  assert not cirq.PauliString({q0: cirq.X}, +1).equal_up_to_coefficient(cirq.PauliString({}, -1))
162
163
 
163
164
 
164
- def test_exponentiation_as_exponent():
165
+ def test_exponentiation_as_exponent() -> None:
165
166
  a, b = cirq.LineQubit.range(2)
167
+ p: cirq.PauliString[cirq.LineQubit]
166
168
  p = cirq.PauliString({a: cirq.X, b: cirq.Y})
167
169
 
168
170
  with pytest.raises(NotImplementedError, match='non-Hermitian'):
@@ -194,7 +196,7 @@ def test_exponentiation_as_exponent():
194
196
  )
195
197
 
196
198
 
197
- def test_exponentiate_single_value_as_exponent():
199
+ def test_exponentiate_single_value_as_exponent() -> None:
198
200
  q = cirq.LineQubit(0)
199
201
 
200
202
  assert cirq.approx_eq(math.e ** (-0.125j * math.pi * cirq.X(q)), cirq.rx(0.25 * math.pi).on(q))
@@ -212,8 +214,9 @@ def test_exponentiate_single_value_as_exponent():
212
214
  assert cirq.approx_eq(cirq.Z(q) ** 0.5, cirq.ZPowGate(exponent=0.5).on(q))
213
215
 
214
216
 
215
- def test_exponentiation_as_base():
217
+ def test_exponentiation_as_base() -> None:
216
218
  a, b = cirq.LineQubit.range(2)
219
+ p: cirq.PauliString[cirq.LineQubit]
217
220
  p = cirq.PauliString({a: cirq.X, b: cirq.Y})
218
221
 
219
222
  with pytest.raises(TypeError, match='unsupported'):
@@ -256,7 +259,7 @@ def test_exponentiation_as_base():
256
259
 
257
260
 
258
261
  @pytest.mark.parametrize('pauli', (cirq.X, cirq.Y, cirq.Z))
259
- def test_list_op_constructor_matches_mapping(pauli):
262
+ def test_list_op_constructor_matches_mapping(pauli) -> None:
260
263
  (q0,) = _make_qubits(1)
261
264
  op = pauli.on(q0)
262
265
  assert cirq.PauliString([op]) == cirq.PauliString({q0: pauli})
@@ -264,7 +267,7 @@ def test_list_op_constructor_matches_mapping(pauli):
264
267
 
265
268
  @pytest.mark.parametrize('pauli1', (cirq.X, cirq.Y, cirq.Z))
266
269
  @pytest.mark.parametrize('pauli2', (cirq.X, cirq.Y, cirq.Z))
267
- def test_exponent_mul_consistency(pauli1, pauli2):
270
+ def test_exponent_mul_consistency(pauli1, pauli2) -> None:
268
271
  a, b = cirq.LineQubit.range(2)
269
272
  op_a, op_b = pauli1(a), pauli2(b)
270
273
 
@@ -285,14 +288,14 @@ def test_exponent_mul_consistency(pauli1, pauli2):
285
288
  assert op_b**3 * op_a == op_b * op_b * op_b * op_a
286
289
 
287
290
 
288
- def test_constructor_flexibility():
291
+ def test_constructor_flexibility() -> None:
289
292
  a, b = cirq.LineQubit.range(2)
290
293
  with pytest.raises(TypeError, match='cirq.PAULI_STRING_LIKE'):
291
294
  _ = cirq.PauliString(cirq.CZ(a, b))
292
295
  with pytest.raises(TypeError, match='cirq.PAULI_STRING_LIKE'):
293
296
  _ = cirq.PauliString('test')
294
297
  with pytest.raises(TypeError, match='S is not a Pauli'):
295
- _ = cirq.PauliString(qubit_pauli_map={a: cirq.S})
298
+ _ = cirq.PauliString(qubit_pauli_map={a: cirq.S}) # type: ignore[dict-item]
296
299
  with pytest.raises(TypeError, match="cirq.PAULI_STRING_LIKE"):
297
300
  _ = cirq.PauliString(cirq.Z(a) + cirq.Z(b))
298
301
 
@@ -318,7 +321,7 @@ def test_constructor_flexibility():
318
321
 
319
322
 
320
323
  @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps())
321
- def test_getitem(qubit_pauli_map):
324
+ def test_getitem(qubit_pauli_map) -> None:
322
325
  other = cirq.NamedQubit('other')
323
326
  pauli_string = cirq.PauliString(qubit_pauli_map=qubit_pauli_map)
324
327
  for key in qubit_pauli_map:
@@ -330,8 +333,9 @@ def test_getitem(qubit_pauli_map):
330
333
 
331
334
 
332
335
  @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps())
333
- def test_get(qubit_pauli_map):
336
+ def test_get(qubit_pauli_map) -> None:
334
337
  other = cirq.NamedQubit('other')
338
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
335
339
  pauli_string = cirq.PauliString(qubit_pauli_map)
336
340
  for key in qubit_pauli_map:
337
341
  assert qubit_pauli_map.get(key) == pauli_string.get(key)
@@ -341,8 +345,9 @@ def test_get(qubit_pauli_map):
341
345
 
342
346
 
343
347
  @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps())
344
- def test_contains(qubit_pauli_map):
348
+ def test_contains(qubit_pauli_map) -> None:
345
349
  other = cirq.NamedQubit('other')
350
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
346
351
  pauli_string = cirq.PauliString(qubit_pauli_map)
347
352
  for key in qubit_pauli_map:
348
353
  assert key in pauli_string
@@ -350,7 +355,8 @@ def test_contains(qubit_pauli_map):
350
355
 
351
356
 
352
357
  @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps())
353
- def test_basic_functionality(qubit_pauli_map):
358
+ def test_basic_functionality(qubit_pauli_map) -> None:
359
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
354
360
  pauli_string = cirq.PauliString(qubit_pauli_map)
355
361
  # Test items
356
362
  assert len(qubit_pauli_map.items()) == len(pauli_string.items())
@@ -372,8 +378,9 @@ def test_basic_functionality(qubit_pauli_map):
372
378
  assert set(tuple(qubit_pauli_map)) == set(tuple(pauli_string))
373
379
 
374
380
 
375
- def test_repr():
381
+ def test_repr() -> None:
376
382
  q0, q1, q2 = _make_qubits(3)
383
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
377
384
  pauli_string = cirq.PauliString({q2: cirq.X, q1: cirq.Y, q0: cirq.Z})
378
385
  cirq.testing.assert_equivalent_repr(pauli_string)
379
386
  cirq.testing.assert_equivalent_repr(-pauli_string)
@@ -382,8 +389,9 @@ def test_repr():
382
389
  cirq.testing.assert_equivalent_repr(cirq.PauliString())
383
390
 
384
391
 
385
- def test_repr_preserves_qubit_order():
392
+ def test_repr_preserves_qubit_order() -> None:
386
393
  q0, q1, q2 = _make_qubits(3)
394
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
387
395
  pauli_string = cirq.PauliString({q2: cirq.X, q1: cirq.Y, q0: cirq.Z})
388
396
  assert eval(repr(pauli_string)).qubits == pauli_string.qubits
389
397
 
@@ -394,14 +402,15 @@ def test_repr_preserves_qubit_order():
394
402
  assert eval(repr(pauli_string)).qubits == pauli_string.qubits
395
403
 
396
404
 
397
- def test_repr_coefficient_of_one():
405
+ def test_repr_coefficient_of_one() -> None:
398
406
  pauli_string = cirq.Z(cirq.LineQubit(0)) * 1
399
407
  assert type(pauli_string) == type(eval(repr(pauli_string)))
400
408
  cirq.testing.assert_equivalent_repr(pauli_string)
401
409
 
402
410
 
403
- def test_str():
411
+ def test_str() -> None:
404
412
  q0, q1, q2 = _make_qubits(3)
413
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
405
414
  pauli_string = cirq.PauliString({q2: cirq.X, q1: cirq.Y, q0: cirq.Z})
406
415
  assert str(cirq.PauliString({})) == 'I'
407
416
  assert str(-cirq.PauliString({})) == '-I'
@@ -426,9 +435,13 @@ def test_str():
426
435
  {q0: (cirq.X, cirq.Y), q1: (cirq.Y, cirq.Z)},
427
436
  ),
428
437
  )
429
- )(*_make_qubits(3)),
438
+ )(
439
+ *_make_qubits(3) # type: ignore[arg-type]
440
+ ),
430
441
  )
431
- def test_zip_items(map1, map2, out):
442
+ def test_zip_items(map1, map2, out) -> None:
443
+ ps1: cirq.PauliString[cirq.NamedQubit]
444
+ ps2: cirq.PauliString[cirq.NamedQubit]
432
445
  ps1 = cirq.PauliString(map1)
433
446
  ps2 = cirq.PauliString(map2)
434
447
  out_actual = tuple(ps1.zip_items(ps2))
@@ -452,9 +465,13 @@ def test_zip_items(map1, map2, out):
452
465
  ((cirq.X, cirq.Y), (cirq.Y, cirq.Z)),
453
466
  ),
454
467
  )
455
- )(*_make_qubits(3)),
468
+ )(
469
+ *_make_qubits(3) # type: ignore[arg-type]
470
+ ),
456
471
  )
457
- def test_zip_paulis(map1, map2, out):
472
+ def test_zip_paulis(map1, map2, out) -> None:
473
+ ps1: cirq.PauliString[cirq.NamedQubit]
474
+ ps2: cirq.PauliString[cirq.NamedQubit]
458
475
  ps1 = cirq.PauliString(map1)
459
476
  ps2 = cirq.PauliString(map2)
460
477
  out_actual = tuple(ps1.zip_paulis(ps2))
@@ -464,9 +481,10 @@ def test_zip_paulis(map1, map2, out):
464
481
  assert set(out_actual) == set(out) # Ignore output order
465
482
 
466
483
 
467
- def test_commutes():
484
+ def test_commutes() -> None:
468
485
  qubits = _make_qubits(3)
469
486
 
487
+ ps1: cirq.PauliString[cirq.NamedQubit]
470
488
  ps1 = cirq.PauliString([cirq.X(qubits[0])])
471
489
  with pytest.raises(TypeError):
472
490
  cirq.commutes(ps1, 'X')
@@ -487,6 +505,7 @@ def test_commutes():
487
505
  (cirq.Y, cirq.X, cirq.Z): True,
488
506
  (cirq.Y, cirq.Z, cirq.X): True,
489
507
  }.items():
508
+ ps2: cirq.PauliString[cirq.NamedQubit]
490
509
  ps2 = cirq.PauliString(dict(zip(qubits, paulis)))
491
510
  assert cirq.commutes(ps1, ps2) == commutes
492
511
 
@@ -500,9 +519,11 @@ def test_commutes():
500
519
  assert cirq.commutes(ps1, ps2) == commutes
501
520
 
502
521
 
503
- def test_negate():
522
+ def test_negate() -> None:
504
523
  q0, q1 = _make_qubits(2)
505
524
  qubit_pauli_map = {q0: cirq.X, q1: cirq.Y}
525
+ ps1: cirq.PauliString[cirq.NamedQubit]
526
+ ps2: cirq.PauliString[cirq.NamedQubit]
506
527
  ps1 = cirq.PauliString(qubit_pauli_map)
507
528
  ps2 = cirq.PauliString(qubit_pauli_map, -1)
508
529
  assert -ps1 == ps2
@@ -516,8 +537,9 @@ def test_negate():
516
537
  assert isinstance(-m, cirq.MutablePauliString)
517
538
 
518
539
 
519
- def test_mul_scalar():
540
+ def test_mul_scalar() -> None:
520
541
  a, b = cirq.LineQubit.range(2)
542
+ p: cirq.PauliString[cirq.LineQubit]
521
543
  p = cirq.PauliString({a: cirq.X, b: cirq.Y})
522
544
  assert -p == -1 * p == -1.0 * p == p * -1 == p * complex(-1)
523
545
  assert -p != 1j * p
@@ -532,8 +554,9 @@ def test_mul_scalar():
532
554
  _ = 'test' * p
533
555
 
534
556
 
535
- def test_div_scalar():
557
+ def test_div_scalar() -> None:
536
558
  a, b = cirq.LineQubit.range(2)
559
+ p: cirq.PauliString[cirq.LineQubit]
537
560
  p = cirq.PauliString({a: cirq.X, b: cirq.Y})
538
561
  assert -p == p / -1 == p / -1.0 == p / (-1 + 0j)
539
562
  assert -p != p / 1j
@@ -543,11 +566,13 @@ def test_div_scalar():
543
566
  _ = p / 'test'
544
567
  with pytest.raises(TypeError):
545
568
  # noinspection PyUnresolvedReferences
546
- _ = 'test' / p
569
+ _ = 'test' / p # type: ignore[operator]
547
570
 
548
571
 
549
- def test_mul_strings():
572
+ def test_mul_strings() -> None:
550
573
  a, b, c, d = cirq.LineQubit.range(4)
574
+ p1: cirq.PauliString[cirq.LineQubit]
575
+ p2: cirq.PauliString[cirq.LineQubit]
551
576
  p1 = cirq.PauliString({a: cirq.X, b: cirq.Y, c: cirq.Z})
552
577
  p2 = cirq.PauliString({b: cirq.X, c: cirq.Y, d: cirq.Z})
553
578
  assert p1 * p2 == -cirq.PauliString({a: cirq.X, b: cirq.Z, c: cirq.X, d: cirq.Z})
@@ -565,7 +590,7 @@ def test_mul_strings():
565
590
  assert -cirq.X(a) == -cirq.PauliString({a: cirq.X})
566
591
 
567
592
 
568
- def test_op_equivalence():
593
+ def test_op_equivalence() -> None:
569
594
  a, b = cirq.LineQubit.range(2)
570
595
  various_x = [
571
596
  cirq.X(a),
@@ -586,7 +611,7 @@ def test_op_equivalence():
586
611
  eq.add_equality_group(cirq.Z(b), cirq.PauliString({b: cirq.Z}))
587
612
 
588
613
 
589
- def test_op_product():
614
+ def test_op_product() -> None:
590
615
  a, b = cirq.LineQubit.range(2)
591
616
 
592
617
  assert cirq.X(a) * cirq.X(b) == cirq.PauliString({a: cirq.X, b: cirq.X})
@@ -598,9 +623,10 @@ def test_op_product():
598
623
  assert cirq.Y(a) * cirq.Z(b) * cirq.X(a) == -1j * cirq.PauliString({a: cirq.Z, b: cirq.Z})
599
624
 
600
625
 
601
- def test_pos():
626
+ def test_pos() -> None:
602
627
  q0, q1 = _make_qubits(2)
603
628
  qubit_pauli_map = {q0: cirq.X, q1: cirq.Y}
629
+ ps1: cirq.PauliString[cirq.NamedQubit]
604
630
  ps1 = cirq.PauliString(qubit_pauli_map)
605
631
  assert ps1 == +ps1
606
632
 
@@ -610,13 +636,14 @@ def test_pos():
610
636
  assert isinstance(+m, cirq.MutablePauliString)
611
637
 
612
638
 
613
- def test_pow():
639
+ def test_pow() -> None:
614
640
  a, b = cirq.LineQubit.range(2)
615
641
 
616
642
  assert cirq.PauliString({a: cirq.X}) ** 0.25 == cirq.X(a) ** 0.25
617
643
  assert cirq.PauliString({a: cirq.Y}) ** 0.25 == cirq.Y(a) ** 0.25
618
644
  assert cirq.PauliString({a: cirq.Z}) ** 0.25 == cirq.Z(a) ** 0.25
619
645
 
646
+ p: cirq.PauliString[cirq.LineQubit]
620
647
  p = cirq.PauliString({a: cirq.X, b: cirq.Y})
621
648
  assert p**1 == p
622
649
  assert p**-1 == p
@@ -626,7 +653,7 @@ def test_pow():
626
653
  assert (1j * p) ** -1 == -1j * p
627
654
 
628
655
 
629
- def test_rpow():
656
+ def test_rpow() -> None:
630
657
  a, b = cirq.LineQubit.range(2)
631
658
 
632
659
  u = cirq.unitary(np.exp(1j * np.pi / 2 * cirq.Z(a) * cirq.Z(b)))
@@ -639,7 +666,7 @@ def test_rpow():
639
666
  np.testing.assert_allclose(u, np.diag([-1, -1, -1, -1]), atol=1e-8)
640
667
 
641
668
 
642
- def test_numpy_ufunc():
669
+ def test_numpy_ufunc() -> None:
643
670
  with pytest.raises(TypeError, match="returned NotImplemented"):
644
671
  _ = np.sin(cirq.PauliString())
645
672
  with pytest.raises(NotImplementedError, match="non-Hermitian"):
@@ -650,25 +677,27 @@ def test_numpy_ufunc():
650
677
  assert x == 2 * cirq.PauliString()
651
678
 
652
679
 
653
- def test_map_qubits():
680
+ def test_map_qubits() -> None:
654
681
  a, b = (cirq.NamedQubit(name) for name in 'ab')
655
682
  q0, q1 = _make_qubits(2)
656
683
  qubit_pauli_map1 = {a: cirq.X, b: cirq.Y}
657
684
  qubit_pauli_map2 = {q0: cirq.X, q1: cirq.Y}
658
685
  qubit_map = {a: q0, b: q1}
686
+ ps1: cirq.PauliString[cirq.NamedQubit]
687
+ ps2: cirq.PauliString[cirq.NamedQubit]
659
688
  ps1 = cirq.PauliString(qubit_pauli_map1)
660
689
  ps2 = cirq.PauliString(qubit_pauli_map2)
661
690
  assert ps1.map_qubits(qubit_map) == ps2
662
691
 
663
692
 
664
- def test_map_qubits_raises():
693
+ def test_map_qubits_raises() -> None:
665
694
  q = cirq.LineQubit.range(3)
666
695
  pauli_string = cirq.X(q[0]) * cirq.Y(q[1]) * cirq.Z(q[2])
667
696
  with pytest.raises(ValueError, match='must have a key for every qubit'):
668
697
  pauli_string.map_qubits({q[0]: q[1]})
669
698
 
670
699
 
671
- def test_to_z_basis_ops():
700
+ def test_to_z_basis_ops() -> None:
672
701
  x0 = np.array([1, 1]) / np.sqrt(2)
673
702
  x1 = np.array([1, -1]) / np.sqrt(2)
674
703
  y0 = np.array([1, 1j]) / np.sqrt(2)
@@ -677,6 +706,7 @@ def test_to_z_basis_ops():
677
706
  z1 = np.array([0, 1])
678
707
 
679
708
  q0, q1, q2, q3, q4, q5 = _make_qubits(6)
709
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
680
710
  pauli_string = cirq.PauliString(
681
711
  {q0: cirq.X, q1: cirq.X, q2: cirq.Y, q3: cirq.Y, q4: cirq.Z, q5: cirq.Z}
682
712
  )
@@ -695,8 +725,9 @@ def test_to_z_basis_ops():
695
725
  )
696
726
 
697
727
 
698
- def test_to_z_basis_ops_product_state():
728
+ def test_to_z_basis_ops_product_state() -> None:
699
729
  q0, q1, q2, q3, q4, q5 = _make_qubits(6)
730
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
700
731
  pauli_string = cirq.PauliString(
701
732
  {q0: cirq.X, q1: cirq.X, q2: cirq.Y, q3: cirq.Y, q4: cirq.Z, q5: cirq.Z}
702
733
  )
@@ -722,10 +753,11 @@ def test_to_z_basis_ops_product_state():
722
753
  )
723
754
 
724
755
 
725
- def test_with_qubits():
756
+ def test_with_qubits() -> None:
726
757
  old_qubits = cirq.LineQubit.range(9)
727
758
  new_qubits = cirq.LineQubit.range(9, 18)
728
759
  qubit_pauli_map = {q: cirq.Pauli.by_index(q.x) for q in old_qubits}
760
+ pauli_string: cirq.PauliString[cirq.LineQubit]
729
761
  pauli_string = cirq.PauliString(qubit_pauli_map, -1)
730
762
  new_pauli_string = pauli_string.with_qubits(*new_qubits)
731
763
 
@@ -735,16 +767,17 @@ def test_with_qubits():
735
767
  assert new_pauli_string.coefficient == -1
736
768
 
737
769
 
738
- def test_with_qubits_raises():
770
+ def test_with_qubits_raises() -> None:
739
771
  q = cirq.LineQubit.range(3)
740
772
  pauli_string = cirq.X(q[0]) * cirq.Y(q[1]) * cirq.Z(q[2])
741
773
  with pytest.raises(ValueError, match='does not match'):
742
774
  pauli_string.with_qubits(q[:2])
743
775
 
744
776
 
745
- def test_with_coefficient():
777
+ def test_with_coefficient() -> None:
746
778
  qubits = cirq.LineQubit.range(4)
747
779
  qubit_pauli_map = {q: cirq.Pauli.by_index(q.x) for q in qubits}
780
+ pauli_string: cirq.PauliString[cirq.LineQubit]
748
781
  pauli_string = cirq.PauliString(qubit_pauli_map, 1.23)
749
782
  ps2 = pauli_string.with_coefficient(1.0)
750
783
  assert ps2.coefficient == 1.0
@@ -754,18 +787,19 @@ def test_with_coefficient():
754
787
 
755
788
 
756
789
  @pytest.mark.parametrize('qubit_pauli_map', _small_sample_qubit_pauli_maps())
757
- def test_consistency(qubit_pauli_map):
790
+ def test_consistency(qubit_pauli_map) -> None:
791
+ pauli_string: cirq.PauliString[cirq.NamedQubit]
758
792
  pauli_string = cirq.PauliString(qubit_pauli_map)
759
793
  cirq.testing.assert_implements_consistent_protocols(pauli_string)
760
794
 
761
795
 
762
- def test_scaled_unitary_consistency():
796
+ def test_scaled_unitary_consistency() -> None:
763
797
  a, b = cirq.LineQubit.range(2)
764
798
  cirq.testing.assert_implements_consistent_protocols(2 * cirq.X(a) * cirq.Y(b))
765
799
  cirq.testing.assert_implements_consistent_protocols(1j * cirq.X(a) * cirq.Y(b))
766
800
 
767
801
 
768
- def test_bool():
802
+ def test_bool() -> None:
769
803
  a = cirq.LineQubit(0)
770
804
  assert not bool(cirq.PauliString({}))
771
805
  assert bool(cirq.PauliString({a: cirq.X}))
@@ -818,11 +852,11 @@ def _pauli_string_matrix_cases():
818
852
 
819
853
 
820
854
  @pytest.mark.parametrize('pauli_string, qubits, expected_matrix', _pauli_string_matrix_cases())
821
- def test_matrix(pauli_string, qubits, expected_matrix):
855
+ def test_matrix(pauli_string, qubits, expected_matrix) -> None:
822
856
  assert np.allclose(pauli_string.matrix(qubits), expected_matrix)
823
857
 
824
858
 
825
- def test_unitary_matrix():
859
+ def test_unitary_matrix() -> None:
826
860
  a, b = cirq.LineQubit.range(2)
827
861
  assert not cirq.has_unitary(2 * cirq.X(a) * cirq.Z(b))
828
862
  assert cirq.unitary(2 * cirq.X(a) * cirq.Z(b), default=None) is None
@@ -852,7 +886,7 @@ def test_unitary_matrix():
852
886
  # fmt: on
853
887
 
854
888
 
855
- def test_decompose():
889
+ def test_decompose() -> None:
856
890
  a, b = cirq.LineQubit.range(2)
857
891
  assert cirq.decompose_once(2 * cirq.X(a) * cirq.Z(b), default=None) is None
858
892
  assert cirq.decompose_once(1j * cirq.X(a) * cirq.Z(b)) == [
@@ -863,13 +897,13 @@ def test_decompose():
863
897
  assert cirq.decompose_once(cirq.Y(b) * cirq.Z(a)) == [cirq.Y(b), cirq.Z(a)]
864
898
 
865
899
 
866
- def test_rejects_non_paulis():
900
+ def test_rejects_non_paulis() -> None:
867
901
  q = cirq.NamedQubit('q')
868
902
  with pytest.raises(TypeError):
869
903
  _ = cirq.PauliString({q: cirq.S})
870
904
 
871
905
 
872
- def test_cannot_multiply_by_non_paulis():
906
+ def test_cannot_multiply_by_non_paulis() -> None:
873
907
  q = cirq.NamedQubit('q')
874
908
  with pytest.raises(TypeError):
875
909
  _ = cirq.X(q) * cirq.Z(q) ** 0.5
@@ -879,13 +913,14 @@ def test_cannot_multiply_by_non_paulis():
879
913
  _ = cirq.Y(q) * cirq.S(q)
880
914
 
881
915
 
882
- def test_filters_identities():
916
+ def test_filters_identities() -> None:
883
917
  q1, q2 = cirq.LineQubit.range(2)
884
918
  assert cirq.PauliString({q1: cirq.I, q2: cirq.X}) == cirq.PauliString({q2: cirq.X})
885
919
 
886
920
 
887
- def test_expectation_from_state_vector_invalid_input():
921
+ def test_expectation_from_state_vector_invalid_input() -> None:
888
922
  q0, q1, q2, q3 = _make_qubits(4)
923
+ ps: cirq.PauliString[cirq.NamedQubit]
889
924
  ps = cirq.PauliString({q0: cirq.X, q1: cirq.Y})
890
925
  wf = np.array([1, 0, 0, 0], dtype=np.complex64)
891
926
  q_map = {q0: 0, q1: 1}
@@ -899,13 +934,13 @@ def test_expectation_from_state_vector_invalid_input():
899
934
 
900
935
  with pytest.raises(TypeError, match='mapping'):
901
936
  # noinspection PyTypeChecker
902
- ps.expectation_from_state_vector(wf, "bad type")
937
+ ps.expectation_from_state_vector(wf, "bad type") # type: ignore[arg-type]
903
938
  with pytest.raises(TypeError, match='mapping'):
904
939
  # noinspection PyTypeChecker
905
- ps.expectation_from_state_vector(wf, {"bad key": 1})
940
+ ps.expectation_from_state_vector(wf, {"bad key": 1}) # type: ignore[dict-item]
906
941
  with pytest.raises(TypeError, match='mapping'):
907
942
  # noinspection PyTypeChecker
908
- ps.expectation_from_state_vector(wf, {q0: "bad value"})
943
+ ps.expectation_from_state_vector(wf, {q0: "bad value"}) # type: ignore[dict-item]
909
944
  with pytest.raises(ValueError, match='complete'):
910
945
  ps.expectation_from_state_vector(wf, {q0: 0})
911
946
  with pytest.raises(ValueError, match='complete'):
@@ -937,8 +972,9 @@ def test_expectation_from_state_vector_invalid_input():
937
972
  ps.expectation_from_state_vector(wf.reshape((4, 4, 1)), q_map_2)
938
973
 
939
974
 
940
- def test_expectation_from_state_vector_check_preconditions():
975
+ def test_expectation_from_state_vector_check_preconditions() -> None:
941
976
  q0, q1, q2, q3 = _make_qubits(4)
977
+ ps: cirq.PauliString[cirq.NamedQubit]
942
978
  ps = cirq.PauliString({q0: cirq.X, q1: cirq.Y})
943
979
  q_map = {q0: 0, q1: 1, q2: 2, q3: 3}
944
980
 
@@ -950,8 +986,9 @@ def test_expectation_from_state_vector_check_preconditions():
950
986
  )
951
987
 
952
988
 
953
- def test_expectation_from_state_vector_basis_states():
989
+ def test_expectation_from_state_vector_basis_states() -> None:
954
990
  q0 = cirq.LineQubit(0)
991
+ x0: cirq.PauliString[cirq.LineQubit]
955
992
  x0 = cirq.PauliString({q0: cirq.X})
956
993
  q_map = {q0: 0}
957
994
 
@@ -972,6 +1009,7 @@ def test_expectation_from_state_vector_basis_states():
972
1009
  atol=1e-7,
973
1010
  )
974
1011
 
1012
+ y0: cirq.PauliString[cirq.LineQubit]
975
1013
  y0 = cirq.PauliString({q0: cirq.Y})
976
1014
  np.testing.assert_allclose(
977
1015
  y0.expectation_from_state_vector(np.array([1, 1j], dtype=complex) / np.sqrt(2), q_map),
@@ -995,11 +1033,13 @@ def test_expectation_from_state_vector_basis_states():
995
1033
  )
996
1034
 
997
1035
 
998
- def test_expectation_from_state_vector_entangled_states():
1036
+ def test_expectation_from_state_vector_entangled_states() -> None:
999
1037
  q0, q1 = _make_qubits(2)
1000
1038
  z0z1_pauli_map = {q0: cirq.Z, q1: cirq.Z}
1039
+ z0z1: cirq.PauliString[cirq.NamedQubit]
1001
1040
  z0z1 = cirq.PauliString(z0z1_pauli_map)
1002
1041
  x0x1_pauli_map = {q0: cirq.X, q1: cirq.X}
1042
+ x0x1: cirq.PauliString[cirq.NamedQubit]
1003
1043
  x0x1 = cirq.PauliString(x0x1_pauli_map)
1004
1044
  q_map = {q0: 0, q1: 1}
1005
1045
  wf1 = np.array([0, 1, 1, 0], dtype=complex) / np.sqrt(2)
@@ -1018,8 +1058,9 @@ def test_expectation_from_state_vector_entangled_states():
1018
1058
  np.testing.assert_allclose(x0x1.expectation_from_state_vector(state, q_map), 1)
1019
1059
 
1020
1060
 
1021
- def test_expectation_from_state_vector_qubit_map():
1061
+ def test_expectation_from_state_vector_qubit_map() -> None:
1022
1062
  q0, q1, q2 = _make_qubits(3)
1063
+ z: cirq.PauliString[cirq.NamedQubit]
1023
1064
  z = cirq.PauliString({q0: cirq.Z})
1024
1065
  wf = np.array([0, 1, 0, 1, 0, 0, 0, 0], dtype=complex) / np.sqrt(2)
1025
1066
  for state in [wf, wf.reshape((2, 2, 2))]:
@@ -1043,7 +1084,7 @@ def test_expectation_from_state_vector_qubit_map():
1043
1084
  )
1044
1085
 
1045
1086
 
1046
- def test_pauli_string_expectation_from_state_vector_pure_state():
1087
+ def test_pauli_string_expectation_from_state_vector_pure_state() -> None:
1047
1088
  qubits = cirq.LineQubit.range(4)
1048
1089
  q_map = {q: i for i, q in enumerate(qubits)}
1049
1090
 
@@ -1054,13 +1095,13 @@ def test_pauli_string_expectation_from_state_vector_pure_state():
1054
1095
  qubit_order=qubits, ignore_terminal_measurements=False, dtype=np.complex128
1055
1096
  )
1056
1097
 
1057
- z0z1 = cirq.PauliString({qubits[0]: cirq.Z, qubits[1]: cirq.Z})
1058
- z0z2 = cirq.PauliString({qubits[0]: cirq.Z, qubits[2]: cirq.Z})
1059
- z0z3 = cirq.PauliString({qubits[0]: cirq.Z, qubits[3]: cirq.Z})
1060
- z0x1 = cirq.PauliString({qubits[0]: cirq.Z, qubits[1]: cirq.X})
1061
- z1x2 = cirq.PauliString({qubits[1]: cirq.Z, qubits[2]: cirq.X})
1062
- x0z1 = cirq.PauliString({qubits[0]: cirq.X, qubits[1]: cirq.Z})
1063
- x3 = cirq.PauliString({qubits[3]: cirq.X})
1098
+ z0z1: cirq.PauliString = cirq.PauliString({qubits[0]: cirq.Z, qubits[1]: cirq.Z})
1099
+ z0z2: cirq.PauliString = cirq.PauliString({qubits[0]: cirq.Z, qubits[2]: cirq.Z})
1100
+ z0z3: cirq.PauliString = cirq.PauliString({qubits[0]: cirq.Z, qubits[3]: cirq.Z})
1101
+ z0x1: cirq.PauliString = cirq.PauliString({qubits[0]: cirq.Z, qubits[1]: cirq.X})
1102
+ z1x2: cirq.PauliString = cirq.PauliString({qubits[1]: cirq.Z, qubits[2]: cirq.X})
1103
+ x0z1: cirq.PauliString = cirq.PauliString({qubits[0]: cirq.X, qubits[1]: cirq.Z})
1104
+ x3: cirq.PauliString = cirq.PauliString({qubits[3]: cirq.X})
1064
1105
 
1065
1106
  for state in [wf, wf.reshape((2, 2, 2, 2))]:
1066
1107
  np.testing.assert_allclose(z0z1.expectation_from_state_vector(state, q_map), -1, atol=1e-8)
@@ -1072,7 +1113,7 @@ def test_pauli_string_expectation_from_state_vector_pure_state():
1072
1113
  np.testing.assert_allclose(x3.expectation_from_state_vector(state, q_map), -1, atol=1e-8)
1073
1114
 
1074
1115
 
1075
- def test_pauli_string_expectation_from_state_vector_pure_state_with_coef():
1116
+ def test_pauli_string_expectation_from_state_vector_pure_state_with_coef() -> None:
1076
1117
  qs = cirq.LineQubit.range(4)
1077
1118
  q_map = {q: i for i, q in enumerate(qs)}
1078
1119
 
@@ -1093,8 +1134,9 @@ def test_pauli_string_expectation_from_state_vector_pure_state_with_coef():
1093
1134
  np.testing.assert_allclose(z1x2.expectation_from_state_vector(state, q_map), 1, atol=1e-8)
1094
1135
 
1095
1136
 
1096
- def test_expectation_from_density_matrix_invalid_input():
1137
+ def test_expectation_from_density_matrix_invalid_input() -> None:
1097
1138
  q0, q1, q2, q3 = _make_qubits(4)
1139
+ ps: cirq.PauliString[cirq.NamedQubit]
1098
1140
  ps = cirq.PauliString({q0: cirq.X, q1: cirq.Y})
1099
1141
  wf = cirq.testing.random_superposition(4)
1100
1142
  rho = np.kron(wf.conjugate().T, wf).reshape((4, 4))
@@ -1109,13 +1151,13 @@ def test_expectation_from_density_matrix_invalid_input():
1109
1151
 
1110
1152
  with pytest.raises(TypeError, match='mapping'):
1111
1153
  # noinspection PyTypeChecker
1112
- ps.expectation_from_density_matrix(rho, "bad type")
1154
+ ps.expectation_from_density_matrix(rho, "bad type") # type: ignore[arg-type]
1113
1155
  with pytest.raises(TypeError, match='mapping'):
1114
1156
  # noinspection PyTypeChecker
1115
- ps.expectation_from_density_matrix(rho, {"bad key": 1})
1157
+ ps.expectation_from_density_matrix(rho, {"bad key": 1}) # type: ignore[dict-item]
1116
1158
  with pytest.raises(TypeError, match='mapping'):
1117
1159
  # noinspection PyTypeChecker
1118
- ps.expectation_from_density_matrix(rho, {q0: "bad value"})
1160
+ ps.expectation_from_density_matrix(rho, {q0: "bad value"}) # type: ignore[dict-item]
1119
1161
  with pytest.raises(ValueError, match='complete'):
1120
1162
  ps.expectation_from_density_matrix(rho, {q0: 0})
1121
1163
  with pytest.raises(ValueError, match='complete'):
@@ -1162,8 +1204,9 @@ def test_expectation_from_density_matrix_invalid_input():
1162
1204
  _ = ps.expectation_from_density_matrix(rho_or_wf, q_map)
1163
1205
 
1164
1206
 
1165
- def test_expectation_from_density_matrix_check_preconditions():
1207
+ def test_expectation_from_density_matrix_check_preconditions() -> None:
1166
1208
  q0, q1 = _make_qubits(2)
1209
+ ps: cirq.PauliString[cirq.NamedQubit]
1167
1210
  ps = cirq.PauliString({q0: cirq.X, q1: cirq.Y})
1168
1211
  q_map = {q0: 0, q1: 1}
1169
1212
 
@@ -1182,9 +1225,10 @@ def test_expectation_from_density_matrix_check_preconditions():
1182
1225
  )
1183
1226
 
1184
1227
 
1185
- def test_expectation_from_density_matrix_basis_states():
1228
+ def test_expectation_from_density_matrix_basis_states() -> None:
1186
1229
  q0 = cirq.LineQubit(0)
1187
1230
  x0_pauli_map = {q0: cirq.X}
1231
+ x0: cirq.PauliString[cirq.LineQubit]
1188
1232
  x0 = cirq.PauliString(x0_pauli_map)
1189
1233
  q_map = {q0: 0}
1190
1234
  np.testing.assert_allclose(
@@ -1202,11 +1246,13 @@ def test_expectation_from_density_matrix_basis_states():
1202
1246
  )
1203
1247
 
1204
1248
 
1205
- def test_expectation_from_density_matrix_entangled_states():
1249
+ def test_expectation_from_density_matrix_entangled_states() -> None:
1206
1250
  q0, q1 = _make_qubits(2)
1207
1251
  z0z1_pauli_map = {q0: cirq.Z, q1: cirq.Z}
1252
+ z0z1: cirq.PauliString[cirq.NamedQubit]
1208
1253
  z0z1 = cirq.PauliString(z0z1_pauli_map)
1209
1254
  x0x1_pauli_map = {q0: cirq.X, q1: cirq.X}
1255
+ x0x1: cirq.PauliString[cirq.NamedQubit]
1210
1256
  x0x1 = cirq.PauliString(x0x1_pauli_map)
1211
1257
  q_map = {q0: 0, q1: 1}
1212
1258
 
@@ -1229,8 +1275,9 @@ def test_expectation_from_density_matrix_entangled_states():
1229
1275
  np.testing.assert_allclose(x0x1.expectation_from_density_matrix(state, q_map), 1)
1230
1276
 
1231
1277
 
1232
- def test_expectation_from_density_matrix_qubit_map():
1278
+ def test_expectation_from_density_matrix_qubit_map() -> None:
1233
1279
  q0, q1, q2 = _make_qubits(3)
1280
+ z: cirq.PauliString[cirq.NamedQubit]
1234
1281
  z = cirq.PauliString({q0: cirq.Z})
1235
1282
  wf = np.array([0, 1, 0, 1, 0, 0, 0, 0], dtype=complex) / np.sqrt(2)
1236
1283
  rho = np.kron(wf, wf).reshape((8, 8))
@@ -1256,8 +1303,9 @@ def test_expectation_from_density_matrix_qubit_map():
1256
1303
  )
1257
1304
 
1258
1305
 
1259
- def test_pauli_string_expectation_from_density_matrix_pure_state():
1306
+ def test_pauli_string_expectation_from_density_matrix_pure_state() -> None:
1260
1307
  qubits = cirq.LineQubit.range(4)
1308
+ q_map: dict[cirq.Qid, int]
1261
1309
  q_map = {q: i for i, q in enumerate(qubits)}
1262
1310
 
1263
1311
  circuit = cirq.Circuit(
@@ -1268,6 +1316,14 @@ def test_pauli_string_expectation_from_density_matrix_pure_state():
1268
1316
  )
1269
1317
  rho = np.outer(state_vector, np.conj(state_vector))
1270
1318
 
1319
+ z0z1: cirq.PauliString[cirq.Qid]
1320
+ z0z2: cirq.PauliString[cirq.Qid]
1321
+ z0z3: cirq.PauliString[cirq.Qid]
1322
+ z0x1: cirq.PauliString[cirq.Qid]
1323
+ z1x2: cirq.PauliString[cirq.Qid]
1324
+ x0z1: cirq.PauliString[cirq.Qid]
1325
+ x3: cirq.PauliString[cirq.Qid]
1326
+
1271
1327
  z0z1 = cirq.PauliString({qubits[0]: cirq.Z, qubits[1]: cirq.Z})
1272
1328
  z0z2 = cirq.PauliString({qubits[0]: cirq.Z, qubits[2]: cirq.Z})
1273
1329
  z0z3 = cirq.PauliString({qubits[0]: cirq.Z, qubits[3]: cirq.Z})
@@ -1286,7 +1342,7 @@ def test_pauli_string_expectation_from_density_matrix_pure_state():
1286
1342
  np.testing.assert_allclose(x3.expectation_from_density_matrix(state, q_map), -1)
1287
1343
 
1288
1344
 
1289
- def test_pauli_string_expectation_from_density_matrix_pure_state_with_coef():
1345
+ def test_pauli_string_expectation_from_density_matrix_pure_state_with_coef() -> None:
1290
1346
  qs = cirq.LineQubit.range(4)
1291
1347
  q_map = {q: i for i, q in enumerate(qs)}
1292
1348
 
@@ -1306,7 +1362,7 @@ def test_pauli_string_expectation_from_density_matrix_pure_state_with_coef():
1306
1362
  np.testing.assert_allclose(z1x2.expectation_from_density_matrix(state, q_map), 1)
1307
1363
 
1308
1364
 
1309
- def test_pauli_string_expectation_from_state_vector_mixed_state_linearity():
1365
+ def test_pauli_string_expectation_from_state_vector_mixed_state_linearity() -> None:
1310
1366
  n_qubits = 6
1311
1367
 
1312
1368
  state_vector1 = cirq.testing.random_superposition(2**n_qubits)
@@ -1316,9 +1372,11 @@ def test_pauli_string_expectation_from_state_vector_mixed_state_linearity():
1316
1372
  density_matrix = rho1 / 2 + rho2 / 2
1317
1373
 
1318
1374
  qubits = cirq.LineQubit.range(n_qubits)
1375
+ q_map: dict[cirq.Qid, int]
1319
1376
  q_map = {q: i for i, q in enumerate(qubits)}
1320
1377
  paulis = [cirq.X, cirq.Y, cirq.Z]
1321
- pauli_string = cirq.PauliString({q: np.random.choice(paulis) for q in qubits})
1378
+ pauli_string: cirq.PauliString[cirq.Qid]
1379
+ pauli_string = cirq.PauliString({q: np.random.choice(paulis) for q in qubits}) # type: ignore[arg-type]
1322
1380
 
1323
1381
  a = pauli_string.expectation_from_state_vector(state_vector1, q_map)
1324
1382
  b = pauli_string.expectation_from_state_vector(state_vector2, q_map)
@@ -1326,7 +1384,7 @@ def test_pauli_string_expectation_from_state_vector_mixed_state_linearity():
1326
1384
  np.testing.assert_allclose(0.5 * (a + b), c)
1327
1385
 
1328
1386
 
1329
- def test_conjugated_by_normal_gates():
1387
+ def test_conjugated_by_normal_gates() -> None:
1330
1388
  a = cirq.LineQubit(0)
1331
1389
 
1332
1390
  assert_conjugation(cirq.X(a), cirq.H(a), cirq.Z(a))
@@ -1343,14 +1401,15 @@ def test_conjugated_by_normal_gates():
1343
1401
  assert_conjugation(cirq.Z(a), clifford_op, -cirq.Z(a))
1344
1402
 
1345
1403
 
1346
- def test_conjugated_by_op_gate_of_clifford_gate_type():
1404
+ def test_conjugated_by_op_gate_of_clifford_gate_type() -> None:
1347
1405
  a = cirq.LineQubit(0)
1348
1406
 
1349
1407
  assert_conjugation(cirq.X(a), cirq.CliffordGate.from_op_list([cirq.H(a)], [a]).on(a), cirq.Z(a))
1350
1408
 
1351
1409
 
1352
- def test_dense():
1410
+ def test_dense() -> None:
1353
1411
  a, b, c, d, e = cirq.LineQubit.range(5)
1412
+ p: cirq.PauliString[cirq.LineQubit]
1354
1413
  p = cirq.PauliString([cirq.X(a), cirq.Y(b), cirq.Z(c)])
1355
1414
  assert p.dense([a, b, c, d]) == cirq.DensePauliString('XYZI')
1356
1415
  assert p.dense([d, e, a, b, c]) == cirq.DensePauliString('IIXYZ')
@@ -1363,7 +1422,7 @@ def test_dense():
1363
1422
 
1364
1423
 
1365
1424
  @pytest.mark.parametrize('qubits', [*itertools.permutations(cirq.LineQubit.range(3))])
1366
- def test_gate_consistent(qubits):
1425
+ def test_gate_consistent(qubits) -> None:
1367
1426
  g = cirq.DensePauliString('XYZ')
1368
1427
  assert g == g(*qubits).gate
1369
1428
  a, b, c = cirq.GridQubit.rect(1, 3)
@@ -1371,8 +1430,9 @@ def test_gate_consistent(qubits):
1371
1430
  assert ps.gate == ps.with_qubits(*qubits).gate
1372
1431
 
1373
1432
 
1374
- def test_conjugated_by_incorrectly_powered_cliffords():
1433
+ def test_conjugated_by_incorrectly_powered_cliffords() -> None:
1375
1434
  a, b = cirq.LineQubit.range(2)
1435
+ p: cirq.PauliString[cirq.LineQubit]
1376
1436
  p = cirq.PauliString([cirq.X(a), cirq.Z(b)])
1377
1437
  cliffords = [
1378
1438
  cirq.H(a),
@@ -1403,7 +1463,7 @@ def test_conjugated_by_incorrectly_powered_cliffords():
1403
1463
  _ = p.conjugated_by(c ** sympy.Symbol('t'))
1404
1464
 
1405
1465
 
1406
- def test_conjugated_by_global_phase():
1466
+ def test_conjugated_by_global_phase() -> None:
1407
1467
  """Global phase gate preserves PauliString."""
1408
1468
  a = cirq.LineQubit(0)
1409
1469
  assert_conjugation(cirq.X(a), cirq.global_phase_operation(1j), cirq.X(a))
@@ -1419,7 +1479,7 @@ def test_conjugated_by_global_phase():
1419
1479
  assert_conjugation(cirq.X(a), DecomposeGlobal().on(a), cirq.X(a))
1420
1480
 
1421
1481
 
1422
- def test_conjugated_by_composite_with_disjoint_sub_gates():
1482
+ def test_conjugated_by_composite_with_disjoint_sub_gates() -> None:
1423
1483
  a, b = cirq.LineQubit.range(2)
1424
1484
 
1425
1485
  class DecomposeDisjoint(cirq.Gate):
@@ -1435,7 +1495,7 @@ def test_conjugated_by_composite_with_disjoint_sub_gates():
1435
1495
  assert ps.conjugated_by(DecomposeDisjoint().on(a, b)) == ps.conjugated_by(cirq.H(b))
1436
1496
 
1437
1497
 
1438
- def test_conjugated_by_clifford_composite():
1498
+ def test_conjugated_by_clifford_composite() -> None:
1439
1499
  class UnknownGate(cirq.Gate):
1440
1500
  def num_qubits(self) -> int:
1441
1501
  return 4
@@ -1452,14 +1512,14 @@ def test_conjugated_by_clifford_composite():
1452
1512
  assert_conjugation(ps, u(a, b, c, d), cirq.Z(a) * cirq.X(b))
1453
1513
 
1454
1514
 
1455
- def test_conjugated_by_move_into_uninvolved():
1515
+ def test_conjugated_by_move_into_uninvolved() -> None:
1456
1516
  a, b, c, d = cirq.LineQubit.range(4)
1457
1517
  ps = cirq.X(a) * cirq.Z(b)
1458
1518
  assert_conjugation_multi_ops(ps, [cirq.SWAP(c, d), cirq.SWAP(b, c)], cirq.X(a) * cirq.Z(d))
1459
1519
  assert_conjugation_multi_ops(ps, [cirq.SWAP(b, c), cirq.SWAP(c, d)], cirq.X(a) * cirq.Z(c))
1460
1520
 
1461
1521
 
1462
- def test_conjugated_by_common_single_qubit_gates():
1522
+ def test_conjugated_by_common_single_qubit_gates() -> None:
1463
1523
  a, b = cirq.LineQubit.range(2)
1464
1524
 
1465
1525
  base_single_qubit_gates = [
@@ -1484,7 +1544,7 @@ def test_conjugated_by_common_single_qubit_gates():
1484
1544
  assert_conjugation(p(a), g(a))
1485
1545
 
1486
1546
 
1487
- def test_conjugated_by_common_two_qubit_gates():
1547
+ def test_conjugated_by_common_two_qubit_gates() -> None:
1488
1548
 
1489
1549
  a, b, c, d = cirq.LineQubit.range(4)
1490
1550
  two_qubit_gates = [
@@ -1505,7 +1565,7 @@ def test_conjugated_by_common_two_qubit_gates():
1505
1565
  ]
1506
1566
  for p1 in [cirq.I, cirq.X, cirq.Y, cirq.Z]:
1507
1567
  for p2 in [cirq.I, cirq.X, cirq.Y, cirq.Z]:
1508
- pd = cirq.DensePauliString([p1, p2])
1568
+ pd: cirq.DensePauliString = cirq.DensePauliString([p1, p2]) # type: ignore[list-item]
1509
1569
  p = pd.sparse([a, b])
1510
1570
  for g in two_qubit_gates:
1511
1571
  # pauli_string on (a,b), clifford on (c,d): pauli_string preserves.
@@ -1515,7 +1575,7 @@ def test_conjugated_by_common_two_qubit_gates():
1515
1575
  assert_conjugation(p, g.on(a, b))
1516
1576
 
1517
1577
 
1518
- def test_conjugated_by_ordering():
1578
+ def test_conjugated_by_ordering() -> None:
1519
1579
  """Tests .conjugated_by([op1, op2]) == .conjugated_by(op2).conjugated_by(op1)"""
1520
1580
  a, b = cirq.LineQubit.range(2)
1521
1581
  inp = cirq.Z(b)
@@ -1524,8 +1584,9 @@ def test_conjugated_by_ordering():
1524
1584
  assert out1 == out2 == cirq.X(a) * cirq.Z(b)
1525
1585
 
1526
1586
 
1527
- def test_pretty_print():
1587
+ def test_pretty_print() -> None:
1528
1588
  a, b, c = cirq.LineQubit.range(3)
1589
+ result: cirq.PauliString[cirq.LineQubit]
1529
1590
  result = cirq.PauliString({a: 'x', b: 'y', c: 'z'})
1530
1591
 
1531
1592
  # Test Jupyter console output from
@@ -1546,7 +1607,7 @@ def test_pretty_print():
1546
1607
  assert p.text_pretty == 'cirq.PauliString(...)'
1547
1608
 
1548
1609
 
1549
- def test_circuit_diagram_info():
1610
+ def test_circuit_diagram_info() -> None:
1550
1611
  a, b, c = cirq.LineQubit.range(3)
1551
1612
 
1552
1613
  assert cirq.circuit_diagram_info(cirq.PauliString(), default=None) is None
@@ -1570,13 +1631,13 @@ def test_circuit_diagram_info():
1570
1631
  )
1571
1632
 
1572
1633
 
1573
- def test_mutable_pauli_string_init_raises():
1634
+ def test_mutable_pauli_string_init_raises() -> None:
1574
1635
  q = cirq.LineQubit.range(3)
1575
1636
  with pytest.raises(ValueError, match='must be between 1 and 3'):
1576
1637
  _ = cirq.MutablePauliString(pauli_int_dict={q[0]: 0, q[1]: 1, q[2]: 2})
1577
1638
 
1578
1639
 
1579
- def test_mutable_pauli_string_equality():
1640
+ def test_mutable_pauli_string_equality() -> None:
1580
1641
  eq = cirq.testing.EqualsTester()
1581
1642
  a, b, c = cirq.LineQubit.range(3)
1582
1643
 
@@ -1624,11 +1685,12 @@ def test_mutable_pauli_string_equality():
1624
1685
  _ = cirq.MutablePauliString("test")
1625
1686
  with pytest.raises(TypeError, match="cirq.PAULI_STRING_LIKE"):
1626
1687
  # noinspection PyTypeChecker
1627
- _ = cirq.MutablePauliString(object())
1688
+ _ = cirq.MutablePauliString(object()) # type: ignore[arg-type]
1628
1689
 
1629
1690
 
1630
- def test_mutable_pauli_string_inplace_multiplication():
1691
+ def test_mutable_pauli_string_inplace_multiplication() -> None:
1631
1692
  a, b, c = cirq.LineQubit.range(3)
1693
+ p: cirq.MutablePauliString[cirq.LineQubit]
1632
1694
  p = cirq.MutablePauliString()
1633
1695
  original = p
1634
1696
 
@@ -1676,7 +1738,7 @@ def test_mutable_pauli_string_inplace_multiplication():
1676
1738
  assert p == -cirq.X(b) and p is original
1677
1739
 
1678
1740
 
1679
- def test_mutable_frozen_copy():
1741
+ def test_mutable_frozen_copy() -> None:
1680
1742
  a, b, c = cirq.LineQubit.range(3)
1681
1743
  p = -cirq.X(a) * cirq.Y(b) * cirq.Z(c)
1682
1744
 
@@ -1696,8 +1758,9 @@ def test_mutable_frozen_copy():
1696
1758
  assert p == pf == pm == pmm == pmf
1697
1759
 
1698
1760
 
1699
- def test_mutable_pauli_string_inplace_conjugate_by():
1761
+ def test_mutable_pauli_string_inplace_conjugate_by() -> None:
1700
1762
  a, b, c = cirq.LineQubit.range(3)
1763
+ p: cirq.MutablePauliString[cirq.LineQubit]
1701
1764
  p = cirq.MutablePauliString(cirq.X(a))
1702
1765
 
1703
1766
  class NoOp(cirq.Operation):
@@ -1828,15 +1891,15 @@ def test_mutable_pauli_string_inplace_conjugate_by():
1828
1891
  assert p2 is p and p == cirq.X(a) * cirq.Y(b)
1829
1892
 
1830
1893
 
1831
- def test_mps_inplace_after_clifford_gate_type():
1894
+ def test_mps_inplace_after_clifford_gate_type() -> None:
1832
1895
  q = cirq.LineQubit(0)
1833
1896
 
1834
- mps = cirq.MutablePauliString(cirq.X(q))
1897
+ mps: cirq.MutablePauliString = cirq.MutablePauliString(cirq.X(q))
1835
1898
  mps2 = mps.inplace_after(cirq.CliffordGate.from_op_list([cirq.H(q)], [q]).on(q))
1836
1899
  assert mps2 is mps and mps == cirq.Z(q)
1837
1900
 
1838
1901
 
1839
- def test_after_before_vs_conjugate_by():
1902
+ def test_after_before_vs_conjugate_by() -> None:
1840
1903
  a, b, c = cirq.LineQubit.range(3)
1841
1904
  p = cirq.X(a) * cirq.Y(b) * cirq.Z(c)
1842
1905
  assert p.before(cirq.S(b)) == p.conjugated_by(cirq.S(b))
@@ -1846,8 +1909,9 @@ def test_after_before_vs_conjugate_by():
1846
1909
  )
1847
1910
 
1848
1911
 
1849
- def test_mutable_pauli_string_dict_functionality():
1912
+ def test_mutable_pauli_string_dict_functionality() -> None:
1850
1913
  a, b, c = cirq.LineQubit.range(3)
1914
+ p: cirq.MutablePauliString[cirq.LineQubit]
1851
1915
  p = cirq.MutablePauliString()
1852
1916
  with pytest.raises(KeyError):
1853
1917
  _ = p[a]
@@ -1886,27 +1950,30 @@ def test_mutable_pauli_string_dict_functionality():
1886
1950
  @pytest.mark.parametrize(
1887
1951
  'pauli', (cirq.X, cirq.Y, cirq.Z, cirq.I, "I", "X", "Y", "Z", "i", "x", "y", "z", 0, 1, 2, 3)
1888
1952
  )
1889
- def test_mutable_pauli_string_dict_pauli_like(pauli):
1953
+ def test_mutable_pauli_string_dict_pauli_like(pauli) -> None:
1954
+ p: cirq.MutablePauliString
1890
1955
  p = cirq.MutablePauliString()
1891
1956
  # Check that is successfully converts.
1892
1957
  p[0] = pauli
1893
1958
 
1894
1959
 
1895
- def test_mutable_pauli_string_dict_pauli_like_not_pauli_like():
1960
+ def test_mutable_pauli_string_dict_pauli_like_not_pauli_like() -> None:
1961
+ p: cirq.MutablePauliString[cirq.Qid]
1896
1962
  p = cirq.MutablePauliString()
1897
1963
  # Check error string includes terms like "X" in error message.
1898
1964
  with pytest.raises(TypeError, match="PAULI_GATE_LIKE.*X"):
1899
- p[0] = 1.2
1965
+ p[0] = 1.2 # type: ignore
1900
1966
 
1901
1967
 
1902
- def test_mutable_pauli_string_text():
1968
+ def test_mutable_pauli_string_text() -> None:
1969
+ p: cirq.MutablePauliString
1903
1970
  p = cirq.MutablePauliString(cirq.X(cirq.LineQubit(0)) * cirq.Y(cirq.LineQubit(1)))
1904
1971
  assert str(cirq.MutablePauliString()) == "mutable I"
1905
1972
  assert str(p) == "mutable X(q(0))*Y(q(1))"
1906
1973
  cirq.testing.assert_equivalent_repr(p)
1907
1974
 
1908
1975
 
1909
- def test_mutable_pauli_string_mul():
1976
+ def test_mutable_pauli_string_mul() -> None:
1910
1977
  a, b = cirq.LineQubit.range(2)
1911
1978
  p = cirq.X(a).mutable_copy()
1912
1979
  q = cirq.Y(b).mutable_copy()
@@ -1918,7 +1985,7 @@ def test_mutable_pauli_string_mul():
1918
1985
  assert isinstance(2 * p, cirq.PauliString)
1919
1986
 
1920
1987
 
1921
- def test_mutable_can_override_mul():
1988
+ def test_mutable_can_override_mul() -> None:
1922
1989
  class LMul:
1923
1990
  def __mul__(self, other):
1924
1991
  return "Yay!"
@@ -1931,15 +1998,17 @@ def test_mutable_can_override_mul():
1931
1998
  assert LMul() * cirq.MutablePauliString() == "Yay!"
1932
1999
 
1933
2000
 
1934
- def test_coefficient_precision():
2001
+ def test_coefficient_precision() -> None:
1935
2002
  qs = cirq.LineQubit.range(4 * 10**3)
2003
+ r: cirq.MutablePauliString[cirq.LineQubit]
2004
+ r2: cirq.MutablePauliString[cirq.LineQubit]
1936
2005
  r = cirq.MutablePauliString({q: cirq.X for q in qs})
1937
2006
  r2 = cirq.MutablePauliString({q: cirq.Y for q in qs})
1938
2007
  r2 *= r
1939
2008
  assert r2.coefficient == 1
1940
2009
 
1941
2010
 
1942
- def test_transform_qubits():
2011
+ def test_transform_qubits() -> None:
1943
2012
  a, b, c = cirq.LineQubit.range(3)
1944
2013
  p = cirq.X(a) * cirq.Z(b)
1945
2014
  p2 = cirq.X(b) * cirq.Z(c)
@@ -1960,9 +2029,10 @@ def test_transform_qubits():
1960
2029
  assert m2 == p2
1961
2030
 
1962
2031
 
1963
- def test_parameterization():
2032
+ def test_parameterization() -> None:
1964
2033
  t = sympy.Symbol('t')
1965
2034
  q = cirq.LineQubit(0)
2035
+ pst: cirq.PauliString[cirq.LineQubit]
1966
2036
  pst = cirq.PauliString({q: 'x'}, coefficient=t)
1967
2037
  assert cirq.is_parameterized(pst)
1968
2038
  assert cirq.parameter_names(pst) == {'t'}
@@ -1991,9 +2061,11 @@ def test_parameterization():
1991
2061
 
1992
2062
 
1993
2063
  @pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
1994
- def test_resolve(resolve_fn):
2064
+ def test_resolve(resolve_fn) -> None:
1995
2065
  t = sympy.Symbol('t')
1996
2066
  q = cirq.LineQubit(0)
2067
+ pst: cirq.PauliString[cirq.LineQubit]
2068
+ ps1: cirq.PauliString[cirq.LineQubit]
1997
2069
  pst = cirq.PauliString({q: 'x'}, coefficient=t)
1998
2070
  ps1 = cirq.PauliString({q: 'x'}, coefficient=1j)
1999
2071
  assert resolve_fn(pst, {'t': 1j}) == ps1