cirq-core 1.7.0.dev20250825174419__py3-none-any.whl → 1.7.0.dev20251203004401__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cirq/__init__.py +1 -0
- cirq/_compat.py +3 -2
- cirq/_compat_test.py +16 -15
- cirq/_doc.py +4 -3
- cirq/_import.py +2 -1
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/circuits/_bucket_priority_queue.py +2 -1
- cirq/circuits/circuit.py +19 -17
- cirq/circuits/circuit_operation.py +2 -1
- cirq/circuits/circuit_operation_test.py +19 -0
- cirq/circuits/circuit_test.py +31 -12
- cirq/circuits/frozen_circuit.py +3 -2
- cirq/circuits/moment.py +3 -15
- cirq/circuits/optimization_pass.py +2 -1
- cirq/circuits/qasm_output.py +39 -10
- cirq/circuits/qasm_output_test.py +51 -2
- cirq/circuits/text_diagram_drawer.py +2 -1
- cirq/contrib/acquaintance/bipartite.py +2 -1
- cirq/contrib/acquaintance/devices.py +1 -1
- cirq/contrib/acquaintance/executor.py +4 -5
- cirq/contrib/acquaintance/executor_test.py +2 -1
- cirq/contrib/acquaintance/gates.py +2 -1
- cirq/contrib/acquaintance/gates_test.py +1 -1
- cirq/contrib/acquaintance/inspection_utils.py +2 -1
- cirq/contrib/acquaintance/mutation_utils.py +2 -1
- cirq/contrib/acquaintance/optimizers.py +2 -1
- cirq/contrib/acquaintance/permutation.py +2 -1
- cirq/contrib/acquaintance/permutation_test.py +1 -1
- cirq/contrib/acquaintance/shift.py +2 -1
- cirq/contrib/acquaintance/shift_swap_network.py +2 -1
- cirq/contrib/acquaintance/strategies/complete.py +3 -2
- cirq/contrib/acquaintance/strategies/cubic.py +2 -1
- cirq/contrib/acquaintance/strategies/quartic_paired.py +2 -1
- cirq/contrib/acquaintance/strategies/quartic_paired_test.py +1 -1
- cirq/contrib/acquaintance/testing.py +2 -1
- cirq/contrib/acquaintance/topological_sort.py +2 -1
- cirq/contrib/bayesian_network/bayesian_network_gate.py +3 -2
- cirq/contrib/circuitdag/circuit_dag.py +4 -2
- cirq/contrib/custom_simulators/custom_state_simulator.py +2 -1
- cirq/contrib/custom_simulators/custom_state_simulator_test.py +1 -1
- cirq/contrib/graph_device/graph_device.py +2 -1
- cirq/contrib/graph_device/graph_device_test.py +2 -1
- cirq/contrib/graph_device/hypergraph.py +2 -1
- cirq/contrib/graph_device/uniform_graph_device.py +2 -1
- cirq/contrib/json.py +14 -2
- cirq/contrib/json_test_data/BayesianNetworkGate.json +10 -0
- cirq/contrib/json_test_data/BayesianNetworkGate.repr +3 -0
- cirq/contrib/json_test_data/QuantumVolumeResult.json +169 -0
- cirq/contrib/json_test_data/QuantumVolumeResult.repr +22 -0
- cirq/contrib/json_test_data/SwapPermutationGate.json +3 -0
- cirq/contrib/json_test_data/SwapPermutationGate.repr +1 -0
- cirq/contrib/json_test_data/spec.py +0 -2
- cirq/contrib/noise_models/noise_models.py +2 -1
- cirq/contrib/paulistring/clifford_optimize.py +20 -2
- cirq/contrib/paulistring/optimize.py +1 -1
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +146 -35
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +81 -178
- cirq/contrib/paulistring/recombine.py +5 -2
- cirq/contrib/paulistring/separate.py +1 -1
- cirq/contrib/qasm_import/_lexer.py +6 -1
- cirq/contrib/qasm_import/_lexer_test.py +1 -1
- cirq/contrib/qasm_import/_parser.py +24 -8
- cirq/contrib/qasm_import/_parser_test.py +44 -6
- cirq/contrib/qcircuit/qcircuit_pdf_test.py +6 -9
- cirq/contrib/quantikz/__init__.py +21 -0
- cirq/contrib/quantikz/circuit_to_latex_quantikz.py +680 -0
- cirq/contrib/quantikz/circuit_to_latex_quantikz_test.py +253 -0
- cirq/contrib/quantikz/circuit_to_latex_render.py +424 -0
- cirq/contrib/quantikz/circuit_to_latex_render_test.py +44 -0
- cirq/contrib/quantum_volume/quantum_volume.py +2 -1
- cirq/contrib/quimb/density_matrix.py +1 -1
- cirq/contrib/quimb/grid_circuits.py +2 -1
- cirq/contrib/quimb/grid_circuits_test.py +1 -1
- cirq/contrib/quimb/mps_simulator.py +4 -3
- cirq/contrib/quimb/state_vector.py +2 -1
- cirq/contrib/quirk/export_to_quirk.py +2 -1
- cirq/contrib/quirk/linearize_circuit.py +1 -1
- cirq/contrib/quirk/quirk_gate.py +2 -1
- cirq/contrib/routing/device.py +1 -1
- cirq/contrib/routing/greedy.py +2 -1
- cirq/contrib/routing/initialization.py +2 -1
- cirq/contrib/routing/router.py +2 -1
- cirq/contrib/routing/swap_network.py +2 -1
- cirq/contrib/routing/utils.py +2 -1
- cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +7 -5
- cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking_test.py +6 -6
- cirq/devices/device.py +2 -1
- cirq/devices/grid_device_metadata.py +2 -1
- cirq/devices/grid_qubit.py +7 -6
- cirq/devices/insertion_noise_model.py +2 -1
- cirq/devices/line_qubit.py +2 -1
- cirq/devices/named_topologies.py +2 -1
- cirq/devices/noise_model.py +2 -1
- cirq/devices/noise_model_test.py +1 -1
- cirq/devices/noise_properties.py +2 -1
- cirq/devices/superconducting_qubits_noise_properties_test.py +2 -1
- cirq/devices/thermal_noise_model.py +2 -1
- cirq/experiments/__init__.py +2 -0
- cirq/experiments/benchmarking/parallel_xeb.py +2 -1
- cirq/experiments/benchmarking/parallel_xeb_test.py +1 -1
- cirq/experiments/fidelity_estimation.py +2 -1
- cirq/experiments/fidelity_estimation_test.py +1 -1
- cirq/experiments/ghz_2d.py +150 -0
- cirq/experiments/ghz_2d_test.py +155 -0
- cirq/experiments/n_qubit_tomography.py +2 -1
- cirq/experiments/n_qubit_tomography_test.py +1 -1
- cirq/experiments/purity_estimation.py +1 -1
- cirq/experiments/qubit_characterizations.py +33 -4
- cirq/experiments/qubit_characterizations_test.py +16 -0
- cirq/experiments/random_quantum_circuit_generation.py +2 -1
- cirq/experiments/random_quantum_circuit_generation_test.py +2 -1
- cirq/experiments/readout_confusion_matrix.py +2 -1
- cirq/experiments/readout_confusion_matrix_test.py +1 -1
- cirq/experiments/single_qubit_readout_calibration.py +2 -1
- cirq/experiments/single_qubit_readout_calibration_test.py +1 -1
- cirq/experiments/t1_decay_experiment.py +2 -1
- cirq/experiments/two_qubit_xeb.py +2 -1
- cirq/experiments/two_qubit_xeb_test.py +1 -1
- cirq/experiments/xeb_fitting.py +2 -1
- cirq/experiments/xeb_fitting_test.py +1 -1
- cirq/experiments/xeb_sampling.py +5 -3
- cirq/experiments/xeb_sampling_test.py +1 -1
- cirq/experiments/xeb_simulation.py +2 -1
- cirq/experiments/xeb_simulation_test.py +2 -1
- cirq/experiments/z_phase_calibration.py +2 -1
- cirq/experiments/z_phase_calibration_test.py +18 -3
- cirq/interop/quirk/cells/__init__.py +1 -2
- cirq/interop/quirk/cells/all_cells.py +2 -1
- cirq/interop/quirk/cells/arithmetic_cells.py +2 -1
- cirq/interop/quirk/cells/cell.py +2 -1
- cirq/interop/quirk/cells/composite_cell.py +2 -1
- cirq/interop/quirk/cells/composite_cell_test.py +1 -1
- cirq/interop/quirk/cells/control_cells.py +2 -1
- cirq/interop/quirk/cells/frequency_space_cells.py +1 -1
- cirq/interop/quirk/cells/ignored_cells.py +1 -1
- cirq/interop/quirk/cells/input_cells.py +2 -1
- cirq/interop/quirk/cells/input_rotation_cells.py +2 -1
- cirq/interop/quirk/cells/measurement_cells.py +2 -1
- cirq/interop/quirk/cells/parse.py +2 -11
- cirq/interop/quirk/cells/qubit_permutation_cells.py +2 -1
- cirq/interop/quirk/cells/scalar_cells.py +2 -1
- cirq/interop/quirk/cells/single_qubit_rotation_cells.py +2 -1
- cirq/interop/quirk/cells/swap_cell.py +2 -1
- cirq/interop/quirk/cells/unsupported_cells.py +1 -1
- cirq/interop/quirk/url_to_circuit.py +2 -1
- cirq/json_resolver_cache.py +0 -2
- cirq/linalg/decompositions.py +6 -2
- cirq/linalg/decompositions_test.py +1 -0
- cirq/linalg/diagonalize.py +1 -1
- cirq/linalg/predicates.py +2 -1
- cirq/linalg/tolerance.py +2 -1
- cirq/linalg/transformations.py +3 -2
- cirq/ops/arithmetic_operation.py +4 -3
- cirq/ops/arithmetic_operation_test.py +1 -1
- cirq/ops/boolean_hamiltonian.py +4 -3
- cirq/ops/classically_controlled_operation.py +11 -11
- cirq/ops/classically_controlled_operation_test.py +26 -2
- cirq/ops/clifford_gate.py +3 -2
- cirq/ops/clifford_gate_test.py +1 -2
- cirq/ops/common_channels.py +2 -1
- cirq/ops/common_gates.py +3 -2
- cirq/ops/control_values.py +2 -1
- cirq/ops/controlled_gate.py +3 -2
- cirq/ops/controlled_gate_test.py +2 -1
- cirq/ops/controlled_operation.py +3 -2
- cirq/ops/controlled_operation_test.py +2 -1
- cirq/ops/dense_pauli_string.py +44 -81
- cirq/ops/dense_pauli_string_test.py +21 -0
- cirq/ops/diagonal_gate.py +3 -2
- cirq/ops/eigen_gate.py +9 -7
- cirq/ops/fourier_transform.py +3 -2
- cirq/ops/fourier_transform_test.py +2 -4
- cirq/ops/fsim_gate.py +3 -2
- cirq/ops/gate_operation.py +23 -12
- cirq/ops/gateset.py +22 -2
- cirq/ops/global_phase_op.py +3 -2
- cirq/ops/greedy_qubit_manager.py +2 -1
- cirq/ops/identity.py +2 -1
- cirq/ops/kraus_channel.py +2 -1
- cirq/ops/linear_combinations.py +12 -17
- cirq/ops/linear_combinations_test.py +23 -1
- cirq/ops/matrix_gates.py +2 -1
- cirq/ops/measure_util.py +8 -6
- cirq/ops/measurement_gate.py +2 -1
- cirq/ops/mixed_unitary_channel.py +2 -1
- cirq/ops/named_qubit.py +2 -2
- cirq/ops/op_tree.py +2 -1
- cirq/ops/parallel_gate.py +3 -2
- cirq/ops/parity_gates.py +2 -1
- cirq/ops/parity_gates_test.py +35 -0
- cirq/ops/pauli_interaction_gate.py +2 -1
- cirq/ops/pauli_measurement_gate.py +2 -1
- cirq/ops/pauli_string.py +37 -57
- cirq/ops/pauli_string_phasor.py +6 -5
- cirq/ops/pauli_string_raw_types.py +2 -1
- cirq/ops/pauli_string_test.py +49 -6
- cirq/ops/pauli_sum_exponential.py +2 -1
- cirq/ops/permutation_gate.py +2 -1
- cirq/ops/phased_iswap_gate.py +3 -2
- cirq/ops/phased_x_gate.py +5 -4
- cirq/ops/phased_x_z_gate.py +12 -5
- cirq/ops/projector.py +2 -1
- cirq/ops/qubit_manager.py +2 -1
- cirq/ops/qubit_order.py +2 -1
- cirq/ops/qubit_order_or_list.py +1 -1
- cirq/ops/random_gate_channel.py +3 -2
- cirq/ops/raw_types.py +33 -16
- cirq/ops/raw_types_test.py +4 -3
- cirq/ops/state_preparation_channel.py +2 -1
- cirq/ops/three_qubit_gates.py +3 -2
- cirq/ops/two_qubit_diagonal_gate.py +3 -2
- cirq/ops/uniform_superposition_gate.py +2 -1
- cirq/ops/wait_gate.py +10 -4
- cirq/protocols/act_on_protocol.py +2 -1
- cirq/protocols/act_on_protocol_test.py +2 -1
- cirq/protocols/apply_channel_protocol.py +2 -1
- cirq/protocols/apply_mixture_protocol.py +2 -1
- cirq/protocols/apply_mixture_protocol_test.py +2 -1
- cirq/protocols/apply_unitary_protocol.py +2 -1
- cirq/protocols/apply_unitary_protocol_test.py +2 -0
- cirq/protocols/approximate_equality_protocol.py +2 -1
- cirq/protocols/circuit_diagram_info_protocol.py +2 -1
- cirq/protocols/control_key_protocol.py +7 -0
- cirq/protocols/decompose_protocol.py +2 -12
- cirq/protocols/has_stabilizer_effect_protocol.py +1 -1
- cirq/protocols/has_stabilizer_effect_protocol_test.py +11 -9
- cirq/protocols/has_unitary_protocol_test.py +3 -3
- cirq/protocols/hash_from_pickle_test.py +2 -2
- cirq/protocols/inverse_protocol.py +2 -1
- cirq/protocols/json_serialization.py +5 -4
- cirq/protocols/json_serialization_test.py +31 -31
- cirq/protocols/kraus_protocol.py +4 -3
- cirq/protocols/kraus_protocol_test.py +7 -7
- cirq/protocols/measurement_key_protocol.py +32 -8
- cirq/protocols/mixture_protocol.py +3 -2
- cirq/protocols/mixture_protocol_test.py +7 -7
- cirq/protocols/mul_protocol_test.py +4 -4
- cirq/protocols/phase_protocol.py +13 -4
- cirq/protocols/pow_protocol.py +2 -1
- cirq/protocols/pow_protocol_test.py +5 -5
- cirq/protocols/qasm.py +2 -1
- cirq/protocols/qid_shape_protocol.py +2 -1
- cirq/protocols/resolve_parameters.py +17 -15
- cirq/protocols/trace_distance_bound.py +2 -1
- cirq/protocols/unitary_protocol.py +21 -21
- cirq/protocols/unitary_protocol_test.py +31 -19
- cirq/qis/channels.py +1 -1
- cirq/qis/channels_test.py +1 -1
- cirq/qis/clifford_tableau.py +16 -15
- cirq/qis/clifford_tableau_test.py +17 -17
- cirq/qis/entropy.py +3 -3
- cirq/qis/entropy_test.py +1 -1
- cirq/qis/quantum_state_representation.py +2 -1
- cirq/qis/states.py +7 -2
- cirq/qis/states_test.py +54 -54
- cirq/sim/classical_simulator.py +25 -14
- cirq/sim/classical_simulator_test.py +85 -30
- cirq/sim/clifford/clifford_simulator.py +7 -6
- cirq/sim/clifford/clifford_simulator_test.py +51 -50
- cirq/sim/clifford/clifford_tableau_simulation_state.py +2 -1
- cirq/sim/clifford/stabilizer_ch_form_simulation_state.py +2 -1
- cirq/sim/clifford/stabilizer_sampler.py +1 -1
- cirq/sim/clifford/stabilizer_simulation_state.py +2 -1
- cirq/sim/clifford/stabilizer_state_ch_form.py +16 -15
- cirq/sim/clifford/stabilizer_state_ch_form_test.py +0 -1
- cirq/sim/density_matrix_simulation_state.py +7 -6
- cirq/sim/density_matrix_simulator.py +3 -2
- cirq/sim/density_matrix_simulator_test.py +94 -84
- cirq/sim/density_matrix_utils.py +2 -1
- cirq/sim/density_matrix_utils_test.py +1 -1
- cirq/sim/mux.py +35 -8
- cirq/sim/mux_test.py +39 -26
- cirq/sim/simulation_product_state.py +2 -1
- cirq/sim/simulation_product_state_test.py +8 -7
- cirq/sim/simulation_state.py +6 -5
- cirq/sim/simulation_state_base.py +3 -2
- cirq/sim/simulation_state_test.py +7 -6
- cirq/sim/simulation_utils.py +2 -1
- cirq/sim/simulator.py +4 -3
- cirq/sim/simulator_base.py +2 -1
- cirq/sim/simulator_base_test.py +51 -36
- cirq/sim/simulator_test.py +41 -36
- cirq/sim/sparse_simulator.py +3 -2
- cirq/sim/sparse_simulator_test.py +92 -82
- cirq/sim/state_vector.py +5 -6
- cirq/sim/state_vector_simulation_state.py +10 -9
- cirq/sim/state_vector_simulator.py +2 -1
- cirq/sim/state_vector_simulator_test.py +9 -9
- cirq/sim/state_vector_test.py +40 -39
- cirq/study/__init__.py +1 -0
- cirq/study/flatten_expressions.py +2 -1
- cirq/study/resolver.py +31 -18
- cirq/study/resolver_test.py +1 -1
- cirq/study/result.py +2 -1
- cirq/study/result_test.py +20 -20
- cirq/study/sweepable.py +2 -1
- cirq/study/sweepable_test.py +20 -20
- cirq/study/sweeps.py +26 -1
- cirq/study/sweeps_test.py +67 -43
- cirq/testing/_compat_test_data/__init__.py +3 -3
- cirq/testing/circuit_compare.py +2 -1
- cirq/testing/circuit_compare_test.py +16 -14
- cirq/testing/consistent_act_on_test.py +1 -1
- cirq/testing/consistent_channels.py +2 -2
- cirq/testing/consistent_controlled_gate_op.py +2 -2
- cirq/testing/consistent_controlled_gate_op_test.py +2 -1
- cirq/testing/consistent_decomposition.py +4 -2
- cirq/testing/consistent_phase_by.py +1 -1
- cirq/testing/consistent_protocols.py +2 -1
- cirq/testing/consistent_protocols_test.py +3 -3
- cirq/testing/consistent_qasm.py +4 -3
- cirq/testing/consistent_qasm_test.py +3 -3
- cirq/testing/consistent_resolve_parameters.py +1 -1
- cirq/testing/consistent_unitary.py +1 -1
- cirq/testing/consistent_unitary_test.py +1 -1
- cirq/testing/deprecation.py +1 -1
- cirq/testing/devices.py +3 -2
- cirq/testing/equals_tester.py +4 -3
- cirq/testing/equivalent_basis_map.py +4 -2
- cirq/testing/json.py +3 -2
- cirq/testing/lin_alg_utils.py +1 -1
- cirq/testing/logs.py +1 -1
- cirq/testing/op_tree.py +1 -1
- cirq/testing/order_tester.py +2 -2
- cirq/testing/pytest_utils.py +2 -1
- cirq/testing/random_circuit.py +2 -1
- cirq/testing/random_circuit_test.py +2 -1
- cirq/testing/repr_pretty_tester.py +3 -3
- cirq/transformers/__init__.py +1 -0
- cirq/transformers/_connected_component.py +231 -0
- cirq/transformers/_connected_component_test.py +200 -0
- cirq/transformers/align_test.py +13 -13
- cirq/transformers/analytical_decompositions/clifford_decomposition.py +8 -7
- cirq/transformers/analytical_decompositions/clifford_decomposition_test.py +5 -5
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +11 -10
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition_test.py +6 -6
- cirq/transformers/analytical_decompositions/cphase_to_fsim.py +3 -2
- cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +11 -10
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +8 -7
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition_test.py +17 -20
- cirq/transformers/analytical_decompositions/single_qubit_decompositions_test.py +33 -27
- cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry_test.py +1 -1
- cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +1 -1
- cirq/transformers/analytical_decompositions/two_qubit_state_preparation_test.py +12 -11
- cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +5 -2
- cirq/transformers/analytical_decompositions/two_qubit_to_cz_test.py +3 -3
- cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +2 -1
- cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +2 -1
- cirq/transformers/analytical_decompositions/two_qubit_to_ms_test.py +2 -2
- cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +2 -1
- cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap_test.py +32 -30
- cirq/transformers/drop_negligible_operations_test.py +7 -7
- cirq/transformers/dynamical_decoupling.py +185 -112
- cirq/transformers/dynamical_decoupling_test.py +195 -201
- cirq/transformers/eject_phased_paulis.py +2 -1
- cirq/transformers/eject_phased_paulis_test.py +3 -2
- cirq/transformers/eject_z.py +5 -3
- cirq/transformers/eject_z_test.py +23 -25
- cirq/transformers/expand_composite.py +3 -2
- cirq/transformers/expand_composite_test.py +14 -14
- cirq/transformers/gauge_compiling/__init__.py +13 -0
- cirq/transformers/gauge_compiling/gauge_compiling.py +3 -2
- cirq/transformers/gauge_compiling/gauge_compiling_test.py +14 -12
- cirq/transformers/gauge_compiling/gauge_compiling_test_utils.py +3 -3
- cirq/transformers/gauge_compiling/idle_moments_gauge.py +225 -0
- cirq/transformers/gauge_compiling/idle_moments_gauge_test.py +193 -0
- cirq/transformers/gauge_compiling/multi_moment_cphase_gauge.py +242 -0
- cirq/transformers/gauge_compiling/multi_moment_cphase_gauge_test.py +243 -0
- cirq/transformers/gauge_compiling/multi_moment_gauge_compiling.py +151 -0
- cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +2 -1
- cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils.py +1 -1
- cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils_test.py +6 -6
- cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +3 -2
- cirq/transformers/measurement_transformers.py +2 -1
- cirq/transformers/measurement_transformers_test.py +45 -39
- cirq/transformers/merge_k_qubit_gates.py +2 -1
- cirq/transformers/merge_k_qubit_gates_test.py +1 -1
- cirq/transformers/merge_single_qubit_gates.py +9 -5
- cirq/transformers/merge_single_qubit_gates_test.py +22 -22
- cirq/transformers/noise_adding_test.py +2 -2
- cirq/transformers/optimize_for_target_gateset.py +2 -1
- cirq/transformers/optimize_for_target_gateset_test.py +11 -9
- cirq/transformers/qubit_management_transformers_test.py +6 -2
- cirq/transformers/routing/mapping_manager.py +2 -1
- cirq/transformers/routing/route_circuit_cqc.py +2 -1
- cirq/transformers/stratify.py +2 -1
- cirq/transformers/symbolize.py +2 -1
- cirq/transformers/tag_transformers.py +2 -1
- cirq/transformers/target_gatesets/compilation_target_gateset.py +2 -1
- cirq/transformers/target_gatesets/cz_gateset.py +2 -1
- cirq/transformers/target_gatesets/cz_gateset_test.py +1 -1
- cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +2 -1
- cirq/transformers/transformer_api.py +2 -1
- cirq/transformers/transformer_primitives.py +271 -145
- cirq/transformers/transformer_primitives_test.py +185 -1
- cirq/value/abc_alt.py +2 -1
- cirq/value/classical_data.py +2 -1
- cirq/value/condition.py +2 -1
- cirq/value/digits.py +9 -2
- cirq/value/duration.py +6 -5
- cirq/value/linear_dict.py +4 -9
- cirq/value/measurement_key.py +2 -1
- cirq/value/periodic_value.py +3 -2
- cirq/value/product_state.py +2 -1
- cirq/value/value_equality_attr.py +2 -1
- cirq/vis/density_matrix.py +1 -1
- cirq/vis/heatmap.py +2 -1
- cirq/vis/histogram.py +2 -1
- cirq/vis/state_histogram.py +2 -1
- cirq/work/collector.py +2 -1
- cirq/work/observable_grouping.py +2 -1
- cirq/work/observable_measurement.py +2 -1
- cirq/work/observable_measurement_data.py +2 -1
- cirq/work/observable_measurement_test.py +1 -1
- cirq/work/observable_readout_calibration.py +2 -1
- cirq/work/observable_readout_calibration_test.py +1 -1
- cirq/work/observable_settings.py +2 -1
- cirq/work/sampler.py +2 -1
- cirq/work/sampler_test.py +1 -1
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/METADATA +5 -6
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/RECORD +425 -406
- cirq/contrib/json_test.py +0 -33
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/WHEEL +0 -0
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,225 @@
|
|
|
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
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import functools
|
|
17
|
+
from collections.abc import Hashable, Iterator, Sequence
|
|
18
|
+
from typing import TYPE_CHECKING
|
|
19
|
+
|
|
20
|
+
import attrs
|
|
21
|
+
import numpy as np
|
|
22
|
+
|
|
23
|
+
import cirq.circuits as circuits
|
|
24
|
+
import cirq.ops as ops
|
|
25
|
+
import cirq.protocols as protocols
|
|
26
|
+
import cirq.transformers.transformer_api as transformer_api
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
import cirq
|
|
30
|
+
|
|
31
|
+
_PAULIS: tuple[cirq.Gate, ...] = (ops.I, ops.X, ops.Y, ops.Z) # type: ignore[has-type]
|
|
32
|
+
_CLIFFORDS = tuple(ops.SingleQubitCliffordGate.all_single_qubit_cliffords)
|
|
33
|
+
_INV_CLIFFORDS = tuple(c**-1 for c in ops.SingleQubitCliffordGate.all_single_qubit_cliffords)
|
|
34
|
+
|
|
35
|
+
_NAME_TO_GATES = {'pauli': _PAULIS, 'clifford': _CLIFFORDS, 'inv_clifford': _INV_CLIFFORDS}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _gauges_arg_converter(gauges: str | Sequence[cirq.Gate] = 'clifford') -> tuple[cirq.Gate, ...]:
|
|
39
|
+
if isinstance(gauges, str):
|
|
40
|
+
if gauges not in _NAME_TO_GATES:
|
|
41
|
+
valid_names = tuple(_NAME_TO_GATES.keys())
|
|
42
|
+
raise ValueError(f"{gauges} is not a valid gauge name, valid names are {valid_names}")
|
|
43
|
+
return _NAME_TO_GATES[gauges]
|
|
44
|
+
return tuple(gauges)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _repr_fn(gauges: tuple[cirq.Gate, ...]) -> str:
|
|
48
|
+
if gauges is _PAULIS or gauges == _PAULIS:
|
|
49
|
+
return '"pauli"'
|
|
50
|
+
if gauges is _CLIFFORDS or gauges == _CLIFFORDS:
|
|
51
|
+
return '"clifford"'
|
|
52
|
+
if gauges is _INV_CLIFFORDS or gauges == _INV_CLIFFORDS:
|
|
53
|
+
return '"inv_clifford"'
|
|
54
|
+
return repr(gauges)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _get_structure(
|
|
58
|
+
active: list[tuple[int, bool]],
|
|
59
|
+
min_length: int,
|
|
60
|
+
n: int,
|
|
61
|
+
gauge_beginning: bool,
|
|
62
|
+
gauge_ending: bool,
|
|
63
|
+
) -> Iterator[tuple[int, int]]:
|
|
64
|
+
if gauge_beginning:
|
|
65
|
+
stop, is_mergable = active[0]
|
|
66
|
+
if min_length <= stop:
|
|
67
|
+
if is_mergable:
|
|
68
|
+
yield (0, stop)
|
|
69
|
+
else:
|
|
70
|
+
yield (0, stop - 1)
|
|
71
|
+
|
|
72
|
+
for i in range(len(active) - 1):
|
|
73
|
+
left_pos, left_is_mergable = active[i]
|
|
74
|
+
right_pos, right_is_mergable = active[i + 1]
|
|
75
|
+
if right_pos - left_pos - 1 >= min_length:
|
|
76
|
+
yield (left_pos + 1 - left_is_mergable, right_pos - 1 + right_is_mergable)
|
|
77
|
+
|
|
78
|
+
if gauge_ending:
|
|
79
|
+
stop, is_mergable = active[-1]
|
|
80
|
+
if min_length <= n - stop - 1:
|
|
81
|
+
if is_mergable:
|
|
82
|
+
yield (stop, n - 1)
|
|
83
|
+
else:
|
|
84
|
+
yield (stop + 1, n - 1)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _merge(g1: cirq.Gate, g2: cirq.Gate, q: cirq.Qid, tags: Sequence[Hashable]) -> cirq.Operation:
|
|
88
|
+
u1 = protocols.unitary(g1)
|
|
89
|
+
u2 = protocols.unitary(g2)
|
|
90
|
+
return ops.PhasedXZGate.from_matrix(u2 @ u1)(q).with_tags(*tags)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@transformer_api.transformer
|
|
94
|
+
@attrs.frozen
|
|
95
|
+
class IdleMomentsGauge:
|
|
96
|
+
r"""A transformer that inserts identity-preserving "gauge" gates around idle qubit moments.
|
|
97
|
+
|
|
98
|
+
This transformer identifies sequences of consecutive idle moments on a single qubit
|
|
99
|
+
that meet a `min_length` threshold. For each such sequence, it inserts a randomly
|
|
100
|
+
selected gate `G` from `gauges` at the start of the idle period and its inverse `G^-1`
|
|
101
|
+
at the end. This ensures the logical circuit behavior remains unchanged ($G \cdot G^{-1} = I$).
|
|
102
|
+
|
|
103
|
+
The primary goal is to introduce specific structure into idle periods, which is
|
|
104
|
+
useful for experiments.
|
|
105
|
+
|
|
106
|
+
Attributes:
|
|
107
|
+
min_length: Minimum number of consecutive idle moments for a gauge to be applied (>= 1).
|
|
108
|
+
|
|
109
|
+
gauges: A sequence of `cirq.Gate` objects to randomly select from.
|
|
110
|
+
Can be a custom tuple or a string alias:
|
|
111
|
+
- `"pauli"`: Uses single-qubit Pauli gates (I, X, Y, Z).
|
|
112
|
+
- `"clifford"`: Uses all 24 single-qubit Clifford gates.
|
|
113
|
+
|
|
114
|
+
gauge_beginning: If `True`, applies a gauge to idle moments at the circuit's start,
|
|
115
|
+
before any other qubit operation. Defaults to `False`.
|
|
116
|
+
|
|
117
|
+
gauge_ending: If `True`, applies a gauge to idle moments at the circuit's end,
|
|
118
|
+
after the last qubit operation. Defaults to `False`.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
min_length: int = attrs.field(
|
|
122
|
+
validator=(attrs.validators.instance_of(int), attrs.validators.ge(1))
|
|
123
|
+
)
|
|
124
|
+
gauges: tuple[cirq.Gate, ...] = attrs.field(
|
|
125
|
+
default='clifford', converter=_gauges_arg_converter, repr=_repr_fn
|
|
126
|
+
)
|
|
127
|
+
gauge_beginning: bool = False
|
|
128
|
+
gauge_ending: bool = False
|
|
129
|
+
|
|
130
|
+
@functools.cached_property
|
|
131
|
+
def gauges_inverse(self) -> tuple[cirq.Gate, ...]:
|
|
132
|
+
if self.gauges is _PAULIS:
|
|
133
|
+
return _PAULIS
|
|
134
|
+
if self.gauges is _CLIFFORDS:
|
|
135
|
+
return _INV_CLIFFORDS
|
|
136
|
+
if self.gauges is _INV_CLIFFORDS:
|
|
137
|
+
return _CLIFFORDS
|
|
138
|
+
return tuple(g**-1 for g in self.gauges)
|
|
139
|
+
|
|
140
|
+
def __call__(
|
|
141
|
+
self,
|
|
142
|
+
circuit: cirq.AbstractCircuit,
|
|
143
|
+
*,
|
|
144
|
+
context: transformer_api.TransformerContext | None = None,
|
|
145
|
+
rng_or_seed: np.random.Generator | int | None = None,
|
|
146
|
+
):
|
|
147
|
+
"""Apply the IdleMomentGauge transformer.
|
|
148
|
+
|
|
149
|
+
Args:
|
|
150
|
+
circuit: The circuit to process.
|
|
151
|
+
context: The TransformerContext.
|
|
152
|
+
rng_or_seed: The source of randomness.
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
A transformed circuit.
|
|
156
|
+
|
|
157
|
+
Raises:
|
|
158
|
+
ValueError: if the TransformerContext has deep=True.
|
|
159
|
+
"""
|
|
160
|
+
rng = (
|
|
161
|
+
rng_or_seed
|
|
162
|
+
if isinstance(rng_or_seed, np.random.Generator)
|
|
163
|
+
else np.random.default_rng(rng_or_seed)
|
|
164
|
+
)
|
|
165
|
+
context = (
|
|
166
|
+
context
|
|
167
|
+
if isinstance(context, transformer_api.TransformerContext)
|
|
168
|
+
else transformer_api.TransformerContext(deep=False)
|
|
169
|
+
)
|
|
170
|
+
if context.deep:
|
|
171
|
+
raise ValueError("IdleMomentsGauge doesn't support deep TransformerContext")
|
|
172
|
+
|
|
173
|
+
tags_to_ignore = frozenset(context.tags_to_ignore)
|
|
174
|
+
all_qubits = circuit.all_qubits()
|
|
175
|
+
|
|
176
|
+
active_moments: dict[cirq.Qid, list[tuple[int, bool]]] = {q: [] for q in all_qubits}
|
|
177
|
+
for m_id, moment in enumerate(circuit):
|
|
178
|
+
if not tags_to_ignore.isdisjoint(moment.tags):
|
|
179
|
+
for q in all_qubits:
|
|
180
|
+
active_moments[q].append((m_id, False))
|
|
181
|
+
else:
|
|
182
|
+
for op in moment:
|
|
183
|
+
is_mergable = (
|
|
184
|
+
len(op.qubits) == 1
|
|
185
|
+
and tags_to_ignore.isdisjoint(op.tags)
|
|
186
|
+
and op.gate is not None
|
|
187
|
+
)
|
|
188
|
+
for q in op.qubits:
|
|
189
|
+
active_moments[q].append((m_id, is_mergable))
|
|
190
|
+
|
|
191
|
+
single_qubit_moments = [
|
|
192
|
+
{op.qubits[0]: op for op in m if len(op.qubits) == 1} for m in circuit
|
|
193
|
+
]
|
|
194
|
+
non_single_qubit_moments = [[op for op in m if len(op.qubits) != 1] for m in circuit]
|
|
195
|
+
|
|
196
|
+
for q, active in active_moments.items():
|
|
197
|
+
for s, e in _get_structure(
|
|
198
|
+
active, self.min_length, len(circuit), self.gauge_beginning, self.gauge_ending
|
|
199
|
+
):
|
|
200
|
+
gate_index = rng.choice(len(self.gauges))
|
|
201
|
+
gate = self.gauges[gate_index]
|
|
202
|
+
gate_inv = self.gauges_inverse[gate_index]
|
|
203
|
+
|
|
204
|
+
if existing_op := single_qubit_moments[s].get(q, None):
|
|
205
|
+
existing_gate = existing_op.gate
|
|
206
|
+
assert existing_gate is not None
|
|
207
|
+
single_qubit_moments[s][q] = _merge(existing_gate, gate, q, existing_op.tags)
|
|
208
|
+
else:
|
|
209
|
+
single_qubit_moments[s][q] = gate(q)
|
|
210
|
+
|
|
211
|
+
if existing_op := single_qubit_moments[e].get(q, None):
|
|
212
|
+
existing_gate = existing_op.gate
|
|
213
|
+
assert existing_gate is not None
|
|
214
|
+
single_qubit_moments[e][q] = _merge(
|
|
215
|
+
gate_inv, existing_gate, q, existing_op.tags
|
|
216
|
+
)
|
|
217
|
+
else:
|
|
218
|
+
single_qubit_moments[e][q] = gate_inv(q)
|
|
219
|
+
|
|
220
|
+
return circuits.Circuit.from_moments(
|
|
221
|
+
*(
|
|
222
|
+
[op for op in sq.values()] + nsq
|
|
223
|
+
for sq, nsq in zip(single_qubit_moments, non_single_qubit_moments, strict=True)
|
|
224
|
+
)
|
|
225
|
+
)
|
|
@@ -0,0 +1,193 @@
|
|
|
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
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
import pytest
|
|
18
|
+
|
|
19
|
+
import cirq
|
|
20
|
+
from cirq.transformers import gauge_compiling
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class _MockRng(np.random.Generator):
|
|
24
|
+
|
|
25
|
+
def __init__(self, vals):
|
|
26
|
+
self._n_calls = 0
|
|
27
|
+
self._res = tuple(vals)
|
|
28
|
+
|
|
29
|
+
def choice(self, seq):
|
|
30
|
+
ret = self._res[self._n_calls]
|
|
31
|
+
self._n_calls += 1
|
|
32
|
+
return ret
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_add_gauge_merges_gates():
|
|
36
|
+
tr = gauge_compiling.IdleMomentsGauge(2, gauges='pauli')
|
|
37
|
+
|
|
38
|
+
circuit = cirq.Circuit.from_moments([], [], [], cirq.X(cirq.q(0)), [], [], cirq.X(cirq.q(0)))
|
|
39
|
+
transformed_circuit = tr(circuit, rng_or_seed=_MockRng([3]))
|
|
40
|
+
|
|
41
|
+
assert transformed_circuit == cirq.Circuit.from_moments(
|
|
42
|
+
[],
|
|
43
|
+
[],
|
|
44
|
+
[],
|
|
45
|
+
cirq.PhasedXZGate(axis_phase_exponent=0.5, x_exponent=1, z_exponent=0)(cirq.q(0)),
|
|
46
|
+
[],
|
|
47
|
+
[],
|
|
48
|
+
cirq.PhasedXZGate(axis_phase_exponent=0.5, x_exponent=1, z_exponent=0)(cirq.q(0)),
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_add_gauge_respects_ignore_tag():
|
|
53
|
+
tr = gauge_compiling.IdleMomentsGauge(2, gauges='pauli')
|
|
54
|
+
|
|
55
|
+
circuit = cirq.Circuit.from_moments(
|
|
56
|
+
cirq.X(cirq.q(0)), [], [], cirq.X(cirq.q(0)).with_tags('ignore')
|
|
57
|
+
)
|
|
58
|
+
transformed_circuit = tr(
|
|
59
|
+
circuit,
|
|
60
|
+
context=cirq.TransformerContext(tags_to_ignore=("ignore",)),
|
|
61
|
+
rng_or_seed=_MockRng([3]),
|
|
62
|
+
)
|
|
63
|
+
assert transformed_circuit == cirq.Circuit.from_moments(
|
|
64
|
+
cirq.PhasedXZGate(axis_phase_exponent=0.5, x_exponent=1, z_exponent=0)(cirq.q(0)),
|
|
65
|
+
[],
|
|
66
|
+
cirq.Z(cirq.q(0)),
|
|
67
|
+
cirq.X(cirq.q(0)).with_tags('ignore'),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_add_gauge_respects_ignore_moment():
|
|
72
|
+
tr = gauge_compiling.IdleMomentsGauge(2, gauges='pauli')
|
|
73
|
+
|
|
74
|
+
circuit = cirq.Circuit.from_moments(
|
|
75
|
+
cirq.X(cirq.q(0)), [], [], cirq.Moment(cirq.X(cirq.q(0))).with_tags('ignore')
|
|
76
|
+
)
|
|
77
|
+
transformed_circuit = tr(
|
|
78
|
+
circuit,
|
|
79
|
+
context=cirq.TransformerContext(tags_to_ignore=("ignore",)),
|
|
80
|
+
rng_or_seed=_MockRng([3]),
|
|
81
|
+
)
|
|
82
|
+
assert transformed_circuit == cirq.Circuit.from_moments(
|
|
83
|
+
cirq.PhasedXZGate(axis_phase_exponent=0.5, x_exponent=1, z_exponent=0)(cirq.q(0)),
|
|
84
|
+
[],
|
|
85
|
+
cirq.Z(cirq.q(0)),
|
|
86
|
+
cirq.Moment(cirq.X(cirq.q(0))).with_tags('ignore'),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_add_gauge_on_prefix():
|
|
91
|
+
tr = gauge_compiling.IdleMomentsGauge(3, gauges='clifford', gauge_beginning=True)
|
|
92
|
+
|
|
93
|
+
circuit = cirq.Circuit.from_moments([], [], [], cirq.CNOT(cirq.q(0), cirq.q(1)))
|
|
94
|
+
transformed_circuit = tr(circuit, rng_or_seed=_MockRng([20, 15]))
|
|
95
|
+
assert transformed_circuit == cirq.Circuit.from_moments(
|
|
96
|
+
[
|
|
97
|
+
cirq.SingleQubitCliffordGate.all_single_qubit_cliffords[20](cirq.q(0)),
|
|
98
|
+
cirq.SingleQubitCliffordGate.all_single_qubit_cliffords[15](cirq.q(1)),
|
|
99
|
+
],
|
|
100
|
+
[],
|
|
101
|
+
[
|
|
102
|
+
cirq.SingleQubitCliffordGate.all_single_qubit_cliffords[20](cirq.q(0)) ** -1,
|
|
103
|
+
cirq.SingleQubitCliffordGate.all_single_qubit_cliffords[15](cirq.q(1)) ** -1,
|
|
104
|
+
],
|
|
105
|
+
cirq.CNOT(cirq.q(0), cirq.q(1)),
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_add_gauge_on_prefix_with_merge():
|
|
110
|
+
tr = gauge_compiling.IdleMomentsGauge(3, gauges=[cirq.Y], gauge_beginning=True)
|
|
111
|
+
|
|
112
|
+
circuit = cirq.Circuit.from_moments([], [], [], cirq.X(cirq.q(0)))
|
|
113
|
+
transformed_circuit = tr(circuit, rng_or_seed=_MockRng([0]))
|
|
114
|
+
assert transformed_circuit == cirq.Circuit.from_moments(
|
|
115
|
+
[cirq.Y(cirq.q(0))],
|
|
116
|
+
[],
|
|
117
|
+
[],
|
|
118
|
+
cirq.PhasedXZGate(axis_phase_exponent=0, x_exponent=0, z_exponent=1)(cirq.q(0)),
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def test_add_gauge_on_suffix():
|
|
123
|
+
tr = gauge_compiling.IdleMomentsGauge(3, gauges='inv_clifford', gauge_ending=True)
|
|
124
|
+
|
|
125
|
+
circuit = cirq.Circuit.from_moments(cirq.CNOT(cirq.q(0), cirq.q(1)), [], [], [])
|
|
126
|
+
transformed_circuit = tr(circuit, rng_or_seed=_MockRng([20, 15]))
|
|
127
|
+
assert transformed_circuit == cirq.Circuit.from_moments(
|
|
128
|
+
cirq.CNOT(cirq.q(0), cirq.q(1)),
|
|
129
|
+
[
|
|
130
|
+
cirq.SingleQubitCliffordGate.all_single_qubit_cliffords[20](cirq.q(0)) ** -1,
|
|
131
|
+
cirq.SingleQubitCliffordGate.all_single_qubit_cliffords[15](cirq.q(1)) ** -1,
|
|
132
|
+
],
|
|
133
|
+
[],
|
|
134
|
+
[
|
|
135
|
+
cirq.SingleQubitCliffordGate.all_single_qubit_cliffords[20](cirq.q(0)),
|
|
136
|
+
cirq.SingleQubitCliffordGate.all_single_qubit_cliffords[15](cirq.q(1)),
|
|
137
|
+
],
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_add_gauge_on_suffix_with_merge():
|
|
142
|
+
tr = gauge_compiling.IdleMomentsGauge(3, gauges=[cirq.Y], gauge_ending=True)
|
|
143
|
+
|
|
144
|
+
circuit = cirq.Circuit.from_moments(cirq.X(cirq.q(0)), [], [], [])
|
|
145
|
+
transformed_circuit = tr(circuit, rng_or_seed=_MockRng([0]))
|
|
146
|
+
assert transformed_circuit == cirq.Circuit.from_moments(
|
|
147
|
+
cirq.PhasedXZGate(axis_phase_exponent=0, x_exponent=0, z_exponent=1)(cirq.q(0)),
|
|
148
|
+
[],
|
|
149
|
+
[],
|
|
150
|
+
cirq.Y(cirq.q(0)),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def test_add_gauge_respects_min_length():
|
|
155
|
+
tr = gauge_compiling.IdleMomentsGauge(2, gauges=[cirq.X])
|
|
156
|
+
|
|
157
|
+
circuit = cirq.Circuit.from_moments(cirq.X(cirq.q(0)), [], cirq.X(cirq.q(0)))
|
|
158
|
+
transformed_circuit = tr(circuit)
|
|
159
|
+
assert transformed_circuit == circuit
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_context_with_deep_raises():
|
|
163
|
+
tr = gauge_compiling.IdleMomentsGauge(2, gauges=[cirq.X])
|
|
164
|
+
|
|
165
|
+
circuit = cirq.Circuit.from_moments(cirq.X(cirq.q(0)), [], cirq.X(cirq.q(0)))
|
|
166
|
+
with pytest.raises(
|
|
167
|
+
ValueError, match="IdleMomentsGauge doesn't support deep TransformerContext"
|
|
168
|
+
):
|
|
169
|
+
_ = tr(circuit, context=cirq.TransformerContext(deep=True))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def test_gauge_with_invalid_name_raises():
|
|
173
|
+
with pytest.raises(ValueError, match='valid gauge'):
|
|
174
|
+
_ = gauge_compiling.IdleMomentsGauge(2, gauges='invalid')
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def test_repr():
|
|
178
|
+
assert repr(gauge_compiling.IdleMomentsGauge(3, gauges='pauli')) == (
|
|
179
|
+
'IdleMomentsGauge(min_length=3, gauges="pauli", '
|
|
180
|
+
'gauge_beginning=False, gauge_ending=False)'
|
|
181
|
+
)
|
|
182
|
+
assert repr(gauge_compiling.IdleMomentsGauge(4, gauges='clifford')) == (
|
|
183
|
+
'IdleMomentsGauge(min_length=4, gauges="clifford", '
|
|
184
|
+
'gauge_beginning=False, gauge_ending=False)'
|
|
185
|
+
)
|
|
186
|
+
assert repr(gauge_compiling.IdleMomentsGauge(5, gauges='inv_clifford')) == (
|
|
187
|
+
'IdleMomentsGauge(min_length=5, gauges="inv_clifford", '
|
|
188
|
+
'gauge_beginning=False, gauge_ending=False)'
|
|
189
|
+
)
|
|
190
|
+
assert repr(gauge_compiling.IdleMomentsGauge(6, gauges=[cirq.X])) == (
|
|
191
|
+
'IdleMomentsGauge(min_length=6, gauges=(cirq.X,), '
|
|
192
|
+
'gauge_beginning=False, gauge_ending=False)'
|
|
193
|
+
)
|
|
@@ -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
|