cirq-core 1.7.0.dev20250924231107__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 (392) 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 +7 -13
  10. cirq/circuits/circuit_operation.py +2 -1
  11. cirq/circuits/circuit_test.py +13 -12
  12. cirq/circuits/frozen_circuit.py +3 -2
  13. cirq/circuits/moment.py +3 -15
  14. cirq/circuits/optimization_pass.py +2 -1
  15. cirq/circuits/qasm_output.py +39 -10
  16. cirq/circuits/qasm_output_test.py +51 -2
  17. cirq/circuits/text_diagram_drawer.py +2 -1
  18. cirq/contrib/acquaintance/bipartite.py +2 -1
  19. cirq/contrib/acquaintance/devices.py +1 -1
  20. cirq/contrib/acquaintance/executor.py +4 -5
  21. cirq/contrib/acquaintance/executor_test.py +2 -1
  22. cirq/contrib/acquaintance/gates.py +2 -1
  23. cirq/contrib/acquaintance/gates_test.py +1 -1
  24. cirq/contrib/acquaintance/inspection_utils.py +2 -1
  25. cirq/contrib/acquaintance/mutation_utils.py +2 -1
  26. cirq/contrib/acquaintance/optimizers.py +2 -1
  27. cirq/contrib/acquaintance/permutation.py +2 -1
  28. cirq/contrib/acquaintance/permutation_test.py +1 -1
  29. cirq/contrib/acquaintance/shift.py +2 -1
  30. cirq/contrib/acquaintance/shift_swap_network.py +2 -1
  31. cirq/contrib/acquaintance/strategies/complete.py +3 -2
  32. cirq/contrib/acquaintance/strategies/cubic.py +2 -1
  33. cirq/contrib/acquaintance/strategies/quartic_paired.py +2 -1
  34. cirq/contrib/acquaintance/strategies/quartic_paired_test.py +1 -1
  35. cirq/contrib/acquaintance/testing.py +2 -1
  36. cirq/contrib/acquaintance/topological_sort.py +2 -1
  37. cirq/contrib/bayesian_network/bayesian_network_gate.py +3 -2
  38. cirq/contrib/circuitdag/circuit_dag.py +4 -2
  39. cirq/contrib/custom_simulators/custom_state_simulator.py +2 -1
  40. cirq/contrib/custom_simulators/custom_state_simulator_test.py +1 -1
  41. cirq/contrib/graph_device/graph_device.py +2 -1
  42. cirq/contrib/graph_device/graph_device_test.py +2 -1
  43. cirq/contrib/graph_device/hypergraph.py +2 -1
  44. cirq/contrib/graph_device/uniform_graph_device.py +2 -1
  45. cirq/contrib/json.py +14 -2
  46. cirq/contrib/json_test_data/BayesianNetworkGate.json +10 -0
  47. cirq/contrib/json_test_data/BayesianNetworkGate.repr +3 -0
  48. cirq/contrib/json_test_data/QuantumVolumeResult.json +169 -0
  49. cirq/contrib/json_test_data/QuantumVolumeResult.repr +22 -0
  50. cirq/contrib/json_test_data/SwapPermutationGate.json +3 -0
  51. cirq/contrib/json_test_data/SwapPermutationGate.repr +1 -0
  52. cirq/contrib/json_test_data/spec.py +0 -2
  53. cirq/contrib/noise_models/noise_models.py +2 -1
  54. cirq/contrib/paulistring/clifford_optimize.py +20 -2
  55. cirq/contrib/paulistring/optimize.py +1 -1
  56. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +146 -35
  57. cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +74 -171
  58. cirq/contrib/paulistring/recombine.py +5 -2
  59. cirq/contrib/paulistring/separate.py +1 -1
  60. cirq/contrib/qasm_import/_parser.py +2 -1
  61. cirq/contrib/qasm_import/_parser_test.py +3 -3
  62. cirq/contrib/quantikz/__init__.py +21 -0
  63. cirq/contrib/quantikz/circuit_to_latex_quantikz.py +680 -0
  64. cirq/contrib/quantikz/circuit_to_latex_quantikz_test.py +253 -0
  65. cirq/contrib/quantikz/circuit_to_latex_render.py +424 -0
  66. cirq/contrib/quantikz/circuit_to_latex_render_test.py +44 -0
  67. cirq/contrib/quantum_volume/quantum_volume.py +2 -1
  68. cirq/contrib/quimb/density_matrix.py +1 -1
  69. cirq/contrib/quimb/grid_circuits.py +2 -1
  70. cirq/contrib/quimb/grid_circuits_test.py +1 -1
  71. cirq/contrib/quimb/mps_simulator.py +4 -3
  72. cirq/contrib/quimb/state_vector.py +2 -1
  73. cirq/contrib/quirk/export_to_quirk.py +2 -1
  74. cirq/contrib/quirk/linearize_circuit.py +1 -1
  75. cirq/contrib/quirk/quirk_gate.py +2 -1
  76. cirq/contrib/routing/device.py +1 -1
  77. cirq/contrib/routing/greedy.py +2 -1
  78. cirq/contrib/routing/initialization.py +2 -1
  79. cirq/contrib/routing/router.py +2 -1
  80. cirq/contrib/routing/swap_network.py +2 -1
  81. cirq/contrib/routing/utils.py +2 -1
  82. cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +7 -5
  83. cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking_test.py +6 -6
  84. cirq/devices/device.py +2 -1
  85. cirq/devices/grid_device_metadata.py +2 -1
  86. cirq/devices/grid_qubit.py +7 -6
  87. cirq/devices/insertion_noise_model.py +2 -1
  88. cirq/devices/line_qubit.py +2 -1
  89. cirq/devices/named_topologies.py +2 -1
  90. cirq/devices/noise_model.py +2 -1
  91. cirq/devices/noise_model_test.py +1 -1
  92. cirq/devices/noise_properties.py +2 -1
  93. cirq/devices/superconducting_qubits_noise_properties_test.py +2 -1
  94. cirq/devices/thermal_noise_model.py +2 -1
  95. cirq/experiments/__init__.py +2 -0
  96. cirq/experiments/benchmarking/parallel_xeb.py +2 -1
  97. cirq/experiments/benchmarking/parallel_xeb_test.py +1 -1
  98. cirq/experiments/fidelity_estimation.py +2 -1
  99. cirq/experiments/fidelity_estimation_test.py +1 -1
  100. cirq/experiments/ghz_2d.py +150 -0
  101. cirq/experiments/ghz_2d_test.py +155 -0
  102. cirq/experiments/n_qubit_tomography.py +2 -1
  103. cirq/experiments/n_qubit_tomography_test.py +1 -1
  104. cirq/experiments/purity_estimation.py +1 -1
  105. cirq/experiments/qubit_characterizations.py +2 -1
  106. cirq/experiments/random_quantum_circuit_generation.py +2 -1
  107. cirq/experiments/random_quantum_circuit_generation_test.py +2 -1
  108. cirq/experiments/readout_confusion_matrix.py +2 -1
  109. cirq/experiments/readout_confusion_matrix_test.py +1 -1
  110. cirq/experiments/single_qubit_readout_calibration.py +2 -1
  111. cirq/experiments/single_qubit_readout_calibration_test.py +1 -1
  112. cirq/experiments/t1_decay_experiment.py +2 -1
  113. cirq/experiments/two_qubit_xeb.py +2 -1
  114. cirq/experiments/two_qubit_xeb_test.py +1 -1
  115. cirq/experiments/xeb_fitting.py +2 -1
  116. cirq/experiments/xeb_fitting_test.py +1 -1
  117. cirq/experiments/xeb_sampling.py +5 -3
  118. cirq/experiments/xeb_sampling_test.py +1 -1
  119. cirq/experiments/xeb_simulation.py +2 -1
  120. cirq/experiments/xeb_simulation_test.py +2 -1
  121. cirq/experiments/z_phase_calibration.py +2 -1
  122. cirq/experiments/z_phase_calibration_test.py +18 -3
  123. cirq/interop/quirk/cells/__init__.py +1 -2
  124. cirq/interop/quirk/cells/all_cells.py +2 -1
  125. cirq/interop/quirk/cells/arithmetic_cells.py +2 -1
  126. cirq/interop/quirk/cells/cell.py +2 -1
  127. cirq/interop/quirk/cells/composite_cell.py +2 -1
  128. cirq/interop/quirk/cells/composite_cell_test.py +1 -1
  129. cirq/interop/quirk/cells/control_cells.py +2 -1
  130. cirq/interop/quirk/cells/frequency_space_cells.py +1 -1
  131. cirq/interop/quirk/cells/ignored_cells.py +1 -1
  132. cirq/interop/quirk/cells/input_cells.py +2 -1
  133. cirq/interop/quirk/cells/input_rotation_cells.py +2 -1
  134. cirq/interop/quirk/cells/measurement_cells.py +2 -1
  135. cirq/interop/quirk/cells/parse.py +2 -11
  136. cirq/interop/quirk/cells/qubit_permutation_cells.py +2 -1
  137. cirq/interop/quirk/cells/scalar_cells.py +2 -1
  138. cirq/interop/quirk/cells/single_qubit_rotation_cells.py +2 -1
  139. cirq/interop/quirk/cells/swap_cell.py +2 -1
  140. cirq/interop/quirk/cells/unsupported_cells.py +1 -1
  141. cirq/interop/quirk/url_to_circuit.py +2 -1
  142. cirq/json_resolver_cache.py +0 -2
  143. cirq/linalg/decompositions.py +6 -2
  144. cirq/linalg/decompositions_test.py +1 -0
  145. cirq/linalg/diagonalize.py +1 -1
  146. cirq/linalg/predicates.py +2 -1
  147. cirq/linalg/tolerance.py +2 -1
  148. cirq/linalg/transformations.py +2 -1
  149. cirq/ops/arithmetic_operation.py +2 -1
  150. cirq/ops/arithmetic_operation_test.py +1 -1
  151. cirq/ops/boolean_hamiltonian.py +4 -3
  152. cirq/ops/classically_controlled_operation.py +3 -2
  153. cirq/ops/clifford_gate.py +2 -1
  154. cirq/ops/clifford_gate_test.py +1 -2
  155. cirq/ops/common_channels.py +2 -1
  156. cirq/ops/common_gates.py +2 -1
  157. cirq/ops/control_values.py +2 -1
  158. cirq/ops/controlled_gate.py +3 -2
  159. cirq/ops/controlled_gate_test.py +2 -1
  160. cirq/ops/controlled_operation.py +3 -2
  161. cirq/ops/controlled_operation_test.py +2 -1
  162. cirq/ops/dense_pauli_string.py +3 -13
  163. cirq/ops/diagonal_gate.py +3 -2
  164. cirq/ops/eigen_gate.py +9 -7
  165. cirq/ops/fourier_transform.py +3 -2
  166. cirq/ops/fourier_transform_test.py +2 -4
  167. cirq/ops/fsim_gate.py +3 -2
  168. cirq/ops/gate_operation.py +8 -12
  169. cirq/ops/gateset.py +22 -2
  170. cirq/ops/global_phase_op.py +3 -2
  171. cirq/ops/greedy_qubit_manager.py +2 -1
  172. cirq/ops/identity.py +2 -1
  173. cirq/ops/kraus_channel.py +2 -1
  174. cirq/ops/linear_combinations.py +8 -3
  175. cirq/ops/linear_combinations_test.py +23 -1
  176. cirq/ops/matrix_gates.py +2 -1
  177. cirq/ops/measure_util.py +2 -1
  178. cirq/ops/measurement_gate.py +2 -1
  179. cirq/ops/mixed_unitary_channel.py +2 -1
  180. cirq/ops/named_qubit.py +2 -2
  181. cirq/ops/op_tree.py +2 -1
  182. cirq/ops/parallel_gate.py +3 -2
  183. cirq/ops/parity_gates.py +2 -1
  184. cirq/ops/pauli_interaction_gate.py +2 -1
  185. cirq/ops/pauli_measurement_gate.py +2 -1
  186. cirq/ops/pauli_string.py +6 -12
  187. cirq/ops/pauli_string_phasor.py +3 -2
  188. cirq/ops/pauli_string_raw_types.py +2 -1
  189. cirq/ops/pauli_string_test.py +2 -2
  190. cirq/ops/pauli_sum_exponential.py +2 -1
  191. cirq/ops/permutation_gate.py +2 -1
  192. cirq/ops/phased_iswap_gate.py +3 -2
  193. cirq/ops/phased_x_gate.py +5 -4
  194. cirq/ops/phased_x_z_gate.py +12 -5
  195. cirq/ops/projector.py +2 -1
  196. cirq/ops/qubit_manager.py +2 -1
  197. cirq/ops/qubit_order.py +2 -1
  198. cirq/ops/qubit_order_or_list.py +1 -1
  199. cirq/ops/random_gate_channel.py +3 -2
  200. cirq/ops/raw_types.py +7 -15
  201. cirq/ops/raw_types_test.py +4 -3
  202. cirq/ops/state_preparation_channel.py +2 -1
  203. cirq/ops/three_qubit_gates.py +3 -2
  204. cirq/ops/two_qubit_diagonal_gate.py +3 -2
  205. cirq/ops/uniform_superposition_gate.py +2 -1
  206. cirq/ops/wait_gate.py +10 -4
  207. cirq/protocols/act_on_protocol.py +2 -1
  208. cirq/protocols/act_on_protocol_test.py +2 -1
  209. cirq/protocols/apply_channel_protocol.py +2 -1
  210. cirq/protocols/apply_mixture_protocol.py +2 -1
  211. cirq/protocols/apply_mixture_protocol_test.py +2 -1
  212. cirq/protocols/apply_unitary_protocol.py +2 -1
  213. cirq/protocols/apply_unitary_protocol_test.py +2 -0
  214. cirq/protocols/approximate_equality_protocol.py +2 -1
  215. cirq/protocols/circuit_diagram_info_protocol.py +2 -1
  216. cirq/protocols/decompose_protocol.py +2 -12
  217. cirq/protocols/has_stabilizer_effect_protocol.py +1 -1
  218. cirq/protocols/hash_from_pickle_test.py +2 -2
  219. cirq/protocols/inverse_protocol.py +2 -1
  220. cirq/protocols/json_serialization.py +2 -1
  221. cirq/protocols/kraus_protocol.py +4 -3
  222. cirq/protocols/kraus_protocol_test.py +2 -2
  223. cirq/protocols/measurement_key_protocol.py +2 -1
  224. cirq/protocols/mixture_protocol.py +2 -1
  225. cirq/protocols/pow_protocol.py +2 -1
  226. cirq/protocols/qasm.py +2 -1
  227. cirq/protocols/qid_shape_protocol.py +2 -1
  228. cirq/protocols/resolve_parameters.py +16 -14
  229. cirq/protocols/trace_distance_bound.py +2 -1
  230. cirq/protocols/unitary_protocol.py +21 -21
  231. cirq/qis/channels.py +1 -1
  232. cirq/qis/channels_test.py +1 -1
  233. cirq/qis/clifford_tableau.py +2 -1
  234. cirq/qis/entropy.py +2 -2
  235. cirq/qis/quantum_state_representation.py +2 -1
  236. cirq/qis/states.py +7 -2
  237. cirq/sim/classical_simulator.py +2 -1
  238. cirq/sim/clifford/clifford_simulator.py +2 -1
  239. cirq/sim/clifford/clifford_simulator_test.py +1 -1
  240. cirq/sim/clifford/clifford_tableau_simulation_state.py +2 -1
  241. cirq/sim/clifford/stabilizer_ch_form_simulation_state.py +2 -1
  242. cirq/sim/clifford/stabilizer_sampler.py +1 -1
  243. cirq/sim/clifford/stabilizer_simulation_state.py +2 -1
  244. cirq/sim/clifford/stabilizer_state_ch_form.py +3 -2
  245. cirq/sim/clifford/stabilizer_state_ch_form_test.py +0 -1
  246. cirq/sim/density_matrix_simulation_state.py +2 -1
  247. cirq/sim/density_matrix_simulator.py +2 -1
  248. cirq/sim/density_matrix_utils.py +2 -1
  249. cirq/sim/mux.py +2 -1
  250. cirq/sim/mux_test.py +0 -1
  251. cirq/sim/simulation_product_state.py +2 -1
  252. cirq/sim/simulation_product_state_test.py +2 -1
  253. cirq/sim/simulation_state.py +2 -1
  254. cirq/sim/simulation_state_base.py +2 -1
  255. cirq/sim/simulation_state_test.py +2 -1
  256. cirq/sim/simulation_utils.py +2 -1
  257. cirq/sim/simulator.py +2 -1
  258. cirq/sim/simulator_base.py +2 -1
  259. cirq/sim/simulator_base_test.py +2 -1
  260. cirq/sim/simulator_test.py +2 -1
  261. cirq/sim/sparse_simulator.py +2 -1
  262. cirq/sim/state_vector.py +2 -1
  263. cirq/sim/state_vector_simulation_state.py +2 -1
  264. cirq/sim/state_vector_simulator.py +2 -1
  265. cirq/sim/state_vector_test.py +1 -2
  266. cirq/study/__init__.py +1 -0
  267. cirq/study/flatten_expressions.py +2 -1
  268. cirq/study/resolver.py +31 -18
  269. cirq/study/resolver_test.py +1 -1
  270. cirq/study/result.py +2 -1
  271. cirq/study/sweepable.py +2 -1
  272. cirq/study/sweeps.py +26 -1
  273. cirq/study/sweeps_test.py +24 -0
  274. cirq/testing/_compat_test_data/__init__.py +3 -3
  275. cirq/testing/circuit_compare.py +2 -1
  276. cirq/testing/consistent_act_on_test.py +1 -1
  277. cirq/testing/consistent_controlled_gate_op.py +1 -1
  278. cirq/testing/consistent_controlled_gate_op_test.py +2 -1
  279. cirq/testing/consistent_protocols.py +2 -1
  280. cirq/testing/consistent_protocols_test.py +3 -3
  281. cirq/testing/consistent_qasm.py +2 -1
  282. cirq/testing/consistent_resolve_parameters.py +1 -1
  283. cirq/testing/consistent_unitary.py +1 -1
  284. cirq/testing/consistent_unitary_test.py +1 -1
  285. cirq/testing/deprecation.py +1 -1
  286. cirq/testing/devices.py +3 -2
  287. cirq/testing/equals_tester.py +4 -3
  288. cirq/testing/equivalent_basis_map.py +4 -2
  289. cirq/testing/json.py +3 -2
  290. cirq/testing/logs.py +1 -1
  291. cirq/testing/op_tree.py +1 -1
  292. cirq/testing/order_tester.py +2 -2
  293. cirq/testing/pytest_utils.py +2 -1
  294. cirq/testing/random_circuit.py +2 -1
  295. cirq/testing/random_circuit_test.py +2 -1
  296. cirq/testing/repr_pretty_tester.py +3 -3
  297. cirq/transformers/__init__.py +1 -0
  298. cirq/transformers/_connected_component.py +231 -0
  299. cirq/transformers/_connected_component_test.py +200 -0
  300. cirq/transformers/align_test.py +13 -13
  301. cirq/transformers/analytical_decompositions/clifford_decomposition.py +8 -7
  302. cirq/transformers/analytical_decompositions/clifford_decomposition_test.py +5 -5
  303. cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +11 -10
  304. cirq/transformers/analytical_decompositions/controlled_gate_decomposition_test.py +6 -6
  305. cirq/transformers/analytical_decompositions/cphase_to_fsim.py +3 -2
  306. cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +11 -10
  307. cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +8 -7
  308. cirq/transformers/analytical_decompositions/quantum_shannon_decomposition_test.py +17 -20
  309. cirq/transformers/analytical_decompositions/single_qubit_decompositions_test.py +33 -27
  310. cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry_test.py +1 -1
  311. cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +1 -1
  312. cirq/transformers/analytical_decompositions/two_qubit_state_preparation_test.py +12 -11
  313. cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +5 -2
  314. cirq/transformers/analytical_decompositions/two_qubit_to_cz_test.py +3 -3
  315. cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +2 -1
  316. cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +2 -1
  317. cirq/transformers/analytical_decompositions/two_qubit_to_ms_test.py +2 -2
  318. cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +2 -1
  319. cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap_test.py +32 -30
  320. cirq/transformers/drop_negligible_operations_test.py +7 -7
  321. cirq/transformers/dynamical_decoupling.py +185 -112
  322. cirq/transformers/dynamical_decoupling_test.py +195 -201
  323. cirq/transformers/eject_phased_paulis.py +2 -1
  324. cirq/transformers/eject_phased_paulis_test.py +3 -2
  325. cirq/transformers/eject_z.py +4 -3
  326. cirq/transformers/eject_z_test.py +23 -25
  327. cirq/transformers/expand_composite.py +3 -2
  328. cirq/transformers/expand_composite_test.py +14 -14
  329. cirq/transformers/gauge_compiling/__init__.py +8 -0
  330. cirq/transformers/gauge_compiling/gauge_compiling.py +3 -2
  331. cirq/transformers/gauge_compiling/gauge_compiling_test.py +14 -12
  332. cirq/transformers/gauge_compiling/gauge_compiling_test_utils.py +3 -3
  333. cirq/transformers/gauge_compiling/idle_moments_gauge.py +5 -2
  334. cirq/transformers/gauge_compiling/multi_moment_cphase_gauge.py +242 -0
  335. cirq/transformers/gauge_compiling/multi_moment_cphase_gauge_test.py +243 -0
  336. cirq/transformers/gauge_compiling/multi_moment_gauge_compiling.py +151 -0
  337. cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +2 -1
  338. cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils.py +1 -1
  339. cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils_test.py +6 -6
  340. cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +2 -1
  341. cirq/transformers/measurement_transformers.py +2 -1
  342. cirq/transformers/measurement_transformers_test.py +45 -39
  343. cirq/transformers/merge_k_qubit_gates.py +2 -1
  344. cirq/transformers/merge_k_qubit_gates_test.py +1 -1
  345. cirq/transformers/merge_single_qubit_gates.py +2 -1
  346. cirq/transformers/merge_single_qubit_gates_test.py +22 -22
  347. cirq/transformers/noise_adding_test.py +2 -2
  348. cirq/transformers/optimize_for_target_gateset.py +2 -1
  349. cirq/transformers/optimize_for_target_gateset_test.py +11 -9
  350. cirq/transformers/qubit_management_transformers_test.py +6 -2
  351. cirq/transformers/routing/mapping_manager.py +2 -1
  352. cirq/transformers/routing/route_circuit_cqc.py +2 -1
  353. cirq/transformers/stratify.py +2 -1
  354. cirq/transformers/symbolize.py +2 -1
  355. cirq/transformers/tag_transformers.py +2 -1
  356. cirq/transformers/target_gatesets/compilation_target_gateset.py +2 -1
  357. cirq/transformers/target_gatesets/cz_gateset.py +2 -1
  358. cirq/transformers/target_gatesets/cz_gateset_test.py +1 -1
  359. cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +2 -1
  360. cirq/transformers/transformer_api.py +2 -1
  361. cirq/transformers/transformer_primitives.py +271 -145
  362. cirq/transformers/transformer_primitives_test.py +185 -1
  363. cirq/value/abc_alt.py +2 -1
  364. cirq/value/classical_data.py +2 -1
  365. cirq/value/condition.py +2 -1
  366. cirq/value/digits.py +9 -2
  367. cirq/value/duration.py +6 -5
  368. cirq/value/linear_dict.py +4 -9
  369. cirq/value/measurement_key.py +2 -1
  370. cirq/value/periodic_value.py +3 -2
  371. cirq/value/product_state.py +2 -1
  372. cirq/value/value_equality_attr.py +2 -1
  373. cirq/vis/density_matrix.py +1 -1
  374. cirq/vis/heatmap.py +2 -1
  375. cirq/vis/histogram.py +2 -1
  376. cirq/vis/state_histogram.py +2 -1
  377. cirq/work/collector.py +2 -1
  378. cirq/work/observable_grouping.py +2 -1
  379. cirq/work/observable_measurement.py +2 -1
  380. cirq/work/observable_measurement_data.py +2 -1
  381. cirq/work/observable_measurement_test.py +1 -1
  382. cirq/work/observable_readout_calibration.py +2 -1
  383. cirq/work/observable_readout_calibration_test.py +1 -1
  384. cirq/work/observable_settings.py +2 -1
  385. cirq/work/sampler.py +2 -1
  386. cirq/work/sampler_test.py +1 -1
  387. {cirq_core-1.7.0.dev20250924231107.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/METADATA +4 -4
  388. {cirq_core-1.7.0.dev20250924231107.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/RECORD +391 -374
  389. cirq/contrib/json_test.py +0 -33
  390. {cirq_core-1.7.0.dev20250924231107.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/WHEEL +0 -0
  391. {cirq_core-1.7.0.dev20250924231107.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/licenses/LICENSE +0 -0
  392. {cirq_core-1.7.0.dev20250924231107.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,242 @@
1
+ # Copyright 2025 The Cirq Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """A Multi-Moment Gauge Transformer for the cphase gate."""
16
+
17
+ from __future__ import annotations
18
+
19
+ from typing import cast
20
+
21
+ import numpy as np
22
+ from attrs import field, frozen
23
+
24
+ from cirq import circuits, ops
25
+ from cirq.transformers.gauge_compiling.multi_moment_gauge_compiling import (
26
+ MultiMomentGaugeTransformer,
27
+ )
28
+
29
+ _PAULIS: np.ndarray = np.array((ops.I, ops.X, ops.Y, ops.Z), dtype=object)
30
+ _COMMUTING_GATES = {ops.I, ops.Z} # I,Z Commute with ZPowGate and CZPowGate; X,Y anti-commute.
31
+
32
+
33
+ def _merge_pauliandzpow(left: _PauliAndZPow, right: _PauliAndZPow) -> _PauliAndZPow:
34
+ # 1. Commute left.zpow and right.pauli:
35
+ # ─left.pauli─left.zpow─right.pauli─right.zpow─
36
+ # ==> ─left.pauli─right.pauli─(+/-left.zpow─right.zpow)─
37
+ if right.pauli in _COMMUTING_GATES:
38
+ new_zpow_exp = left.zpow.exponent + right.zpow.exponent
39
+ else:
40
+ new_zpow_exp = -left.zpow.exponent + right.zpow.exponent
41
+
42
+ # 2. Merge left.pauli and right.pauli
43
+ new_pauli = left.pauli
44
+ if right.pauli is not ops.I:
45
+ if new_pauli is ops.I:
46
+ new_pauli = right.pauli
47
+ else:
48
+ # left.pauli * right.pauli
49
+ new_pauli = cast(ops.Pauli, new_pauli).phased_pauli_product(
50
+ cast(ops.Pauli, right.pauli)
51
+ )[1]
52
+
53
+ return _PauliAndZPow(pauli=new_pauli, zpow=ops.ZPowGate(exponent=new_zpow_exp))
54
+
55
+
56
+ @frozen
57
+ class _PauliAndZPow:
58
+ """A gate represented by a Pauli followed by a ZPowGate.
59
+
60
+ The order is ─Pauli──ZPowGate─.
61
+
62
+ Attributes:
63
+ pauli: The Pauli gate.
64
+ zpow: The ZPowGate.
65
+ """
66
+
67
+ pauli: ops.Pauli | ops.IdentityGate = ops.I
68
+ zpow: ops.ZPowGate = ops.ZPowGate(exponent=0)
69
+
70
+ def merge_left(self, left: _PauliAndZPow) -> _PauliAndZPow:
71
+ """Merges another `_PauliAndZPow` from the left.
72
+
73
+ Calculates `─left─self─` and returns a new `_PauliAndZPow` instance.
74
+ """
75
+ return _merge_pauliandzpow(left, self)
76
+
77
+ def merge_right(self, right: _PauliAndZPow) -> _PauliAndZPow:
78
+ """Merges another `_PauliAndZPow` from the right.
79
+
80
+ Calculates `─self─right─` and returns a new `_PauliAndZPow` instance.
81
+ """
82
+ return _merge_pauliandzpow(self, right)
83
+
84
+ def after_cphase(
85
+ self, cphase: ops.CZPowGate
86
+ ) -> tuple[ops.CZPowGate, _PauliAndZPow, _PauliAndZPow]:
87
+ """Pull self through cphase.
88
+
89
+ Returns:
90
+ A tuple of
91
+ (updated cphase gate, pull_through of this qubit, pull_through of the other qubit).
92
+ """
93
+ if self.pauli in _COMMUTING_GATES:
94
+ return cphase, _PauliAndZPow(self.pauli, self.zpow), _PauliAndZPow()
95
+ else:
96
+ # Taking self.pauli==X gate as an example:
97
+ # 0: ─X─Z^t──@────── 0: ─X──@─────Z^t─ 0: ─@──────X──Z^t──
98
+ # │ ==> │ ==> │
99
+ # 1: ────────@^exp── 1: ────@^exp───── 1: ─@^-exp─Z^exp───
100
+ # Similarly for X|Y on qubit 0/1, the result is always flipping cphase and
101
+ # add an extra Rz rotation on the other qubit.
102
+ return (
103
+ cast(ops.CZPowGate, cphase**-1),
104
+ _PauliAndZPow(self.pauli, self.zpow),
105
+ _PauliAndZPow(zpow=ops.ZPowGate(exponent=cphase.exponent)),
106
+ )
107
+
108
+ def after_pauli(self, pauli: ops.Pauli | ops.IdentityGate) -> _PauliAndZPow:
109
+ """Calculates ─self─pauli─ ==> ─pauli─output─."""
110
+ if pauli in _COMMUTING_GATES:
111
+ return _PauliAndZPow(self.pauli, self.zpow)
112
+ else:
113
+ return _PauliAndZPow(self.pauli, ops.ZPowGate(exponent=-self.zpow.exponent))
114
+
115
+ def after_zpow(self, zpow: ops.ZPowGate) -> tuple[ops.ZPowGate, _PauliAndZPow]:
116
+ """Calculates ─self─zpow─ ==> ─+/-zpow─output─."""
117
+ if self.pauli in _COMMUTING_GATES:
118
+ return zpow, _PauliAndZPow(self.pauli, self.zpow)
119
+ else:
120
+ return ops.ZPowGate(exponent=-zpow.exponent), self
121
+
122
+ def __str__(self) -> str:
123
+ return f"─{self.pauli}──{self.zpow}─"
124
+
125
+ def to_single_qubit_gate(self) -> ops.PhasedXZGate | ops.ZPowGate | ops.IdentityGate:
126
+ """Converts the _PauliAndZPow to a single-qubit gate."""
127
+ exp = self.zpow.exponent
128
+ match self.pauli:
129
+ case ops.I:
130
+ if exp % 2 == 0:
131
+ return ops.I
132
+ return self.zpow
133
+ case ops.X:
134
+ return ops.PhasedXZGate(x_exponent=1, z_exponent=exp, axis_phase_exponent=0)
135
+ case ops.Y:
136
+ return ops.PhasedXZGate(x_exponent=1, z_exponent=exp - 1, axis_phase_exponent=0)
137
+ case _: # ops.Z
138
+ return ops.ZPowGate(exponent=1 + exp)
139
+
140
+
141
+ def _pull_through_single_cphase(
142
+ cphase: ops.CZPowGate, input0: _PauliAndZPow, input1: _PauliAndZPow
143
+ ) -> tuple[ops.CZPowGate, _PauliAndZPow, _PauliAndZPow]:
144
+ """Pulls input0 and input1 through a CZPowGate.
145
+ Input: Output:
146
+ 0: ─(input0)─@───── 0: ─@────────(output0)─
147
+ │ ==> │
148
+ 1: ─(input1)─@^exp─ 1: ─@^+/-exp─(output1)─
149
+ """
150
+
151
+ # Step 1; pull input0 through CZPowGate.
152
+ # 0: ─input0─@───── 0: ────────@─────────output0─
153
+ # │ ==> │
154
+ # 1: ─input1─@^exp─ 1: ─input1─@^+/-exp──output1─
155
+ output_cphase, output0, output1 = input0.after_cphase(cphase)
156
+
157
+ # Step 2; similar to step 1, pull input1 through CZPowGate.
158
+ # 0: ─@──────────pulled0────output0─ 0: ─@────────output0─
159
+ # ==> │ ==> │
160
+ # 1: ─@^+/-exp───pulled1────output1─ 1: ─@^+/-exp─output1─
161
+ output_cphase, pulled1, pulled0 = input1.after_cphase(output_cphase)
162
+ output0 = output0.merge_left(pulled0)
163
+ output1 = output1.merge_left(pulled1)
164
+
165
+ return output_cphase, output0, output1
166
+
167
+
168
+ _TARGET_GATESET: ops.Gateset = ops.Gateset(ops.CZPowGate)
169
+ _SUPPORTED_GATESET: ops.Gateset = ops.Gateset(ops.Pauli, ops.IdentityGate, ops.ZPowGate)
170
+
171
+
172
+ @frozen
173
+ class CPhaseGaugeTransformerMM(MultiMomentGaugeTransformer):
174
+ """A gauge transformer for the cphase gate."""
175
+
176
+ target: ops.GateFamily | ops.Gateset = field(default=_TARGET_GATESET, init=False)
177
+ supported_gates: ops.GateFamily | ops.Gateset = field(default=_SUPPORTED_GATESET)
178
+
179
+ def sample_left_moment(
180
+ self, active_qubits: frozenset[ops.Qid], prng: np.random.Generator
181
+ ) -> circuits.Moment:
182
+ return circuits.Moment([cast(ops.Gate, prng.choice(_PAULIS)).on(q) for q in active_qubits])
183
+
184
+ def gauge_on_moments(
185
+ self, moments_to_gauge: list[circuits.Moment], prng: np.random.Generator
186
+ ) -> list[circuits.Moment]:
187
+ """Gauges a block of moments that contains at least a cphase gate in each of the moment.
188
+
189
+ Args:
190
+ moments_to_gauge: A list of moments to be gauged.
191
+ prng: A pseudorandom number generator.
192
+
193
+ Returns:
194
+ A list of moments after gauging.
195
+ """
196
+ active_qubits = circuits.Circuit.from_moments(*moments_to_gauge).all_qubits()
197
+ left_moment = self.sample_left_moment(active_qubits, prng)
198
+ pulled: dict[ops.Qid, _PauliAndZPow] = {
199
+ op.qubits[0]: _PauliAndZPow(pauli=cast(ops.Pauli | ops.IdentityGate, op.gate))
200
+ for op in left_moment
201
+ if op.gate
202
+ }
203
+ ret: list[circuits.Moment] = [left_moment]
204
+ # The loop iterates through each moment of the target block, propagating
205
+ # the `pulled` gauge from left to right. In each iteration, `prev` holds
206
+ # the gauge to the left of the current `moment`, and the loop computes
207
+ # the transformed `moment` and the new `pulled` gauge to its right.
208
+ for moment in moments_to_gauge:
209
+ # Calculate --prev--moment-- ==> --updated_momment--pulled--
210
+ prev = pulled
211
+ pulled = {}
212
+ ops_at_updated_moment: list[ops.Operation] = []
213
+ for op in moment:
214
+ # Pull prev through ops at the moment.
215
+ if op.gate:
216
+ match op.gate:
217
+ case ops.CZPowGate():
218
+ q0, q1 = op.qubits
219
+ new_gate, pulled[q0], pulled[q1] = _pull_through_single_cphase(
220
+ op.gate, prev[q0], prev[q1]
221
+ )
222
+ ops_at_updated_moment.append(new_gate.on(q0, q1))
223
+ case ops.Pauli() | ops.IdentityGate():
224
+ q = op.qubits[0]
225
+ ops_at_updated_moment.append(op)
226
+ pulled[q] = prev[q].after_pauli(op.gate)
227
+ case ops.ZPowGate():
228
+ q = op.qubits[0]
229
+ new_zpow, pulled[q] = prev[q].after_zpow(op.gate)
230
+ ops_at_updated_moment.append(new_zpow.on(q))
231
+ case _:
232
+ raise ValueError(f"Gate type {type(op.gate)} is not supported.")
233
+ # Keep the other ops of prev
234
+ for q, gate in prev.items():
235
+ if q not in pulled:
236
+ pulled[q] = gate
237
+ ret.append(circuits.Moment(ops_at_updated_moment))
238
+ last_moment = circuits.Moment(
239
+ [gate.to_single_qubit_gate().on(q) for q, gate in pulled.items()]
240
+ )
241
+ ret.append(last_moment)
242
+ return ret
@@ -0,0 +1,243 @@
1
+ # Copyright 2025 The Cirq Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+
17
+ import numpy as np
18
+ import pytest
19
+
20
+ import cirq
21
+ from cirq import I, X, Y, Z, ZPowGate
22
+ from cirq.transformers.gauge_compiling.multi_moment_cphase_gauge import (
23
+ _PauliAndZPow,
24
+ CPhaseGaugeTransformerMM,
25
+ )
26
+
27
+
28
+ def test_gauge_on_single_cphase():
29
+ """Test case.
30
+ Input:
31
+ 0: ───@───────
32
+
33
+ 1: ───@^0.2───
34
+ Example output:
35
+ 0: ───X───@────────PhXZ(a=0,x=1,z=0)───
36
+
37
+ 1: ───I───@^-0.2───Z^0.2───────────────
38
+ """
39
+ q0, q1 = cirq.LineQubit.range(2)
40
+
41
+ input_circuit = cirq.Circuit(cirq.Moment(cirq.CZ(q0, q1) ** 0.2))
42
+
43
+ class _TestCPhaseGaugeTransformerMM(CPhaseGaugeTransformerMM):
44
+ def sample_left_moment(
45
+ self,
46
+ active_qubits: frozenset[cirq.Qid],
47
+ rng: np.random.Generator = np.random.default_rng(),
48
+ ) -> cirq.Moment:
49
+ return cirq.Moment(g1(q0), g2(q1))
50
+
51
+ for g1 in [X, Y, Z, I]:
52
+ for g2 in [X, Y, Z, I]: # Test with all possible samples of the left moment.
53
+ cphase_transformer = _TestCPhaseGaugeTransformerMM()
54
+ output_circuit = cphase_transformer(input_circuit)
55
+ cirq.testing.assert_circuits_have_same_unitary_given_final_permutation(
56
+ input_circuit, output_circuit, {q: q for q in input_circuit.all_qubits()}
57
+ )
58
+
59
+
60
+ def test_gauge_on_cz_moments():
61
+ """Test case.
62
+ Input:
63
+ ┌──┐
64
+ 0: ───@────@─────H───────@───@───
65
+ │ │ │ │
66
+ 1: ───@────┼@────────────@───@───
67
+ ││
68
+ 2: ───@────@┼────────@───@───@───
69
+ │ │ │ │ │
70
+ 3: ───@─────@────────@───@───@───
71
+ └──┘
72
+ Example output:
73
+ ┌──┐
74
+ 0: ───X───@────@─────PhXZ(a=0,x=1,z=1)──────H───X───────@───@───PhXZ(a=0,x=1,z=2)────
75
+ │ │ │ │
76
+ 1: ───I───@────┼@────Z──────────────────────────X───────@───@───PhXZ(a=2,x=1,z=-2)───
77
+ ││
78
+ 2: ───Y───@────@┼────PhXZ(a=1.5,x=1,z=-1)───────Z───@───@───@───Z────────────────────
79
+ │ │ │ │ │
80
+ 3: ───Z───@─────@────Z^0────────────────────────I───@───@───@───Z^0──────────────────
81
+ └──┘
82
+ """
83
+ q0, q1, q2, q3 = cirq.LineQubit.range(4)
84
+ input_circuit = cirq.Circuit(
85
+ cirq.Moment(cirq.CZ(q0, q1), cirq.CZ(q2, q3)),
86
+ cirq.Moment(cirq.CZ(q0, q2), cirq.CZ(q1, q3)),
87
+ cirq.Moment(cirq.H(q0)),
88
+ cirq.Moment(cirq.CZ(q2, q3)),
89
+ cirq.Moment(cirq.CZ(q0, q1), cirq.CZ(q2, q3)),
90
+ cirq.Moment(cirq.CZ(q0, q1), cirq.CZ(q2, q3)),
91
+ )
92
+ transformer = CPhaseGaugeTransformerMM()
93
+
94
+ output_circuit = transformer(input_circuit)
95
+ cirq.testing.assert_circuits_have_same_unitary_given_final_permutation(
96
+ input_circuit, output_circuit, {q: q for q in input_circuit.all_qubits()}
97
+ )
98
+
99
+
100
+ def test_is_target_moment():
101
+ q0, q1, q2 = cirq.LineQubit.range(3)
102
+
103
+ target_moments = [
104
+ cirq.Moment(cirq.CZ(q0, q1) ** 0.2),
105
+ cirq.Moment(cirq.CZ(q0, q1) ** 0.2, cirq.X(q2)),
106
+ ]
107
+ non_target_moments = [
108
+ cirq.Moment(cirq.X(q0), cirq.Y(q1)),
109
+ cirq.Moment(cirq.CZ(q0, q1) ** 0.2, cirq.Rz(rads=-0.8).on(q2)),
110
+ cirq.Moment(cirq.CZ(q0, q1).with_tags("ignore")),
111
+ cirq.Moment(cirq.CZ(q0, q1)).with_tags("ignore"),
112
+ ]
113
+ cphase_transformer = CPhaseGaugeTransformerMM(supported_gates=cirq.Gateset(cirq.Pauli))
114
+ for m in target_moments:
115
+ assert cphase_transformer.is_target_moment(m)
116
+ for m in non_target_moments:
117
+ assert not cphase_transformer.is_target_moment(
118
+ m, cirq.TransformerContext(tags_to_ignore={'ignore'})
119
+ )
120
+
121
+
122
+ def test_gauge_on_cphase_moments():
123
+ """Test case.
124
+ Input:
125
+ ┌──┐
126
+ 0: ───@────────@─────H───Rz(-0.255π)───────────@───────@───────
127
+ │ │ │ │
128
+ 1: ───@^0.2────┼@──────────────────────────────@^0.1───@───────
129
+ ││
130
+ 2: ───@────────@┼────────@─────────────@───────@───────@───────
131
+ │ │ │ │ │ │
132
+ 3: ───@─────────@────────@^0.2─────────@^0.2───@───────@^0.2───
133
+ └──┘
134
+ Example output:
135
+ ┌──┐
136
+ 0: ───Y───@─────────@─────PhXZ(a=0,x=1,z=0)───H───X───Rz(0.255π)────────────@───────@────────PhXZ(a=0,x=1,z=1.1)───
137
+ │ │ │ │
138
+ 1: ───I───@^-0.2────┼@────Z^0.2───────────────────Y─────────────────────────@^0.1───@────────PhXZ(a=0,x=1,z=0.1)───
139
+ ││
140
+ 2: ───X───@─────────@┼────PhXZ(a=0,x=1,z=1)───────X───@────────────@────────@───────@────────PhXZ(a=0,x=1,z=0)─────
141
+ │ │ │ │ │ │
142
+ 3: ───Z───@──────────@────I───────────────────────I───@^-0.2───────@^-0.2───@───────@^-0.2───Z^-0.4────────────────
143
+ └──┘
144
+ """ # noqa: E501
145
+ q0, q1, q2, q3 = cirq.LineQubit.range(4)
146
+ cphase_transformer = CPhaseGaugeTransformerMM()
147
+ for seed in range(5):
148
+ input_circuit = cirq.Circuit(
149
+ cirq.Moment(cirq.CZ(q0, q1) ** 0.2, cirq.CZ(q2, q3)),
150
+ cirq.Moment(cirq.CZ(q0, q2), cirq.CZ(q1, q3)),
151
+ cirq.Moment(cirq.H(q0)),
152
+ cirq.Moment(cirq.CZ(q2, q3) ** 0.2, cirq.Rz(rads=-0.8).on(q0)),
153
+ cirq.Moment(cirq.CZ(q2, q3) ** 0.2),
154
+ cirq.Moment(cirq.CZ(q0, q1) ** 0.1, cirq.CZ(q2, q3)),
155
+ cirq.Moment(cirq.CZ(q0, q1), cirq.CZ(q2, q3) ** 0.2),
156
+ )
157
+
158
+ output_circuit = cphase_transformer(input_circuit, rng_or_seed=seed)
159
+ cirq.testing.assert_circuits_have_same_unitary_given_final_permutation(
160
+ input_circuit, output_circuit, {q: q for q in input_circuit.all_qubits()}
161
+ )
162
+
163
+
164
+ def test_gauge_on_czpow_only_moments():
165
+ q0, q1, q2 = cirq.LineQubit.range(3)
166
+
167
+ input_circuit = cirq.Circuit(cirq.Moment(cirq.CZ(q0, q1) ** 0.2, X(q2)))
168
+ cphase_transformer = CPhaseGaugeTransformerMM(supported_gates=cirq.Gateset())
169
+ output_circuit = cphase_transformer(input_circuit)
170
+
171
+ # Since X isn't in supported_gates, the moment won't be gauged.
172
+ assert input_circuit == output_circuit
173
+
174
+
175
+ def test_gauge_on_supported_gates():
176
+ q0, q1, q2, q3 = cirq.LineQubit.range(4)
177
+ cphase_transformer = CPhaseGaugeTransformerMM()
178
+ for g1 in [X, Z**0.6, I, Z]:
179
+ for g2 in [Y, cirq.Rz(rads=0.2), Z**0.7]:
180
+ input_circuit = cirq.Circuit(
181
+ cirq.Moment(cirq.CZ(q0, q1) ** 0.2, g1(q2), g2(q3)),
182
+ cirq.Moment(cirq.CZ(q0, q2), g2(q1), g1(q3)),
183
+ )
184
+ output_circuit = cphase_transformer(input_circuit)
185
+ cirq.testing.assert_circuits_have_same_unitary_given_final_permutation(
186
+ input_circuit, output_circuit, {q: q for q in input_circuit.all_qubits()}
187
+ )
188
+
189
+
190
+ def test_gauge_on_unsupported_gates():
191
+ q0, q1, q2, q3 = cirq.LineQubit.range(4)
192
+
193
+ cphase_transformer = CPhaseGaugeTransformerMM(supported_gates=cirq.Gateset(cirq.CNOT))
194
+ with pytest.raises(ValueError, match="Gate type .* is not supported."):
195
+ cphase_transformer(cirq.Circuit(cirq.CNOT(q0, q1), cirq.CZ(q2, q3)))
196
+
197
+
198
+ def test_pauli_and_phxz_util_str():
199
+ assert str(_PauliAndZPow(pauli=X)) == '─X──Z**0─'
200
+ assert str(_PauliAndZPow(pauli=X, zpow=Z**0.1)) == '─X──Z**0.1─'
201
+
202
+
203
+ def test_pauli_and_phxz_util_gate_merges():
204
+ """Tests _PauliAndZPow's merge_left() and merge_right()."""
205
+ for left_pauli in [X, Y, Z, I]:
206
+ for right_pauli in [X, Y, Z, I]:
207
+ left = _PauliAndZPow(pauli=left_pauli, zpow=ZPowGate(exponent=0.2))
208
+ right = _PauliAndZPow(pauli=right_pauli, zpow=ZPowGate(exponent=0.6))
209
+ merge1 = right.merge_left(left)
210
+ merge2 = left.merge_right(right)
211
+
212
+ assert np.allclose(
213
+ cirq.unitary(merge1.to_single_qubit_gate()),
214
+ cirq.unitary(merge2.to_single_qubit_gate()),
215
+ )
216
+ q = cirq.LineQubit(0)
217
+ cirq.testing.assert_allclose_up_to_global_phase(
218
+ cirq.unitary(
219
+ cirq.Circuit(
220
+ left.to_single_qubit_gate().on(q), right.to_single_qubit_gate().on(q)
221
+ )
222
+ ),
223
+ cirq.unitary(merge1.to_single_qubit_gate()),
224
+ atol=1e-6,
225
+ )
226
+
227
+
228
+ def test_pauli_and_phxz_util_to_1q_gate():
229
+ """Tests _PauliAndZPow.to_single_qubit_gate()."""
230
+ q = cirq.LineQubit(0)
231
+ for pauli in [cirq.X, cirq.Y, cirq.Z, cirq.I]:
232
+ for zpow in [cirq.ZPowGate(exponent=exp) for exp in [0, 0.1, 0.5, 1, 10.2]]:
233
+ cirq.testing.assert_circuits_have_same_unitary_given_final_permutation(
234
+ cirq.Circuit(pauli(q), zpow(q)),
235
+ cirq.Circuit(_PauliAndZPow(pauli=pauli, zpow=zpow).to_single_qubit_gate().on(q)),
236
+ {q: q},
237
+ )
238
+
239
+
240
+ def test_deep_not_supported():
241
+ with pytest.raises(ValueError, match="GaugeTransformer cannot be used with deep=True"):
242
+ t = CPhaseGaugeTransformerMM()
243
+ t(cirq.Circuit(), context=cirq.TransformerContext(deep=True))
@@ -0,0 +1,151 @@
1
+ # Copyright 2025 The Cirq Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Defines the abstraction for multi-moment gauge compiling as a cirq transformer."""
16
+
17
+ import abc
18
+
19
+ import attrs
20
+ import numpy as np
21
+
22
+ from cirq import circuits, ops
23
+ from cirq.transformers import transformer_api
24
+
25
+
26
+ @transformer_api.transformer
27
+ @attrs.frozen
28
+ class MultiMomentGaugeTransformer(abc.ABC):
29
+ """A gauge transformer that wraps target blocks of moments with single-qubit gates.
30
+
31
+ In detail, a "gauging moment" of single-qubit gates is inserted before a target block of
32
+ moments. These gates are then commuted through the block, resulting in a corresponding
33
+ moment of gates after it.
34
+
35
+ q₀: ... ───LG0───╭───────────╮────RG0───...
36
+ │ │
37
+ q₁: ... ───LG1───┤ moments ├────RG1───...
38
+ │ to be │
39
+ q₂: ... ───LG2───┤ gauged on ├────RG2───...
40
+ │ │
41
+ q₃: ... ───LG3───╰───────────╯────RG3───...
42
+
43
+ Attributes:
44
+ target: The target gate, gate family or gateset, must exist in each of the moment in
45
+ the "moments to be gauged".
46
+ supported_gates: The gates that are supported in the "moments to be gauged".
47
+ """
48
+
49
+ target: ops.GateFamily | ops.Gateset
50
+ supported_gates: ops.GateFamily | ops.Gateset
51
+
52
+ @abc.abstractmethod
53
+ def sample_left_moment(
54
+ self, active_qubits: frozenset[ops.Qid], prng: np.random.Generator
55
+ ) -> circuits.Moment:
56
+ """Samples a random single-qubit moment to be inserted before the target block.
57
+
58
+ Args:
59
+ active_qubits: The qubits on which the sampled gates should be applied.
60
+ prng: A pseudorandom number generator.
61
+
62
+ Returns:
63
+ The sampled moment.
64
+ """
65
+
66
+ @abc.abstractmethod
67
+ def gauge_on_moments(
68
+ self, moments_to_gauge: list[circuits.Moment], prng: np.random.Generator
69
+ ) -> list[circuits.Moment]:
70
+ """Gauges a block of moments.
71
+
72
+ Args:
73
+ moments_to_gauge: A list of moments to be gauged.
74
+ prng: A pseudorandom number generator.
75
+
76
+ Returns:
77
+ A list of moments after gauging.
78
+ """
79
+
80
+ def is_target_moment(
81
+ self, moment: circuits.Moment, context: transformer_api.TransformerContext | None = None
82
+ ) -> bool:
83
+ """Checks if a moment is a target for gauging.
84
+
85
+ A moment is a target moment if it contains at least one target op and
86
+ all its operations are supported by this transformer.
87
+ """
88
+ # skip the moment if the moment is tagged to be ignored
89
+ if context and set(moment.tags).intersection(context.tags_to_ignore):
90
+ return False
91
+
92
+ has_target_gates: bool = False
93
+ for op in moment:
94
+ if (
95
+ context
96
+ and isinstance(op, ops.TaggedOperation)
97
+ and set(op.tags).intersection(context.tags_to_ignore)
98
+ ): # skip the moment if the op is tagged to be ignored
99
+ return False
100
+ if op.gate:
101
+ if op in self.target:
102
+ has_target_gates = True
103
+ elif op not in self.supported_gates:
104
+ return False
105
+ return has_target_gates
106
+
107
+ def __call__(
108
+ self,
109
+ circuit: circuits.AbstractCircuit,
110
+ *,
111
+ context: transformer_api.TransformerContext | None = None,
112
+ rng_or_seed: np.random.Generator | int | None = None,
113
+ ) -> circuits.AbstractCircuit:
114
+ """Apply the transformer to the given circuit.
115
+
116
+ Args:
117
+ circuit: The circuit to transform.
118
+ context: `cirq.TransformerContext` storing common configurable options for transformers.
119
+ prng: A pseudorandom number generator.
120
+
121
+ Returns:
122
+ The transformed circuit.
123
+
124
+ Raises:
125
+ ValueError: if the TransformerContext has deep=True.
126
+
127
+ """
128
+ if context is None:
129
+ context = transformer_api.TransformerContext(deep=False)
130
+ if context.deep:
131
+ raise ValueError('GaugeTransformer cannot be used with deep=True')
132
+ rng = (
133
+ rng_or_seed
134
+ if isinstance(rng_or_seed, np.random.Generator)
135
+ else np.random.default_rng(rng_or_seed)
136
+ )
137
+
138
+ output_moments: list[circuits.Moment] = []
139
+ moments_to_gauge: list[circuits.Moment] = []
140
+ for moment in circuit:
141
+ if self.is_target_moment(moment, context):
142
+ moments_to_gauge.append(moment)
143
+ else:
144
+ if moments_to_gauge:
145
+ output_moments.extend(self.gauge_on_moments(moments_to_gauge, rng))
146
+ moments_to_gauge.clear()
147
+ output_moments.append(moment)
148
+ if moments_to_gauge:
149
+ output_moments.extend(self.gauge_on_moments(moments_to_gauge, rng))
150
+
151
+ return circuits.Circuit.from_moments(*output_moments)
@@ -16,8 +16,9 @@
16
16
 
17
17
  from __future__ import annotations
18
18
 
19
+ from collections.abc import Sequence
19
20
  from numbers import Real
20
- from typing import Sequence, TYPE_CHECKING
21
+ from typing import TYPE_CHECKING
21
22
 
22
23
  from cirq.ops import CZ, CZPowGate, Gate, Gateset, S, X
23
24
  from cirq.transformers.gauge_compiling.gauge_compiling import (
@@ -3,7 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import itertools
6
- from typing import Sequence
6
+ from collections.abc import Sequence
7
7
 
8
8
  import numpy as np
9
9