cirq-core 1.6.0.dev20250520054601__py3-none-any.whl → 1.6.0.dev20250520181654__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 (289) hide show
  1. cirq/_compat.py +15 -17
  2. cirq/_compat_test.py +6 -9
  3. cirq/_doc.py +2 -2
  4. cirq/_import.py +6 -6
  5. cirq/_version.py +1 -1
  6. cirq/_version_test.py +1 -1
  7. cirq/circuits/_block_diagram_drawer.py +9 -10
  8. cirq/circuits/_box_drawing_character_data.py +6 -8
  9. cirq/circuits/_bucket_priority_queue.py +7 -7
  10. cirq/circuits/circuit.py +118 -125
  11. cirq/circuits/circuit_operation.py +38 -52
  12. cirq/circuits/circuit_test.py +4 -4
  13. cirq/circuits/frozen_circuit.py +13 -23
  14. cirq/circuits/moment.py +23 -29
  15. cirq/circuits/optimization_pass.py +4 -4
  16. cirq/circuits/optimization_pass_test.py +4 -6
  17. cirq/circuits/qasm_output.py +11 -11
  18. cirq/circuits/text_diagram_drawer.py +21 -36
  19. cirq/contrib/acquaintance/bipartite.py +5 -8
  20. cirq/contrib/acquaintance/executor.py +5 -5
  21. cirq/contrib/acquaintance/executor_test.py +3 -3
  22. cirq/contrib/acquaintance/gates.py +16 -26
  23. cirq/contrib/acquaintance/gates_test.py +3 -3
  24. cirq/contrib/acquaintance/mutation_utils.py +4 -4
  25. cirq/contrib/acquaintance/optimizers.py +4 -4
  26. cirq/contrib/acquaintance/permutation.py +15 -27
  27. cirq/contrib/acquaintance/shift.py +3 -3
  28. cirq/contrib/acquaintance/shift_swap_network.py +4 -4
  29. cirq/contrib/acquaintance/strategies/cubic.py +2 -2
  30. cirq/contrib/acquaintance/strategies/quartic_paired.py +6 -6
  31. cirq/contrib/bayesian_network/bayesian_network_gate.py +9 -10
  32. cirq/contrib/circuitdag/circuit_dag.py +2 -2
  33. cirq/contrib/custom_simulators/custom_state_simulator.py +3 -3
  34. cirq/contrib/custom_simulators/custom_state_simulator_test.py +4 -4
  35. cirq/contrib/graph_device/graph_device.py +5 -5
  36. cirq/contrib/graph_device/hypergraph.py +12 -12
  37. cirq/contrib/graph_device/uniform_graph_device.py +4 -4
  38. cirq/contrib/paulistring/clifford_optimize.py +2 -2
  39. cirq/contrib/paulistring/clifford_target_gateset.py +7 -7
  40. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +31 -31
  41. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +23 -23
  42. cirq/contrib/paulistring/recombine.py +3 -3
  43. cirq/contrib/paulistring/separate.py +2 -2
  44. cirq/contrib/qasm_import/_parser.py +20 -32
  45. cirq/contrib/qcircuit/qcircuit_diagram_info.py +3 -5
  46. cirq/contrib/quantum_volume/quantum_volume.py +24 -24
  47. cirq/contrib/quimb/density_matrix.py +12 -14
  48. cirq/contrib/quimb/mps_simulator.py +20 -20
  49. cirq/contrib/quimb/state_vector.py +6 -10
  50. cirq/contrib/quirk/export_to_quirk.py +3 -3
  51. cirq/contrib/quirk/quirk_gate.py +15 -15
  52. cirq/contrib/routing/device.py +3 -3
  53. cirq/contrib/routing/greedy.py +10 -21
  54. cirq/contrib/routing/initialization.py +2 -2
  55. cirq/contrib/routing/swap_network.py +3 -3
  56. cirq/contrib/routing/utils.py +2 -2
  57. cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +8 -8
  58. cirq/contrib/svg/svg.py +3 -3
  59. cirq/devices/grid_device_metadata.py +10 -10
  60. cirq/devices/grid_qubit.py +20 -20
  61. cirq/devices/insertion_noise_model.py +5 -5
  62. cirq/devices/line_qubit.py +13 -13
  63. cirq/devices/named_topologies.py +18 -29
  64. cirq/devices/noise_model.py +3 -3
  65. cirq/devices/noise_properties.py +2 -2
  66. cirq/devices/noise_properties_test.py +1 -3
  67. cirq/devices/noise_utils.py +7 -7
  68. cirq/devices/superconducting_qubits_noise_properties.py +21 -21
  69. cirq/devices/superconducting_qubits_noise_properties_test.py +5 -7
  70. cirq/devices/thermal_noise_model.py +14 -14
  71. cirq/devices/unconstrained_device.py +2 -2
  72. cirq/experiments/benchmarking/parallel_xeb.py +29 -31
  73. cirq/experiments/n_qubit_tomography.py +5 -7
  74. cirq/experiments/qubit_characterizations.py +29 -40
  75. cirq/experiments/qubit_characterizations_test.py +1 -1
  76. cirq/experiments/random_quantum_circuit_generation.py +19 -33
  77. cirq/experiments/random_quantum_circuit_generation_test.py +6 -6
  78. cirq/experiments/readout_confusion_matrix.py +14 -14
  79. cirq/experiments/single_qubit_readout_calibration.py +12 -12
  80. cirq/experiments/t2_decay_experiment.py +7 -7
  81. cirq/experiments/two_qubit_xeb.py +32 -32
  82. cirq/experiments/two_qubit_xeb_test.py +5 -5
  83. cirq/experiments/xeb_fitting.py +25 -25
  84. cirq/experiments/xeb_sampling.py +22 -33
  85. cirq/experiments/xeb_simulation.py +5 -5
  86. cirq/experiments/xeb_simulation_test.py +3 -3
  87. cirq/experiments/z_phase_calibration.py +19 -19
  88. cirq/interop/quirk/cells/arithmetic_cells.py +23 -36
  89. cirq/interop/quirk/cells/cell.py +9 -21
  90. cirq/interop/quirk/cells/composite_cell.py +7 -22
  91. cirq/interop/quirk/cells/control_cells.py +8 -8
  92. cirq/interop/quirk/cells/input_cells.py +4 -4
  93. cirq/interop/quirk/cells/input_rotation_cells.py +5 -5
  94. cirq/interop/quirk/cells/parse.py +20 -23
  95. cirq/interop/quirk/cells/qubit_permutation_cells.py +3 -3
  96. cirq/interop/quirk/cells/swap_cell.py +3 -3
  97. cirq/interop/quirk/cells/testing.py +5 -7
  98. cirq/interop/quirk/url_to_circuit.py +17 -33
  99. cirq/json_resolver_cache.py +6 -6
  100. cirq/linalg/decompositions.py +20 -31
  101. cirq/linalg/diagonalize.py +4 -4
  102. cirq/linalg/diagonalize_test.py +3 -4
  103. cirq/linalg/operator_spaces.py +5 -5
  104. cirq/linalg/predicates.py +7 -7
  105. cirq/linalg/transformations.py +20 -20
  106. cirq/ops/arithmetic_operation.py +13 -15
  107. cirq/ops/boolean_hamiltonian.py +17 -17
  108. cirq/ops/classically_controlled_operation.py +13 -25
  109. cirq/ops/clifford_gate.py +31 -35
  110. cirq/ops/clifford_gate_test.py +2 -3
  111. cirq/ops/common_channels.py +30 -32
  112. cirq/ops/common_gates.py +64 -74
  113. cirq/ops/control_values.py +12 -12
  114. cirq/ops/controlled_gate.py +15 -30
  115. cirq/ops/controlled_gate_test.py +5 -5
  116. cirq/ops/controlled_operation.py +12 -25
  117. cirq/ops/controlled_operation_test.py +5 -5
  118. cirq/ops/dense_pauli_string.py +23 -34
  119. cirq/ops/dense_pauli_string_test.py +1 -2
  120. cirq/ops/diagonal_gate.py +9 -20
  121. cirq/ops/diagonal_gate_test.py +1 -3
  122. cirq/ops/eigen_gate.py +11 -23
  123. cirq/ops/eigen_gate_test.py +6 -8
  124. cirq/ops/fourier_transform.py +5 -5
  125. cirq/ops/fsim_gate.py +14 -14
  126. cirq/ops/gate_operation.py +23 -44
  127. cirq/ops/gateset.py +23 -37
  128. cirq/ops/gateset_test.py +2 -2
  129. cirq/ops/global_phase_op.py +8 -10
  130. cirq/ops/greedy_qubit_manager.py +6 -6
  131. cirq/ops/identity.py +9 -9
  132. cirq/ops/kraus_channel.py +7 -7
  133. cirq/ops/linear_combinations.py +29 -48
  134. cirq/ops/matrix_gates.py +8 -8
  135. cirq/ops/measure_util.py +13 -14
  136. cirq/ops/measurement_gate.py +18 -29
  137. cirq/ops/mixed_unitary_channel.py +8 -8
  138. cirq/ops/named_qubit.py +10 -10
  139. cirq/ops/op_tree.py +7 -7
  140. cirq/ops/parallel_gate.py +5 -5
  141. cirq/ops/parity_gates.py +14 -14
  142. cirq/ops/pauli_gates.py +8 -10
  143. cirq/ops/pauli_interaction_gate.py +6 -6
  144. cirq/ops/pauli_measurement_gate.py +11 -23
  145. cirq/ops/pauli_string.py +35 -52
  146. cirq/ops/pauli_string_phasor.py +4 -14
  147. cirq/ops/pauli_string_raw_types.py +3 -3
  148. cirq/ops/pauli_sum_exponential.py +2 -2
  149. cirq/ops/permutation_gate.py +4 -4
  150. cirq/ops/phased_iswap_gate.py +9 -9
  151. cirq/ops/phased_x_gate.py +10 -10
  152. cirq/ops/phased_x_z_gate.py +11 -11
  153. cirq/ops/projector.py +6 -6
  154. cirq/ops/qubit_manager.py +6 -6
  155. cirq/ops/qubit_order.py +3 -3
  156. cirq/ops/random_gate_channel.py +4 -4
  157. cirq/ops/raw_types.py +48 -70
  158. cirq/ops/state_preparation_channel.py +3 -3
  159. cirq/ops/swap_gates.py +9 -9
  160. cirq/ops/tags.py +2 -4
  161. cirq/ops/three_qubit_gates.py +20 -38
  162. cirq/ops/two_qubit_diagonal_gate.py +5 -5
  163. cirq/ops/uniform_superposition_gate.py +2 -2
  164. cirq/ops/wait_gate.py +5 -5
  165. cirq/protocols/act_on_protocol_test.py +3 -3
  166. cirq/protocols/apply_channel_protocol.py +8 -14
  167. cirq/protocols/apply_mixture_protocol.py +14 -16
  168. cirq/protocols/apply_mixture_protocol_test.py +5 -6
  169. cirq/protocols/apply_unitary_protocol.py +17 -19
  170. cirq/protocols/circuit_diagram_info_protocol.py +19 -30
  171. cirq/protocols/decompose_protocol.py +30 -34
  172. cirq/protocols/inverse_protocol.py +7 -7
  173. cirq/protocols/json_serialization.py +32 -51
  174. cirq/protocols/json_serialization_test.py +9 -10
  175. cirq/protocols/kraus_protocol.py +4 -4
  176. cirq/protocols/kraus_protocol_test.py +3 -3
  177. cirq/protocols/measurement_key_protocol.py +11 -13
  178. cirq/protocols/mixture_protocol.py +4 -4
  179. cirq/protocols/qasm.py +11 -13
  180. cirq/protocols/qid_shape_protocol.py +6 -8
  181. cirq/qis/clifford_tableau.py +12 -12
  182. cirq/qis/measures.py +7 -7
  183. cirq/qis/quantum_state_representation.py +3 -3
  184. cirq/qis/states.py +51 -51
  185. cirq/sim/classical_simulator.py +10 -10
  186. cirq/sim/clifford/clifford_simulator.py +6 -6
  187. cirq/sim/clifford/clifford_tableau_simulation_state_test.py +1 -3
  188. cirq/sim/clifford/stabilizer_sampler.py +4 -4
  189. cirq/sim/clifford/stabilizer_state_ch_form.py +3 -3
  190. cirq/sim/density_matrix_simulation_state.py +15 -15
  191. cirq/sim/density_matrix_simulator.py +11 -11
  192. cirq/sim/density_matrix_utils.py +9 -9
  193. cirq/sim/mux.py +9 -9
  194. cirq/sim/simulation_product_state.py +9 -9
  195. cirq/sim/simulation_product_state_test.py +2 -2
  196. cirq/sim/simulation_state.py +14 -27
  197. cirq/sim/simulation_state_base.py +8 -24
  198. cirq/sim/simulation_utils.py +3 -4
  199. cirq/sim/simulator.py +28 -43
  200. cirq/sim/simulator_base.py +12 -25
  201. cirq/sim/simulator_base_test.py +6 -6
  202. cirq/sim/simulator_test.py +7 -7
  203. cirq/sim/sparse_simulator.py +8 -8
  204. cirq/sim/state_vector.py +8 -8
  205. cirq/sim/state_vector_simulation_state.py +17 -17
  206. cirq/sim/state_vector_simulator.py +4 -4
  207. cirq/study/flatten_expressions.py +12 -14
  208. cirq/study/resolver.py +9 -11
  209. cirq/study/result.py +11 -24
  210. cirq/study/sweepable.py +5 -5
  211. cirq/study/sweeps.py +27 -40
  212. cirq/testing/circuit_compare.py +5 -5
  213. cirq/testing/consistent_controlled_gate_op_test.py +7 -11
  214. cirq/testing/consistent_protocols.py +10 -10
  215. cirq/testing/consistent_protocols_test.py +7 -7
  216. cirq/testing/consistent_qasm.py +4 -4
  217. cirq/testing/consistent_qasm_test.py +2 -3
  218. cirq/testing/devices.py +4 -5
  219. cirq/testing/equals_tester.py +2 -2
  220. cirq/testing/equivalent_basis_map.py +4 -4
  221. cirq/testing/equivalent_repr_eval.py +3 -3
  222. cirq/testing/json.py +14 -14
  223. cirq/testing/logs.py +3 -3
  224. cirq/testing/no_identifier_qubit.py +2 -3
  225. cirq/testing/random_circuit.py +7 -7
  226. cirq/testing/random_circuit_test.py +3 -3
  227. cirq/transformers/analytical_decompositions/clifford_decomposition.py +16 -16
  228. cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +13 -13
  229. cirq/transformers/analytical_decompositions/cphase_to_fsim.py +5 -5
  230. cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +3 -3
  231. cirq/transformers/analytical_decompositions/pauli_string_decomposition.py +3 -3
  232. cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +4 -4
  233. cirq/transformers/analytical_decompositions/single_qubit_decompositions.py +6 -7
  234. cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry.py +2 -2
  235. cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +7 -7
  236. cirq/transformers/analytical_decompositions/two_qubit_state_preparation.py +4 -4
  237. cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +7 -7
  238. cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +11 -11
  239. cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +5 -5
  240. cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +14 -14
  241. cirq/transformers/dynamical_decoupling.py +13 -13
  242. cirq/transformers/dynamical_decoupling_test.py +4 -4
  243. cirq/transformers/eject_phased_paulis.py +16 -16
  244. cirq/transformers/eject_z.py +5 -7
  245. cirq/transformers/gauge_compiling/gauge_compiling.py +38 -38
  246. cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +2 -2
  247. cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +8 -8
  248. cirq/transformers/insertion_sort.py +5 -5
  249. cirq/transformers/measurement_transformers.py +14 -14
  250. cirq/transformers/merge_k_qubit_gates_test.py +1 -3
  251. cirq/transformers/merge_single_qubit_gates_test.py +1 -3
  252. cirq/transformers/qubit_management_transformers.py +5 -5
  253. cirq/transformers/routing/initial_mapper.py +4 -4
  254. cirq/transformers/routing/line_initial_mapper.py +9 -9
  255. cirq/transformers/routing/mapping_manager.py +7 -7
  256. cirq/transformers/routing/route_circuit_cqc.py +27 -27
  257. cirq/transformers/routing/visualize_routed_circuit.py +4 -4
  258. cirq/transformers/stratify.py +8 -8
  259. cirq/transformers/synchronize_terminal_measurements.py +6 -6
  260. cirq/transformers/target_gatesets/compilation_target_gateset.py +8 -8
  261. cirq/transformers/target_gatesets/compilation_target_gateset_test.py +2 -2
  262. cirq/transformers/target_gatesets/cz_gateset.py +4 -4
  263. cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +5 -5
  264. cirq/transformers/transformer_api.py +11 -26
  265. cirq/transformers/transformer_primitives.py +24 -36
  266. cirq/transformers/transformer_primitives_test.py +3 -3
  267. cirq/value/classical_data.py +18 -18
  268. cirq/value/condition.py +8 -8
  269. cirq/value/digits.py +7 -7
  270. cirq/value/duration.py +12 -12
  271. cirq/value/linear_dict.py +8 -12
  272. cirq/value/measurement_key.py +8 -8
  273. cirq/value/product_state.py +9 -9
  274. cirq/value/value_equality_attr.py +4 -4
  275. cirq/vis/heatmap.py +23 -35
  276. cirq/work/collector.py +9 -17
  277. cirq/work/observable_grouping.py +4 -7
  278. cirq/work/observable_measurement.py +29 -41
  279. cirq/work/observable_measurement_data.py +14 -14
  280. cirq/work/observable_measurement_test.py +2 -2
  281. cirq/work/observable_settings.py +9 -10
  282. cirq/work/pauli_sum_collector.py +5 -5
  283. cirq/work/sampler.py +17 -17
  284. cirq/work/zeros_sampler.py +3 -3
  285. {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/METADATA +1 -1
  286. {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/RECORD +289 -289
  287. {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/WHEEL +1 -1
  288. {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/licenses/LICENSE +0 -0
  289. {cirq_core-1.6.0.dev20250520054601.dist-info → cirq_core-1.6.0.dev20250520181654.dist-info}/top_level.txt +0 -0
@@ -18,7 +18,7 @@ from __future__ import annotations
18
18
 
19
19
  import warnings
20
20
  from types import NotImplementedType
21
- from typing import Any, Sequence, Tuple, TypeVar, Union
21
+ from typing import Any, Sequence, TypeVar
22
22
 
23
23
  import numpy as np
24
24
  from typing_extensions import Protocol
@@ -33,7 +33,7 @@ from cirq.protocols.unitary_protocol import unitary
33
33
  # Sequence[np.ndarray] to ensure the method has the correct type signature in
34
34
  # that case. It is checked for using `is`, so it won't have a false positive
35
35
  # if the user provides a different (np.array([]),) value.
36
- RaiseTypeErrorIfNotProvided: Tuple[np.ndarray] = (np.array([]),)
36
+ RaiseTypeErrorIfNotProvided: tuple[np.ndarray] = (np.array([]),)
37
37
 
38
38
 
39
39
  TDefault = TypeVar('TDefault')
@@ -43,7 +43,7 @@ class SupportsKraus(Protocol):
43
43
  """An object that may be describable as a quantum channel."""
44
44
 
45
45
  @doc_private
46
- def _kraus_(self) -> Union[Sequence[np.ndarray], NotImplementedType]:
46
+ def _kraus_(self) -> Sequence[np.ndarray] | NotImplementedType:
47
47
  r"""A list of Kraus matrices describing the quantum channel.
48
48
 
49
49
  These matrices are the terms in the operator sum representation of a
@@ -96,7 +96,7 @@ class SupportsKraus(Protocol):
96
96
 
97
97
  def kraus(
98
98
  val: Any, default: Any = RaiseTypeErrorIfNotProvided
99
- ) -> Union[Tuple[np.ndarray, ...], TDefault]:
99
+ ) -> tuple[np.ndarray, ...] | TDefault:
100
100
  r"""Returns a list of matrices describing the channel for the given value.
101
101
 
102
102
  These matrices are the terms in the operator sum representation of
@@ -16,14 +16,14 @@
16
16
 
17
17
  from __future__ import annotations
18
18
 
19
- from typing import Iterable, List, Sequence, Tuple
19
+ from typing import Iterable, Sequence
20
20
 
21
21
  import numpy as np
22
22
  import pytest
23
23
 
24
24
  import cirq
25
25
 
26
- LOCAL_DEFAULT: List[np.ndarray] = [np.array([])]
26
+ LOCAL_DEFAULT: list[np.ndarray] = [np.array([])]
27
27
 
28
28
 
29
29
  def test_kraus_no_methods() -> None:
@@ -104,7 +104,7 @@ def test_kraus_fallback_to_mixture() -> None:
104
104
  m = ((0.3, cirq.unitary(cirq.X)), (0.4, cirq.unitary(cirq.Y)), (0.3, cirq.unitary(cirq.Z)))
105
105
 
106
106
  class ReturnsMixture:
107
- def _mixture_(self) -> Iterable[Tuple[float, np.ndarray]]:
107
+ def _mixture_(self) -> Iterable[tuple[float, np.ndarray]]:
108
108
  return m
109
109
 
110
110
  c = (
@@ -17,7 +17,7 @@
17
17
  from __future__ import annotations
18
18
 
19
19
  from types import NotImplementedType
20
- from typing import Any, FrozenSet, Mapping, Optional, Tuple, TYPE_CHECKING, Union
20
+ from typing import Any, Mapping, TYPE_CHECKING
21
21
 
22
22
  from typing_extensions import Protocol
23
23
 
@@ -72,9 +72,7 @@ class SupportsMeasurementKey(Protocol):
72
72
  """
73
73
 
74
74
  @doc_private
75
- def _measurement_key_objs_(
76
- self,
77
- ) -> Union[FrozenSet[cirq.MeasurementKey], NotImplementedType, None]:
75
+ def _measurement_key_objs_(self) -> frozenset[cirq.MeasurementKey] | NotImplementedType | None:
78
76
  """Return the key objects for measurements performed by the receiving object.
79
77
 
80
78
  When a measurement occurs, either on hardware, or in a simulation,
@@ -92,7 +90,7 @@ class SupportsMeasurementKey(Protocol):
92
90
  """
93
91
 
94
92
  @doc_private
95
- def _measurement_key_names_(self) -> Union[FrozenSet[str], NotImplementedType, None]:
93
+ def _measurement_key_names_(self) -> frozenset[str] | NotImplementedType | None:
96
94
  """Return the string keys for measurements performed by the receiving object.
97
95
 
98
96
  When a measurement occurs, either on hardware, or in a simulation,
@@ -178,7 +176,7 @@ def measurement_key_name(val: Any, default: Any = RaiseTypeErrorIfNotProvided):
178
176
 
179
177
  def _measurement_key_objs_from_magic_methods(
180
178
  val: Any,
181
- ) -> Union[FrozenSet[cirq.MeasurementKey], NotImplementedType, None]:
179
+ ) -> frozenset[cirq.MeasurementKey] | NotImplementedType | None:
182
180
  """Uses the measurement key related magic methods to get the `MeasurementKey`s for this
183
181
  object."""
184
182
 
@@ -196,7 +194,7 @@ def _measurement_key_objs_from_magic_methods(
196
194
 
197
195
  def _measurement_key_names_from_magic_methods(
198
196
  val: Any,
199
- ) -> Union[FrozenSet[str], NotImplementedType, None]:
197
+ ) -> frozenset[str] | NotImplementedType | None:
200
198
  """Uses the measurement key related magic methods to get the key strings for this object."""
201
199
 
202
200
  getter = getattr(val, '_measurement_key_names_', None)
@@ -212,7 +210,7 @@ def _measurement_key_names_from_magic_methods(
212
210
  return result
213
211
 
214
212
 
215
- def measurement_key_objs(val: Any) -> FrozenSet[cirq.MeasurementKey]:
213
+ def measurement_key_objs(val: Any) -> frozenset[cirq.MeasurementKey]:
216
214
  """Gets the measurement key objects of measurements within the given value.
217
215
 
218
216
  Args:
@@ -231,7 +229,7 @@ def measurement_key_objs(val: Any) -> FrozenSet[cirq.MeasurementKey]:
231
229
  return frozenset()
232
230
 
233
231
 
234
- def measurement_key_names(val: Any) -> FrozenSet[str]:
232
+ def measurement_key_names(val: Any) -> frozenset[str]:
235
233
  """Gets the measurement key strings of measurements within the given value.
236
234
 
237
235
  Args:
@@ -256,7 +254,7 @@ def measurement_key_names(val: Any) -> FrozenSet[str]:
256
254
  return frozenset()
257
255
 
258
256
 
259
- def _is_measurement_from_magic_method(val: Any) -> Optional[bool]:
257
+ def _is_measurement_from_magic_method(val: Any) -> bool | None:
260
258
  """Uses `is_measurement` magic method to determine if this object is a measurement."""
261
259
  getter = getattr(val, '_is_measurement_', None)
262
260
  return NotImplemented if getter is None else getter()
@@ -292,7 +290,7 @@ def with_measurement_key_mapping(val: Any, key_map: Mapping[str, str]):
292
290
  return NotImplemented if getter is None else getter(key_map)
293
291
 
294
292
 
295
- def with_key_path(val: Any, path: Tuple[str, ...]):
293
+ def with_key_path(val: Any, path: tuple[str, ...]):
296
294
  """Adds the path to the target's measurement keys.
297
295
 
298
296
  The path usually refers to an identifier or a list of identifiers from a subcircuit that
@@ -303,7 +301,7 @@ def with_key_path(val: Any, path: Tuple[str, ...]):
303
301
  return NotImplemented if getter is None else getter(path)
304
302
 
305
303
 
306
- def with_key_path_prefix(val: Any, prefix: Tuple[str, ...]):
304
+ def with_key_path_prefix(val: Any, prefix: tuple[str, ...]):
307
305
  """Prefixes the path to the target's measurement keys.
308
306
 
309
307
  The path usually refers to an identifier or a list of identifiers from a subcircuit that
@@ -319,7 +317,7 @@ def with_key_path_prefix(val: Any, prefix: Tuple[str, ...]):
319
317
 
320
318
 
321
319
  def with_rescoped_keys(
322
- val: Any, path: Tuple[str, ...], bindable_keys: Optional[FrozenSet[cirq.MeasurementKey]] = None
320
+ val: Any, path: tuple[str, ...], bindable_keys: frozenset[cirq.MeasurementKey] | None = None
323
321
  ):
324
322
  """Rescopes any measurement and control keys to the provided path, given the existing keys.
325
323
 
@@ -17,7 +17,7 @@
17
17
  from __future__ import annotations
18
18
 
19
19
  from types import NotImplementedType
20
- from typing import Any, Sequence, Tuple, Union
20
+ from typing import Any, Sequence
21
21
 
22
22
  import numpy as np
23
23
  from typing_extensions import Protocol
@@ -29,14 +29,14 @@ from cirq.protocols.unitary_protocol import unitary
29
29
 
30
30
  # This is a special indicator value used by the inverse method to determine
31
31
  # whether or not the caller provided a 'default' argument.
32
- RaiseTypeErrorIfNotProvided: Sequence[Tuple[float, Any]] = ((0.0, []),)
32
+ RaiseTypeErrorIfNotProvided: Sequence[tuple[float, Any]] = ((0.0, []),)
33
33
 
34
34
 
35
35
  class SupportsMixture(Protocol):
36
36
  """An object that decomposes into a probability distribution of unitaries."""
37
37
 
38
38
  @doc_private
39
- def _mixture_(self) -> Union[Sequence[Tuple[float, Any]], NotImplementedType]:
39
+ def _mixture_(self) -> Sequence[tuple[float, Any]] | NotImplementedType:
40
40
  """Decompose into a probability distribution of unitaries.
41
41
 
42
42
  This method is used by the global `cirq.mixture` method.
@@ -67,7 +67,7 @@ class SupportsMixture(Protocol):
67
67
 
68
68
  def mixture(
69
69
  val: Any, default: Any = RaiseTypeErrorIfNotProvided
70
- ) -> Sequence[Tuple[float, np.ndarray]]:
70
+ ) -> Sequence[tuple[float, np.ndarray]]:
71
71
  """Return a sequence of tuples representing a probabilistic unitary.
72
72
 
73
73
  A mixture is described by an iterable of tuples of the form
cirq/protocols/qasm.py CHANGED
@@ -16,7 +16,7 @@ from __future__ import annotations
16
16
 
17
17
  import string
18
18
  from types import NotImplementedType
19
- from typing import Any, Dict, Iterable, Optional, Tuple, TYPE_CHECKING, TypeVar, Union
19
+ from typing import Any, Iterable, TYPE_CHECKING, TypeVar
20
20
 
21
21
  from typing_extensions import Protocol
22
22
 
@@ -38,9 +38,9 @@ class QasmArgs(string.Formatter):
38
38
  self,
39
39
  precision: int = 10,
40
40
  version: str = '2.0',
41
- qubit_id_map: Optional[Dict[cirq.Qid, str]] = None,
42
- meas_key_id_map: Optional[Dict[str, str]] = None,
43
- meas_key_bitcount: Optional[Dict[str, int]] = None,
41
+ qubit_id_map: dict[cirq.Qid, str] | None = None,
42
+ meas_key_id_map: dict[str, str] | None = None,
43
+ meas_key_bitcount: dict[str, int] | None = None,
44
44
  ) -> None:
45
45
  """Inits QasmArgs.
46
46
 
@@ -99,7 +99,7 @@ class SupportsQasm(Protocol):
99
99
  """
100
100
 
101
101
  @doc_private
102
- def _qasm_(self) -> Union[None, NotImplementedType, str]:
102
+ def _qasm_(self) -> None | NotImplementedType | str:
103
103
  pass
104
104
 
105
105
 
@@ -112,7 +112,7 @@ class SupportsQasmWithArgs(Protocol):
112
112
  """
113
113
 
114
114
  @doc_private
115
- def _qasm_(self, args: QasmArgs) -> Union[None, NotImplementedType, str]:
115
+ def _qasm_(self, args: QasmArgs) -> None | NotImplementedType | str:
116
116
  pass
117
117
 
118
118
 
@@ -125,9 +125,7 @@ class SupportsQasmWithArgsAndQubits(Protocol):
125
125
  """
126
126
 
127
127
  @doc_private
128
- def _qasm_(
129
- self, qubits: Tuple[cirq.Qid], args: QasmArgs
130
- ) -> Union[None, NotImplementedType, str]:
128
+ def _qasm_(self, qubits: tuple[cirq.Qid], args: QasmArgs) -> None | NotImplementedType | str:
131
129
  pass
132
130
 
133
131
 
@@ -135,10 +133,10 @@ class SupportsQasmWithArgsAndQubits(Protocol):
135
133
  def qasm(
136
134
  val: Any,
137
135
  *,
138
- args: Optional[QasmArgs] = None,
139
- qubits: Optional[Iterable[cirq.Qid]] = None,
136
+ args: QasmArgs | None = None,
137
+ qubits: Iterable[cirq.Qid] | None = None,
140
138
  default: TDefault = RaiseTypeErrorIfNotProvided,
141
- ) -> Union[str, TDefault]:
139
+ ) -> str | TDefault:
142
140
  """Returns QASM code for the given value, if possible.
143
141
 
144
142
  Different values require different sets of arguments. The general rule of
@@ -171,7 +169,7 @@ def qasm(
171
169
  method = getattr(val, '_qasm_', None)
172
170
  result = NotImplemented
173
171
  if method is not None:
174
- kwargs: Dict[str, Any] = {}
172
+ kwargs: dict[str, Any] = {}
175
173
  if args is not None:
176
174
  kwargs['args'] = args
177
175
  # pylint: disable=not-callable
@@ -15,7 +15,7 @@
15
15
  from __future__ import annotations
16
16
 
17
17
  from types import NotImplementedType
18
- from typing import Any, Sequence, Tuple, TypeVar, Union
18
+ from typing import Any, Sequence, TypeVar
19
19
 
20
20
  from typing_extensions import Protocol
21
21
 
@@ -24,7 +24,7 @@ from cirq._doc import doc_private, document
24
24
 
25
25
  # This is a special indicator value used by the methods to determine whether or
26
26
  # not the caller provided a 'default' argument. It must be of type
27
- # Tuple[int, ...] to ensure the method has the correct type signature in that
27
+ # tuple[int, ...] to ensure the method has the correct type signature in that
28
28
  # case. It is checked for using `is`, so it won't have a false positive if the
29
29
  # user provides a different (0,) value.
30
30
  RaiseTypeErrorIfNotProvided: Any = (0,)
@@ -39,7 +39,7 @@ class SupportsExplicitQidShape(Protocol):
39
39
  number qubits/qudits/qids, each with a specific number of quantum levels."""
40
40
 
41
41
  @doc_private
42
- def _qid_shape_(self) -> Union[Tuple[int, ...], NotImplementedType]:
42
+ def _qid_shape_(self) -> tuple[int, ...] | NotImplementedType:
43
43
  """A tuple specifying the number of quantum levels of each qid this
44
44
  object operates on, e.g. (2, 2, 2) for a three-qubit gate.
45
45
 
@@ -67,7 +67,7 @@ class SupportsExplicitNumQubits(Protocol):
67
67
  number of qubits."""
68
68
 
69
69
  @document
70
- def _num_qubits_(self) -> Union[int, NotImplementedType]:
70
+ def _num_qubits_(self) -> int | NotImplementedType:
71
71
  """The number of qubits, qudits, or qids this object operates on.
72
72
 
73
73
  This method is used by the global `cirq.num_qubits` method (and by
@@ -82,7 +82,7 @@ class SupportsExplicitNumQubits(Protocol):
82
82
 
83
83
  def qid_shape(
84
84
  val: Any, default: TDefault = RaiseTypeErrorIfNotProvided
85
- ) -> Union[Tuple[int, ...], TDefault]:
85
+ ) -> tuple[int, ...] | TDefault:
86
86
  """Returns a tuple describing the number of quantum levels of each
87
87
  qubit/qudit/qid `val` operates on.
88
88
 
@@ -136,9 +136,7 @@ def qid_shape(
136
136
  raise TypeError(f"object of type '{type(val)}' has no _num_qubits_ or _qid_shape_ methods.")
137
137
 
138
138
 
139
- def num_qubits(
140
- val: Any, default: TDefault = RaiseTypeErrorIfNotProvidedInt
141
- ) -> Union[int, TDefault]:
139
+ def num_qubits(val: Any, default: TDefault = RaiseTypeErrorIfNotProvidedInt) -> int | TDefault:
142
140
  """Returns the number of qubits, qudits, or qids `val` operates on.
143
141
 
144
142
  Args:
@@ -15,7 +15,7 @@
15
15
  from __future__ import annotations
16
16
 
17
17
  import abc
18
- from typing import Any, Dict, List, Optional, Sequence, TYPE_CHECKING
18
+ from typing import Any, Sequence, TYPE_CHECKING
19
19
 
20
20
  import numpy as np
21
21
 
@@ -147,9 +147,9 @@ class CliffordTableau(StabilizerState):
147
147
  self,
148
148
  num_qubits,
149
149
  initial_state: int = 0,
150
- rs: Optional[np.ndarray] = None,
151
- xs: Optional[np.ndarray] = None,
152
- zs: Optional[np.ndarray] = None,
150
+ rs: np.ndarray | None = None,
151
+ xs: np.ndarray | None = None,
152
+ zs: np.ndarray | None = None,
153
153
  ):
154
154
  """Initializes CliffordTableau
155
155
  Args:
@@ -166,7 +166,7 @@ class CliffordTableau(StabilizerState):
166
166
  self._xs = self._reconstruct_xs(xs)
167
167
  self._zs = self._reconstruct_zs(zs)
168
168
 
169
- def _reconstruct_rs(self, rs: Optional[np.ndarray]) -> np.ndarray:
169
+ def _reconstruct_rs(self, rs: np.ndarray | None) -> np.ndarray:
170
170
  if rs is None:
171
171
  new_rs = np.zeros(2 * self.n + 1, dtype=bool)
172
172
  for i, val in enumerate(
@@ -185,7 +185,7 @@ class CliffordTableau(StabilizerState):
185
185
  )
186
186
  return new_rs
187
187
 
188
- def _reconstruct_xs(self, xs: Optional[np.ndarray]) -> np.ndarray:
188
+ def _reconstruct_xs(self, xs: np.ndarray | None) -> np.ndarray:
189
189
  if xs is None:
190
190
  new_xs = np.zeros((2 * self.n + 1, self.n), dtype=bool)
191
191
  for i in range(self.n):
@@ -207,7 +207,7 @@ class CliffordTableau(StabilizerState):
207
207
  )
208
208
  return new_xs
209
209
 
210
- def _reconstruct_zs(self, zs: Optional[np.ndarray]) -> np.ndarray:
210
+ def _reconstruct_zs(self, zs: np.ndarray | None) -> np.ndarray:
211
211
  if zs is None:
212
212
  new_zs = np.zeros((2 * self.n + 1, self.n), dtype=bool)
213
213
  for i in range(self.n):
@@ -260,7 +260,7 @@ class CliffordTableau(StabilizerState):
260
260
  """Returns the 2n * 2n matrix representation of the Clifford tableau."""
261
261
  return np.concatenate([self.xs, self.zs], axis=1)
262
262
 
263
- def _json_dict_(self) -> Dict[str, Any]:
263
+ def _json_dict_(self) -> dict[str, Any]:
264
264
  return protocols.obj_to_dict_helper(self, ['n', 'rs', 'xs', 'zs'])
265
265
 
266
266
  @classmethod
@@ -511,12 +511,12 @@ class CliffordTableau(StabilizerState):
511
511
  pauli_mask += "I"
512
512
  return DensePauliString(pauli_mask, coefficient=coefficient)
513
513
 
514
- def stabilizers(self) -> List[cirq.DensePauliString]:
514
+ def stabilizers(self) -> list[cirq.DensePauliString]:
515
515
  """Returns the stabilizer generators of the state. These
516
516
  are n operators {S_1,S_2,...,S_n} such that S_i |psi> = |psi>"""
517
517
  return [self._row_to_dense_pauli(i) for i in range(self.n, 2 * self.n)]
518
518
 
519
- def destabilizers(self) -> List[cirq.DensePauliString]:
519
+ def destabilizers(self) -> list[cirq.DensePauliString]:
520
520
  """Returns the destabilizer generators of the state. These
521
521
  are n operators {S_1,S_2,...,S_n} such that along with the stabilizer
522
522
  generators above generate the full Pauli group on n qubits."""
@@ -665,14 +665,14 @@ class CliffordTableau(StabilizerState):
665
665
 
666
666
  def measure(
667
667
  self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
668
- ) -> List[int]:
668
+ ) -> list[int]:
669
669
  return [self._measure(axis, random_state.parse_random_state(seed)) for axis in axes]
670
670
 
671
671
  @cached_method
672
672
  def __hash__(self) -> int:
673
673
  return hash(self.matrix().tobytes() + self.rs.tobytes())
674
674
 
675
- def __getstate__(self) -> Dict[str, Any]:
675
+ def __getstate__(self) -> dict[str, Any]:
676
676
  # clear cached hash value when pickling, see #6674
677
677
  state = self.__dict__
678
678
  hash_attr = _method_cache_name(self.__hash__)
cirq/qis/measures.py CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  from __future__ import annotations
18
18
 
19
- from typing import Optional, Tuple, TYPE_CHECKING
19
+ from typing import TYPE_CHECKING
20
20
 
21
21
  import numpy as np
22
22
 
@@ -44,7 +44,7 @@ def _sqrt_positive_semidefinite_matrix(mat: np.ndarray) -> np.ndarray:
44
44
  return vecs @ (np.sqrt(np.abs(eigs)) * vecs).T.conj()
45
45
 
46
46
 
47
- def _validate_int_state(state: int, qid_shape: Optional[Tuple[int, ...]]) -> None:
47
+ def _validate_int_state(state: int, qid_shape: tuple[int, ...] | None) -> None:
48
48
  if state < 0:
49
49
  raise ValueError(
50
50
  'Invalid state: A state specified as an integer must be non-negative, '
@@ -60,7 +60,7 @@ def _validate_int_state(state: int, qid_shape: Optional[Tuple[int, ...]]) -> Non
60
60
  )
61
61
 
62
62
 
63
- def _validate_product_state(state: cirq.ProductState, qid_shape: Optional[Tuple[int, ...]]) -> None:
63
+ def _validate_product_state(state: cirq.ProductState, qid_shape: tuple[int, ...] | None) -> None:
64
64
  if qid_shape is not None and qid_shape != (2,) * len(state):
65
65
  raise ValueError(
66
66
  'Invalid state for given qid shape: '
@@ -72,7 +72,7 @@ def _validate_product_state(state: cirq.ProductState, qid_shape: Optional[Tuple[
72
72
  def fidelity(
73
73
  state1: cirq.QUANTUM_STATE_LIKE,
74
74
  state2: cirq.QUANTUM_STATE_LIKE,
75
- qid_shape: Optional[Tuple[int, ...]] = None,
75
+ qid_shape: tuple[int, ...] | None = None,
76
76
  validate: bool = True,
77
77
  atol: float = 1e-7,
78
78
  ) -> float:
@@ -156,10 +156,10 @@ def fidelity(
156
156
  def _numpy_arrays_to_state_vectors_or_density_matrices(
157
157
  state1: np.ndarray,
158
158
  state2: np.ndarray,
159
- qid_shape: Optional[Tuple[int, ...]],
159
+ qid_shape: tuple[int, ...] | None,
160
160
  validate: bool,
161
161
  atol: float,
162
- ) -> Tuple[np.ndarray, np.ndarray]:
162
+ ) -> tuple[np.ndarray, np.ndarray]:
163
163
  if state1.ndim > 2 or (state1.ndim == 2 and state1.shape[0] != state1.shape[1]):
164
164
  # State tensor, convert to state vector
165
165
  state1 = state1.reshape(-1)
@@ -256,7 +256,7 @@ def _fidelity_state_vectors_or_density_matrices(state1: np.ndarray, state2: np.n
256
256
 
257
257
  def von_neumann_entropy(
258
258
  state: cirq.QUANTUM_STATE_LIKE,
259
- qid_shape: Optional[Tuple[int, ...]] = None,
259
+ qid_shape: tuple[int, ...] | None = None,
260
260
  validate: bool = True,
261
261
  atol: float = 1e-7,
262
262
  ) -> float:
@@ -15,7 +15,7 @@
15
15
  from __future__ import annotations
16
16
 
17
17
  import abc
18
- from typing import List, Sequence, Tuple, TYPE_CHECKING
18
+ from typing import Sequence, TYPE_CHECKING
19
19
 
20
20
  import numpy as np
21
21
  from typing_extensions import Self
@@ -41,7 +41,7 @@ class QuantumStateRepresentation(metaclass=abc.ABCMeta):
41
41
  @abc.abstractmethod
42
42
  def measure(
43
43
  self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
44
- ) -> List[int]:
44
+ ) -> list[int]:
45
45
  """Measures the state.
46
46
 
47
47
  Args:
@@ -74,7 +74,7 @@ class QuantumStateRepresentation(metaclass=abc.ABCMeta):
74
74
  """Joins two state spaces together."""
75
75
  raise NotImplementedError()
76
76
 
77
- def factor(self, axes: Sequence[int], *, validate=True, atol=1e-07) -> Tuple[Self, Self]:
77
+ def factor(self, axes: Sequence[int], *, validate=True, atol=1e-07) -> tuple[Self, Self]:
78
78
  """Splits two state spaces after a measurement or reset."""
79
79
  raise NotImplementedError()
80
80