cirq-core 1.5.0.dev20250409225226__py3-none-any.whl → 1.6.0__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 (732) hide show
  1. cirq/__init__.py +16 -17
  2. cirq/_compat.py +21 -20
  3. cirq/_compat_test.py +14 -34
  4. cirq/_doc.py +4 -2
  5. cirq/_import.py +8 -6
  6. cirq/_import_test.py +4 -2
  7. cirq/_version.py +6 -6
  8. cirq/_version_test.py +2 -2
  9. cirq/circuits/_block_diagram_drawer.py +11 -10
  10. cirq/circuits/_block_diagram_drawer_test.py +8 -6
  11. cirq/circuits/_box_drawing_character_data.py +8 -8
  12. cirq/circuits/_box_drawing_character_data_test.py +3 -1
  13. cirq/circuits/_bucket_priority_queue.py +9 -7
  14. cirq/circuits/_bucket_priority_queue_test.py +22 -20
  15. cirq/circuits/circuit.py +248 -172
  16. cirq/circuits/circuit_operation.py +73 -83
  17. cirq/circuits/circuit_operation_test.py +128 -90
  18. cirq/circuits/circuit_test.py +211 -151
  19. cirq/circuits/frozen_circuit.py +23 -60
  20. cirq/circuits/frozen_circuit_test.py +31 -8
  21. cirq/circuits/insert_strategy.py +7 -5
  22. cirq/circuits/insert_strategy_test.py +4 -2
  23. cirq/circuits/moment.py +88 -40
  24. cirq/circuits/moment_test.py +128 -51
  25. cirq/circuits/optimization_pass.py +5 -5
  26. cirq/circuits/optimization_pass_test.py +10 -10
  27. cirq/circuits/qasm_output.py +11 -11
  28. cirq/circuits/qasm_output_test.py +25 -22
  29. cirq/circuits/text_diagram_drawer.py +23 -38
  30. cirq/circuits/text_diagram_drawer_test.py +19 -17
  31. cirq/conftest.py +4 -3
  32. cirq/contrib/__init__.py +4 -4
  33. cirq/contrib/acquaintance/__init__.py +1 -1
  34. cirq/contrib/acquaintance/bipartite.py +5 -8
  35. cirq/contrib/acquaintance/bipartite_test.py +18 -13
  36. cirq/contrib/acquaintance/devices.py +2 -2
  37. cirq/contrib/acquaintance/devices_test.py +5 -3
  38. cirq/contrib/acquaintance/executor.py +5 -5
  39. cirq/contrib/acquaintance/executor_test.py +13 -9
  40. cirq/contrib/acquaintance/gates.py +18 -28
  41. cirq/contrib/acquaintance/gates_test.py +24 -20
  42. cirq/contrib/acquaintance/inspection_utils.py +8 -4
  43. cirq/contrib/acquaintance/inspection_utils_test.py +4 -2
  44. cirq/contrib/acquaintance/mutation_utils.py +4 -4
  45. cirq/contrib/acquaintance/mutation_utils_test.py +4 -2
  46. cirq/contrib/acquaintance/optimizers.py +4 -4
  47. cirq/contrib/acquaintance/optimizers_test.py +4 -1
  48. cirq/contrib/acquaintance/permutation.py +15 -27
  49. cirq/contrib/acquaintance/permutation_test.py +26 -17
  50. cirq/contrib/acquaintance/shift.py +4 -4
  51. cirq/contrib/acquaintance/shift_swap_network.py +4 -4
  52. cirq/contrib/acquaintance/shift_swap_network_test.py +9 -6
  53. cirq/contrib/acquaintance/shift_test.py +8 -6
  54. cirq/contrib/acquaintance/strategies/cubic.py +2 -2
  55. cirq/contrib/acquaintance/strategies/cubic_test.py +4 -2
  56. cirq/contrib/acquaintance/strategies/quartic_paired.py +6 -6
  57. cirq/contrib/acquaintance/strategies/quartic_paired_test.py +10 -6
  58. cirq/contrib/acquaintance/testing.py +2 -0
  59. cirq/contrib/acquaintance/topological_sort.py +2 -2
  60. cirq/contrib/acquaintance/topological_sort_test.py +3 -1
  61. cirq/contrib/bayesian_network/bayesian_network_gate.py +9 -10
  62. cirq/contrib/bayesian_network/bayesian_network_gate_test.py +14 -9
  63. cirq/contrib/circuitdag/circuit_dag.py +4 -4
  64. cirq/contrib/circuitdag/circuit_dag_test.py +17 -15
  65. cirq/contrib/custom_simulators/custom_state_simulator.py +5 -5
  66. cirq/contrib/custom_simulators/custom_state_simulator_test.py +22 -17
  67. cirq/contrib/graph_device/graph_device.py +12 -11
  68. cirq/contrib/graph_device/graph_device_test.py +18 -14
  69. cirq/contrib/graph_device/hypergraph.py +16 -14
  70. cirq/contrib/graph_device/hypergraph_test.py +13 -11
  71. cirq/contrib/graph_device/uniform_graph_device.py +6 -4
  72. cirq/contrib/graph_device/uniform_graph_device_test.py +11 -3
  73. cirq/contrib/hacks/disable_validation.py +6 -1
  74. cirq/contrib/hacks/disable_validation_test.py +3 -1
  75. cirq/contrib/json.py +31 -5
  76. cirq/contrib/json_test.py +6 -3
  77. cirq/contrib/json_test_data/DampedReadoutNoiseModel.json +12 -0
  78. cirq/contrib/json_test_data/DampedReadoutNoiseModel.repr +4 -0
  79. cirq/contrib/json_test_data/DepolarizingNoiseModel.json +12 -0
  80. cirq/contrib/json_test_data/DepolarizingNoiseModel.repr +4 -0
  81. cirq/contrib/json_test_data/DepolarizingWithDampedReadoutNoiseModel.json +6 -0
  82. cirq/contrib/json_test_data/DepolarizingWithDampedReadoutNoiseModel.repr +1 -0
  83. cirq/contrib/json_test_data/DepolarizingWithReadoutNoiseModel.json +5 -0
  84. cirq/contrib/json_test_data/DepolarizingWithReadoutNoiseModel.repr +1 -0
  85. cirq/contrib/json_test_data/ReadoutNoiseModel.json +12 -0
  86. cirq/contrib/json_test_data/ReadoutNoiseModel.repr +4 -0
  87. cirq/contrib/json_test_data/__init__.py +17 -0
  88. cirq/contrib/json_test_data/spec.py +32 -0
  89. cirq/contrib/noise_models/noise_models.py +119 -5
  90. cirq/contrib/noise_models/noise_models_test.py +37 -9
  91. cirq/contrib/paulistring/clifford_optimize.py +6 -4
  92. cirq/contrib/paulistring/clifford_optimize_test.py +6 -5
  93. cirq/contrib/paulistring/clifford_target_gateset.py +10 -10
  94. cirq/contrib/paulistring/clifford_target_gateset_test.py +13 -11
  95. cirq/contrib/paulistring/optimize.py +2 -0
  96. cirq/contrib/paulistring/optimize_test.py +4 -3
  97. cirq/contrib/paulistring/pauli_string_dag.py +2 -0
  98. cirq/contrib/paulistring/pauli_string_dag_test.py +3 -1
  99. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +255 -120
  100. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +398 -19
  101. cirq/contrib/paulistring/pauli_string_optimize.py +7 -1
  102. cirq/contrib/paulistring/pauli_string_optimize_test.py +5 -3
  103. cirq/contrib/paulistring/recombine.py +6 -4
  104. cirq/contrib/paulistring/recombine_test.py +3 -1
  105. cirq/contrib/paulistring/separate.py +9 -6
  106. cirq/contrib/paulistring/separate_test.py +3 -1
  107. cirq/contrib/qasm_import/_lexer.py +3 -2
  108. cirq/contrib/qasm_import/_lexer_test.py +49 -13
  109. cirq/contrib/qasm_import/_parser.py +547 -83
  110. cirq/contrib/qasm_import/_parser_test.py +988 -97
  111. cirq/contrib/qasm_import/exception.py +2 -0
  112. cirq/contrib/qasm_import/qasm.py +8 -2
  113. cirq/contrib/qasm_import/qasm_test.py +7 -4
  114. cirq/contrib/qcircuit/qcircuit_diagram_info.py +5 -5
  115. cirq/contrib/qcircuit/qcircuit_diagram_info_test.py +4 -1
  116. cirq/contrib/qcircuit/qcircuit_pdf.py +7 -3
  117. cirq/contrib/qcircuit/qcircuit_pdf_test.py +3 -1
  118. cirq/contrib/qcircuit/qcircuit_test.py +10 -8
  119. cirq/contrib/quantum_volume/quantum_volume.py +31 -27
  120. cirq/contrib/quantum_volume/quantum_volume_test.py +19 -16
  121. cirq/contrib/quimb/density_matrix.py +15 -14
  122. cirq/contrib/quimb/density_matrix_test.py +10 -7
  123. cirq/contrib/quimb/grid_circuits.py +5 -2
  124. cirq/contrib/quimb/grid_circuits_test.py +3 -0
  125. cirq/contrib/quimb/mps_simulator.py +20 -20
  126. cirq/contrib/quimb/mps_simulator_test.py +3 -0
  127. cirq/contrib/quimb/state_vector.py +12 -11
  128. cirq/contrib/quimb/state_vector_test.py +3 -0
  129. cirq/contrib/quirk/export_to_quirk.py +5 -3
  130. cirq/contrib/quirk/export_to_quirk_test.py +18 -16
  131. cirq/contrib/quirk/linearize_circuit.py +2 -0
  132. cirq/contrib/quirk/quirk_gate.py +18 -17
  133. cirq/contrib/routing/device.py +5 -3
  134. cirq/contrib/routing/device_test.py +2 -0
  135. cirq/contrib/routing/greedy.py +10 -21
  136. cirq/contrib/routing/greedy_test.py +4 -2
  137. cirq/contrib/routing/initialization.py +2 -2
  138. cirq/contrib/routing/initialization_test.py +5 -3
  139. cirq/contrib/routing/router.py +9 -5
  140. cirq/contrib/routing/router_test.py +2 -0
  141. cirq/contrib/routing/swap_network.py +3 -3
  142. cirq/contrib/routing/swap_network_test.py +3 -1
  143. cirq/contrib/routing/utils.py +2 -2
  144. cirq/contrib/routing/utils_test.py +3 -0
  145. cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +15 -9
  146. cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking_test.py +3 -0
  147. cirq/contrib/svg/svg.py +3 -3
  148. cirq/contrib/svg/svg_test.py +8 -5
  149. cirq/devices/device.py +4 -4
  150. cirq/devices/device_test.py +7 -4
  151. cirq/devices/grid_device_metadata.py +10 -10
  152. cirq/devices/grid_device_metadata_test.py +3 -0
  153. cirq/devices/grid_qubit.py +29 -21
  154. cirq/devices/grid_qubit_test.py +3 -0
  155. cirq/devices/insertion_noise_model.py +7 -7
  156. cirq/devices/insertion_noise_model_test.py +7 -5
  157. cirq/devices/line_qubit.py +13 -13
  158. cirq/devices/line_qubit_test.py +2 -0
  159. cirq/devices/named_topologies.py +18 -29
  160. cirq/devices/named_topologies_test.py +13 -10
  161. cirq/devices/noise_model.py +3 -3
  162. cirq/devices/noise_model_test.py +19 -15
  163. cirq/devices/noise_properties.py +15 -6
  164. cirq/devices/noise_properties_test.py +34 -3
  165. cirq/devices/noise_utils.py +11 -9
  166. cirq/devices/noise_utils_test.py +2 -0
  167. cirq/devices/superconducting_qubits_noise_properties.py +23 -22
  168. cirq/devices/superconducting_qubits_noise_properties_test.py +6 -6
  169. cirq/devices/thermal_noise_model.py +107 -37
  170. cirq/devices/thermal_noise_model_test.py +21 -0
  171. cirq/devices/unconstrained_device.py +5 -3
  172. cirq/devices/unconstrained_device_test.py +2 -0
  173. cirq/experiments/__init__.py +4 -2
  174. cirq/experiments/benchmarking/__init__.py +17 -0
  175. cirq/experiments/benchmarking/parallel_xeb.py +677 -0
  176. cirq/experiments/benchmarking/parallel_xeb_test.py +447 -0
  177. cirq/experiments/fidelity_estimation.py +14 -8
  178. cirq/experiments/fidelity_estimation_test.py +3 -0
  179. cirq/experiments/n_qubit_tomography.py +17 -16
  180. cirq/experiments/n_qubit_tomography_test.py +8 -5
  181. cirq/experiments/purity_estimation.py +2 -0
  182. cirq/experiments/purity_estimation_test.py +2 -0
  183. cirq/experiments/qubit_characterizations.py +207 -103
  184. cirq/experiments/qubit_characterizations_test.py +40 -12
  185. cirq/experiments/random_quantum_circuit_generation.py +56 -70
  186. cirq/experiments/random_quantum_circuit_generation_test.py +11 -8
  187. cirq/experiments/readout_confusion_matrix.py +24 -22
  188. cirq/experiments/readout_confusion_matrix_test.py +2 -0
  189. cirq/experiments/single_qubit_readout_calibration.py +30 -15
  190. cirq/experiments/single_qubit_readout_calibration_test.py +5 -2
  191. cirq/experiments/t1_decay_experiment.py +9 -7
  192. cirq/experiments/t1_decay_experiment_test.py +13 -11
  193. cirq/experiments/t2_decay_experiment.py +16 -13
  194. cirq/experiments/t2_decay_experiment_test.py +2 -0
  195. cirq/experiments/two_qubit_xeb.py +64 -57
  196. cirq/experiments/two_qubit_xeb_test.py +10 -6
  197. cirq/experiments/xeb_fitting.py +39 -35
  198. cirq/experiments/xeb_sampling.py +37 -44
  199. cirq/experiments/xeb_sampling_test.py +3 -0
  200. cirq/experiments/xeb_simulation.py +14 -10
  201. cirq/experiments/xeb_simulation_test.py +5 -5
  202. cirq/experiments/z_phase_calibration.py +32 -29
  203. cirq/experiments/z_phase_calibration_test.py +3 -4
  204. cirq/interop/quirk/cells/__init__.py +1 -1
  205. cirq/interop/quirk/cells/all_cells.py +7 -2
  206. cirq/interop/quirk/cells/arithmetic_cells.py +29 -41
  207. cirq/interop/quirk/cells/arithmetic_cells_test.py +17 -14
  208. cirq/interop/quirk/cells/cell.py +19 -28
  209. cirq/interop/quirk/cells/cell_test.py +3 -0
  210. cirq/interop/quirk/cells/composite_cell.py +13 -28
  211. cirq/interop/quirk/cells/composite_cell_test.py +2 -0
  212. cirq/interop/quirk/cells/control_cells.py +15 -15
  213. cirq/interop/quirk/cells/control_cells_test.py +7 -5
  214. cirq/interop/quirk/cells/frequency_space_cells.py +4 -3
  215. cirq/interop/quirk/cells/frequency_space_cells_test.py +3 -1
  216. cirq/interop/quirk/cells/ignored_cells.py +3 -0
  217. cirq/interop/quirk/cells/ignored_cells_test.py +3 -1
  218. cirq/interop/quirk/cells/input_cells.py +7 -5
  219. cirq/interop/quirk/cells/input_cells_test.py +7 -5
  220. cirq/interop/quirk/cells/input_rotation_cells.py +15 -13
  221. cirq/interop/quirk/cells/input_rotation_cells_test.py +9 -7
  222. cirq/interop/quirk/cells/measurement_cells.py +5 -2
  223. cirq/interop/quirk/cells/measurement_cells_test.py +3 -1
  224. cirq/interop/quirk/cells/parse.py +22 -23
  225. cirq/interop/quirk/cells/parse_test.py +12 -10
  226. cirq/interop/quirk/cells/qubit_permutation_cells.py +5 -3
  227. cirq/interop/quirk/cells/qubit_permutation_cells_test.py +9 -7
  228. cirq/interop/quirk/cells/scalar_cells.py +4 -1
  229. cirq/interop/quirk/cells/scalar_cells_test.py +3 -1
  230. cirq/interop/quirk/cells/single_qubit_rotation_cells.py +5 -2
  231. cirq/interop/quirk/cells/single_qubit_rotation_cells_test.py +5 -3
  232. cirq/interop/quirk/cells/swap_cell.py +8 -6
  233. cirq/interop/quirk/cells/swap_cell_test.py +6 -4
  234. cirq/interop/quirk/cells/testing.py +6 -6
  235. cirq/interop/quirk/cells/testing_test.py +8 -6
  236. cirq/interop/quirk/cells/unsupported_cells.py +3 -0
  237. cirq/interop/quirk/cells/unsupported_cells_test.py +4 -2
  238. cirq/interop/quirk/url_to_circuit.py +23 -36
  239. cirq/interop/quirk/url_to_circuit_test.py +4 -1
  240. cirq/json_resolver_cache.py +14 -12
  241. cirq/linalg/__init__.py +4 -6
  242. cirq/linalg/combinators.py +7 -5
  243. cirq/linalg/combinators_test.py +10 -7
  244. cirq/linalg/decompositions.py +24 -35
  245. cirq/linalg/decompositions_test.py +3 -1
  246. cirq/linalg/diagonalize.py +6 -4
  247. cirq/linalg/diagonalize_test.py +15 -14
  248. cirq/linalg/operator_spaces.py +14 -14
  249. cirq/linalg/operator_spaces_test.py +13 -11
  250. cirq/linalg/predicates.py +18 -9
  251. cirq/linalg/predicates_test.py +5 -0
  252. cirq/linalg/tolerance.py +6 -3
  253. cirq/linalg/tolerance_test.py +6 -4
  254. cirq/linalg/transformations.py +23 -20
  255. cirq/linalg/transformations_test.py +73 -43
  256. cirq/neutral_atoms/convert_to_neutral_atom_gates.py +9 -3
  257. cirq/neutral_atoms/convert_to_neutral_atom_gates_test.py +3 -1
  258. cirq/neutral_atoms/neutral_atom_devices.py +2 -0
  259. cirq/ops/__init__.py +2 -0
  260. cirq/ops/arithmetic_operation.py +21 -21
  261. cirq/ops/arithmetic_operation_test.py +7 -8
  262. cirq/ops/boolean_hamiltonian.py +23 -22
  263. cirq/ops/boolean_hamiltonian_test.py +12 -9
  264. cirq/ops/classically_controlled_operation.py +31 -36
  265. cirq/ops/classically_controlled_operation_test.py +121 -117
  266. cirq/ops/clifford_gate.py +98 -81
  267. cirq/ops/clifford_gate_test.py +72 -57
  268. cirq/ops/common_channels.py +44 -44
  269. cirq/ops/common_channels_test.py +83 -81
  270. cirq/ops/common_gate_families.py +9 -7
  271. cirq/ops/common_gate_families_test.py +11 -7
  272. cirq/ops/common_gates.py +164 -183
  273. cirq/ops/common_gates_test.py +135 -95
  274. cirq/ops/control_values.py +23 -26
  275. cirq/ops/control_values_test.py +22 -20
  276. cirq/ops/controlled_gate.py +64 -112
  277. cirq/ops/controlled_gate_test.py +130 -35
  278. cirq/ops/controlled_operation.py +24 -35
  279. cirq/ops/controlled_operation_test.py +8 -6
  280. cirq/ops/dense_pauli_string.py +38 -49
  281. cirq/ops/dense_pauli_string_test.py +4 -2
  282. cirq/ops/diagonal_gate.py +18 -31
  283. cirq/ops/diagonal_gate_test.py +13 -13
  284. cirq/ops/eigen_gate.py +29 -29
  285. cirq/ops/eigen_gate_test.py +45 -28
  286. cirq/ops/fourier_transform.py +14 -20
  287. cirq/ops/fourier_transform_test.py +15 -12
  288. cirq/ops/fsim_gate.py +43 -42
  289. cirq/ops/fsim_gate_test.py +29 -29
  290. cirq/ops/gate_features.py +2 -0
  291. cirq/ops/gate_features_test.py +5 -3
  292. cirq/ops/gate_operation.py +43 -65
  293. cirq/ops/gate_operation_test.py +46 -42
  294. cirq/ops/gateset.py +28 -40
  295. cirq/ops/gateset_test.py +4 -2
  296. cirq/ops/global_phase_op.py +45 -20
  297. cirq/ops/global_phase_op_test.py +44 -20
  298. cirq/ops/greedy_qubit_manager.py +10 -8
  299. cirq/ops/greedy_qubit_manager_test.py +5 -3
  300. cirq/ops/identity.py +14 -12
  301. cirq/ops/identity_test.py +24 -20
  302. cirq/ops/kraus_channel.py +11 -8
  303. cirq/ops/kraus_channel_test.py +14 -11
  304. cirq/ops/linear_combinations.py +65 -77
  305. cirq/ops/linear_combinations_test.py +14 -9
  306. cirq/ops/matrix_gates.py +21 -18
  307. cirq/ops/matrix_gates_test.py +16 -0
  308. cirq/ops/measure_util.py +15 -20
  309. cirq/ops/measure_util_test.py +2 -0
  310. cirq/ops/measurement_gate.py +26 -37
  311. cirq/ops/measurement_gate_test.py +2 -0
  312. cirq/ops/mixed_unitary_channel.py +12 -9
  313. cirq/ops/mixed_unitary_channel_test.py +14 -11
  314. cirq/ops/named_qubit.py +16 -13
  315. cirq/ops/named_qubit_test.py +15 -13
  316. cirq/ops/op_tree.py +9 -7
  317. cirq/ops/op_tree_test.py +22 -19
  318. cirq/ops/parallel_gate.py +15 -17
  319. cirq/ops/parallel_gate_test.py +18 -16
  320. cirq/ops/parity_gates.py +23 -25
  321. cirq/ops/parity_gates_test.py +36 -32
  322. cirq/ops/pauli_gates.py +22 -21
  323. cirq/ops/pauli_gates_test.py +29 -20
  324. cirq/ops/pauli_interaction_gate.py +15 -19
  325. cirq/ops/pauli_interaction_gate_test.py +10 -8
  326. cirq/ops/pauli_measurement_gate.py +23 -35
  327. cirq/ops/pauli_measurement_gate_test.py +2 -0
  328. cirq/ops/pauli_string.py +92 -120
  329. cirq/ops/pauli_string_phasor.py +52 -45
  330. cirq/ops/pauli_string_phasor_test.py +4 -5
  331. cirq/ops/pauli_string_raw_types.py +9 -7
  332. cirq/ops/pauli_string_raw_types_test.py +2 -0
  333. cirq/ops/pauli_string_test.py +31 -154
  334. cirq/ops/pauli_sum_exponential.py +12 -12
  335. cirq/ops/pauli_sum_exponential_test.py +12 -10
  336. cirq/ops/permutation_gate.py +8 -6
  337. cirq/ops/permutation_gate_test.py +10 -8
  338. cirq/ops/phased_iswap_gate.py +16 -16
  339. cirq/ops/phased_iswap_gate_test.py +17 -15
  340. cirq/ops/phased_x_gate.py +16 -17
  341. cirq/ops/phased_x_gate_test.py +18 -16
  342. cirq/ops/phased_x_z_gate.py +24 -22
  343. cirq/ops/phased_x_z_gate_test.py +17 -11
  344. cirq/ops/projector.py +16 -11
  345. cirq/ops/projector_test.py +19 -16
  346. cirq/ops/qid_util.py +7 -5
  347. cirq/ops/qid_util_test.py +2 -0
  348. cirq/ops/qubit_manager.py +11 -9
  349. cirq/ops/qubit_manager_test.py +6 -4
  350. cirq/ops/qubit_order.py +11 -14
  351. cirq/ops/qubit_order_or_list.py +4 -2
  352. cirq/ops/qubit_order_test.py +12 -10
  353. cirq/ops/random_gate_channel.py +12 -10
  354. cirq/ops/random_gate_channel_test.py +14 -11
  355. cirq/ops/raw_types.py +109 -129
  356. cirq/ops/raw_types_test.py +63 -57
  357. cirq/ops/state_preparation_channel.py +7 -7
  358. cirq/ops/state_preparation_channel_test.py +11 -9
  359. cirq/ops/swap_gates.py +13 -15
  360. cirq/ops/swap_gates_test.py +19 -17
  361. cirq/ops/tags.py +5 -3
  362. cirq/ops/tags_test.py +4 -2
  363. cirq/ops/three_qubit_gates.py +43 -76
  364. cirq/ops/three_qubit_gates_test.py +19 -17
  365. cirq/ops/two_qubit_diagonal_gate.py +13 -13
  366. cirq/ops/two_qubit_diagonal_gate_test.py +10 -8
  367. cirq/ops/uniform_superposition_gate.py +5 -3
  368. cirq/ops/uniform_superposition_gate_test.py +5 -3
  369. cirq/ops/wait_gate.py +17 -14
  370. cirq/ops/wait_gate_test.py +9 -6
  371. cirq/protocols/__init__.py +0 -3
  372. cirq/protocols/act_on_protocol.py +8 -6
  373. cirq/protocols/act_on_protocol_test.py +15 -12
  374. cirq/protocols/apply_channel_protocol.py +10 -14
  375. cirq/protocols/apply_channel_protocol_test.py +2 -0
  376. cirq/protocols/apply_mixture_protocol.py +13 -42
  377. cirq/protocols/apply_mixture_protocol_test.py +7 -5
  378. cirq/protocols/apply_unitary_protocol.py +39 -34
  379. cirq/protocols/apply_unitary_protocol_test.py +4 -1
  380. cirq/protocols/approximate_equality_protocol.py +2 -0
  381. cirq/protocols/approximate_equality_protocol_test.py +2 -0
  382. cirq/protocols/circuit_diagram_info_protocol.py +58 -42
  383. cirq/protocols/circuit_diagram_info_protocol_test.py +70 -12
  384. cirq/protocols/commutes_protocol.py +8 -7
  385. cirq/protocols/commutes_protocol_test.py +2 -0
  386. cirq/protocols/control_key_protocol.py +6 -4
  387. cirq/protocols/control_key_protocol_test.py +3 -1
  388. cirq/protocols/decompose_protocol.py +49 -48
  389. cirq/protocols/decompose_protocol_test.py +27 -16
  390. cirq/protocols/equal_up_to_global_phase_protocol.py +2 -0
  391. cirq/protocols/equal_up_to_global_phase_protocol_test.py +9 -6
  392. cirq/protocols/has_stabilizer_effect_protocol.py +7 -5
  393. cirq/protocols/has_stabilizer_effect_protocol_test.py +7 -5
  394. cirq/protocols/has_unitary_protocol.py +10 -6
  395. cirq/protocols/has_unitary_protocol_test.py +13 -8
  396. cirq/protocols/hash_from_pickle_test.py +2 -11
  397. cirq/protocols/inverse_protocol.py +13 -16
  398. cirq/protocols/inverse_protocol_test.py +5 -3
  399. cirq/protocols/json_serialization.py +35 -54
  400. cirq/protocols/json_serialization_test.py +14 -21
  401. cirq/protocols/json_test_data/CXSWAP.json +46 -0
  402. cirq/protocols/json_test_data/CXSWAP.repr +13 -0
  403. cirq/protocols/json_test_data/CZSWAP.json +46 -0
  404. cirq/protocols/json_test_data/CZSWAP.repr +13 -0
  405. cirq/protocols/json_test_data/CircuitOperation.json +6 -3
  406. cirq/protocols/json_test_data/CircuitOperation.repr_inward +4 -2
  407. cirq/protocols/json_test_data/Moment.json +24 -1
  408. cirq/protocols/json_test_data/Moment.repr +6 -1
  409. cirq/protocols/json_test_data/ThermalNoiseModel.json +32 -0
  410. cirq/protocols/json_test_data/ThermalNoiseModel.repr +1 -0
  411. cirq/protocols/json_test_data/spec.py +6 -2
  412. cirq/protocols/kraus_protocol.py +47 -7
  413. cirq/protocols/kraus_protocol_test.py +86 -12
  414. cirq/protocols/measurement_key_protocol.py +15 -16
  415. cirq/protocols/measurement_key_protocol_test.py +13 -11
  416. cirq/protocols/mixture_protocol.py +7 -5
  417. cirq/protocols/mixture_protocol_test.py +4 -2
  418. cirq/protocols/mul_protocol.py +2 -3
  419. cirq/protocols/mul_protocol_test.py +2 -0
  420. cirq/protocols/pauli_expansion_protocol.py +6 -3
  421. cirq/protocols/pauli_expansion_protocol_test.py +5 -3
  422. cirq/protocols/phase_protocol.py +2 -0
  423. cirq/protocols/phase_protocol_test.py +3 -1
  424. cirq/protocols/pow_protocol.py +11 -16
  425. cirq/protocols/pow_protocol_test.py +2 -0
  426. cirq/protocols/qasm.py +14 -20
  427. cirq/protocols/qasm_test.py +6 -3
  428. cirq/protocols/qid_shape_protocol.py +8 -8
  429. cirq/protocols/qid_shape_protocol_test.py +3 -1
  430. cirq/protocols/resolve_parameters.py +5 -3
  431. cirq/protocols/resolve_parameters_test.py +8 -7
  432. cirq/protocols/trace_distance_bound.py +6 -4
  433. cirq/protocols/trace_distance_bound_test.py +3 -1
  434. cirq/protocols/unitary_protocol.py +17 -7
  435. cirq/protocols/unitary_protocol_test.py +12 -2
  436. cirq/qis/channels.py +6 -2
  437. cirq/qis/channels_test.py +20 -16
  438. cirq/qis/clifford_tableau.py +21 -19
  439. cirq/qis/clifford_tableau_test.py +2 -2
  440. cirq/qis/entropy.py +14 -3
  441. cirq/qis/entropy_test.py +3 -1
  442. cirq/qis/measures.py +13 -13
  443. cirq/qis/measures_test.py +20 -14
  444. cirq/qis/noise_utils.py +2 -0
  445. cirq/qis/noise_utils_test.py +9 -7
  446. cirq/qis/quantum_state_representation.py +7 -8
  447. cirq/qis/states.py +58 -56
  448. cirq/qis/states_test.py +2 -0
  449. cirq/sim/classical_simulator.py +23 -22
  450. cirq/sim/classical_simulator_test.py +2 -0
  451. cirq/sim/clifford/clifford_simulator.py +23 -21
  452. cirq/sim/clifford/clifford_simulator_test.py +7 -4
  453. cirq/sim/clifford/clifford_tableau_simulation_state.py +10 -7
  454. cirq/sim/clifford/clifford_tableau_simulation_state_test.py +5 -5
  455. cirq/sim/clifford/stabilizer_ch_form_simulation_state.py +8 -6
  456. cirq/sim/clifford/stabilizer_ch_form_simulation_state_test.py +8 -6
  457. cirq/sim/clifford/stabilizer_sampler.py +9 -7
  458. cirq/sim/clifford/stabilizer_sampler_test.py +4 -2
  459. cirq/sim/clifford/stabilizer_simulation_state.py +14 -13
  460. cirq/sim/clifford/stabilizer_simulation_state_test.py +6 -4
  461. cirq/sim/clifford/stabilizer_state_ch_form.py +13 -11
  462. cirq/sim/clifford/stabilizer_state_ch_form_test.py +4 -2
  463. cirq/sim/density_matrix_simulation_state.py +26 -27
  464. cirq/sim/density_matrix_simulation_state_test.py +10 -8
  465. cirq/sim/density_matrix_simulator.py +30 -28
  466. cirq/sim/density_matrix_simulator_test.py +48 -48
  467. cirq/sim/density_matrix_utils.py +13 -11
  468. cirq/sim/density_matrix_utils_test.py +38 -36
  469. cirq/sim/mux.py +33 -31
  470. cirq/sim/mux_test.py +3 -0
  471. cirq/sim/simulation_product_state.py +15 -15
  472. cirq/sim/simulation_product_state_test.py +29 -26
  473. cirq/sim/simulation_state.py +29 -38
  474. cirq/sim/simulation_state_base.py +21 -32
  475. cirq/sim/simulation_state_test.py +15 -13
  476. cirq/sim/simulation_utils.py +5 -2
  477. cirq/sim/simulation_utils_test.py +5 -2
  478. cirq/sim/simulator.py +90 -106
  479. cirq/sim/simulator_base.py +33 -45
  480. cirq/sim/simulator_base_test.py +20 -15
  481. cirq/sim/simulator_test.py +23 -14
  482. cirq/sim/sparse_simulator.py +19 -17
  483. cirq/sim/sparse_simulator_test.py +41 -40
  484. cirq/sim/state_vector.py +15 -12
  485. cirq/sim/state_vector_simulation_state.py +31 -31
  486. cirq/sim/state_vector_simulation_state_test.py +16 -14
  487. cirq/sim/state_vector_simulator.py +17 -14
  488. cirq/sim/state_vector_simulator_test.py +2 -0
  489. cirq/sim/state_vector_test.py +6 -3
  490. cirq/study/flatten_expressions.py +16 -15
  491. cirq/study/flatten_expressions_test.py +13 -11
  492. cirq/study/resolver.py +18 -17
  493. cirq/study/resolver_test.py +22 -20
  494. cirq/study/result.py +17 -27
  495. cirq/study/result_test.py +2 -0
  496. cirq/study/sweepable.py +12 -10
  497. cirq/study/sweepable_test.py +3 -0
  498. cirq/study/sweeps.py +42 -61
  499. cirq/study/sweeps_test.py +33 -0
  500. cirq/testing/__init__.py +7 -11
  501. cirq/testing/_compat_test_data/module_a/__init__.py +1 -0
  502. cirq/testing/_compat_test_data/module_a/module_b/__init__.py +1 -0
  503. cirq/testing/_compat_test_data/module_a/sub/__init__.py +1 -0
  504. cirq/testing/circuit_compare.py +8 -17
  505. cirq/testing/circuit_compare_test.py +2 -0
  506. cirq/testing/consistent_act_on.py +13 -11
  507. cirq/testing/consistent_act_on_test.py +5 -3
  508. cirq/testing/consistent_channels.py +2 -0
  509. cirq/testing/consistent_channels_test.py +10 -8
  510. cirq/testing/consistent_controlled_gate_op.py +5 -5
  511. cirq/testing/consistent_controlled_gate_op_test.py +18 -18
  512. cirq/testing/consistent_decomposition.py +2 -2
  513. cirq/testing/consistent_decomposition_test.py +4 -2
  514. cirq/testing/consistent_pauli_expansion.py +2 -0
  515. cirq/testing/consistent_pauli_expansion_test.py +3 -1
  516. cirq/testing/consistent_phase_by.py +2 -0
  517. cirq/testing/consistent_phase_by_test.py +3 -1
  518. cirq/testing/consistent_protocols.py +14 -20
  519. cirq/testing/consistent_protocols_test.py +13 -11
  520. cirq/testing/consistent_qasm.py +6 -4
  521. cirq/testing/consistent_qasm_test.py +7 -7
  522. cirq/testing/consistent_resolve_parameters.py +2 -0
  523. cirq/testing/consistent_specified_has_unitary.py +2 -2
  524. cirq/testing/consistent_specified_has_unitary_test.py +6 -4
  525. cirq/testing/consistent_unitary.py +1 -0
  526. cirq/testing/consistent_unitary_test.py +4 -2
  527. cirq/testing/deprecation.py +5 -2
  528. cirq/testing/deprecation_test.py +5 -2
  529. cirq/testing/devices.py +7 -4
  530. cirq/testing/devices_test.py +7 -4
  531. cirq/testing/equals_tester.py +4 -2
  532. cirq/testing/equals_tester_test.py +21 -17
  533. cirq/testing/equivalent_basis_map.py +6 -4
  534. cirq/testing/equivalent_basis_map_test.py +6 -4
  535. cirq/testing/equivalent_repr_eval.py +6 -4
  536. cirq/testing/equivalent_repr_eval_test.py +5 -3
  537. cirq/testing/gate_features.py +2 -0
  538. cirq/testing/gate_features_test.py +7 -5
  539. cirq/testing/json.py +19 -15
  540. cirq/testing/json_test.py +5 -3
  541. cirq/testing/lin_alg_utils.py +10 -11
  542. cirq/testing/lin_alg_utils_test.py +14 -12
  543. cirq/testing/logs.py +7 -6
  544. cirq/testing/logs_test.py +9 -7
  545. cirq/testing/no_identifier_qubit.py +4 -2
  546. cirq/testing/no_identifier_qubit_test.py +5 -3
  547. cirq/testing/op_tree.py +2 -0
  548. cirq/testing/op_tree_test.py +4 -1
  549. cirq/testing/order_tester.py +2 -0
  550. cirq/testing/order_tester_test.py +8 -6
  551. cirq/testing/pytest_utils.py +2 -0
  552. cirq/testing/pytest_utils_test.py +4 -2
  553. cirq/testing/random_circuit.py +21 -20
  554. cirq/testing/random_circuit_test.py +12 -9
  555. cirq/testing/repr_pretty_tester.py +1 -0
  556. cirq/testing/repr_pretty_tester_test.py +5 -3
  557. cirq/testing/routing_devices.py +4 -1
  558. cirq/testing/routing_devices_test.py +9 -6
  559. cirq/testing/sample_circuits.py +4 -1
  560. cirq/testing/sample_circuits_test.py +3 -1
  561. cirq/testing/sample_gates.py +3 -0
  562. cirq/testing/sample_gates_test.py +5 -2
  563. cirq/transformers/__init__.py +11 -4
  564. cirq/transformers/align.py +9 -7
  565. cirq/transformers/align_test.py +2 -0
  566. cirq/transformers/analytical_decompositions/__init__.py +3 -6
  567. cirq/transformers/analytical_decompositions/clifford_decomposition.py +18 -16
  568. cirq/transformers/analytical_decompositions/clifford_decomposition_test.py +2 -0
  569. cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +19 -16
  570. cirq/transformers/analytical_decompositions/controlled_gate_decomposition_test.py +2 -0
  571. cirq/transformers/analytical_decompositions/cphase_to_fsim.py +11 -9
  572. cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +5 -3
  573. cirq/transformers/analytical_decompositions/pauli_string_decomposition.py +5 -3
  574. cirq/transformers/analytical_decompositions/pauli_string_decomposition_test.py +5 -3
  575. cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +141 -44
  576. cirq/transformers/analytical_decompositions/quantum_shannon_decomposition_test.py +35 -1
  577. cirq/transformers/analytical_decompositions/single_qubit_decompositions.py +8 -7
  578. cirq/transformers/analytical_decompositions/single_qubit_decompositions_test.py +2 -0
  579. cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry.py +7 -4
  580. cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry_test.py +3 -0
  581. cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +11 -19
  582. cirq/transformers/analytical_decompositions/three_qubit_decomposition_test.py +8 -33
  583. cirq/transformers/analytical_decompositions/two_qubit_state_preparation.py +9 -11
  584. cirq/transformers/analytical_decompositions/two_qubit_state_preparation_test.py +2 -0
  585. cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +91 -27
  586. cirq/transformers/analytical_decompositions/two_qubit_to_cz_test.py +36 -7
  587. cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +20 -21
  588. cirq/transformers/analytical_decompositions/two_qubit_to_fsim_test.py +8 -6
  589. cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +13 -15
  590. cirq/transformers/analytical_decompositions/two_qubit_to_ms_test.py +3 -1
  591. cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +39 -41
  592. cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap_test.py +2 -0
  593. cirq/transformers/drop_empty_moments.py +5 -3
  594. cirq/transformers/drop_empty_moments_test.py +4 -2
  595. cirq/transformers/drop_negligible_operations.py +7 -5
  596. cirq/transformers/drop_negligible_operations_test.py +2 -0
  597. cirq/transformers/dynamical_decoupling.py +49 -42
  598. cirq/transformers/dynamical_decoupling_test.py +223 -205
  599. cirq/transformers/eject_phased_paulis.py +28 -26
  600. cirq/transformers/eject_phased_paulis_test.py +12 -9
  601. cirq/transformers/eject_z.py +12 -12
  602. cirq/transformers/eject_z_test.py +2 -2
  603. cirq/transformers/expand_composite.py +6 -4
  604. cirq/transformers/expand_composite_test.py +3 -1
  605. cirq/transformers/gauge_compiling/__init__.py +3 -1
  606. cirq/transformers/gauge_compiling/cphase_gauge.py +2 -0
  607. cirq/transformers/gauge_compiling/cphase_gauge_test.py +2 -0
  608. cirq/transformers/gauge_compiling/cz_gauge.py +2 -0
  609. cirq/transformers/gauge_compiling/cz_gauge_test.py +1 -0
  610. cirq/transformers/gauge_compiling/gauge_compiling.py +45 -41
  611. cirq/transformers/gauge_compiling/gauge_compiling_test.py +2 -0
  612. cirq/transformers/gauge_compiling/gauge_compiling_test_utils.py +1 -0
  613. cirq/transformers/gauge_compiling/gauge_compiling_test_utils_test.py +5 -1
  614. cirq/transformers/gauge_compiling/iswap_gauge.py +2 -0
  615. cirq/transformers/gauge_compiling/iswap_gauge_test.py +1 -0
  616. cirq/transformers/gauge_compiling/spin_inversion_gauge.py +2 -0
  617. cirq/transformers/gauge_compiling/spin_inversion_gauge_test.py +2 -0
  618. cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +7 -6
  619. cirq/transformers/gauge_compiling/sqrt_cz_gauge_test.py +2 -0
  620. cirq/transformers/gauge_compiling/sqrt_iswap_gauge.py +2 -0
  621. cirq/transformers/gauge_compiling/sqrt_iswap_gauge_test.py +2 -0
  622. cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils.py +6 -3
  623. cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils_test.py +3 -0
  624. cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +12 -9
  625. cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation_test.py +9 -7
  626. cirq/transformers/insertion_sort.py +8 -6
  627. cirq/transformers/insertion_sort_test.py +3 -1
  628. cirq/transformers/measurement_transformers.py +29 -29
  629. cirq/transformers/measurement_transformers_test.py +2 -0
  630. cirq/transformers/merge_k_qubit_gates.py +12 -10
  631. cirq/transformers/merge_k_qubit_gates_test.py +18 -18
  632. cirq/transformers/merge_single_qubit_gates.py +197 -20
  633. cirq/transformers/merge_single_qubit_gates_test.py +177 -5
  634. cirq/transformers/noise_adding.py +5 -3
  635. cirq/transformers/noise_adding_test.py +2 -0
  636. cirq/transformers/optimize_for_target_gateset.py +19 -17
  637. cirq/transformers/optimize_for_target_gateset_test.py +11 -8
  638. cirq/transformers/qubit_management_transformers.py +13 -11
  639. cirq/transformers/qubit_management_transformers_test.py +5 -3
  640. cirq/transformers/randomized_measurements.py +16 -14
  641. cirq/transformers/randomized_measurements_test.py +10 -4
  642. cirq/transformers/routing/initial_mapper.py +6 -4
  643. cirq/transformers/routing/initial_mapper_test.py +2 -0
  644. cirq/transformers/routing/line_initial_mapper.py +16 -14
  645. cirq/transformers/routing/line_initial_mapper_test.py +9 -7
  646. cirq/transformers/routing/mapping_manager.py +10 -10
  647. cirq/transformers/routing/mapping_manager_test.py +2 -0
  648. cirq/transformers/routing/route_circuit_cqc.py +33 -31
  649. cirq/transformers/routing/route_circuit_cqc_test.py +15 -13
  650. cirq/transformers/routing/visualize_routed_circuit.py +8 -7
  651. cirq/transformers/routing/visualize_routed_circuit_test.py +4 -2
  652. cirq/transformers/stratify.py +17 -15
  653. cirq/transformers/stratify_test.py +3 -0
  654. cirq/transformers/symbolize.py +103 -0
  655. cirq/transformers/symbolize_test.py +62 -0
  656. cirq/transformers/synchronize_terminal_measurements.py +10 -10
  657. cirq/transformers/synchronize_terminal_measurements_test.py +12 -10
  658. cirq/transformers/tag_transformers.py +97 -0
  659. cirq/transformers/tag_transformers_test.py +103 -0
  660. cirq/transformers/target_gatesets/compilation_target_gateset.py +21 -19
  661. cirq/transformers/target_gatesets/compilation_target_gateset_test.py +20 -16
  662. cirq/transformers/target_gatesets/cz_gateset.py +7 -5
  663. cirq/transformers/target_gatesets/cz_gateset_test.py +21 -19
  664. cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +9 -7
  665. cirq/transformers/target_gatesets/sqrt_iswap_gateset_test.py +25 -25
  666. cirq/transformers/transformer_api.py +34 -47
  667. cirq/transformers/transformer_api_test.py +9 -8
  668. cirq/transformers/transformer_primitives.py +39 -49
  669. cirq/transformers/transformer_primitives_test.py +10 -17
  670. cirq/value/abc_alt.py +6 -4
  671. cirq/value/abc_alt_test.py +5 -3
  672. cirq/value/angle.py +11 -12
  673. cirq/value/angle_test.py +5 -3
  674. cirq/value/classical_data.py +27 -27
  675. cirq/value/classical_data_test.py +11 -8
  676. cirq/value/condition.py +26 -24
  677. cirq/value/condition_test.py +2 -0
  678. cirq/value/digits.py +14 -11
  679. cirq/value/digits_test.py +2 -0
  680. cirq/value/duration.py +23 -20
  681. cirq/value/duration_test.py +2 -0
  682. cirq/value/linear_dict.py +25 -30
  683. cirq/value/linear_dict_test.py +10 -8
  684. cirq/value/measurement_key.py +12 -12
  685. cirq/value/measurement_key_test.py +2 -0
  686. cirq/value/periodic_value.py +4 -4
  687. cirq/value/periodic_value_test.py +11 -7
  688. cirq/value/probability.py +3 -1
  689. cirq/value/probability_test.py +4 -2
  690. cirq/value/product_state.py +15 -13
  691. cirq/value/product_state_test.py +4 -1
  692. cirq/value/random_state.py +2 -0
  693. cirq/value/random_state_test.py +5 -3
  694. cirq/value/timestamp.py +11 -7
  695. cirq/value/timestamp_test.py +14 -12
  696. cirq/value/type_alias.py +4 -4
  697. cirq/value/value_equality_attr.py +8 -9
  698. cirq/value/value_equality_attr_test.py +14 -11
  699. cirq/vis/density_matrix.py +3 -3
  700. cirq/vis/density_matrix_test.py +20 -17
  701. cirq/vis/heatmap.py +24 -37
  702. cirq/vis/heatmap_test.py +3 -0
  703. cirq/vis/histogram.py +9 -6
  704. cirq/vis/histogram_test.py +5 -2
  705. cirq/vis/state_histogram.py +10 -8
  706. cirq/vis/state_histogram_test.py +7 -5
  707. cirq/vis/vis_utils.py +4 -1
  708. cirq/vis/vis_utils_test.py +4 -1
  709. cirq/work/collector.py +12 -18
  710. cirq/work/collector_test.py +15 -10
  711. cirq/work/observable_grouping.py +6 -7
  712. cirq/work/observable_grouping_test.py +10 -9
  713. cirq/work/observable_measurement.py +47 -45
  714. cirq/work/observable_measurement_data.py +22 -17
  715. cirq/work/observable_measurement_data_test.py +4 -1
  716. cirq/work/observable_measurement_test.py +48 -29
  717. cirq/work/observable_readout_calibration.py +5 -2
  718. cirq/work/observable_readout_calibration_test.py +5 -2
  719. cirq/work/observable_settings.py +13 -22
  720. cirq/work/observable_settings_test.py +9 -7
  721. cirq/work/pauli_sum_collector.py +12 -10
  722. cirq/work/pauli_sum_collector_test.py +9 -9
  723. cirq/work/sampler.py +42 -43
  724. cirq/work/sampler_test.py +31 -24
  725. cirq/work/zeros_sampler.py +6 -4
  726. cirq/work/zeros_sampler_test.py +7 -5
  727. {cirq_core-1.5.0.dev20250409225226.dist-info → cirq_core-1.6.0.dist-info}/METADATA +7 -8
  728. cirq_core-1.6.0.dist-info/RECORD +1241 -0
  729. {cirq_core-1.5.0.dev20250409225226.dist-info → cirq_core-1.6.0.dist-info}/WHEEL +1 -1
  730. cirq_core-1.5.0.dev20250409225226.dist-info/RECORD +0 -1216
  731. {cirq_core-1.5.0.dev20250409225226.dist-info → cirq_core-1.6.0.dist-info}/licenses/LICENSE +0 -0
  732. {cirq_core-1.5.0.dev20250409225226.dist-info → cirq_core-1.6.0.dist-info}/top_level.txt +0 -0
@@ -12,6 +12,8 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ from __future__ import annotations
16
+
15
17
  import numpy as np
16
18
  import pytest
17
19
  import sympy
@@ -27,23 +29,23 @@ QFT2 = np.array([[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j
27
29
  @pytest.mark.parametrize(
28
30
  'eigen_gate_type', [cirq.CZPowGate, cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate]
29
31
  )
30
- def test_phase_insensitive_eigen_gates_consistent_protocols(eigen_gate_type):
32
+ def test_phase_insensitive_eigen_gates_consistent_protocols(eigen_gate_type) -> None:
31
33
  cirq.testing.assert_eigengate_implements_consistent_protocols(eigen_gate_type)
32
34
 
33
35
 
34
36
  @pytest.mark.parametrize('eigen_gate_type', [cirq.CNotPowGate, cirq.HPowGate])
35
- def test_phase_sensitive_eigen_gates_consistent_protocols(eigen_gate_type):
37
+ def test_phase_sensitive_eigen_gates_consistent_protocols(eigen_gate_type) -> None:
36
38
  cirq.testing.assert_eigengate_implements_consistent_protocols(eigen_gate_type)
37
39
 
38
40
 
39
- def test_cz_init():
41
+ def test_cz_init() -> None:
40
42
  assert cirq.CZPowGate(exponent=0.5).exponent == 0.5
41
43
  assert cirq.CZPowGate(exponent=5).exponent == 5
42
44
  assert (cirq.CZ**0.5).exponent == 0.5
43
45
 
44
46
 
45
47
  @pytest.mark.parametrize('theta,pi', [(0.4, np.pi), (sympy.Symbol("theta"), sympy.pi)])
46
- def test_transformations(theta, pi):
48
+ def test_transformations(theta, pi) -> None:
47
49
  initialRx = cirq.rx(theta)
48
50
  expectedPowx = cirq.X ** (theta / pi)
49
51
  receivedPowx = initialRx.with_canonical_global_phase()
@@ -64,19 +66,19 @@ def test_transformations(theta, pi):
64
66
  assert backToRz == initialRz
65
67
 
66
68
 
67
- def test_cz_str():
69
+ def test_cz_str() -> None:
68
70
  assert str(cirq.CZ) == 'CZ'
69
71
  assert str(cirq.CZ**0.5) == 'CZ**0.5'
70
72
  assert str(cirq.CZ**-0.25) == 'CZ**-0.25'
71
73
 
72
74
 
73
- def test_cz_repr():
75
+ def test_cz_repr() -> None:
74
76
  assert repr(cirq.CZ) == 'cirq.CZ'
75
77
  assert repr(cirq.CZ**0.5) == '(cirq.CZ**0.5)'
76
78
  assert repr(cirq.CZ**-0.25) == '(cirq.CZ**-0.25)'
77
79
 
78
80
 
79
- def test_cz_unitary():
81
+ def test_cz_unitary() -> None:
80
82
  assert np.allclose(
81
83
  cirq.unitary(cirq.CZ), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]])
82
84
  )
@@ -96,7 +98,7 @@ def test_cz_unitary():
96
98
  )
97
99
 
98
100
 
99
- def test_z_init():
101
+ def test_z_init() -> None:
100
102
  z = cirq.ZPowGate(exponent=5)
101
103
  assert z.exponent == 5
102
104
 
@@ -107,19 +109,19 @@ def test_z_init():
107
109
 
108
110
 
109
111
  @pytest.mark.parametrize(
110
- 'input_gate, specialized_output',
112
+ 'input_gate, specialized_output, base_gate',
111
113
  [
112
- (cirq.Z, cirq.CZ),
113
- (cirq.CZ, cirq.CCZ),
114
- (cirq.X, cirq.CX),
115
- (cirq.CX, cirq.CCX),
116
- (cirq.ZPowGate(exponent=0.5), cirq.CZPowGate(exponent=0.5)),
117
- (cirq.CZPowGate(exponent=0.5), cirq.CCZPowGate(exponent=0.5)),
118
- (cirq.XPowGate(exponent=0.5), cirq.CXPowGate(exponent=0.5)),
119
- (cirq.CXPowGate(exponent=0.5), cirq.CCXPowGate(exponent=0.5)),
114
+ (cirq.Z, cirq.CZ, cirq.Z),
115
+ (cirq.CZ, cirq.CCZ, cirq.Z),
116
+ (cirq.X, cirq.CX, cirq.X),
117
+ (cirq.CX, cirq.CCX, cirq.X),
118
+ (cirq.ZPowGate(exponent=0.5), cirq.CZPowGate(exponent=0.5), cirq.S),
119
+ (cirq.CZPowGate(exponent=0.5), cirq.CCZPowGate(exponent=0.5), cirq.S),
120
+ (cirq.XPowGate(exponent=0.5), cirq.CXPowGate(exponent=0.5), cirq.XPowGate(exponent=0.5)),
121
+ (cirq.CXPowGate(exponent=0.5), cirq.CCXPowGate(exponent=0.5), cirq.XPowGate(exponent=0.5)),
120
122
  ],
121
123
  )
122
- def test_specialized_control(input_gate, specialized_output):
124
+ def test_specialized_control(input_gate, specialized_output, base_gate) -> None:
123
125
  # Single qubit control on the input gate gives the specialized output
124
126
  assert input_gate.controlled() == specialized_output
125
127
  assert input_gate.controlled(num_controls=1) == specialized_output
@@ -149,20 +151,24 @@ def test_specialized_control(input_gate, specialized_output):
149
151
  )
150
152
 
151
153
  # When a control_value 1 qubit is not acting first, results in a regular
152
- # ControlledGate on the input gate instance.
154
+ # ControlledGate on the base gate instance, with any extra control layer
155
+ # of the input gate being absorbed into the ControlledGate.
156
+ absorbed = 0 if base_gate == input_gate else 1
157
+ absorbed_values = ((1,),) * absorbed
158
+ absorbed_shape = (2,) * absorbed
153
159
  assert input_gate.controlled(num_controls=1, control_qid_shape=(3,)) == cirq.ControlledGate(
154
- input_gate, num_controls=1, control_qid_shape=(3,)
160
+ base_gate, num_controls=1 + absorbed, control_qid_shape=(3,) + absorbed_shape
155
161
  )
156
162
  assert input_gate.controlled(control_values=((0,), (1,), (0,))) == cirq.ControlledGate(
157
- input_gate, num_controls=3, control_values=((0,), (1,), (0,))
163
+ base_gate, num_controls=3 + absorbed, control_values=((0,), (1,), (0,)) + absorbed_values
158
164
  )
159
165
  assert input_gate.controlled(control_qid_shape=(3, 2, 3)) == cirq.ControlledGate(
160
- input_gate, num_controls=3, control_qid_shape=(3, 2, 3)
166
+ base_gate, num_controls=3 + absorbed, control_qid_shape=(3, 2, 3) + absorbed_shape
161
167
  )
162
168
  assert input_gate.controlled(control_qid_shape=(3,)).controlled(
163
169
  control_qid_shape=(2,)
164
170
  ).controlled(control_qid_shape=(4,)) != cirq.ControlledGate(
165
- input_gate, num_controls=3, control_qid_shape=(3, 2, 4)
171
+ base_gate, num_controls=3 + absorbed, control_qid_shape=(3, 2, 4) + absorbed_shape
166
172
  )
167
173
 
168
174
 
@@ -175,7 +181,7 @@ def test_specialized_control(input_gate, specialized_output):
175
181
  (cirq.XPowGate(exponent=0.5), cirq.CCXPowGate(exponent=0.5)),
176
182
  ],
177
183
  )
178
- def test_specialized_control_two_step(input_gate, specialized_output):
184
+ def test_specialized_control_two_step(input_gate, specialized_output) -> None:
179
185
  # Two-qubit control on the input gate gives the specialized output
180
186
  assert input_gate.controlled().controlled() == specialized_output
181
187
  assert input_gate.controlled(num_controls=2) == specialized_output
@@ -197,7 +203,7 @@ def test_specialized_control_two_step(input_gate, specialized_output):
197
203
  (cirq.CXPowGate(global_shift=-0.5, exponent=0.5), cirq.CCXPowGate),
198
204
  ],
199
205
  )
200
- def test_no_specialized_control_for_global_shift_non_zero(gate, specialized_type):
206
+ def test_no_specialized_control_for_global_shift_non_zero(gate, specialized_type) -> None:
201
207
  assert not isinstance(gate.controlled(), specialized_type)
202
208
 
203
209
 
@@ -223,11 +229,11 @@ def test_no_specialized_control_for_global_shift_non_zero(gate, specialized_type
223
229
  ),
224
230
  ],
225
231
  )
226
- def test_global_phase_controlled_gate(gate, matrix):
232
+ def test_global_phase_controlled_gate(gate, matrix) -> None:
227
233
  np.testing.assert_equal(cirq.unitary(gate.controlled()), matrix)
228
234
 
229
235
 
230
- def test_rot_gates_eq():
236
+ def test_rot_gates_eq() -> None:
231
237
  eq = cirq.testing.EqualsTester()
232
238
  gates = [
233
239
  lambda p: cirq.CZ**p,
@@ -257,14 +263,14 @@ def test_rot_gates_eq():
257
263
  eq.add_equality_group(cirq.CZPowGate(), cirq.CZPowGate(exponent=1), cirq.CZ)
258
264
 
259
265
 
260
- def test_z_unitary():
266
+ def test_z_unitary() -> None:
261
267
  assert np.allclose(cirq.unitary(cirq.Z), np.array([[1, 0], [0, -1]]))
262
268
  assert np.allclose(cirq.unitary(cirq.Z**0.5), np.array([[1, 0], [0, 1j]]))
263
269
  assert np.allclose(cirq.unitary(cirq.Z**0), np.array([[1, 0], [0, 1]]))
264
270
  assert np.allclose(cirq.unitary(cirq.Z**-0.5), np.array([[1, 0], [0, -1j]]))
265
271
 
266
272
 
267
- def test_y_unitary():
273
+ def test_y_unitary() -> None:
268
274
  assert np.allclose(cirq.unitary(cirq.Y), np.array([[0, -1j], [1j, 0]]))
269
275
 
270
276
  assert np.allclose(
@@ -278,7 +284,7 @@ def test_y_unitary():
278
284
  )
279
285
 
280
286
 
281
- def test_x_unitary():
287
+ def test_x_unitary() -> None:
282
288
  assert np.allclose(cirq.unitary(cirq.X), np.array([[0, 1], [1, 0]]))
283
289
 
284
290
  assert np.allclose(
@@ -292,28 +298,28 @@ def test_x_unitary():
292
298
  )
293
299
 
294
300
 
295
- def test_h_unitary():
301
+ def test_h_unitary() -> None:
296
302
  sqrt = cirq.unitary(cirq.H**0.5)
297
303
  m = np.dot(sqrt, sqrt)
298
304
  assert np.allclose(m, cirq.unitary(cirq.H), atol=1e-8)
299
305
 
300
306
 
301
- def test_h_init():
307
+ def test_h_init() -> None:
302
308
  h = cirq.HPowGate(exponent=0.5)
303
309
  assert h.exponent == 0.5
304
310
 
305
311
 
306
- def test_h_str():
312
+ def test_h_str() -> None:
307
313
  assert str(cirq.H) == 'H'
308
314
  assert str(cirq.H**0.5) == 'H**0.5'
309
315
 
310
316
 
311
- def test_phase_exponent():
317
+ def test_phase_exponent() -> None:
312
318
  assert cirq.XPowGate(exponent=0.5).phase_exponent == 0.0
313
319
  assert cirq.YPowGate(exponent=0.5).phase_exponent == 0.5
314
320
 
315
321
 
316
- def test_x_act_on_tableau():
322
+ def test_x_act_on_tableau() -> None:
317
323
  with pytest.raises(TypeError, match="Failed to act"):
318
324
  cirq.act_on(cirq.X, ExampleSimulationState(), qubits=())
319
325
  original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
@@ -362,7 +368,7 @@ class MinusOnePhaseGate(cirq.testing.SingleQubitGate):
362
368
  return np.array([[-1, 0], [0, -1]])
363
369
 
364
370
 
365
- def test_y_act_on_tableau():
371
+ def test_y_act_on_tableau() -> None:
366
372
  with pytest.raises(TypeError, match="Failed to act"):
367
373
  cirq.act_on(cirq.Y, ExampleSimulationState(), qubits=())
368
374
  original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
@@ -400,7 +406,7 @@ def test_y_act_on_tableau():
400
406
  cirq.act_on(cirq.Y**foo, state, [cirq.LineQubit(1)])
401
407
 
402
408
 
403
- def test_z_h_act_on_tableau():
409
+ def test_z_h_act_on_tableau() -> None:
404
410
  with pytest.raises(TypeError, match="Failed to act"):
405
411
  cirq.act_on(cirq.Z, ExampleSimulationState(), qubits=())
406
412
  with pytest.raises(TypeError, match="Failed to act"):
@@ -453,7 +459,7 @@ def test_z_h_act_on_tableau():
453
459
  cirq.act_on(cirq.H**1.5, state, [cirq.LineQubit(1)])
454
460
 
455
461
 
456
- def test_cx_act_on_tableau():
462
+ def test_cx_act_on_tableau() -> None:
457
463
  with pytest.raises(TypeError, match="Failed to act"):
458
464
  cirq.act_on(cirq.CX, ExampleSimulationState(), qubits=())
459
465
  original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
@@ -497,7 +503,7 @@ def test_cx_act_on_tableau():
497
503
  cirq.act_on(cirq.CX**1.5, state, cirq.LineQubit.range(2))
498
504
 
499
505
 
500
- def test_cz_act_on_tableau():
506
+ def test_cz_act_on_tableau() -> None:
501
507
  with pytest.raises(TypeError, match="Failed to act"):
502
508
  cirq.act_on(cirq.CZ, ExampleSimulationState(), qubits=())
503
509
  original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
@@ -541,7 +547,7 @@ def test_cz_act_on_tableau():
541
547
  cirq.act_on(cirq.CZ**1.5, state, cirq.LineQubit.range(2))
542
548
 
543
549
 
544
- def test_cz_act_on_equivalent_to_h_cx_h_tableau():
550
+ def test_cz_act_on_equivalent_to_h_cx_h_tableau() -> None:
545
551
  state1 = cirq.CliffordTableauSimulationState(
546
552
  tableau=cirq.CliffordTableau(num_qubits=2),
547
553
  qubits=cirq.LineQubit.range(2),
@@ -601,7 +607,7 @@ foo = sympy.Symbol('foo')
601
607
  ([cirq.CZ, MinusOnePhaseGate()], 'Original'),
602
608
  ],
603
609
  )
604
- def test_act_on_ch_form(input_gate_sequence, outcome):
610
+ def test_act_on_ch_form(input_gate_sequence, outcome) -> None:
605
611
  original_state = cirq.StabilizerStateChForm(num_qubits=5, initial_state=31)
606
612
  num_qubits = cirq.num_qubits(input_gate_sequence[0])
607
613
  if num_qubits == 1:
@@ -663,13 +669,13 @@ def test_act_on_ch_form(input_gate_sequence, outcome):
663
669
  (cirq.CZ**0.5, False),
664
670
  ],
665
671
  )
666
- def test_act_on_consistency(input_gate, assert_implemented):
672
+ def test_act_on_consistency(input_gate, assert_implemented) -> None:
667
673
  cirq.testing.assert_all_implemented_act_on_effects_match_unitary(
668
674
  input_gate, assert_implemented, assert_implemented
669
675
  )
670
676
 
671
677
 
672
- def test_runtime_types_of_rot_gates():
678
+ def test_runtime_types_of_rot_gates() -> None:
673
679
  for gate_type in [
674
680
  lambda p: cirq.CZPowGate(exponent=p),
675
681
  lambda p: cirq.XPowGate(exponent=p),
@@ -687,7 +693,7 @@ def test_runtime_types_of_rot_gates():
687
693
  assert cirq.inverse(c) == gate_type(-0.5)
688
694
 
689
695
 
690
- def test_interchangeable_qubit_eq():
696
+ def test_interchangeable_qubit_eq() -> None:
691
697
  a = cirq.NamedQubit('a')
692
698
  b = cirq.NamedQubit('b')
693
699
  c = cirq.NamedQubit('c')
@@ -701,7 +707,7 @@ def test_interchangeable_qubit_eq():
701
707
  eq.add_equality_group(cirq.CNOT(a, c))
702
708
 
703
709
 
704
- def test_identity_multiplication():
710
+ def test_identity_multiplication() -> None:
705
711
  a, b, c = cirq.LineQubit.range(3)
706
712
  assert cirq.I(a) * cirq.CX(a, b) == cirq.CX(a, b)
707
713
  assert cirq.CX(a, b) * cirq.I(a) == cirq.CX(a, b)
@@ -714,7 +720,7 @@ def test_identity_multiplication():
714
720
  assert cirq.I(c) * cirq.T(b) ** 0.25 == cirq.T(b) ** 0.25
715
721
 
716
722
 
717
- def test_text_diagrams():
723
+ def test_text_diagrams() -> None:
718
724
  a = cirq.NamedQubit('a')
719
725
  b = cirq.NamedQubit('b')
720
726
  circuit = cirq.Circuit(
@@ -754,7 +760,7 @@ b: -----------------------------@---X---@---X^0.5---@-------------------I---@^t-
754
760
  )
755
761
 
756
762
 
757
- def test_cnot_unitary():
763
+ def test_cnot_unitary() -> None:
758
764
  np.testing.assert_almost_equal(
759
765
  cirq.unitary(cirq.CNOT**0.5),
760
766
  np.array(
@@ -768,13 +774,13 @@ def test_cnot_unitary():
768
774
  )
769
775
 
770
776
 
771
- def test_cnot_decompose():
777
+ def test_cnot_decompose() -> None:
772
778
  a = cirq.NamedQubit('a')
773
779
  b = cirq.NamedQubit('b')
774
780
  assert cirq.decompose_once(cirq.CNOT(a, b) ** sympy.Symbol('x')) is not None
775
781
 
776
782
 
777
- def test_repr():
783
+ def test_repr() -> None:
778
784
  assert repr(cirq.X) == 'cirq.X'
779
785
  assert repr(cirq.X**0.5) == '(cirq.X**0.5)'
780
786
 
@@ -808,7 +814,7 @@ def test_repr():
808
814
  assert repr(cirq.CZ**0.2) == '(cirq.CZ**0.2)'
809
815
 
810
816
 
811
- def test_str():
817
+ def test_str() -> None:
812
818
  assert str(cirq.X) == 'X'
813
819
  assert str(cirq.X**0.5) == 'X**0.5'
814
820
  assert str(cirq.rx(np.pi)) == 'Rx(π)'
@@ -844,7 +850,7 @@ def test_str():
844
850
  assert str(cirq.cphase(np.pi / 2)) == 'CZ**0.5'
845
851
 
846
852
 
847
- def test_rx_unitary():
853
+ def test_rx_unitary() -> None:
848
854
  s = np.sqrt(0.5)
849
855
  np.testing.assert_allclose(
850
856
  cirq.unitary(cirq.rx(np.pi / 2)), np.array([[s, -s * 1j], [-s * 1j, s]])
@@ -863,7 +869,7 @@ def test_rx_unitary():
863
869
  np.testing.assert_allclose(cirq.unitary(cirq.rx(-np.pi)), np.array([[0, 1j], [1j, 0]]))
864
870
 
865
871
 
866
- def test_ry_unitary():
872
+ def test_ry_unitary() -> None:
867
873
  s = np.sqrt(0.5)
868
874
  np.testing.assert_allclose(cirq.unitary(cirq.ry(np.pi / 2)), np.array([[s, -s], [s, s]]))
869
875
 
@@ -878,7 +884,7 @@ def test_ry_unitary():
878
884
  np.testing.assert_allclose(cirq.unitary(cirq.ry(-np.pi)), np.array([[0, 1], [-1, 0]]))
879
885
 
880
886
 
881
- def test_rz_unitary():
887
+ def test_rz_unitary() -> None:
882
888
  s = np.sqrt(0.5)
883
889
  np.testing.assert_allclose(
884
890
  cirq.unitary(cirq.rz(np.pi / 2)), np.array([[s - s * 1j, 0], [0, s + s * 1j]])
@@ -901,17 +907,17 @@ def test_rz_unitary():
901
907
  'angle_rads, expected_unitary',
902
908
  [(0, np.eye(4)), (1, np.diag([1, 1, 1, np.exp(1j)])), (np.pi / 2, np.diag([1, 1, 1, 1j]))],
903
909
  )
904
- def test_cphase_unitary(angle_rads, expected_unitary):
910
+ def test_cphase_unitary(angle_rads, expected_unitary) -> None:
905
911
  np.testing.assert_allclose(cirq.unitary(cirq.cphase(angle_rads)), expected_unitary)
906
912
 
907
913
 
908
- def test_parameterized_cphase():
914
+ def test_parameterized_cphase() -> None:
909
915
  assert cirq.cphase(sympy.pi) == cirq.CZ
910
916
  assert cirq.cphase(sympy.pi / 2) == cirq.CZ**0.5
911
917
 
912
918
 
913
919
  @pytest.mark.parametrize('gate', [cirq.X, cirq.Y, cirq.Z])
914
- def test_x_y_z_stabilizer(gate):
920
+ def test_x_y_z_stabilizer(gate) -> None:
915
921
  assert cirq.has_stabilizer_effect(gate)
916
922
  assert cirq.has_stabilizer_effect(gate**0.5)
917
923
  assert cirq.has_stabilizer_effect(gate**0)
@@ -922,7 +928,7 @@ def test_x_y_z_stabilizer(gate):
922
928
  assert not cirq.has_stabilizer_effect(gate**foo)
923
929
 
924
930
 
925
- def test_h_stabilizer():
931
+ def test_h_stabilizer() -> None:
926
932
  gate = cirq.H
927
933
  assert cirq.has_stabilizer_effect(gate)
928
934
  assert not cirq.has_stabilizer_effect(gate**0.5)
@@ -935,7 +941,7 @@ def test_h_stabilizer():
935
941
 
936
942
 
937
943
  @pytest.mark.parametrize('gate', [cirq.CX, cirq.CZ])
938
- def test_cx_cz_stabilizer(gate):
944
+ def test_cx_cz_stabilizer(gate) -> None:
939
945
  assert cirq.has_stabilizer_effect(gate)
940
946
  assert not cirq.has_stabilizer_effect(gate**0.5)
941
947
  assert cirq.has_stabilizer_effect(gate**0)
@@ -946,13 +952,13 @@ def test_cx_cz_stabilizer(gate):
946
952
  assert not cirq.has_stabilizer_effect(gate**foo)
947
953
 
948
954
 
949
- def test_phase_by_xy():
955
+ def test_phase_by_xy() -> None:
950
956
  assert cirq.phase_by(cirq.X, 0.25, 0) == cirq.Y
951
957
  assert cirq.phase_by(cirq.X**0.5, 0.25, 0) == cirq.Y**0.5
952
958
  assert cirq.phase_by(cirq.X**-0.5, 0.25, 0) == cirq.Y**-0.5
953
959
 
954
960
 
955
- def test_ixyz_circuit_diagram():
961
+ def test_ixyz_circuit_diagram() -> None:
956
962
  q = cirq.NamedQubit('q')
957
963
  ix = cirq.XPowGate(exponent=1, global_shift=0.5)
958
964
  iy = cirq.YPowGate(exponent=1, global_shift=0.5)
@@ -997,7 +1003,7 @@ q: ───Z───Z───Z───S───S───
997
1003
  (sympy.Symbol("theta"), sympy.Symbol("exp")),
998
1004
  ],
999
1005
  )
1000
- def test_rxyz_exponent(theta, exp):
1006
+ def test_rxyz_exponent(theta, exp) -> None:
1001
1007
  def resolve(gate):
1002
1008
  return cirq.resolve_parameters(gate, {'theta': np.pi / 4}, {'exp': 1 / 4})
1003
1009
 
@@ -1006,7 +1012,7 @@ def test_rxyz_exponent(theta, exp):
1006
1012
  assert resolve(cirq.Rz(rads=theta) ** exp) == resolve(cirq.Rz(rads=theta * exp))
1007
1013
 
1008
1014
 
1009
- def test_rxyz_circuit_diagram():
1015
+ def test_rxyz_circuit_diagram() -> None:
1010
1016
  q = cirq.NamedQubit('q')
1011
1017
 
1012
1018
  cirq.testing.assert_has_diagram(
@@ -1063,7 +1069,7 @@ q: ───Rz(π)───Rz(-π)───Rz(-π)───Rz(0.5π)───Rz(
1063
1069
  )
1064
1070
 
1065
1071
 
1066
- def test_trace_distance():
1072
+ def test_trace_distance() -> None:
1067
1073
  foo = sympy.Symbol('foo')
1068
1074
  sx = cirq.X**foo
1069
1075
  sy = cirq.Y**foo
@@ -1088,13 +1094,13 @@ def test_trace_distance():
1088
1094
  assert cirq.approx_eq(cirq.trace_distance_bound(cirq.CZ ** (1 / 9)), np.sin(np.pi / 18))
1089
1095
 
1090
1096
 
1091
- def test_commutes():
1097
+ def test_commutes() -> None:
1092
1098
  assert cirq.commutes(cirq.ZPowGate(exponent=sympy.Symbol('t')), cirq.Z)
1093
1099
  assert cirq.commutes(cirq.Z, cirq.Z(cirq.LineQubit(0)), default=None) is None
1094
1100
  assert cirq.commutes(cirq.Z**0.1, cirq.XPowGate(exponent=0))
1095
1101
 
1096
1102
 
1097
- def test_approx_eq():
1103
+ def test_approx_eq() -> None:
1098
1104
  assert cirq.approx_eq(cirq.Z**0.1, cirq.Z**0.2, atol=0.3)
1099
1105
  assert not cirq.approx_eq(cirq.Z**0.1, cirq.Z**0.2, atol=0.05)
1100
1106
  assert cirq.approx_eq(cirq.Y**0.1, cirq.Y**0.2, atol=0.3)
@@ -1103,15 +1109,15 @@ def test_approx_eq():
1103
1109
  assert not cirq.approx_eq(cirq.X**0.1, cirq.X**0.2, atol=0.05)
1104
1110
 
1105
1111
 
1106
- def test_xpow_dim_3():
1112
+ def test_xpow_dim_3() -> None:
1107
1113
  x = cirq.XPowGate(dimension=3)
1108
1114
  assert cirq.X != x
1109
1115
  # fmt: off
1110
- expected = [
1116
+ expected = np.asarray([
1111
1117
  [0, 0, 1],
1112
1118
  [1, 0, 0],
1113
1119
  [0, 1, 0],
1114
- ]
1120
+ ])
1115
1121
  # fmt: on
1116
1122
  assert np.allclose(cirq.unitary(x), expected)
1117
1123
 
@@ -1119,28 +1125,28 @@ def test_xpow_dim_3():
1119
1125
  circuit = cirq.Circuit([x(cirq.LineQid(0, 3)) ** 0.5] * 6)
1120
1126
  svs = [step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit)]
1121
1127
  # fmt: off
1122
- expected = [
1128
+ expected = np.asarray([
1123
1129
  [0.67, 0.67, 0.33],
1124
1130
  [0.0, 1.0, 0.0],
1125
1131
  [0.33, 0.67, 0.67],
1126
1132
  [0.0, 0.0, 1.0],
1127
1133
  [0.67, 0.33, 0.67],
1128
1134
  [1.0, 0.0, 0.0],
1129
- ]
1135
+ ])
1130
1136
  # fmt: on
1131
1137
  assert np.allclose(np.abs(svs), expected, atol=1e-2)
1132
1138
 
1133
1139
 
1134
- def test_xpow_dim_4():
1140
+ def test_xpow_dim_4() -> None:
1135
1141
  x = cirq.XPowGate(dimension=4)
1136
1142
  assert cirq.X != x
1137
1143
  # fmt: off
1138
- expected = [
1144
+ expected = np.asarray([
1139
1145
  [0, 0, 0, 1],
1140
1146
  [1, 0, 0, 0],
1141
1147
  [0, 1, 0, 0],
1142
1148
  [0, 0, 1, 0],
1143
- ]
1149
+ ])
1144
1150
  # fmt: on
1145
1151
  assert np.allclose(cirq.unitary(x), expected)
1146
1152
 
@@ -1148,7 +1154,7 @@ def test_xpow_dim_4():
1148
1154
  circuit = cirq.Circuit([x(cirq.LineQid(0, 4)) ** 0.5] * 8)
1149
1155
  svs = [step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit)]
1150
1156
  # fmt: off
1151
- expected = [
1157
+ expected = np.asarray([
1152
1158
  [0.65, 0.65, 0.27, 0.27],
1153
1159
  [0.0, 1.0, 0.0, 0.0],
1154
1160
  [0.27, 0.65, 0.65, 0.27],
@@ -1157,22 +1163,22 @@ def test_xpow_dim_4():
1157
1163
  [0.0, 0.0, 0.0, 1.0],
1158
1164
  [0.65, 0.27, 0.27, 0.65],
1159
1165
  [1.0, 0.0, 0.0, 0.0],
1160
- ]
1166
+ ])
1161
1167
  # fmt: on
1162
1168
  assert np.allclose(np.abs(svs), expected, atol=1e-2)
1163
1169
 
1164
1170
 
1165
- def test_zpow_dim_3():
1171
+ def test_zpow_dim_3() -> None:
1166
1172
  L = np.exp(2 * np.pi * 1j / 3)
1167
1173
  L2 = L**2
1168
1174
  z = cirq.ZPowGate(dimension=3)
1169
1175
  assert cirq.Z != z
1170
1176
  # fmt: off
1171
- expected = [
1177
+ expected = np.asarray([
1172
1178
  [1, 0, 0],
1173
1179
  [0, L, 0],
1174
1180
  [0, 0, L2],
1175
- ]
1181
+ ])
1176
1182
  # fmt: on
1177
1183
  assert np.allclose(cirq.unitary(z), expected)
1178
1184
 
@@ -1181,21 +1187,21 @@ def test_zpow_dim_3():
1181
1187
  svs = [
1182
1188
  step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=0)
1183
1189
  ]
1184
- expected = [[1, 0, 0]] * 6
1190
+ expected = np.asarray([[1, 0, 0]] * 6)
1185
1191
  assert np.allclose((svs), expected)
1186
1192
 
1187
1193
  svs = [
1188
1194
  step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=1)
1189
1195
  ]
1190
1196
  # fmt: off
1191
- expected = [
1197
+ expected = np.asarray([
1192
1198
  [0, L**0.5, 0],
1193
1199
  [0, L**1.0, 0],
1194
1200
  [0, L**1.5, 0],
1195
1201
  [0, L**2.0, 0],
1196
1202
  [0, L**2.5, 0],
1197
1203
  [0, 1, 0],
1198
- ]
1204
+ ])
1199
1205
  # fmt: on
1200
1206
  assert np.allclose((svs), expected)
1201
1207
 
@@ -1203,28 +1209,28 @@ def test_zpow_dim_3():
1203
1209
  step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=2)
1204
1210
  ]
1205
1211
  # fmt: off
1206
- expected = [
1212
+ expected = np.asarray([
1207
1213
  [0, 0, L],
1208
1214
  [0, 0, L2],
1209
1215
  [0, 0, 1],
1210
1216
  [0, 0, L],
1211
1217
  [0, 0, L2],
1212
1218
  [0, 0, 1],
1213
- ]
1219
+ ])
1214
1220
  # fmt: on
1215
1221
  assert np.allclose((svs), expected)
1216
1222
 
1217
1223
 
1218
- def test_zpow_dim_4():
1224
+ def test_zpow_dim_4() -> None:
1219
1225
  z = cirq.ZPowGate(dimension=4)
1220
1226
  assert cirq.Z != z
1221
1227
  # fmt: off
1222
- expected = [
1228
+ expected = np.asarray([
1223
1229
  [1, 0, 0, 0],
1224
1230
  [0, 1j, 0, 0],
1225
1231
  [0, 0, -1, 0],
1226
1232
  [0, 0, 0, -1j],
1227
- ]
1233
+ ])
1228
1234
  # fmt: on
1229
1235
  assert np.allclose(cirq.unitary(z), expected)
1230
1236
 
@@ -1233,14 +1239,14 @@ def test_zpow_dim_4():
1233
1239
  svs = [
1234
1240
  step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=0)
1235
1241
  ]
1236
- expected = [[1, 0, 0, 0]] * 8
1242
+ expected = np.asarray([[1, 0, 0, 0]] * 8)
1237
1243
  assert np.allclose((svs), expected)
1238
1244
 
1239
1245
  svs = [
1240
1246
  step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=1)
1241
1247
  ]
1242
1248
  # fmt: off
1243
- expected = [
1249
+ expected = np.asarray([
1244
1250
  [0, 1j**0.5, 0, 0],
1245
1251
  [0, 1j**1.0, 0, 0],
1246
1252
  [0, 1j**1.5, 0, 0],
@@ -1249,7 +1255,7 @@ def test_zpow_dim_4():
1249
1255
  [0, 1j**3.0, 0, 0],
1250
1256
  [0, 1j**3.5, 0, 0],
1251
1257
  [0, 1, 0, 0],
1252
- ]
1258
+ ])
1253
1259
  # fmt: on
1254
1260
  assert np.allclose(svs, expected)
1255
1261
 
@@ -1257,7 +1263,7 @@ def test_zpow_dim_4():
1257
1263
  step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=2)
1258
1264
  ]
1259
1265
  # fmt: off
1260
- expected = [
1266
+ expected = np.asarray([
1261
1267
  [0, 0, 1j, 0],
1262
1268
  [0, 0, -1, 0],
1263
1269
  [0, 0, -1j, 0],
@@ -1266,7 +1272,7 @@ def test_zpow_dim_4():
1266
1272
  [0, 0, -1, 0],
1267
1273
  [0, 0, -1j, 0],
1268
1274
  [0, 0, 1, 0],
1269
- ]
1275
+ ])
1270
1276
  # fmt: on
1271
1277
  assert np.allclose(svs, expected)
1272
1278
 
@@ -1274,7 +1280,7 @@ def test_zpow_dim_4():
1274
1280
  step.state_vector(copy=True) for step in sim.simulate_moment_steps(circuit, initial_state=3)
1275
1281
  ]
1276
1282
  # fmt: off
1277
- expected = [
1283
+ expected = np.asarray([
1278
1284
  [0, 0, 0, 1j**1.5],
1279
1285
  [0, 0, 0, 1j**3],
1280
1286
  [0, 0, 0, 1j**0.5],
@@ -1283,12 +1289,12 @@ def test_zpow_dim_4():
1283
1289
  [0, 0, 0, 1j**1],
1284
1290
  [0, 0, 0, 1j**2.5],
1285
1291
  [0, 0, 0, 1],
1286
- ]
1292
+ ])
1287
1293
  # fmt: on
1288
1294
  assert np.allclose(svs, expected)
1289
1295
 
1290
1296
 
1291
- def test_wrong_dims():
1297
+ def test_wrong_dims() -> None:
1292
1298
  x3 = cirq.XPowGate(dimension=3)
1293
1299
  with pytest.raises(ValueError, match='Wrong shape'):
1294
1300
  _ = x3.on(cirq.LineQubit(0))
@@ -1310,9 +1316,43 @@ def test_wrong_dims():
1310
1316
 
1311
1317
  @pytest.mark.parametrize('gate_type', [cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate])
1312
1318
  @pytest.mark.parametrize('exponent', [sympy.Symbol('s'), sympy.Symbol('s') * 2])
1313
- def test_parameterized_pauli_expansion(gate_type, exponent):
1319
+ def test_parameterized_pauli_expansion(gate_type, exponent) -> None:
1314
1320
  gate = gate_type(exponent=exponent)
1315
1321
  pauli = cirq.pauli_expansion(gate)
1316
1322
  gate_resolved = cirq.resolve_parameters(gate, {'s': 0.5})
1317
1323
  pauli_resolved = cirq.resolve_parameters(pauli, {'s': 0.5})
1318
1324
  assert cirq.approx_eq(pauli_resolved, cirq.pauli_expansion(gate_resolved))
1325
+
1326
+
1327
+ @pytest.mark.parametrize('gate_type', [cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate, cirq.CZPowGate])
1328
+ @pytest.mark.parametrize('exponent', [0, 0.5, 2, 3, -0.5, -2, -3, sympy.Symbol('s')])
1329
+ def test_decompose_with_extracted_phases(gate_type: type, exponent: cirq.TParamVal) -> None:
1330
+ context = cirq.DecompositionContext(cirq.SimpleQubitManager(), extract_global_phases=True)
1331
+ test_shift = 2 / 3 # Interesting because e.g. X(shift=2/3) ** 3 == X with no phase
1332
+ gate = gate_type(exponent=exponent, global_shift=test_shift)
1333
+ op = gate.on(*cirq.LineQubit.range(cirq.num_qubits(gate)))
1334
+ decomposed = cirq.decompose(op, context=context)
1335
+
1336
+ # The first gate should be the original gate, but with shift removed.
1337
+ gate0 = decomposed[0].gate
1338
+ assert isinstance(gate0, gate_type)
1339
+ assert isinstance(gate0, cirq.EigenGate)
1340
+ assert gate0.global_shift == 0
1341
+ assert gate0.exponent == exponent
1342
+ if exponent % 3 == 0:
1343
+ # Since test_shift == 2/3, gate**3 nullifies the phase, leaving only the unphased gate.
1344
+ assert len(decomposed) == 1
1345
+ else:
1346
+ # Other exponents emit a global phase gate to compensate.
1347
+ assert len(decomposed) == 2
1348
+ gate1 = decomposed[1].gate
1349
+ assert isinstance(gate1, cirq.GlobalPhaseGate)
1350
+ assert gate1.coefficient == 1j ** (2 * exponent * test_shift)
1351
+
1352
+ # Sanity check that the decomposition is equivalent to the original.
1353
+ decomposed_circuit = cirq.Circuit(decomposed)
1354
+ if cirq.is_parameterized(exponent):
1355
+ resolver = {'s': -1.234} # arbitrary
1356
+ op = cirq.resolve_parameters(op, resolver)
1357
+ decomposed_circuit = cirq.resolve_parameters(decomposed_circuit, resolver)
1358
+ np.testing.assert_allclose(cirq.unitary(op), cirq.unitary(decomposed_circuit), atol=1e-10)