cirq-core 1.7.0.dev20250825174419__py3-none-any.whl → 1.7.0.dev20251203004401__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.
Files changed (426) hide show
  1. cirq/__init__.py +1 -0
  2. cirq/_compat.py +3 -2
  3. cirq/_compat_test.py +16 -15
  4. cirq/_doc.py +4 -3
  5. cirq/_import.py +2 -1
  6. cirq/_version.py +1 -1
  7. cirq/_version_test.py +1 -1
  8. cirq/circuits/_bucket_priority_queue.py +2 -1
  9. cirq/circuits/circuit.py +19 -17
  10. cirq/circuits/circuit_operation.py +2 -1
  11. cirq/circuits/circuit_operation_test.py +19 -0
  12. cirq/circuits/circuit_test.py +31 -12
  13. cirq/circuits/frozen_circuit.py +3 -2
  14. cirq/circuits/moment.py +3 -15
  15. cirq/circuits/optimization_pass.py +2 -1
  16. cirq/circuits/qasm_output.py +39 -10
  17. cirq/circuits/qasm_output_test.py +51 -2
  18. cirq/circuits/text_diagram_drawer.py +2 -1
  19. cirq/contrib/acquaintance/bipartite.py +2 -1
  20. cirq/contrib/acquaintance/devices.py +1 -1
  21. cirq/contrib/acquaintance/executor.py +4 -5
  22. cirq/contrib/acquaintance/executor_test.py +2 -1
  23. cirq/contrib/acquaintance/gates.py +2 -1
  24. cirq/contrib/acquaintance/gates_test.py +1 -1
  25. cirq/contrib/acquaintance/inspection_utils.py +2 -1
  26. cirq/contrib/acquaintance/mutation_utils.py +2 -1
  27. cirq/contrib/acquaintance/optimizers.py +2 -1
  28. cirq/contrib/acquaintance/permutation.py +2 -1
  29. cirq/contrib/acquaintance/permutation_test.py +1 -1
  30. cirq/contrib/acquaintance/shift.py +2 -1
  31. cirq/contrib/acquaintance/shift_swap_network.py +2 -1
  32. cirq/contrib/acquaintance/strategies/complete.py +3 -2
  33. cirq/contrib/acquaintance/strategies/cubic.py +2 -1
  34. cirq/contrib/acquaintance/strategies/quartic_paired.py +2 -1
  35. cirq/contrib/acquaintance/strategies/quartic_paired_test.py +1 -1
  36. cirq/contrib/acquaintance/testing.py +2 -1
  37. cirq/contrib/acquaintance/topological_sort.py +2 -1
  38. cirq/contrib/bayesian_network/bayesian_network_gate.py +3 -2
  39. cirq/contrib/circuitdag/circuit_dag.py +4 -2
  40. cirq/contrib/custom_simulators/custom_state_simulator.py +2 -1
  41. cirq/contrib/custom_simulators/custom_state_simulator_test.py +1 -1
  42. cirq/contrib/graph_device/graph_device.py +2 -1
  43. cirq/contrib/graph_device/graph_device_test.py +2 -1
  44. cirq/contrib/graph_device/hypergraph.py +2 -1
  45. cirq/contrib/graph_device/uniform_graph_device.py +2 -1
  46. cirq/contrib/json.py +14 -2
  47. cirq/contrib/json_test_data/BayesianNetworkGate.json +10 -0
  48. cirq/contrib/json_test_data/BayesianNetworkGate.repr +3 -0
  49. cirq/contrib/json_test_data/QuantumVolumeResult.json +169 -0
  50. cirq/contrib/json_test_data/QuantumVolumeResult.repr +22 -0
  51. cirq/contrib/json_test_data/SwapPermutationGate.json +3 -0
  52. cirq/contrib/json_test_data/SwapPermutationGate.repr +1 -0
  53. cirq/contrib/json_test_data/spec.py +0 -2
  54. cirq/contrib/noise_models/noise_models.py +2 -1
  55. cirq/contrib/paulistring/clifford_optimize.py +20 -2
  56. cirq/contrib/paulistring/optimize.py +1 -1
  57. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +146 -35
  58. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +81 -178
  59. cirq/contrib/paulistring/recombine.py +5 -2
  60. cirq/contrib/paulistring/separate.py +1 -1
  61. cirq/contrib/qasm_import/_lexer.py +6 -1
  62. cirq/contrib/qasm_import/_lexer_test.py +1 -1
  63. cirq/contrib/qasm_import/_parser.py +24 -8
  64. cirq/contrib/qasm_import/_parser_test.py +44 -6
  65. cirq/contrib/qcircuit/qcircuit_pdf_test.py +6 -9
  66. cirq/contrib/quantikz/__init__.py +21 -0
  67. cirq/contrib/quantikz/circuit_to_latex_quantikz.py +680 -0
  68. cirq/contrib/quantikz/circuit_to_latex_quantikz_test.py +253 -0
  69. cirq/contrib/quantikz/circuit_to_latex_render.py +424 -0
  70. cirq/contrib/quantikz/circuit_to_latex_render_test.py +44 -0
  71. cirq/contrib/quantum_volume/quantum_volume.py +2 -1
  72. cirq/contrib/quimb/density_matrix.py +1 -1
  73. cirq/contrib/quimb/grid_circuits.py +2 -1
  74. cirq/contrib/quimb/grid_circuits_test.py +1 -1
  75. cirq/contrib/quimb/mps_simulator.py +4 -3
  76. cirq/contrib/quimb/state_vector.py +2 -1
  77. cirq/contrib/quirk/export_to_quirk.py +2 -1
  78. cirq/contrib/quirk/linearize_circuit.py +1 -1
  79. cirq/contrib/quirk/quirk_gate.py +2 -1
  80. cirq/contrib/routing/device.py +1 -1
  81. cirq/contrib/routing/greedy.py +2 -1
  82. cirq/contrib/routing/initialization.py +2 -1
  83. cirq/contrib/routing/router.py +2 -1
  84. cirq/contrib/routing/swap_network.py +2 -1
  85. cirq/contrib/routing/utils.py +2 -1
  86. cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +7 -5
  87. cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking_test.py +6 -6
  88. cirq/devices/device.py +2 -1
  89. cirq/devices/grid_device_metadata.py +2 -1
  90. cirq/devices/grid_qubit.py +7 -6
  91. cirq/devices/insertion_noise_model.py +2 -1
  92. cirq/devices/line_qubit.py +2 -1
  93. cirq/devices/named_topologies.py +2 -1
  94. cirq/devices/noise_model.py +2 -1
  95. cirq/devices/noise_model_test.py +1 -1
  96. cirq/devices/noise_properties.py +2 -1
  97. cirq/devices/superconducting_qubits_noise_properties_test.py +2 -1
  98. cirq/devices/thermal_noise_model.py +2 -1
  99. cirq/experiments/__init__.py +2 -0
  100. cirq/experiments/benchmarking/parallel_xeb.py +2 -1
  101. cirq/experiments/benchmarking/parallel_xeb_test.py +1 -1
  102. cirq/experiments/fidelity_estimation.py +2 -1
  103. cirq/experiments/fidelity_estimation_test.py +1 -1
  104. cirq/experiments/ghz_2d.py +150 -0
  105. cirq/experiments/ghz_2d_test.py +155 -0
  106. cirq/experiments/n_qubit_tomography.py +2 -1
  107. cirq/experiments/n_qubit_tomography_test.py +1 -1
  108. cirq/experiments/purity_estimation.py +1 -1
  109. cirq/experiments/qubit_characterizations.py +33 -4
  110. cirq/experiments/qubit_characterizations_test.py +16 -0
  111. cirq/experiments/random_quantum_circuit_generation.py +2 -1
  112. cirq/experiments/random_quantum_circuit_generation_test.py +2 -1
  113. cirq/experiments/readout_confusion_matrix.py +2 -1
  114. cirq/experiments/readout_confusion_matrix_test.py +1 -1
  115. cirq/experiments/single_qubit_readout_calibration.py +2 -1
  116. cirq/experiments/single_qubit_readout_calibration_test.py +1 -1
  117. cirq/experiments/t1_decay_experiment.py +2 -1
  118. cirq/experiments/two_qubit_xeb.py +2 -1
  119. cirq/experiments/two_qubit_xeb_test.py +1 -1
  120. cirq/experiments/xeb_fitting.py +2 -1
  121. cirq/experiments/xeb_fitting_test.py +1 -1
  122. cirq/experiments/xeb_sampling.py +5 -3
  123. cirq/experiments/xeb_sampling_test.py +1 -1
  124. cirq/experiments/xeb_simulation.py +2 -1
  125. cirq/experiments/xeb_simulation_test.py +2 -1
  126. cirq/experiments/z_phase_calibration.py +2 -1
  127. cirq/experiments/z_phase_calibration_test.py +18 -3
  128. cirq/interop/quirk/cells/__init__.py +1 -2
  129. cirq/interop/quirk/cells/all_cells.py +2 -1
  130. cirq/interop/quirk/cells/arithmetic_cells.py +2 -1
  131. cirq/interop/quirk/cells/cell.py +2 -1
  132. cirq/interop/quirk/cells/composite_cell.py +2 -1
  133. cirq/interop/quirk/cells/composite_cell_test.py +1 -1
  134. cirq/interop/quirk/cells/control_cells.py +2 -1
  135. cirq/interop/quirk/cells/frequency_space_cells.py +1 -1
  136. cirq/interop/quirk/cells/ignored_cells.py +1 -1
  137. cirq/interop/quirk/cells/input_cells.py +2 -1
  138. cirq/interop/quirk/cells/input_rotation_cells.py +2 -1
  139. cirq/interop/quirk/cells/measurement_cells.py +2 -1
  140. cirq/interop/quirk/cells/parse.py +2 -11
  141. cirq/interop/quirk/cells/qubit_permutation_cells.py +2 -1
  142. cirq/interop/quirk/cells/scalar_cells.py +2 -1
  143. cirq/interop/quirk/cells/single_qubit_rotation_cells.py +2 -1
  144. cirq/interop/quirk/cells/swap_cell.py +2 -1
  145. cirq/interop/quirk/cells/unsupported_cells.py +1 -1
  146. cirq/interop/quirk/url_to_circuit.py +2 -1
  147. cirq/json_resolver_cache.py +0 -2
  148. cirq/linalg/decompositions.py +6 -2
  149. cirq/linalg/decompositions_test.py +1 -0
  150. cirq/linalg/diagonalize.py +1 -1
  151. cirq/linalg/predicates.py +2 -1
  152. cirq/linalg/tolerance.py +2 -1
  153. cirq/linalg/transformations.py +3 -2
  154. cirq/ops/arithmetic_operation.py +4 -3
  155. cirq/ops/arithmetic_operation_test.py +1 -1
  156. cirq/ops/boolean_hamiltonian.py +4 -3
  157. cirq/ops/classically_controlled_operation.py +11 -11
  158. cirq/ops/classically_controlled_operation_test.py +26 -2
  159. cirq/ops/clifford_gate.py +3 -2
  160. cirq/ops/clifford_gate_test.py +1 -2
  161. cirq/ops/common_channels.py +2 -1
  162. cirq/ops/common_gates.py +3 -2
  163. cirq/ops/control_values.py +2 -1
  164. cirq/ops/controlled_gate.py +3 -2
  165. cirq/ops/controlled_gate_test.py +2 -1
  166. cirq/ops/controlled_operation.py +3 -2
  167. cirq/ops/controlled_operation_test.py +2 -1
  168. cirq/ops/dense_pauli_string.py +44 -81
  169. cirq/ops/dense_pauli_string_test.py +21 -0
  170. cirq/ops/diagonal_gate.py +3 -2
  171. cirq/ops/eigen_gate.py +9 -7
  172. cirq/ops/fourier_transform.py +3 -2
  173. cirq/ops/fourier_transform_test.py +2 -4
  174. cirq/ops/fsim_gate.py +3 -2
  175. cirq/ops/gate_operation.py +23 -12
  176. cirq/ops/gateset.py +22 -2
  177. cirq/ops/global_phase_op.py +3 -2
  178. cirq/ops/greedy_qubit_manager.py +2 -1
  179. cirq/ops/identity.py +2 -1
  180. cirq/ops/kraus_channel.py +2 -1
  181. cirq/ops/linear_combinations.py +12 -17
  182. cirq/ops/linear_combinations_test.py +23 -1
  183. cirq/ops/matrix_gates.py +2 -1
  184. cirq/ops/measure_util.py +8 -6
  185. cirq/ops/measurement_gate.py +2 -1
  186. cirq/ops/mixed_unitary_channel.py +2 -1
  187. cirq/ops/named_qubit.py +2 -2
  188. cirq/ops/op_tree.py +2 -1
  189. cirq/ops/parallel_gate.py +3 -2
  190. cirq/ops/parity_gates.py +2 -1
  191. cirq/ops/parity_gates_test.py +35 -0
  192. cirq/ops/pauli_interaction_gate.py +2 -1
  193. cirq/ops/pauli_measurement_gate.py +2 -1
  194. cirq/ops/pauli_string.py +37 -57
  195. cirq/ops/pauli_string_phasor.py +6 -5
  196. cirq/ops/pauli_string_raw_types.py +2 -1
  197. cirq/ops/pauli_string_test.py +49 -6
  198. cirq/ops/pauli_sum_exponential.py +2 -1
  199. cirq/ops/permutation_gate.py +2 -1
  200. cirq/ops/phased_iswap_gate.py +3 -2
  201. cirq/ops/phased_x_gate.py +5 -4
  202. cirq/ops/phased_x_z_gate.py +12 -5
  203. cirq/ops/projector.py +2 -1
  204. cirq/ops/qubit_manager.py +2 -1
  205. cirq/ops/qubit_order.py +2 -1
  206. cirq/ops/qubit_order_or_list.py +1 -1
  207. cirq/ops/random_gate_channel.py +3 -2
  208. cirq/ops/raw_types.py +33 -16
  209. cirq/ops/raw_types_test.py +4 -3
  210. cirq/ops/state_preparation_channel.py +2 -1
  211. cirq/ops/three_qubit_gates.py +3 -2
  212. cirq/ops/two_qubit_diagonal_gate.py +3 -2
  213. cirq/ops/uniform_superposition_gate.py +2 -1
  214. cirq/ops/wait_gate.py +10 -4
  215. cirq/protocols/act_on_protocol.py +2 -1
  216. cirq/protocols/act_on_protocol_test.py +2 -1
  217. cirq/protocols/apply_channel_protocol.py +2 -1
  218. cirq/protocols/apply_mixture_protocol.py +2 -1
  219. cirq/protocols/apply_mixture_protocol_test.py +2 -1
  220. cirq/protocols/apply_unitary_protocol.py +2 -1
  221. cirq/protocols/apply_unitary_protocol_test.py +2 -0
  222. cirq/protocols/approximate_equality_protocol.py +2 -1
  223. cirq/protocols/circuit_diagram_info_protocol.py +2 -1
  224. cirq/protocols/control_key_protocol.py +7 -0
  225. cirq/protocols/decompose_protocol.py +2 -12
  226. cirq/protocols/has_stabilizer_effect_protocol.py +1 -1
  227. cirq/protocols/has_stabilizer_effect_protocol_test.py +11 -9
  228. cirq/protocols/has_unitary_protocol_test.py +3 -3
  229. cirq/protocols/hash_from_pickle_test.py +2 -2
  230. cirq/protocols/inverse_protocol.py +2 -1
  231. cirq/protocols/json_serialization.py +5 -4
  232. cirq/protocols/json_serialization_test.py +31 -31
  233. cirq/protocols/kraus_protocol.py +4 -3
  234. cirq/protocols/kraus_protocol_test.py +7 -7
  235. cirq/protocols/measurement_key_protocol.py +32 -8
  236. cirq/protocols/mixture_protocol.py +3 -2
  237. cirq/protocols/mixture_protocol_test.py +7 -7
  238. cirq/protocols/mul_protocol_test.py +4 -4
  239. cirq/protocols/phase_protocol.py +13 -4
  240. cirq/protocols/pow_protocol.py +2 -1
  241. cirq/protocols/pow_protocol_test.py +5 -5
  242. cirq/protocols/qasm.py +2 -1
  243. cirq/protocols/qid_shape_protocol.py +2 -1
  244. cirq/protocols/resolve_parameters.py +17 -15
  245. cirq/protocols/trace_distance_bound.py +2 -1
  246. cirq/protocols/unitary_protocol.py +21 -21
  247. cirq/protocols/unitary_protocol_test.py +31 -19
  248. cirq/qis/channels.py +1 -1
  249. cirq/qis/channels_test.py +1 -1
  250. cirq/qis/clifford_tableau.py +16 -15
  251. cirq/qis/clifford_tableau_test.py +17 -17
  252. cirq/qis/entropy.py +3 -3
  253. cirq/qis/entropy_test.py +1 -1
  254. cirq/qis/quantum_state_representation.py +2 -1
  255. cirq/qis/states.py +7 -2
  256. cirq/qis/states_test.py +54 -54
  257. cirq/sim/classical_simulator.py +25 -14
  258. cirq/sim/classical_simulator_test.py +85 -30
  259. cirq/sim/clifford/clifford_simulator.py +7 -6
  260. cirq/sim/clifford/clifford_simulator_test.py +51 -50
  261. cirq/sim/clifford/clifford_tableau_simulation_state.py +2 -1
  262. cirq/sim/clifford/stabilizer_ch_form_simulation_state.py +2 -1
  263. cirq/sim/clifford/stabilizer_sampler.py +1 -1
  264. cirq/sim/clifford/stabilizer_simulation_state.py +2 -1
  265. cirq/sim/clifford/stabilizer_state_ch_form.py +16 -15
  266. cirq/sim/clifford/stabilizer_state_ch_form_test.py +0 -1
  267. cirq/sim/density_matrix_simulation_state.py +7 -6
  268. cirq/sim/density_matrix_simulator.py +3 -2
  269. cirq/sim/density_matrix_simulator_test.py +94 -84
  270. cirq/sim/density_matrix_utils.py +2 -1
  271. cirq/sim/density_matrix_utils_test.py +1 -1
  272. cirq/sim/mux.py +35 -8
  273. cirq/sim/mux_test.py +39 -26
  274. cirq/sim/simulation_product_state.py +2 -1
  275. cirq/sim/simulation_product_state_test.py +8 -7
  276. cirq/sim/simulation_state.py +6 -5
  277. cirq/sim/simulation_state_base.py +3 -2
  278. cirq/sim/simulation_state_test.py +7 -6
  279. cirq/sim/simulation_utils.py +2 -1
  280. cirq/sim/simulator.py +4 -3
  281. cirq/sim/simulator_base.py +2 -1
  282. cirq/sim/simulator_base_test.py +51 -36
  283. cirq/sim/simulator_test.py +41 -36
  284. cirq/sim/sparse_simulator.py +3 -2
  285. cirq/sim/sparse_simulator_test.py +92 -82
  286. cirq/sim/state_vector.py +5 -6
  287. cirq/sim/state_vector_simulation_state.py +10 -9
  288. cirq/sim/state_vector_simulator.py +2 -1
  289. cirq/sim/state_vector_simulator_test.py +9 -9
  290. cirq/sim/state_vector_test.py +40 -39
  291. cirq/study/__init__.py +1 -0
  292. cirq/study/flatten_expressions.py +2 -1
  293. cirq/study/resolver.py +31 -18
  294. cirq/study/resolver_test.py +1 -1
  295. cirq/study/result.py +2 -1
  296. cirq/study/result_test.py +20 -20
  297. cirq/study/sweepable.py +2 -1
  298. cirq/study/sweepable_test.py +20 -20
  299. cirq/study/sweeps.py +26 -1
  300. cirq/study/sweeps_test.py +67 -43
  301. cirq/testing/_compat_test_data/__init__.py +3 -3
  302. cirq/testing/circuit_compare.py +2 -1
  303. cirq/testing/circuit_compare_test.py +16 -14
  304. cirq/testing/consistent_act_on_test.py +1 -1
  305. cirq/testing/consistent_channels.py +2 -2
  306. cirq/testing/consistent_controlled_gate_op.py +2 -2
  307. cirq/testing/consistent_controlled_gate_op_test.py +2 -1
  308. cirq/testing/consistent_decomposition.py +4 -2
  309. cirq/testing/consistent_phase_by.py +1 -1
  310. cirq/testing/consistent_protocols.py +2 -1
  311. cirq/testing/consistent_protocols_test.py +3 -3
  312. cirq/testing/consistent_qasm.py +4 -3
  313. cirq/testing/consistent_qasm_test.py +3 -3
  314. cirq/testing/consistent_resolve_parameters.py +1 -1
  315. cirq/testing/consistent_unitary.py +1 -1
  316. cirq/testing/consistent_unitary_test.py +1 -1
  317. cirq/testing/deprecation.py +1 -1
  318. cirq/testing/devices.py +3 -2
  319. cirq/testing/equals_tester.py +4 -3
  320. cirq/testing/equivalent_basis_map.py +4 -2
  321. cirq/testing/json.py +3 -2
  322. cirq/testing/lin_alg_utils.py +1 -1
  323. cirq/testing/logs.py +1 -1
  324. cirq/testing/op_tree.py +1 -1
  325. cirq/testing/order_tester.py +2 -2
  326. cirq/testing/pytest_utils.py +2 -1
  327. cirq/testing/random_circuit.py +2 -1
  328. cirq/testing/random_circuit_test.py +2 -1
  329. cirq/testing/repr_pretty_tester.py +3 -3
  330. cirq/transformers/__init__.py +1 -0
  331. cirq/transformers/_connected_component.py +231 -0
  332. cirq/transformers/_connected_component_test.py +200 -0
  333. cirq/transformers/align_test.py +13 -13
  334. cirq/transformers/analytical_decompositions/clifford_decomposition.py +8 -7
  335. cirq/transformers/analytical_decompositions/clifford_decomposition_test.py +5 -5
  336. cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +11 -10
  337. cirq/transformers/analytical_decompositions/controlled_gate_decomposition_test.py +6 -6
  338. cirq/transformers/analytical_decompositions/cphase_to_fsim.py +3 -2
  339. cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +11 -10
  340. cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +8 -7
  341. cirq/transformers/analytical_decompositions/quantum_shannon_decomposition_test.py +17 -20
  342. cirq/transformers/analytical_decompositions/single_qubit_decompositions_test.py +33 -27
  343. cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry_test.py +1 -1
  344. cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +1 -1
  345. cirq/transformers/analytical_decompositions/two_qubit_state_preparation_test.py +12 -11
  346. cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +5 -2
  347. cirq/transformers/analytical_decompositions/two_qubit_to_cz_test.py +3 -3
  348. cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +2 -1
  349. cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +2 -1
  350. cirq/transformers/analytical_decompositions/two_qubit_to_ms_test.py +2 -2
  351. cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +2 -1
  352. cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap_test.py +32 -30
  353. cirq/transformers/drop_negligible_operations_test.py +7 -7
  354. cirq/transformers/dynamical_decoupling.py +185 -112
  355. cirq/transformers/dynamical_decoupling_test.py +195 -201
  356. cirq/transformers/eject_phased_paulis.py +2 -1
  357. cirq/transformers/eject_phased_paulis_test.py +3 -2
  358. cirq/transformers/eject_z.py +5 -3
  359. cirq/transformers/eject_z_test.py +23 -25
  360. cirq/transformers/expand_composite.py +3 -2
  361. cirq/transformers/expand_composite_test.py +14 -14
  362. cirq/transformers/gauge_compiling/__init__.py +13 -0
  363. cirq/transformers/gauge_compiling/gauge_compiling.py +3 -2
  364. cirq/transformers/gauge_compiling/gauge_compiling_test.py +14 -12
  365. cirq/transformers/gauge_compiling/gauge_compiling_test_utils.py +3 -3
  366. cirq/transformers/gauge_compiling/idle_moments_gauge.py +225 -0
  367. cirq/transformers/gauge_compiling/idle_moments_gauge_test.py +193 -0
  368. cirq/transformers/gauge_compiling/multi_moment_cphase_gauge.py +242 -0
  369. cirq/transformers/gauge_compiling/multi_moment_cphase_gauge_test.py +243 -0
  370. cirq/transformers/gauge_compiling/multi_moment_gauge_compiling.py +151 -0
  371. cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +2 -1
  372. cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils.py +1 -1
  373. cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils_test.py +6 -6
  374. cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +3 -2
  375. cirq/transformers/measurement_transformers.py +2 -1
  376. cirq/transformers/measurement_transformers_test.py +45 -39
  377. cirq/transformers/merge_k_qubit_gates.py +2 -1
  378. cirq/transformers/merge_k_qubit_gates_test.py +1 -1
  379. cirq/transformers/merge_single_qubit_gates.py +9 -5
  380. cirq/transformers/merge_single_qubit_gates_test.py +22 -22
  381. cirq/transformers/noise_adding_test.py +2 -2
  382. cirq/transformers/optimize_for_target_gateset.py +2 -1
  383. cirq/transformers/optimize_for_target_gateset_test.py +11 -9
  384. cirq/transformers/qubit_management_transformers_test.py +6 -2
  385. cirq/transformers/routing/mapping_manager.py +2 -1
  386. cirq/transformers/routing/route_circuit_cqc.py +2 -1
  387. cirq/transformers/stratify.py +2 -1
  388. cirq/transformers/symbolize.py +2 -1
  389. cirq/transformers/tag_transformers.py +2 -1
  390. cirq/transformers/target_gatesets/compilation_target_gateset.py +2 -1
  391. cirq/transformers/target_gatesets/cz_gateset.py +2 -1
  392. cirq/transformers/target_gatesets/cz_gateset_test.py +1 -1
  393. cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +2 -1
  394. cirq/transformers/transformer_api.py +2 -1
  395. cirq/transformers/transformer_primitives.py +271 -145
  396. cirq/transformers/transformer_primitives_test.py +185 -1
  397. cirq/value/abc_alt.py +2 -1
  398. cirq/value/classical_data.py +2 -1
  399. cirq/value/condition.py +2 -1
  400. cirq/value/digits.py +9 -2
  401. cirq/value/duration.py +6 -5
  402. cirq/value/linear_dict.py +4 -9
  403. cirq/value/measurement_key.py +2 -1
  404. cirq/value/periodic_value.py +3 -2
  405. cirq/value/product_state.py +2 -1
  406. cirq/value/value_equality_attr.py +2 -1
  407. cirq/vis/density_matrix.py +1 -1
  408. cirq/vis/heatmap.py +2 -1
  409. cirq/vis/histogram.py +2 -1
  410. cirq/vis/state_histogram.py +2 -1
  411. cirq/work/collector.py +2 -1
  412. cirq/work/observable_grouping.py +2 -1
  413. cirq/work/observable_measurement.py +2 -1
  414. cirq/work/observable_measurement_data.py +2 -1
  415. cirq/work/observable_measurement_test.py +1 -1
  416. cirq/work/observable_readout_calibration.py +2 -1
  417. cirq/work/observable_readout_calibration_test.py +1 -1
  418. cirq/work/observable_settings.py +2 -1
  419. cirq/work/sampler.py +2 -1
  420. cirq/work/sampler_test.py +1 -1
  421. {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/METADATA +5 -6
  422. {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/RECORD +425 -406
  423. cirq/contrib/json_test.py +0 -33
  424. {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/WHEEL +0 -0
  425. {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/licenses/LICENSE +0 -0
  426. {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/top_level.txt +0 -0
@@ -15,7 +15,8 @@
15
15
  from __future__ import annotations
16
16
 
17
17
  import numbers
18
- from typing import AbstractSet, Any, cast, Protocol, Self, TYPE_CHECKING, TypeVar
18
+ from collections.abc import Set
19
+ from typing import Any, cast, Protocol, Self, TYPE_CHECKING, TypeVar
19
20
 
20
21
  import sympy
21
22
 
@@ -40,7 +41,7 @@ class SupportsParameterization(Protocol):
40
41
  and False otherwise."""
41
42
 
42
43
  @doc_private
43
- def _parameter_names_(self) -> AbstractSet[str]:
44
+ def _parameter_names_(self) -> Set[str]:
44
45
  """Returns a collection of string names of parameters that require
45
46
  resolution. If _is_parameterized_ is False, the collection is empty.
46
47
  The converse is not necessarily true, because some objects may report
@@ -92,7 +93,7 @@ def is_parameterized(val: Any) -> bool:
92
93
  return bool(parameter_names(val))
93
94
 
94
95
 
95
- def parameter_names(val: Any) -> AbstractSet[str]:
96
+ def parameter_names(val: Any) -> Set[str]:
96
97
  """Returns parameter names for this object.
97
98
 
98
99
  Args:
@@ -119,7 +120,7 @@ def parameter_names(val: Any) -> AbstractSet[str]:
119
120
  return set()
120
121
 
121
122
 
122
- def parameter_symbols(val: Any) -> AbstractSet[sympy.Symbol]:
123
+ def parameter_symbols(val: Any) -> Set[sympy.Symbol]:
123
124
  """Returns parameter symbols for this object.
124
125
 
125
126
  Args:
@@ -169,14 +170,8 @@ def resolve_parameters(
169
170
  return val
170
171
 
171
172
  # Ensure it is a dictionary wrapped in a ParamResolver.
172
- param_resolver = study.ParamResolver(param_resolver)
173
-
174
- # Handle special cases for sympy expressions and sequences.
175
- # These may not in fact preserve types, but we pretend they do by casting.
176
- if isinstance(val, sympy.Expr):
177
- return cast(T, param_resolver.value_of(val, recursive))
178
- if isinstance(val, (list, tuple)):
179
- return cast(T, type(val)(resolve_parameters(e, param_resolver, recursive) for e in val))
173
+ if not isinstance(param_resolver, study.ParamResolver):
174
+ param_resolver = study.ParamResolver(param_resolver)
180
175
 
181
176
  is_parameterized = (
182
177
  val._is_parameterized_() if hasattr(val, '_is_parameterized_') else NotImplemented
@@ -192,10 +187,17 @@ def resolve_parameters(
192
187
 
193
188
  if result is not NotImplemented:
194
189
  return result
195
- else:
196
- return val
190
+
191
+ # Handle special cases for sympy expressions and sequences.
192
+ # These may not in fact preserve types, but we pretend they do by casting.
193
+ if isinstance(val, sympy.Expr):
194
+ return cast(T, param_resolver.value_of(val, recursive))
195
+ if isinstance(val, (list, tuple)):
196
+ return cast(T, type(val)(resolve_parameters(e, param_resolver, recursive) for e in val))
197
+
198
+ return val
197
199
 
198
200
 
199
- def resolve_parameters_once(val: Any, param_resolver: cirq.ParamResolverOrSimilarType):
201
+ def resolve_parameters_once(val: T, param_resolver: cirq.ParamResolverOrSimilarType) -> T:
200
202
  """Performs a single parameter resolution step using the param resolver."""
201
203
  return resolve_parameters(val, param_resolver, False)
@@ -14,7 +14,8 @@
14
14
 
15
15
  from __future__ import annotations
16
16
 
17
- from typing import Any, Protocol, Sequence, TypeVar
17
+ from collections.abc import Sequence
18
+ from typing import Any, Protocol, TypeVar
18
19
 
19
20
  import numpy as np
20
21
 
@@ -24,7 +24,7 @@ from cirq.protocols import qid_shape_protocol
24
24
  from cirq.protocols.apply_unitary_protocol import apply_unitaries, ApplyUnitaryArgs
25
25
  from cirq.protocols.decompose_protocol import _try_decompose_into_operations_and_qubits
26
26
 
27
- # This is a special indicator value used by the unitary method to determine
27
+ # This is a special indicator value used by the function unitary() to determine
28
28
  # whether or not the caller provided a 'default' argument. It must be of type
29
29
  # np.ndarray to ensure the method has the correct type signature in that case.
30
30
  # It is checked for using `is`, so it won't have a false positive if the user
@@ -38,23 +38,23 @@ class SupportsUnitary(Protocol):
38
38
  """An object that may be describable by a unitary matrix."""
39
39
 
40
40
  @doc_private
41
- def _unitary_(self) -> np.ndarray | NotImplementedType:
41
+ def _unitary_(self) -> np.ndarray | NotImplementedType | None:
42
42
  """A unitary matrix describing this value, e.g. the matrix of a gate.
43
43
 
44
44
  This method is used by the global `cirq.unitary` method. If this method
45
45
  is not present, or returns NotImplemented, it is assumed that the
46
46
  receiving object doesn't have a unitary matrix (resulting in a TypeError
47
- or default result when calling `cirq.unitary` on it). (The ability to
47
+ or default result when calling `cirq.unitary` on it). The ability to
48
48
  return NotImplemented is useful when a class cannot know if it has a
49
- matrix until runtime, e.g. cirq.X**c normally has a matrix but
50
- cirq.X**sympy.Symbol('a') doesn't.)
49
+ matrix until runtime; e.g., `cirq.X**c` normally has a matrix but
50
+ `cirq.X**sympy.Symbol('a')` doesn't.
51
51
 
52
52
  The order of cells in the matrix is always implicit with respect to the
53
- object being called. For example, for gates the matrix must be ordered
53
+ object being called. For example, for gates, the matrix must be ordered
54
54
  with respect to the list of qubits that the gate is applied to. For
55
55
  operations, the matrix is ordered to match the list returned by its
56
56
  `qubits` attribute. The qubit-to-amplitude order mapping matches the
57
- ordering of numpy.kron(A, B), where A is a qubit earlier in the list
57
+ ordering of `numpy.kron(A, B)`, where A is a qubit earlier in the list
58
58
  than the qubit B.
59
59
 
60
60
  Returns:
@@ -68,7 +68,7 @@ class SupportsUnitary(Protocol):
68
68
 
69
69
  This method is used by the global `cirq.has_unitary` method. If this
70
70
  method is not present, or returns NotImplemented, it will fallback
71
- to using _unitary_ with a default value, or False if neither exist.
71
+ to using `_unitary_()` with a default value, or False if neither exist.
72
72
 
73
73
  Returns:
74
74
  True if the value has a unitary matrix representation, False
@@ -81,17 +81,17 @@ def unitary(
81
81
  ) -> np.ndarray | TDefault:
82
82
  """Returns a unitary matrix describing the given value.
83
83
 
84
- The matrix is determined by any one of the following techniques:
84
+ The matrix is determined by the first of these strategies that succeeds:
85
85
 
86
- - If the value is a numpy array, it is returned directly.
87
- - The value has a `_unitary_` method that returns something besides None or
88
- NotImplemented. The matrix is whatever the method returned.
86
+ - If the value is a NumPy array, it is returned directly.
87
+ - The value has a `_unitary_` method that returns something besides `None` or
88
+ `NotImplemented`. The matrix is whatever the method returned.
89
+ - The value has an `_apply_unitary_` method, and it returns something
90
+ besides `None` or `NotImplemented`. The matrix is created by applying
91
+ `_apply_unitary_` to an identity matrix.
89
92
  - The value has a `_decompose_` method that returns a list of operations,
90
93
  and each operation in the list has a unitary effect. The matrix is
91
94
  created by aggregating the sub-operations' unitary effects.
92
- - The value has an `_apply_unitary_` method, and it returns something
93
- besides None or NotImplemented. The matrix is created by applying
94
- `_apply_unitary_` to an identity matrix.
95
95
 
96
96
  If none of these techniques succeeds, it is assumed that `val` doesn't have
97
97
  a unitary effect. The order in which techniques are attempted is
@@ -100,8 +100,8 @@ def unitary(
100
100
  Args:
101
101
  val: The value to describe with a unitary matrix.
102
102
  default: Determines the fallback behavior when `val` doesn't have
103
- a unitary effect. If `default` is not set, a TypeError is raised. If
104
- `default` is set to a value, that value is returned.
103
+ a unitary effect. If `default` is not set, a `TypeError` is raised.
104
+ If `default` is set to a value, that value is returned.
105
105
 
106
106
  Returns:
107
107
  If `val` has a unitary effect, the corresponding unitary matrix.
@@ -135,13 +135,13 @@ def unitary(
135
135
  f"type: {type(val)}\n"
136
136
  f"value: {val!r}\n"
137
137
  "\n"
138
- "The value failed to satisfy any of the following criteria:\n"
138
+ "The given value failed to satisfy any of the following criteria:\n"
139
139
  "- A `_unitary_(self)` method that returned a value "
140
- "besides None or NotImplemented.\n"
140
+ "besides `None` or `NotImplemented`.\n"
141
141
  "- A `_decompose_(self)` method that returned a "
142
142
  "list of unitary operations.\n"
143
- "- An `_apply_unitary_(self, args) method that returned a value "
144
- "besides None or NotImplemented."
143
+ "- An `_apply_unitary_(self, args)` method that returned a value "
144
+ "besides `None` or `NotImplemented`."
145
145
  )
146
146
 
147
147
 
@@ -56,7 +56,7 @@ class ReturnsNotImplemented(cirq.Gate):
56
56
  def _unitary_(self):
57
57
  return NotImplemented
58
58
 
59
- def num_qubits(self):
59
+ def num_qubits(self) -> int:
60
60
  return 1
61
61
 
62
62
 
@@ -64,7 +64,7 @@ class ReturnsMatrix(cirq.Gate):
64
64
  def _unitary_(self) -> np.ndarray:
65
65
  return m1
66
66
 
67
- def num_qubits(self):
67
+ def num_qubits(self) -> int:
68
68
  return 1 # pragma: no cover
69
69
 
70
70
 
@@ -80,7 +80,7 @@ class FullyImplemented(cirq.Gate):
80
80
  return None
81
81
  return m1
82
82
 
83
- def num_qubits(self):
83
+ def num_qubits(self) -> int:
84
84
  return 1
85
85
 
86
86
 
@@ -88,7 +88,7 @@ class DecomposableGate(cirq.Gate):
88
88
  def __init__(self, unitary_value):
89
89
  self.unitary_value = unitary_value
90
90
 
91
- def num_qubits(self):
91
+ def num_qubits(self) -> int:
92
92
  return 1
93
93
 
94
94
  def _decompose_(self, qubits):
@@ -154,7 +154,7 @@ class OtherComposite:
154
154
  yield cirq.X(cirq.LineQubit(3))
155
155
 
156
156
 
157
- def test_unitary():
157
+ def test_unitary() -> None:
158
158
  with pytest.raises(TypeError, match='unitary effect'):
159
159
  _ = cirq.unitary(NoMethod())
160
160
  with pytest.raises(TypeError, match='unitary effect'):
@@ -186,7 +186,7 @@ def test_unitary():
186
186
  assert cirq.unitary(FullyImplemented(False), default=None) is None
187
187
 
188
188
 
189
- def test_has_unitary():
189
+ def test_has_unitary() -> None:
190
190
  assert not cirq.has_unitary(NoMethod())
191
191
  assert not cirq.has_unitary(ReturnsNotImplemented())
192
192
  assert cirq.has_unitary(ReturnsMatrix())
@@ -215,7 +215,7 @@ def _test_gate_that_allocates_qubits(gate):
215
215
  @pytest.mark.parametrize('ancilla_bitsize', [1, 4])
216
216
  def test_decompose_gate_that_allocates_clean_qubits(
217
217
  theta: float, phase_state: int, target_bitsize: int, ancilla_bitsize: int
218
- ):
218
+ ) -> None:
219
219
  gate = testing.PhaseUsingCleanAncilla(theta, phase_state, target_bitsize, ancilla_bitsize)
220
220
  _test_gate_that_allocates_qubits(gate)
221
221
 
@@ -225,26 +225,38 @@ def test_decompose_gate_that_allocates_clean_qubits(
225
225
  @pytest.mark.parametrize('ancilla_bitsize', [1, 4])
226
226
  def test_decompose_gate_that_allocates_dirty_qubits(
227
227
  phase_state: int, target_bitsize: int, ancilla_bitsize: int
228
- ):
228
+ ) -> None:
229
229
  gate = testing.PhaseUsingDirtyAncilla(phase_state, target_bitsize, ancilla_bitsize)
230
230
  _test_gate_that_allocates_qubits(gate)
231
231
 
232
232
 
233
- def test_decompose_and_get_unitary():
233
+ def test_decompose_and_get_unitary() -> None:
234
234
  from cirq.protocols.unitary_protocol import _strat_unitary_from_decompose
235
235
 
236
- np.testing.assert_allclose(_strat_unitary_from_decompose(DecomposableOperation((a,), True)), m1)
237
236
  np.testing.assert_allclose(
238
- _strat_unitary_from_decompose(DecomposableOperation((a, b), True)), m2
237
+ _strat_unitary_from_decompose(DecomposableOperation((a,), True)), m1 # type: ignore[arg-type]
238
+ )
239
+ np.testing.assert_allclose(
240
+ _strat_unitary_from_decompose(DecomposableOperation((a, b), True)), m2 # type: ignore[arg-type]
241
+ )
242
+ np.testing.assert_allclose(
243
+ _strat_unitary_from_decompose(DecomposableOrder((a, b, c))), m3 # type: ignore[arg-type]
244
+ )
245
+ np.testing.assert_allclose(
246
+ _strat_unitary_from_decompose(ExampleOperation((a,))), np.eye(2) # type: ignore[arg-type]
247
+ )
248
+ np.testing.assert_allclose(
249
+ _strat_unitary_from_decompose(ExampleOperation((a, b))), np.eye(4) # type: ignore[arg-type]
250
+ )
251
+ np.testing.assert_allclose(
252
+ _strat_unitary_from_decompose(ExampleComposite()), np.eye(1) # type: ignore[arg-type]
253
+ )
254
+ np.testing.assert_allclose(
255
+ _strat_unitary_from_decompose(OtherComposite()), m2 # type: ignore[arg-type]
239
256
  )
240
- np.testing.assert_allclose(_strat_unitary_from_decompose(DecomposableOrder((a, b, c))), m3)
241
- np.testing.assert_allclose(_strat_unitary_from_decompose(ExampleOperation((a,))), np.eye(2))
242
- np.testing.assert_allclose(_strat_unitary_from_decompose(ExampleOperation((a, b))), np.eye(4))
243
- np.testing.assert_allclose(_strat_unitary_from_decompose(ExampleComposite()), np.eye(1))
244
- np.testing.assert_allclose(_strat_unitary_from_decompose(OtherComposite()), m2)
245
257
 
246
258
 
247
- def test_decomposed_has_unitary():
259
+ def test_decomposed_has_unitary() -> None:
248
260
  # Gates
249
261
  assert cirq.has_unitary(DecomposableGate(True))
250
262
  assert not cirq.has_unitary(DecomposableGate(False))
@@ -263,7 +275,7 @@ def test_decomposed_has_unitary():
263
275
  assert cirq.has_unitary(OtherComposite())
264
276
 
265
277
 
266
- def test_decomposed_unitary():
278
+ def test_decomposed_unitary() -> None:
267
279
  # Gates
268
280
  np.testing.assert_allclose(cirq.unitary(DecomposableGate(True)), m1)
269
281
 
@@ -283,7 +295,7 @@ def test_decomposed_unitary():
283
295
  np.testing.assert_allclose(cirq.unitary(OtherComposite()), m2)
284
296
 
285
297
 
286
- def test_unitary_from_apply_unitary():
298
+ def test_unitary_from_apply_unitary() -> None:
287
299
  class ApplyGate(cirq.Gate):
288
300
  def num_qubits(self):
289
301
  return 1
cirq/qis/channels.py CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  from __future__ import annotations
18
18
 
19
- from typing import Sequence
19
+ from collections.abc import Sequence
20
20
 
21
21
  import numpy as np
22
22
 
cirq/qis/channels_test.py CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  from __future__ import annotations
18
18
 
19
- from typing import Iterable, Sequence
19
+ from collections.abc import Iterable, Sequence
20
20
 
21
21
  import numpy as np
22
22
  import pytest
@@ -15,7 +15,8 @@
15
15
  from __future__ import annotations
16
16
 
17
17
  import abc
18
- from typing import Any, Sequence, TYPE_CHECKING
18
+ from collections.abc import Sequence
19
+ from typing import Any, TYPE_CHECKING
19
20
 
20
21
  import numpy as np
21
22
 
@@ -38,7 +39,7 @@ class StabilizerState(
38
39
  """
39
40
 
40
41
  @abc.abstractmethod
41
- def apply_x(self, axis: int, exponent: float = 1, global_shift: float = 0):
42
+ def apply_x(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
42
43
  """Apply an X operation to the state.
43
44
 
44
45
  Args:
@@ -51,7 +52,7 @@ class StabilizerState(
51
52
  """
52
53
 
53
54
  @abc.abstractmethod
54
- def apply_y(self, axis: int, exponent: float = 1, global_shift: float = 0):
55
+ def apply_y(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
55
56
  """Apply an Y operation to the state.
56
57
 
57
58
  Args:
@@ -64,7 +65,7 @@ class StabilizerState(
64
65
  """
65
66
 
66
67
  @abc.abstractmethod
67
- def apply_z(self, axis: int, exponent: float = 1, global_shift: float = 0):
68
+ def apply_z(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
68
69
  """Apply a Z operation to the state.
69
70
 
70
71
  Args:
@@ -77,7 +78,7 @@ class StabilizerState(
77
78
  """
78
79
 
79
80
  @abc.abstractmethod
80
- def apply_h(self, axis: int, exponent: float = 1, global_shift: float = 0):
81
+ def apply_h(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
81
82
  """Apply an H operation to the state.
82
83
 
83
84
  Args:
@@ -92,7 +93,7 @@ class StabilizerState(
92
93
  @abc.abstractmethod
93
94
  def apply_cz(
94
95
  self, control_axis: int, target_axis: int, exponent: float = 1, global_shift: float = 0
95
- ):
96
+ ) -> None:
96
97
  """Apply a CZ operation to the state.
97
98
 
98
99
  Args:
@@ -108,7 +109,7 @@ class StabilizerState(
108
109
  @abc.abstractmethod
109
110
  def apply_cx(
110
111
  self, control_axis: int, target_axis: int, exponent: float = 1, global_shift: float = 0
111
- ):
112
+ ) -> None:
112
113
  """Apply a CX operation to the state.
113
114
 
114
115
  Args:
@@ -122,7 +123,7 @@ class StabilizerState(
122
123
  """
123
124
 
124
125
  @abc.abstractmethod
125
- def apply_global_phase(self, coefficient: linear_dict.Scalar):
126
+ def apply_global_phase(self, coefficient: linear_dict.Scalar) -> None:
126
127
  """Apply a global phase to the state.
127
128
 
128
129
  Args:
@@ -561,7 +562,7 @@ class CliffordTableau(StabilizerState):
561
562
 
562
563
  return int(self.rs[p])
563
564
 
564
- def apply_x(self, axis: int, exponent: float = 1, global_shift: float = 0):
565
+ def apply_x(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
565
566
  if exponent % 2 == 0:
566
567
  return
567
568
  if exponent % 0.5 != 0.0:
@@ -576,7 +577,7 @@ class CliffordTableau(StabilizerState):
576
577
  self.rs[:] ^= self.xs[:, axis] & self.zs[:, axis]
577
578
  self.xs[:, axis] ^= self.zs[:, axis]
578
579
 
579
- def apply_y(self, axis: int, exponent: float = 1, global_shift: float = 0):
580
+ def apply_y(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
580
581
  if exponent % 2 == 0:
581
582
  return
582
583
  if exponent % 0.5 != 0.0:
@@ -597,7 +598,7 @@ class CliffordTableau(StabilizerState):
597
598
  self.xs[:, axis].copy(),
598
599
  )
599
600
 
600
- def apply_z(self, axis: int, exponent: float = 1, global_shift: float = 0):
601
+ def apply_z(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
601
602
  if exponent % 2 == 0:
602
603
  return
603
604
  if exponent % 0.5 != 0.0:
@@ -612,7 +613,7 @@ class CliffordTableau(StabilizerState):
612
613
  self.rs[:] ^= self.xs[:, axis] & (~self.zs[:, axis])
613
614
  self.zs[:, axis] ^= self.xs[:, axis]
614
615
 
615
- def apply_h(self, axis: int, exponent: float = 1, global_shift: float = 0):
616
+ def apply_h(self, axis: int, exponent: float = 1, global_shift: float = 0) -> None:
616
617
  if exponent % 2 == 0:
617
618
  return
618
619
  if exponent % 1 != 0:
@@ -622,7 +623,7 @@ class CliffordTableau(StabilizerState):
622
623
 
623
624
  def apply_cz(
624
625
  self, control_axis: int, target_axis: int, exponent: float = 1, global_shift: float = 0
625
- ):
626
+ ) -> None:
626
627
  if exponent % 2 == 0:
627
628
  return
628
629
  if exponent % 1 != 0:
@@ -647,7 +648,7 @@ class CliffordTableau(StabilizerState):
647
648
 
648
649
  def apply_cx(
649
650
  self, control_axis: int, target_axis: int, exponent: float = 1, global_shift: float = 0
650
- ):
651
+ ) -> None:
651
652
  if exponent % 2 == 0:
652
653
  return
653
654
  if exponent % 1 != 0:
@@ -660,7 +661,7 @@ class CliffordTableau(StabilizerState):
660
661
  self.xs[:, target_axis] ^= self.xs[:, control_axis]
661
662
  self.zs[:, control_axis] ^= self.zs[:, target_axis]
662
663
 
663
- def apply_global_phase(self, coefficient: linear_dict.Scalar):
664
+ def apply_global_phase(self, coefficient: linear_dict.Scalar) -> None:
664
665
  pass
665
666
 
666
667
  def measure(
@@ -47,7 +47,7 @@ def _CNOT(table, q1, q2):
47
47
 
48
48
 
49
49
  @pytest.mark.parametrize('num_qubits', range(1, 4))
50
- def test_tableau_initial_state_string(num_qubits):
50
+ def test_tableau_initial_state_string(num_qubits) -> None:
51
51
  for i in range(2**num_qubits):
52
52
  t = cirq.CliffordTableau(initial_state=i, num_qubits=num_qubits)
53
53
  splitted_represent_string = str(t).split('\n')
@@ -58,7 +58,7 @@ def test_tableau_initial_state_string(num_qubits):
58
58
  assert splitted_represent_string[n] == expected_string
59
59
 
60
60
 
61
- def test_tableau_invalid_initial_state():
61
+ def test_tableau_invalid_initial_state() -> None:
62
62
  with pytest.raises(ValueError, match="2*num_qubits columns and of type bool."):
63
63
  cirq.CliffordTableau(1, rs=np.zeros(1, dtype=bool))
64
64
 
@@ -73,7 +73,7 @@ def test_tableau_invalid_initial_state():
73
73
  cirq.CliffordTableau(1, zs=np.zeros(1, dtype=bool))
74
74
 
75
75
 
76
- def test_stabilizers():
76
+ def test_stabilizers() -> None:
77
77
  # Note: the stabilizers are not unique for one state. We just use the one
78
78
  # produced by the tableau algorithm.
79
79
  # 1. Final state is |1>: Stabalized by -Z.
@@ -101,7 +101,7 @@ def test_stabilizers():
101
101
  assert stabilizers[1] == cirq.DensePauliString('IX', coefficient=1)
102
102
 
103
103
 
104
- def test_destabilizers():
104
+ def test_destabilizers() -> None:
105
105
  # Note: Like stablizers, the destabilizers are not unique for one state, too.
106
106
  # We just use the one produced by the tableau algorithm.
107
107
  # Under the clifford tableau algorithm, there are several properties that the
@@ -135,7 +135,7 @@ def test_destabilizers():
135
135
  assert destabilizers[1] == cirq.DensePauliString('IZ', coefficient=1)
136
136
 
137
137
 
138
- def test_measurement():
138
+ def test_measurement() -> None:
139
139
  repetitions = 500
140
140
  prng = np.random.RandomState(seed=123456)
141
141
 
@@ -195,7 +195,7 @@ def test_measurement():
195
195
  assert sum(np.asarray(res) == 3) >= (repetitions / 4 * 0.9)
196
196
 
197
197
 
198
- def test_validate_tableau():
198
+ def test_validate_tableau() -> None:
199
199
  num_qubits = 4
200
200
  for i in range(2**num_qubits):
201
201
  t = cirq.CliffordTableau(initial_state=i, num_qubits=num_qubits)
@@ -218,7 +218,7 @@ def test_validate_tableau():
218
218
  assert not t._validate()
219
219
 
220
220
 
221
- def test_rowsum():
221
+ def test_rowsum() -> None:
222
222
  # Note: rowsum should not apply on two rows that anti-commute each other.
223
223
  t = cirq.CliffordTableau(num_qubits=2)
224
224
  # XI * IX ==> XX
@@ -246,7 +246,7 @@ def test_rowsum():
246
246
  assert t.stabilizers()[1] == cirq.DensePauliString('YX', coefficient=1)
247
247
 
248
248
 
249
- def test_json_dict():
249
+ def test_json_dict() -> None:
250
250
  t = cirq.CliffordTableau._from_json_dict_(n=1, rs=[0, 0], xs=[[1], [0]], zs=[[0], [1]])
251
251
  assert t.destabilizers()[0] == cirq.DensePauliString('X', coefficient=1)
252
252
  assert t.stabilizers()[0] == cirq.DensePauliString('Z', coefficient=1)
@@ -266,7 +266,7 @@ def test_json_dict():
266
266
  assert json_dict[k] == v
267
267
 
268
268
 
269
- def test_str():
269
+ def test_str() -> None:
270
270
  t = cirq.CliffordTableau(num_qubits=2)
271
271
  splitted_represent_string = str(t).split('\n')
272
272
  assert len(splitted_represent_string) == 2
@@ -288,11 +288,11 @@ def test_str():
288
288
  assert splitted_represent_string[1] == '+ I Y '
289
289
 
290
290
 
291
- def test_repr():
291
+ def test_repr() -> None:
292
292
  cirq.testing.assert_equivalent_repr(cirq.CliffordTableau(num_qubits=1))
293
293
 
294
294
 
295
- def test_str_full():
295
+ def test_str_full() -> None:
296
296
  t = cirq.CliffordTableau(num_qubits=1)
297
297
  expected_str = r"""stable | destable
298
298
  -------+----------
@@ -317,7 +317,7 @@ def test_str_full():
317
317
  assert t._str_full_() == expected_str
318
318
 
319
319
 
320
- def test_copy():
320
+ def test_copy() -> None:
321
321
  t = cirq.CliffordTableau(num_qubits=3, initial_state=3)
322
322
  new_t = t.copy()
323
323
 
@@ -340,7 +340,7 @@ def _three_identical_table(num_qubits):
340
340
  return t1, t2, t3
341
341
 
342
342
 
343
- def test_tableau_then():
343
+ def test_tableau_then() -> None:
344
344
 
345
345
  t1, t2, expected_t = _three_identical_table(1)
346
346
  assert expected_t == t1.then(t2)
@@ -427,7 +427,7 @@ def test_tableau_then():
427
427
  assert expected_t == t1.then(t2)
428
428
 
429
429
 
430
- def test_tableau_matmul():
430
+ def test_tableau_matmul() -> None:
431
431
  t1, t2, expected_t = _three_identical_table(1)
432
432
  _ = [_H(t, 0) for t in (t1, expected_t)]
433
433
  _ = [_H(t, 0) for t in (t2, expected_t)]
@@ -443,17 +443,17 @@ def test_tableau_matmul():
443
443
  t1 @ 21
444
444
 
445
445
 
446
- def test_tableau_then_with_bad_input():
446
+ def test_tableau_then_with_bad_input() -> None:
447
447
  t1 = cirq.CliffordTableau(1)
448
448
  t2 = cirq.CliffordTableau(2)
449
449
  with pytest.raises(ValueError, match="Mismatched number of qubits of two tableaux: 1 vs 2."):
450
450
  t1.then(t2)
451
451
 
452
452
  with pytest.raises(TypeError):
453
- t1.then(cirq.X)
453
+ t1.then(cirq.X) # type: ignore[arg-type]
454
454
 
455
455
 
456
- def test_inverse():
456
+ def test_inverse() -> None:
457
457
  t = cirq.CliffordTableau(num_qubits=1)
458
458
  assert t.inverse() == t
459
459
 
cirq/qis/entropy.py CHANGED
@@ -14,10 +14,10 @@
14
14
 
15
15
  from __future__ import annotations
16
16
 
17
- from collections.abc import Sequence
17
+ from collections.abc import Iterator, Sequence
18
18
  from concurrent.futures import ThreadPoolExecutor
19
19
  from itertools import product
20
- from typing import Any, cast, Iterator
20
+ from typing import Any, cast
21
21
 
22
22
  import numpy as np
23
23
  import numpy.typing as npt
@@ -93,7 +93,7 @@ def _compute_bitstrings_contribution_to_purity(bitstrings: npt.NDArray[np.int8])
93
93
 
94
94
  def process_renyi_entropy_from_bitstrings(
95
95
  measured_bitstrings: npt.NDArray[np.int8],
96
- subsystem: tuple[int] | None = None,
96
+ subsystem: tuple[int, ...] | None = None,
97
97
  pool: ThreadPoolExecutor | None = None,
98
98
  ) -> float:
99
99
  """Compute the Rényi entropy of an array of bitstrings.
cirq/qis/entropy_test.py CHANGED
@@ -23,7 +23,7 @@ from cirq.qis.entropy import process_renyi_entropy_from_bitstrings
23
23
 
24
24
 
25
25
  @pytest.mark.parametrize('pool', [None, ThreadPoolExecutor(max_workers=1)])
26
- def test_process_renyi_entropy_from_bitstrings(pool):
26
+ def test_process_renyi_entropy_from_bitstrings(pool) -> None:
27
27
  bitstrings = np.array(
28
28
  [
29
29
  [[0, 1, 1, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 1, 1]],
@@ -15,7 +15,8 @@
15
15
  from __future__ import annotations
16
16
 
17
17
  import abc
18
- from typing import Self, Sequence, TYPE_CHECKING
18
+ from collections.abc import Sequence
19
+ from typing import Self, TYPE_CHECKING
19
20
 
20
21
  import numpy as np
21
22
 
cirq/qis/states.py CHANGED
@@ -17,7 +17,8 @@
17
17
  from __future__ import annotations
18
18
 
19
19
  import itertools
20
- from typing import Any, cast, Iterable, Sequence, TYPE_CHECKING, Union
20
+ from collections.abc import Iterable, Sequence
21
+ from typing import Any, cast, TYPE_CHECKING, Union
21
22
 
22
23
  import numpy as np
23
24
 
@@ -1019,7 +1020,11 @@ def validate_density_matrix(
1019
1020
  if not np.isclose(trace, 1.0, atol=atol):
1020
1021
  raise ValueError(f'Density matrix does not have trace 1. Instead, it has trace {trace}.')
1021
1022
  if not np.all(np.linalg.eigvalsh(density_matrix) > -atol):
1022
- raise ValueError('The density matrix is not positive semidefinite.')
1023
+ raise ValueError(
1024
+ 'The density matrix is not positive semidefinite. '
1025
+ 'This may happen for simulations using lower-precision `dtype` such as '
1026
+ '`np.complex64`. Try using `np.complex128` in such a case.'
1027
+ )
1023
1028
 
1024
1029
 
1025
1030
  def _qid_shape_from_args(