cirq-core 1.5.0.dev20250409222543__py3-none-any.whl → 1.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cirq-core might be problematic. Click here for more details.
- cirq/__init__.py +16 -17
- cirq/_compat.py +21 -20
- cirq/_compat_test.py +14 -34
- cirq/_doc.py +4 -2
- cirq/_import.py +8 -6
- cirq/_import_test.py +4 -2
- cirq/_version.py +6 -6
- cirq/_version_test.py +2 -2
- cirq/circuits/_block_diagram_drawer.py +11 -10
- cirq/circuits/_block_diagram_drawer_test.py +8 -6
- cirq/circuits/_box_drawing_character_data.py +8 -8
- cirq/circuits/_box_drawing_character_data_test.py +3 -1
- cirq/circuits/_bucket_priority_queue.py +9 -7
- cirq/circuits/_bucket_priority_queue_test.py +22 -20
- cirq/circuits/circuit.py +248 -172
- cirq/circuits/circuit_operation.py +73 -83
- cirq/circuits/circuit_operation_test.py +128 -90
- cirq/circuits/circuit_test.py +211 -151
- cirq/circuits/frozen_circuit.py +23 -60
- cirq/circuits/frozen_circuit_test.py +31 -8
- cirq/circuits/insert_strategy.py +7 -5
- cirq/circuits/insert_strategy_test.py +4 -2
- cirq/circuits/moment.py +88 -40
- cirq/circuits/moment_test.py +128 -51
- cirq/circuits/optimization_pass.py +5 -5
- cirq/circuits/optimization_pass_test.py +10 -10
- cirq/circuits/qasm_output.py +11 -11
- cirq/circuits/qasm_output_test.py +25 -22
- cirq/circuits/text_diagram_drawer.py +23 -38
- cirq/circuits/text_diagram_drawer_test.py +19 -17
- cirq/conftest.py +4 -3
- cirq/contrib/__init__.py +4 -4
- cirq/contrib/acquaintance/__init__.py +1 -1
- cirq/contrib/acquaintance/bipartite.py +5 -8
- cirq/contrib/acquaintance/bipartite_test.py +18 -13
- cirq/contrib/acquaintance/devices.py +2 -2
- cirq/contrib/acquaintance/devices_test.py +5 -3
- cirq/contrib/acquaintance/executor.py +5 -5
- cirq/contrib/acquaintance/executor_test.py +13 -9
- cirq/contrib/acquaintance/gates.py +18 -28
- cirq/contrib/acquaintance/gates_test.py +24 -20
- cirq/contrib/acquaintance/inspection_utils.py +8 -4
- cirq/contrib/acquaintance/inspection_utils_test.py +4 -2
- cirq/contrib/acquaintance/mutation_utils.py +4 -4
- cirq/contrib/acquaintance/mutation_utils_test.py +4 -2
- cirq/contrib/acquaintance/optimizers.py +4 -4
- cirq/contrib/acquaintance/optimizers_test.py +4 -1
- cirq/contrib/acquaintance/permutation.py +15 -27
- cirq/contrib/acquaintance/permutation_test.py +26 -17
- cirq/contrib/acquaintance/shift.py +4 -4
- cirq/contrib/acquaintance/shift_swap_network.py +4 -4
- cirq/contrib/acquaintance/shift_swap_network_test.py +9 -6
- cirq/contrib/acquaintance/shift_test.py +8 -6
- cirq/contrib/acquaintance/strategies/cubic.py +2 -2
- cirq/contrib/acquaintance/strategies/cubic_test.py +4 -2
- cirq/contrib/acquaintance/strategies/quartic_paired.py +6 -6
- cirq/contrib/acquaintance/strategies/quartic_paired_test.py +10 -6
- cirq/contrib/acquaintance/testing.py +2 -0
- cirq/contrib/acquaintance/topological_sort.py +2 -2
- cirq/contrib/acquaintance/topological_sort_test.py +3 -1
- cirq/contrib/bayesian_network/bayesian_network_gate.py +9 -10
- cirq/contrib/bayesian_network/bayesian_network_gate_test.py +14 -9
- cirq/contrib/circuitdag/circuit_dag.py +4 -4
- cirq/contrib/circuitdag/circuit_dag_test.py +17 -15
- cirq/contrib/custom_simulators/custom_state_simulator.py +5 -5
- cirq/contrib/custom_simulators/custom_state_simulator_test.py +22 -17
- cirq/contrib/graph_device/graph_device.py +12 -11
- cirq/contrib/graph_device/graph_device_test.py +18 -14
- cirq/contrib/graph_device/hypergraph.py +16 -14
- cirq/contrib/graph_device/hypergraph_test.py +13 -11
- cirq/contrib/graph_device/uniform_graph_device.py +6 -4
- cirq/contrib/graph_device/uniform_graph_device_test.py +11 -3
- cirq/contrib/hacks/disable_validation.py +6 -1
- cirq/contrib/hacks/disable_validation_test.py +3 -1
- cirq/contrib/json.py +31 -5
- cirq/contrib/json_test.py +6 -3
- cirq/contrib/json_test_data/DampedReadoutNoiseModel.json +12 -0
- cirq/contrib/json_test_data/DampedReadoutNoiseModel.repr +4 -0
- cirq/contrib/json_test_data/DepolarizingNoiseModel.json +12 -0
- cirq/contrib/json_test_data/DepolarizingNoiseModel.repr +4 -0
- cirq/contrib/json_test_data/DepolarizingWithDampedReadoutNoiseModel.json +6 -0
- cirq/contrib/json_test_data/DepolarizingWithDampedReadoutNoiseModel.repr +1 -0
- cirq/contrib/json_test_data/DepolarizingWithReadoutNoiseModel.json +5 -0
- cirq/contrib/json_test_data/DepolarizingWithReadoutNoiseModel.repr +1 -0
- cirq/contrib/json_test_data/ReadoutNoiseModel.json +12 -0
- cirq/contrib/json_test_data/ReadoutNoiseModel.repr +4 -0
- cirq/contrib/json_test_data/__init__.py +17 -0
- cirq/contrib/json_test_data/spec.py +32 -0
- cirq/contrib/noise_models/noise_models.py +119 -5
- cirq/contrib/noise_models/noise_models_test.py +37 -9
- cirq/contrib/paulistring/clifford_optimize.py +6 -4
- cirq/contrib/paulistring/clifford_optimize_test.py +6 -5
- cirq/contrib/paulistring/clifford_target_gateset.py +10 -10
- cirq/contrib/paulistring/clifford_target_gateset_test.py +13 -11
- cirq/contrib/paulistring/optimize.py +2 -0
- cirq/contrib/paulistring/optimize_test.py +4 -3
- cirq/contrib/paulistring/pauli_string_dag.py +2 -0
- cirq/contrib/paulistring/pauli_string_dag_test.py +3 -1
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +255 -120
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +398 -19
- cirq/contrib/paulistring/pauli_string_optimize.py +7 -1
- cirq/contrib/paulistring/pauli_string_optimize_test.py +5 -3
- cirq/contrib/paulistring/recombine.py +6 -4
- cirq/contrib/paulistring/recombine_test.py +3 -1
- cirq/contrib/paulistring/separate.py +9 -6
- cirq/contrib/paulistring/separate_test.py +3 -1
- cirq/contrib/qasm_import/_lexer.py +3 -2
- cirq/contrib/qasm_import/_lexer_test.py +49 -13
- cirq/contrib/qasm_import/_parser.py +547 -83
- cirq/contrib/qasm_import/_parser_test.py +988 -97
- cirq/contrib/qasm_import/exception.py +2 -0
- cirq/contrib/qasm_import/qasm.py +8 -2
- cirq/contrib/qasm_import/qasm_test.py +7 -4
- cirq/contrib/qcircuit/qcircuit_diagram_info.py +5 -5
- cirq/contrib/qcircuit/qcircuit_diagram_info_test.py +4 -1
- cirq/contrib/qcircuit/qcircuit_pdf.py +7 -3
- cirq/contrib/qcircuit/qcircuit_pdf_test.py +3 -1
- cirq/contrib/qcircuit/qcircuit_test.py +10 -8
- cirq/contrib/quantum_volume/quantum_volume.py +31 -27
- cirq/contrib/quantum_volume/quantum_volume_test.py +19 -16
- cirq/contrib/quimb/density_matrix.py +15 -14
- cirq/contrib/quimb/density_matrix_test.py +10 -7
- cirq/contrib/quimb/grid_circuits.py +5 -2
- cirq/contrib/quimb/grid_circuits_test.py +3 -0
- cirq/contrib/quimb/mps_simulator.py +20 -20
- cirq/contrib/quimb/mps_simulator_test.py +3 -0
- cirq/contrib/quimb/state_vector.py +12 -11
- cirq/contrib/quimb/state_vector_test.py +3 -0
- cirq/contrib/quirk/export_to_quirk.py +5 -3
- cirq/contrib/quirk/export_to_quirk_test.py +18 -16
- cirq/contrib/quirk/linearize_circuit.py +2 -0
- cirq/contrib/quirk/quirk_gate.py +18 -17
- cirq/contrib/routing/device.py +5 -3
- cirq/contrib/routing/device_test.py +2 -0
- cirq/contrib/routing/greedy.py +10 -21
- cirq/contrib/routing/greedy_test.py +4 -2
- cirq/contrib/routing/initialization.py +2 -2
- cirq/contrib/routing/initialization_test.py +5 -3
- cirq/contrib/routing/router.py +9 -5
- cirq/contrib/routing/router_test.py +2 -0
- cirq/contrib/routing/swap_network.py +3 -3
- cirq/contrib/routing/swap_network_test.py +3 -1
- cirq/contrib/routing/utils.py +2 -2
- cirq/contrib/routing/utils_test.py +3 -0
- cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +15 -9
- cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking_test.py +3 -0
- cirq/contrib/svg/svg.py +3 -3
- cirq/contrib/svg/svg_test.py +8 -5
- cirq/devices/device.py +4 -4
- cirq/devices/device_test.py +7 -4
- cirq/devices/grid_device_metadata.py +10 -10
- cirq/devices/grid_device_metadata_test.py +3 -0
- cirq/devices/grid_qubit.py +29 -21
- cirq/devices/grid_qubit_test.py +3 -0
- cirq/devices/insertion_noise_model.py +7 -7
- cirq/devices/insertion_noise_model_test.py +7 -5
- cirq/devices/line_qubit.py +13 -13
- cirq/devices/line_qubit_test.py +2 -0
- cirq/devices/named_topologies.py +18 -29
- cirq/devices/named_topologies_test.py +13 -10
- cirq/devices/noise_model.py +3 -3
- cirq/devices/noise_model_test.py +19 -15
- cirq/devices/noise_properties.py +15 -6
- cirq/devices/noise_properties_test.py +34 -3
- cirq/devices/noise_utils.py +11 -9
- cirq/devices/noise_utils_test.py +2 -0
- cirq/devices/superconducting_qubits_noise_properties.py +23 -22
- cirq/devices/superconducting_qubits_noise_properties_test.py +6 -6
- cirq/devices/thermal_noise_model.py +107 -37
- cirq/devices/thermal_noise_model_test.py +21 -0
- cirq/devices/unconstrained_device.py +5 -3
- cirq/devices/unconstrained_device_test.py +2 -0
- cirq/experiments/__init__.py +4 -2
- cirq/experiments/benchmarking/__init__.py +17 -0
- cirq/experiments/benchmarking/parallel_xeb.py +677 -0
- cirq/experiments/benchmarking/parallel_xeb_test.py +447 -0
- cirq/experiments/fidelity_estimation.py +14 -8
- cirq/experiments/fidelity_estimation_test.py +3 -0
- cirq/experiments/n_qubit_tomography.py +17 -16
- cirq/experiments/n_qubit_tomography_test.py +8 -5
- cirq/experiments/purity_estimation.py +2 -0
- cirq/experiments/purity_estimation_test.py +2 -0
- cirq/experiments/qubit_characterizations.py +207 -103
- cirq/experiments/qubit_characterizations_test.py +40 -12
- cirq/experiments/random_quantum_circuit_generation.py +56 -70
- cirq/experiments/random_quantum_circuit_generation_test.py +11 -8
- cirq/experiments/readout_confusion_matrix.py +24 -22
- cirq/experiments/readout_confusion_matrix_test.py +2 -0
- cirq/experiments/single_qubit_readout_calibration.py +30 -15
- cirq/experiments/single_qubit_readout_calibration_test.py +5 -2
- cirq/experiments/t1_decay_experiment.py +9 -7
- cirq/experiments/t1_decay_experiment_test.py +13 -11
- cirq/experiments/t2_decay_experiment.py +16 -13
- cirq/experiments/t2_decay_experiment_test.py +2 -0
- cirq/experiments/two_qubit_xeb.py +64 -57
- cirq/experiments/two_qubit_xeb_test.py +10 -6
- cirq/experiments/xeb_fitting.py +39 -35
- cirq/experiments/xeb_sampling.py +37 -44
- cirq/experiments/xeb_sampling_test.py +3 -0
- cirq/experiments/xeb_simulation.py +14 -10
- cirq/experiments/xeb_simulation_test.py +5 -5
- cirq/experiments/z_phase_calibration.py +32 -29
- cirq/experiments/z_phase_calibration_test.py +3 -4
- cirq/interop/quirk/cells/__init__.py +1 -1
- cirq/interop/quirk/cells/all_cells.py +7 -2
- cirq/interop/quirk/cells/arithmetic_cells.py +29 -41
- cirq/interop/quirk/cells/arithmetic_cells_test.py +17 -14
- cirq/interop/quirk/cells/cell.py +19 -28
- cirq/interop/quirk/cells/cell_test.py +3 -0
- cirq/interop/quirk/cells/composite_cell.py +13 -28
- cirq/interop/quirk/cells/composite_cell_test.py +2 -0
- cirq/interop/quirk/cells/control_cells.py +15 -15
- cirq/interop/quirk/cells/control_cells_test.py +7 -5
- cirq/interop/quirk/cells/frequency_space_cells.py +4 -3
- cirq/interop/quirk/cells/frequency_space_cells_test.py +3 -1
- cirq/interop/quirk/cells/ignored_cells.py +3 -0
- cirq/interop/quirk/cells/ignored_cells_test.py +3 -1
- cirq/interop/quirk/cells/input_cells.py +7 -5
- cirq/interop/quirk/cells/input_cells_test.py +7 -5
- cirq/interop/quirk/cells/input_rotation_cells.py +15 -13
- cirq/interop/quirk/cells/input_rotation_cells_test.py +9 -7
- cirq/interop/quirk/cells/measurement_cells.py +5 -2
- cirq/interop/quirk/cells/measurement_cells_test.py +3 -1
- cirq/interop/quirk/cells/parse.py +22 -23
- cirq/interop/quirk/cells/parse_test.py +12 -10
- cirq/interop/quirk/cells/qubit_permutation_cells.py +5 -3
- cirq/interop/quirk/cells/qubit_permutation_cells_test.py +9 -7
- cirq/interop/quirk/cells/scalar_cells.py +4 -1
- cirq/interop/quirk/cells/scalar_cells_test.py +3 -1
- cirq/interop/quirk/cells/single_qubit_rotation_cells.py +5 -2
- cirq/interop/quirk/cells/single_qubit_rotation_cells_test.py +5 -3
- cirq/interop/quirk/cells/swap_cell.py +8 -6
- cirq/interop/quirk/cells/swap_cell_test.py +6 -4
- cirq/interop/quirk/cells/testing.py +6 -6
- cirq/interop/quirk/cells/testing_test.py +8 -6
- cirq/interop/quirk/cells/unsupported_cells.py +3 -0
- cirq/interop/quirk/cells/unsupported_cells_test.py +4 -2
- cirq/interop/quirk/url_to_circuit.py +23 -36
- cirq/interop/quirk/url_to_circuit_test.py +4 -1
- cirq/json_resolver_cache.py +14 -12
- cirq/linalg/__init__.py +4 -6
- cirq/linalg/combinators.py +7 -5
- cirq/linalg/combinators_test.py +10 -7
- cirq/linalg/decompositions.py +24 -35
- cirq/linalg/decompositions_test.py +3 -1
- cirq/linalg/diagonalize.py +6 -4
- cirq/linalg/diagonalize_test.py +15 -14
- cirq/linalg/operator_spaces.py +14 -14
- cirq/linalg/operator_spaces_test.py +13 -11
- cirq/linalg/predicates.py +18 -9
- cirq/linalg/predicates_test.py +5 -0
- cirq/linalg/tolerance.py +6 -3
- cirq/linalg/tolerance_test.py +6 -4
- cirq/linalg/transformations.py +23 -20
- cirq/linalg/transformations_test.py +73 -43
- cirq/neutral_atoms/convert_to_neutral_atom_gates.py +9 -3
- cirq/neutral_atoms/convert_to_neutral_atom_gates_test.py +3 -1
- cirq/neutral_atoms/neutral_atom_devices.py +2 -0
- cirq/ops/__init__.py +2 -0
- cirq/ops/arithmetic_operation.py +21 -21
- cirq/ops/arithmetic_operation_test.py +7 -8
- cirq/ops/boolean_hamiltonian.py +23 -22
- cirq/ops/boolean_hamiltonian_test.py +12 -9
- cirq/ops/classically_controlled_operation.py +31 -36
- cirq/ops/classically_controlled_operation_test.py +121 -117
- cirq/ops/clifford_gate.py +98 -81
- cirq/ops/clifford_gate_test.py +72 -57
- cirq/ops/common_channels.py +44 -44
- cirq/ops/common_channels_test.py +83 -81
- cirq/ops/common_gate_families.py +9 -7
- cirq/ops/common_gate_families_test.py +11 -7
- cirq/ops/common_gates.py +164 -183
- cirq/ops/common_gates_test.py +135 -95
- cirq/ops/control_values.py +23 -26
- cirq/ops/control_values_test.py +22 -20
- cirq/ops/controlled_gate.py +64 -112
- cirq/ops/controlled_gate_test.py +130 -35
- cirq/ops/controlled_operation.py +24 -35
- cirq/ops/controlled_operation_test.py +8 -6
- cirq/ops/dense_pauli_string.py +38 -49
- cirq/ops/dense_pauli_string_test.py +4 -2
- cirq/ops/diagonal_gate.py +18 -31
- cirq/ops/diagonal_gate_test.py +13 -13
- cirq/ops/eigen_gate.py +29 -29
- cirq/ops/eigen_gate_test.py +45 -28
- cirq/ops/fourier_transform.py +14 -20
- cirq/ops/fourier_transform_test.py +15 -12
- cirq/ops/fsim_gate.py +43 -42
- cirq/ops/fsim_gate_test.py +29 -29
- cirq/ops/gate_features.py +2 -0
- cirq/ops/gate_features_test.py +5 -3
- cirq/ops/gate_operation.py +43 -65
- cirq/ops/gate_operation_test.py +46 -42
- cirq/ops/gateset.py +28 -40
- cirq/ops/gateset_test.py +4 -2
- cirq/ops/global_phase_op.py +45 -20
- cirq/ops/global_phase_op_test.py +44 -20
- cirq/ops/greedy_qubit_manager.py +10 -8
- cirq/ops/greedy_qubit_manager_test.py +5 -3
- cirq/ops/identity.py +14 -12
- cirq/ops/identity_test.py +24 -20
- cirq/ops/kraus_channel.py +11 -8
- cirq/ops/kraus_channel_test.py +14 -11
- cirq/ops/linear_combinations.py +65 -77
- cirq/ops/linear_combinations_test.py +14 -9
- cirq/ops/matrix_gates.py +21 -18
- cirq/ops/matrix_gates_test.py +16 -0
- cirq/ops/measure_util.py +15 -20
- cirq/ops/measure_util_test.py +2 -0
- cirq/ops/measurement_gate.py +26 -37
- cirq/ops/measurement_gate_test.py +2 -0
- cirq/ops/mixed_unitary_channel.py +12 -9
- cirq/ops/mixed_unitary_channel_test.py +14 -11
- cirq/ops/named_qubit.py +16 -13
- cirq/ops/named_qubit_test.py +15 -13
- cirq/ops/op_tree.py +9 -7
- cirq/ops/op_tree_test.py +22 -19
- cirq/ops/parallel_gate.py +15 -17
- cirq/ops/parallel_gate_test.py +18 -16
- cirq/ops/parity_gates.py +23 -25
- cirq/ops/parity_gates_test.py +36 -32
- cirq/ops/pauli_gates.py +22 -21
- cirq/ops/pauli_gates_test.py +29 -20
- cirq/ops/pauli_interaction_gate.py +15 -19
- cirq/ops/pauli_interaction_gate_test.py +10 -8
- cirq/ops/pauli_measurement_gate.py +23 -35
- cirq/ops/pauli_measurement_gate_test.py +2 -0
- cirq/ops/pauli_string.py +92 -120
- cirq/ops/pauli_string_phasor.py +52 -45
- cirq/ops/pauli_string_phasor_test.py +4 -5
- cirq/ops/pauli_string_raw_types.py +9 -7
- cirq/ops/pauli_string_raw_types_test.py +2 -0
- cirq/ops/pauli_string_test.py +31 -154
- cirq/ops/pauli_sum_exponential.py +12 -12
- cirq/ops/pauli_sum_exponential_test.py +12 -10
- cirq/ops/permutation_gate.py +8 -6
- cirq/ops/permutation_gate_test.py +10 -8
- cirq/ops/phased_iswap_gate.py +16 -16
- cirq/ops/phased_iswap_gate_test.py +17 -15
- cirq/ops/phased_x_gate.py +16 -17
- cirq/ops/phased_x_gate_test.py +18 -16
- cirq/ops/phased_x_z_gate.py +24 -22
- cirq/ops/phased_x_z_gate_test.py +17 -11
- cirq/ops/projector.py +16 -11
- cirq/ops/projector_test.py +19 -16
- cirq/ops/qid_util.py +7 -5
- cirq/ops/qid_util_test.py +2 -0
- cirq/ops/qubit_manager.py +11 -9
- cirq/ops/qubit_manager_test.py +6 -4
- cirq/ops/qubit_order.py +11 -14
- cirq/ops/qubit_order_or_list.py +4 -2
- cirq/ops/qubit_order_test.py +12 -10
- cirq/ops/random_gate_channel.py +12 -10
- cirq/ops/random_gate_channel_test.py +14 -11
- cirq/ops/raw_types.py +109 -129
- cirq/ops/raw_types_test.py +63 -57
- cirq/ops/state_preparation_channel.py +7 -7
- cirq/ops/state_preparation_channel_test.py +11 -9
- cirq/ops/swap_gates.py +13 -15
- cirq/ops/swap_gates_test.py +19 -17
- cirq/ops/tags.py +5 -3
- cirq/ops/tags_test.py +4 -2
- cirq/ops/three_qubit_gates.py +43 -76
- cirq/ops/three_qubit_gates_test.py +19 -17
- cirq/ops/two_qubit_diagonal_gate.py +13 -13
- cirq/ops/two_qubit_diagonal_gate_test.py +10 -8
- cirq/ops/uniform_superposition_gate.py +5 -3
- cirq/ops/uniform_superposition_gate_test.py +5 -3
- cirq/ops/wait_gate.py +17 -14
- cirq/ops/wait_gate_test.py +9 -6
- cirq/protocols/__init__.py +0 -3
- cirq/protocols/act_on_protocol.py +8 -6
- cirq/protocols/act_on_protocol_test.py +15 -12
- cirq/protocols/apply_channel_protocol.py +10 -14
- cirq/protocols/apply_channel_protocol_test.py +2 -0
- cirq/protocols/apply_mixture_protocol.py +13 -42
- cirq/protocols/apply_mixture_protocol_test.py +7 -5
- cirq/protocols/apply_unitary_protocol.py +39 -34
- cirq/protocols/apply_unitary_protocol_test.py +4 -1
- cirq/protocols/approximate_equality_protocol.py +2 -0
- cirq/protocols/approximate_equality_protocol_test.py +2 -0
- cirq/protocols/circuit_diagram_info_protocol.py +58 -42
- cirq/protocols/circuit_diagram_info_protocol_test.py +70 -12
- cirq/protocols/commutes_protocol.py +8 -7
- cirq/protocols/commutes_protocol_test.py +2 -0
- cirq/protocols/control_key_protocol.py +6 -4
- cirq/protocols/control_key_protocol_test.py +3 -1
- cirq/protocols/decompose_protocol.py +49 -48
- cirq/protocols/decompose_protocol_test.py +27 -16
- cirq/protocols/equal_up_to_global_phase_protocol.py +2 -0
- cirq/protocols/equal_up_to_global_phase_protocol_test.py +9 -6
- cirq/protocols/has_stabilizer_effect_protocol.py +7 -5
- cirq/protocols/has_stabilizer_effect_protocol_test.py +7 -5
- cirq/protocols/has_unitary_protocol.py +10 -6
- cirq/protocols/has_unitary_protocol_test.py +13 -8
- cirq/protocols/hash_from_pickle_test.py +2 -11
- cirq/protocols/inverse_protocol.py +13 -16
- cirq/protocols/inverse_protocol_test.py +5 -3
- cirq/protocols/json_serialization.py +35 -54
- cirq/protocols/json_serialization_test.py +14 -21
- cirq/protocols/json_test_data/CXSWAP.json +46 -0
- cirq/protocols/json_test_data/CXSWAP.repr +13 -0
- cirq/protocols/json_test_data/CZSWAP.json +46 -0
- cirq/protocols/json_test_data/CZSWAP.repr +13 -0
- cirq/protocols/json_test_data/CircuitOperation.json +6 -3
- cirq/protocols/json_test_data/CircuitOperation.repr_inward +4 -2
- cirq/protocols/json_test_data/Moment.json +24 -1
- cirq/protocols/json_test_data/Moment.repr +6 -1
- cirq/protocols/json_test_data/ThermalNoiseModel.json +32 -0
- cirq/protocols/json_test_data/ThermalNoiseModel.repr +1 -0
- cirq/protocols/json_test_data/spec.py +6 -2
- cirq/protocols/kraus_protocol.py +47 -7
- cirq/protocols/kraus_protocol_test.py +86 -12
- cirq/protocols/measurement_key_protocol.py +15 -16
- cirq/protocols/measurement_key_protocol_test.py +13 -11
- cirq/protocols/mixture_protocol.py +7 -5
- cirq/protocols/mixture_protocol_test.py +4 -2
- cirq/protocols/mul_protocol.py +2 -3
- cirq/protocols/mul_protocol_test.py +2 -0
- cirq/protocols/pauli_expansion_protocol.py +6 -3
- cirq/protocols/pauli_expansion_protocol_test.py +5 -3
- cirq/protocols/phase_protocol.py +2 -0
- cirq/protocols/phase_protocol_test.py +3 -1
- cirq/protocols/pow_protocol.py +11 -16
- cirq/protocols/pow_protocol_test.py +2 -0
- cirq/protocols/qasm.py +14 -20
- cirq/protocols/qasm_test.py +6 -3
- cirq/protocols/qid_shape_protocol.py +8 -8
- cirq/protocols/qid_shape_protocol_test.py +3 -1
- cirq/protocols/resolve_parameters.py +5 -3
- cirq/protocols/resolve_parameters_test.py +8 -7
- cirq/protocols/trace_distance_bound.py +6 -4
- cirq/protocols/trace_distance_bound_test.py +3 -1
- cirq/protocols/unitary_protocol.py +17 -7
- cirq/protocols/unitary_protocol_test.py +12 -2
- cirq/qis/channels.py +6 -2
- cirq/qis/channels_test.py +20 -16
- cirq/qis/clifford_tableau.py +21 -19
- cirq/qis/clifford_tableau_test.py +2 -2
- cirq/qis/entropy.py +14 -3
- cirq/qis/entropy_test.py +3 -1
- cirq/qis/measures.py +13 -13
- cirq/qis/measures_test.py +20 -14
- cirq/qis/noise_utils.py +2 -0
- cirq/qis/noise_utils_test.py +9 -7
- cirq/qis/quantum_state_representation.py +7 -8
- cirq/qis/states.py +58 -56
- cirq/qis/states_test.py +2 -0
- cirq/sim/classical_simulator.py +23 -22
- cirq/sim/classical_simulator_test.py +2 -0
- cirq/sim/clifford/clifford_simulator.py +23 -21
- cirq/sim/clifford/clifford_simulator_test.py +7 -4
- cirq/sim/clifford/clifford_tableau_simulation_state.py +10 -7
- cirq/sim/clifford/clifford_tableau_simulation_state_test.py +5 -5
- cirq/sim/clifford/stabilizer_ch_form_simulation_state.py +8 -6
- cirq/sim/clifford/stabilizer_ch_form_simulation_state_test.py +8 -6
- cirq/sim/clifford/stabilizer_sampler.py +9 -7
- cirq/sim/clifford/stabilizer_sampler_test.py +4 -2
- cirq/sim/clifford/stabilizer_simulation_state.py +14 -13
- cirq/sim/clifford/stabilizer_simulation_state_test.py +6 -4
- cirq/sim/clifford/stabilizer_state_ch_form.py +13 -11
- cirq/sim/clifford/stabilizer_state_ch_form_test.py +4 -2
- cirq/sim/density_matrix_simulation_state.py +26 -27
- cirq/sim/density_matrix_simulation_state_test.py +10 -8
- cirq/sim/density_matrix_simulator.py +30 -28
- cirq/sim/density_matrix_simulator_test.py +48 -48
- cirq/sim/density_matrix_utils.py +13 -11
- cirq/sim/density_matrix_utils_test.py +38 -36
- cirq/sim/mux.py +33 -31
- cirq/sim/mux_test.py +3 -0
- cirq/sim/simulation_product_state.py +15 -15
- cirq/sim/simulation_product_state_test.py +29 -26
- cirq/sim/simulation_state.py +29 -38
- cirq/sim/simulation_state_base.py +21 -32
- cirq/sim/simulation_state_test.py +15 -13
- cirq/sim/simulation_utils.py +5 -2
- cirq/sim/simulation_utils_test.py +5 -2
- cirq/sim/simulator.py +90 -106
- cirq/sim/simulator_base.py +33 -45
- cirq/sim/simulator_base_test.py +20 -15
- cirq/sim/simulator_test.py +23 -14
- cirq/sim/sparse_simulator.py +19 -17
- cirq/sim/sparse_simulator_test.py +41 -40
- cirq/sim/state_vector.py +15 -12
- cirq/sim/state_vector_simulation_state.py +31 -31
- cirq/sim/state_vector_simulation_state_test.py +16 -14
- cirq/sim/state_vector_simulator.py +17 -14
- cirq/sim/state_vector_simulator_test.py +2 -0
- cirq/sim/state_vector_test.py +6 -3
- cirq/study/flatten_expressions.py +16 -15
- cirq/study/flatten_expressions_test.py +13 -11
- cirq/study/resolver.py +18 -17
- cirq/study/resolver_test.py +22 -20
- cirq/study/result.py +17 -27
- cirq/study/result_test.py +2 -0
- cirq/study/sweepable.py +12 -10
- cirq/study/sweepable_test.py +3 -0
- cirq/study/sweeps.py +42 -61
- cirq/study/sweeps_test.py +33 -0
- cirq/testing/__init__.py +7 -11
- cirq/testing/_compat_test_data/module_a/__init__.py +1 -0
- cirq/testing/_compat_test_data/module_a/module_b/__init__.py +1 -0
- cirq/testing/_compat_test_data/module_a/sub/__init__.py +1 -0
- cirq/testing/circuit_compare.py +8 -17
- cirq/testing/circuit_compare_test.py +2 -0
- cirq/testing/consistent_act_on.py +13 -11
- cirq/testing/consistent_act_on_test.py +5 -3
- cirq/testing/consistent_channels.py +2 -0
- cirq/testing/consistent_channels_test.py +10 -8
- cirq/testing/consistent_controlled_gate_op.py +5 -5
- cirq/testing/consistent_controlled_gate_op_test.py +18 -18
- cirq/testing/consistent_decomposition.py +2 -2
- cirq/testing/consistent_decomposition_test.py +4 -2
- cirq/testing/consistent_pauli_expansion.py +2 -0
- cirq/testing/consistent_pauli_expansion_test.py +3 -1
- cirq/testing/consistent_phase_by.py +2 -0
- cirq/testing/consistent_phase_by_test.py +3 -1
- cirq/testing/consistent_protocols.py +14 -20
- cirq/testing/consistent_protocols_test.py +13 -11
- cirq/testing/consistent_qasm.py +6 -4
- cirq/testing/consistent_qasm_test.py +7 -7
- cirq/testing/consistent_resolve_parameters.py +2 -0
- cirq/testing/consistent_specified_has_unitary.py +2 -2
- cirq/testing/consistent_specified_has_unitary_test.py +6 -4
- cirq/testing/consistent_unitary.py +1 -0
- cirq/testing/consistent_unitary_test.py +4 -2
- cirq/testing/deprecation.py +5 -2
- cirq/testing/deprecation_test.py +5 -2
- cirq/testing/devices.py +7 -4
- cirq/testing/devices_test.py +7 -4
- cirq/testing/equals_tester.py +4 -2
- cirq/testing/equals_tester_test.py +21 -17
- cirq/testing/equivalent_basis_map.py +6 -4
- cirq/testing/equivalent_basis_map_test.py +6 -4
- cirq/testing/equivalent_repr_eval.py +6 -4
- cirq/testing/equivalent_repr_eval_test.py +5 -3
- cirq/testing/gate_features.py +2 -0
- cirq/testing/gate_features_test.py +7 -5
- cirq/testing/json.py +19 -15
- cirq/testing/json_test.py +5 -3
- cirq/testing/lin_alg_utils.py +10 -11
- cirq/testing/lin_alg_utils_test.py +14 -12
- cirq/testing/logs.py +7 -6
- cirq/testing/logs_test.py +9 -7
- cirq/testing/no_identifier_qubit.py +4 -2
- cirq/testing/no_identifier_qubit_test.py +5 -3
- cirq/testing/op_tree.py +2 -0
- cirq/testing/op_tree_test.py +4 -1
- cirq/testing/order_tester.py +2 -0
- cirq/testing/order_tester_test.py +8 -6
- cirq/testing/pytest_utils.py +2 -0
- cirq/testing/pytest_utils_test.py +4 -2
- cirq/testing/random_circuit.py +21 -20
- cirq/testing/random_circuit_test.py +12 -9
- cirq/testing/repr_pretty_tester.py +1 -0
- cirq/testing/repr_pretty_tester_test.py +5 -3
- cirq/testing/routing_devices.py +4 -1
- cirq/testing/routing_devices_test.py +9 -6
- cirq/testing/sample_circuits.py +4 -1
- cirq/testing/sample_circuits_test.py +3 -1
- cirq/testing/sample_gates.py +3 -0
- cirq/testing/sample_gates_test.py +5 -2
- cirq/transformers/__init__.py +11 -4
- cirq/transformers/align.py +9 -7
- cirq/transformers/align_test.py +2 -0
- cirq/transformers/analytical_decompositions/__init__.py +3 -6
- cirq/transformers/analytical_decompositions/clifford_decomposition.py +18 -16
- cirq/transformers/analytical_decompositions/clifford_decomposition_test.py +2 -0
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +19 -16
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition_test.py +2 -0
- cirq/transformers/analytical_decompositions/cphase_to_fsim.py +11 -9
- cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +5 -3
- cirq/transformers/analytical_decompositions/pauli_string_decomposition.py +5 -3
- cirq/transformers/analytical_decompositions/pauli_string_decomposition_test.py +5 -3
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +141 -44
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition_test.py +35 -1
- cirq/transformers/analytical_decompositions/single_qubit_decompositions.py +8 -7
- cirq/transformers/analytical_decompositions/single_qubit_decompositions_test.py +2 -0
- cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry.py +7 -4
- cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry_test.py +3 -0
- cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +11 -19
- cirq/transformers/analytical_decompositions/three_qubit_decomposition_test.py +8 -33
- cirq/transformers/analytical_decompositions/two_qubit_state_preparation.py +9 -11
- cirq/transformers/analytical_decompositions/two_qubit_state_preparation_test.py +2 -0
- cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +91 -27
- cirq/transformers/analytical_decompositions/two_qubit_to_cz_test.py +36 -7
- cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +20 -21
- cirq/transformers/analytical_decompositions/two_qubit_to_fsim_test.py +8 -6
- cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +13 -15
- cirq/transformers/analytical_decompositions/two_qubit_to_ms_test.py +3 -1
- cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +39 -41
- cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap_test.py +2 -0
- cirq/transformers/drop_empty_moments.py +5 -3
- cirq/transformers/drop_empty_moments_test.py +4 -2
- cirq/transformers/drop_negligible_operations.py +7 -5
- cirq/transformers/drop_negligible_operations_test.py +2 -0
- cirq/transformers/dynamical_decoupling.py +49 -42
- cirq/transformers/dynamical_decoupling_test.py +223 -205
- cirq/transformers/eject_phased_paulis.py +28 -26
- cirq/transformers/eject_phased_paulis_test.py +12 -9
- cirq/transformers/eject_z.py +12 -12
- cirq/transformers/eject_z_test.py +2 -2
- cirq/transformers/expand_composite.py +6 -4
- cirq/transformers/expand_composite_test.py +3 -1
- cirq/transformers/gauge_compiling/__init__.py +3 -1
- cirq/transformers/gauge_compiling/cphase_gauge.py +2 -0
- cirq/transformers/gauge_compiling/cphase_gauge_test.py +2 -0
- cirq/transformers/gauge_compiling/cz_gauge.py +2 -0
- cirq/transformers/gauge_compiling/cz_gauge_test.py +1 -0
- cirq/transformers/gauge_compiling/gauge_compiling.py +45 -41
- cirq/transformers/gauge_compiling/gauge_compiling_test.py +2 -0
- cirq/transformers/gauge_compiling/gauge_compiling_test_utils.py +1 -0
- cirq/transformers/gauge_compiling/gauge_compiling_test_utils_test.py +5 -1
- cirq/transformers/gauge_compiling/iswap_gauge.py +2 -0
- cirq/transformers/gauge_compiling/iswap_gauge_test.py +1 -0
- cirq/transformers/gauge_compiling/spin_inversion_gauge.py +2 -0
- cirq/transformers/gauge_compiling/spin_inversion_gauge_test.py +2 -0
- cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +7 -6
- cirq/transformers/gauge_compiling/sqrt_cz_gauge_test.py +2 -0
- cirq/transformers/gauge_compiling/sqrt_iswap_gauge.py +2 -0
- cirq/transformers/gauge_compiling/sqrt_iswap_gauge_test.py +2 -0
- cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils.py +6 -3
- cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils_test.py +3 -0
- cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +12 -9
- cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation_test.py +9 -7
- cirq/transformers/insertion_sort.py +8 -6
- cirq/transformers/insertion_sort_test.py +3 -1
- cirq/transformers/measurement_transformers.py +29 -29
- cirq/transformers/measurement_transformers_test.py +2 -0
- cirq/transformers/merge_k_qubit_gates.py +12 -10
- cirq/transformers/merge_k_qubit_gates_test.py +18 -18
- cirq/transformers/merge_single_qubit_gates.py +197 -20
- cirq/transformers/merge_single_qubit_gates_test.py +177 -5
- cirq/transformers/noise_adding.py +5 -3
- cirq/transformers/noise_adding_test.py +2 -0
- cirq/transformers/optimize_for_target_gateset.py +19 -17
- cirq/transformers/optimize_for_target_gateset_test.py +11 -8
- cirq/transformers/qubit_management_transformers.py +13 -11
- cirq/transformers/qubit_management_transformers_test.py +5 -3
- cirq/transformers/randomized_measurements.py +16 -14
- cirq/transformers/randomized_measurements_test.py +10 -4
- cirq/transformers/routing/initial_mapper.py +6 -4
- cirq/transformers/routing/initial_mapper_test.py +2 -0
- cirq/transformers/routing/line_initial_mapper.py +16 -14
- cirq/transformers/routing/line_initial_mapper_test.py +9 -7
- cirq/transformers/routing/mapping_manager.py +10 -10
- cirq/transformers/routing/mapping_manager_test.py +2 -0
- cirq/transformers/routing/route_circuit_cqc.py +33 -31
- cirq/transformers/routing/route_circuit_cqc_test.py +15 -13
- cirq/transformers/routing/visualize_routed_circuit.py +8 -7
- cirq/transformers/routing/visualize_routed_circuit_test.py +4 -2
- cirq/transformers/stratify.py +17 -15
- cirq/transformers/stratify_test.py +3 -0
- cirq/transformers/symbolize.py +103 -0
- cirq/transformers/symbolize_test.py +62 -0
- cirq/transformers/synchronize_terminal_measurements.py +10 -10
- cirq/transformers/synchronize_terminal_measurements_test.py +12 -10
- cirq/transformers/tag_transformers.py +97 -0
- cirq/transformers/tag_transformers_test.py +103 -0
- cirq/transformers/target_gatesets/compilation_target_gateset.py +21 -19
- cirq/transformers/target_gatesets/compilation_target_gateset_test.py +20 -16
- cirq/transformers/target_gatesets/cz_gateset.py +7 -5
- cirq/transformers/target_gatesets/cz_gateset_test.py +21 -19
- cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +9 -7
- cirq/transformers/target_gatesets/sqrt_iswap_gateset_test.py +25 -25
- cirq/transformers/transformer_api.py +34 -47
- cirq/transformers/transformer_api_test.py +9 -8
- cirq/transformers/transformer_primitives.py +39 -49
- cirq/transformers/transformer_primitives_test.py +10 -17
- cirq/value/abc_alt.py +6 -4
- cirq/value/abc_alt_test.py +5 -3
- cirq/value/angle.py +11 -12
- cirq/value/angle_test.py +5 -3
- cirq/value/classical_data.py +27 -27
- cirq/value/classical_data_test.py +11 -8
- cirq/value/condition.py +26 -24
- cirq/value/condition_test.py +2 -0
- cirq/value/digits.py +14 -11
- cirq/value/digits_test.py +2 -0
- cirq/value/duration.py +23 -20
- cirq/value/duration_test.py +2 -0
- cirq/value/linear_dict.py +25 -30
- cirq/value/linear_dict_test.py +10 -8
- cirq/value/measurement_key.py +12 -12
- cirq/value/measurement_key_test.py +2 -0
- cirq/value/periodic_value.py +4 -4
- cirq/value/periodic_value_test.py +11 -7
- cirq/value/probability.py +3 -1
- cirq/value/probability_test.py +4 -2
- cirq/value/product_state.py +15 -13
- cirq/value/product_state_test.py +4 -1
- cirq/value/random_state.py +2 -0
- cirq/value/random_state_test.py +5 -3
- cirq/value/timestamp.py +11 -7
- cirq/value/timestamp_test.py +14 -12
- cirq/value/type_alias.py +4 -4
- cirq/value/value_equality_attr.py +8 -9
- cirq/value/value_equality_attr_test.py +14 -11
- cirq/vis/density_matrix.py +3 -3
- cirq/vis/density_matrix_test.py +20 -17
- cirq/vis/heatmap.py +24 -37
- cirq/vis/heatmap_test.py +3 -0
- cirq/vis/histogram.py +9 -6
- cirq/vis/histogram_test.py +5 -2
- cirq/vis/state_histogram.py +10 -8
- cirq/vis/state_histogram_test.py +7 -5
- cirq/vis/vis_utils.py +4 -1
- cirq/vis/vis_utils_test.py +4 -1
- cirq/work/collector.py +12 -18
- cirq/work/collector_test.py +15 -10
- cirq/work/observable_grouping.py +6 -7
- cirq/work/observable_grouping_test.py +10 -9
- cirq/work/observable_measurement.py +47 -45
- cirq/work/observable_measurement_data.py +22 -17
- cirq/work/observable_measurement_data_test.py +4 -1
- cirq/work/observable_measurement_test.py +48 -29
- cirq/work/observable_readout_calibration.py +5 -2
- cirq/work/observable_readout_calibration_test.py +5 -2
- cirq/work/observable_settings.py +13 -22
- cirq/work/observable_settings_test.py +9 -7
- cirq/work/pauli_sum_collector.py +12 -10
- cirq/work/pauli_sum_collector_test.py +9 -9
- cirq/work/sampler.py +42 -43
- cirq/work/sampler_test.py +31 -24
- cirq/work/zeros_sampler.py +6 -4
- cirq/work/zeros_sampler_test.py +7 -5
- {cirq_core-1.5.0.dev20250409222543.dist-info → cirq_core-1.6.0.dist-info}/METADATA +7 -8
- cirq_core-1.6.0.dist-info/RECORD +1241 -0
- {cirq_core-1.5.0.dev20250409222543.dist-info → cirq_core-1.6.0.dist-info}/WHEEL +1 -1
- cirq_core-1.5.0.dev20250409222543.dist-info/RECORD +0 -1216
- {cirq_core-1.5.0.dev20250409222543.dist-info → cirq_core-1.6.0.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.5.0.dev20250409222543.dist-info → cirq_core-1.6.0.dist-info}/top_level.txt +0 -0
cirq/ops/eigen_gate.py
CHANGED
|
@@ -11,24 +11,15 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
14
17
|
import abc
|
|
15
18
|
import fractions
|
|
16
19
|
import math
|
|
17
20
|
import numbers
|
|
18
21
|
from types import NotImplementedType
|
|
19
|
-
from typing import
|
|
20
|
-
AbstractSet,
|
|
21
|
-
Any,
|
|
22
|
-
cast,
|
|
23
|
-
Dict,
|
|
24
|
-
Iterable,
|
|
25
|
-
List,
|
|
26
|
-
NamedTuple,
|
|
27
|
-
Optional,
|
|
28
|
-
Tuple,
|
|
29
|
-
TYPE_CHECKING,
|
|
30
|
-
Union,
|
|
31
|
-
)
|
|
22
|
+
from typing import AbstractSet, Any, cast, Iterable, NamedTuple, TYPE_CHECKING
|
|
32
23
|
|
|
33
24
|
import numpy as np
|
|
34
25
|
import sympy
|
|
@@ -112,9 +103,15 @@ class EigenGate(raw_types.Gate):
|
|
|
112
103
|
`cirq.unitary(cirq.rx(pi))` equals -iX instead of X.
|
|
113
104
|
|
|
114
105
|
Raises:
|
|
106
|
+
TypeError: If the supplied exponent is a string.
|
|
115
107
|
ValueError: If the supplied exponent is a complex number with an
|
|
116
108
|
imaginary component.
|
|
117
109
|
"""
|
|
110
|
+
if isinstance(exponent, str):
|
|
111
|
+
raise TypeError(
|
|
112
|
+
"Gate exponent must be a number or sympy expression. "
|
|
113
|
+
f"Received a string instead: {exponent!r}"
|
|
114
|
+
)
|
|
118
115
|
if isinstance(exponent, complex):
|
|
119
116
|
if exponent.imag:
|
|
120
117
|
raise ValueError(f"Gate exponent must be real. Invalid Value: {exponent}")
|
|
@@ -131,20 +128,18 @@ class EigenGate(raw_types.Gate):
|
|
|
131
128
|
return self._global_shift
|
|
132
129
|
|
|
133
130
|
# virtual method
|
|
134
|
-
def _with_exponent(self, exponent: value.TParamVal) ->
|
|
131
|
+
def _with_exponent(self, exponent: value.TParamVal) -> EigenGate:
|
|
135
132
|
"""Return the same kind of gate, but with a different exponent.
|
|
136
133
|
|
|
137
134
|
Child classes should override this method if they have an __init__
|
|
138
135
|
method with a differing signature.
|
|
139
136
|
"""
|
|
140
|
-
# pylint: disable=unexpected-keyword-arg
|
|
141
137
|
if self._global_shift == 0:
|
|
142
138
|
return type(self)(exponent=exponent)
|
|
143
139
|
return type(self)(exponent=exponent, global_shift=self._global_shift)
|
|
144
|
-
# pylint: enable=unexpected-keyword-arg
|
|
145
140
|
|
|
146
141
|
def _diagram_exponent(
|
|
147
|
-
self, args:
|
|
142
|
+
self, args: protocols.CircuitDiagramInfoArgs, *, ignore_global_phase: bool = True
|
|
148
143
|
):
|
|
149
144
|
"""The exponent to use in circuit diagrams.
|
|
150
145
|
|
|
@@ -199,7 +194,7 @@ class EigenGate(raw_types.Gate):
|
|
|
199
194
|
return result
|
|
200
195
|
|
|
201
196
|
def _format_exponent_as_angle(
|
|
202
|
-
self, args:
|
|
197
|
+
self, args: protocols.CircuitDiagramInfoArgs, order: int = 2
|
|
203
198
|
) -> str:
|
|
204
199
|
"""Returns string with exponent expressed as angle in radians.
|
|
205
200
|
|
|
@@ -216,7 +211,7 @@ class EigenGate(raw_types.Gate):
|
|
|
216
211
|
return args.format_radians(radians=2 * pi * exponent / order)
|
|
217
212
|
|
|
218
213
|
# virtual method
|
|
219
|
-
def _eigen_shifts(self) ->
|
|
214
|
+
def _eigen_shifts(self) -> list[float]:
|
|
220
215
|
"""Describes the eigenvalues of the gate's matrix.
|
|
221
216
|
|
|
222
217
|
By default, this just extracts the shifts by calling
|
|
@@ -232,7 +227,7 @@ class EigenGate(raw_types.Gate):
|
|
|
232
227
|
return [e[0] for e in self._eigen_components()]
|
|
233
228
|
|
|
234
229
|
@abc.abstractmethod
|
|
235
|
-
def _eigen_components(self) ->
|
|
230
|
+
def _eigen_components(self) -> list[EigenComponent | tuple[float, np.ndarray]]:
|
|
236
231
|
"""Describes the eigendecomposition of the gate's matrix.
|
|
237
232
|
|
|
238
233
|
Returns:
|
|
@@ -284,7 +279,7 @@ class EigenGate(raw_types.Gate):
|
|
|
284
279
|
]
|
|
285
280
|
"""
|
|
286
281
|
|
|
287
|
-
def _period(self) ->
|
|
282
|
+
def _period(self) -> float | None:
|
|
288
283
|
"""Determines how the exponent parameter is canonicalized when equating.
|
|
289
284
|
|
|
290
285
|
Returns:
|
|
@@ -297,7 +292,12 @@ class EigenGate(raw_types.Gate):
|
|
|
297
292
|
real_periods = [abs(2 / e) for e in exponents if e != 0]
|
|
298
293
|
return _approximate_common_period(real_periods)
|
|
299
294
|
|
|
300
|
-
def __pow__(self, exponent:
|
|
295
|
+
def __pow__(self, exponent: value.TParamVal) -> EigenGate:
|
|
296
|
+
if isinstance(exponent, str):
|
|
297
|
+
raise TypeError(
|
|
298
|
+
"Gate exponent must be a number or sympy expression. "
|
|
299
|
+
f"Received a string instead: {exponent!r}"
|
|
300
|
+
)
|
|
301
301
|
new_exponent = protocols.mul(self._exponent, exponent, NotImplemented)
|
|
302
302
|
if new_exponent is NotImplemented:
|
|
303
303
|
return NotImplemented # pragma: no cover
|
|
@@ -317,7 +317,7 @@ class EigenGate(raw_types.Gate):
|
|
|
317
317
|
shifts = (f(self._exponent) * f(self._global_shift + e) for e in self._eigen_shifts())
|
|
318
318
|
return tuple(s if symbolic(s) else value.PeriodicValue(f(s), 2) for s in shifts)
|
|
319
319
|
|
|
320
|
-
def _trace_distance_bound_(self) ->
|
|
320
|
+
def _trace_distance_bound_(self) -> float | None:
|
|
321
321
|
if protocols.is_parameterized(self._exponent):
|
|
322
322
|
return None
|
|
323
323
|
angles = np.pi * (np.array(self._eigen_shifts()) * self._exponent % 2)
|
|
@@ -326,7 +326,7 @@ class EigenGate(raw_types.Gate):
|
|
|
326
326
|
def _has_unitary_(self) -> bool:
|
|
327
327
|
return not self._is_parameterized_()
|
|
328
328
|
|
|
329
|
-
def _unitary_(self) ->
|
|
329
|
+
def _unitary_(self) -> np.ndarray | NotImplementedType:
|
|
330
330
|
if self._is_parameterized_():
|
|
331
331
|
return NotImplemented
|
|
332
332
|
e = cast(float, self._exponent)
|
|
@@ -344,7 +344,7 @@ class EigenGate(raw_types.Gate):
|
|
|
344
344
|
def _parameter_names_(self) -> AbstractSet[str]:
|
|
345
345
|
return protocols.parameter_names(self._exponent)
|
|
346
346
|
|
|
347
|
-
def _resolve_parameters_(self, resolver:
|
|
347
|
+
def _resolve_parameters_(self, resolver: cirq.ParamResolver, recursive: bool) -> EigenGate:
|
|
348
348
|
exponent = resolver.value_of(self._exponent, recursive)
|
|
349
349
|
if isinstance(exponent, numbers.Complex):
|
|
350
350
|
if isinstance(exponent, numbers.Real):
|
|
@@ -369,7 +369,7 @@ class EigenGate(raw_types.Gate):
|
|
|
369
369
|
other_without_phase._global_shift = 0
|
|
370
370
|
return protocols.approx_eq(self_without_phase, other_without_phase, atol=atol)
|
|
371
371
|
|
|
372
|
-
def _json_dict_(self) ->
|
|
372
|
+
def _json_dict_(self) -> dict[str, Any]:
|
|
373
373
|
return protocols.obj_to_dict_helper(self, ['exponent', 'global_shift'])
|
|
374
374
|
|
|
375
375
|
def _measurement_key_objs_(self):
|
|
@@ -384,8 +384,8 @@ def _lcm(vals: Iterable[int]) -> int:
|
|
|
384
384
|
|
|
385
385
|
|
|
386
386
|
def _approximate_common_period(
|
|
387
|
-
periods:
|
|
388
|
-
) ->
|
|
387
|
+
periods: list[float], approx_denom: int = 60, reject_atol: float = 1e-8
|
|
388
|
+
) -> float | None:
|
|
389
389
|
"""Finds a value that is nearly an integer multiple of multiple periods.
|
|
390
390
|
|
|
391
391
|
The returned value should be the smallest non-negative number with this
|
|
@@ -429,7 +429,7 @@ def _approximate_common_period(
|
|
|
429
429
|
return common
|
|
430
430
|
|
|
431
431
|
|
|
432
|
-
def _common_rational_period(rational_periods:
|
|
432
|
+
def _common_rational_period(rational_periods: list[fractions.Fraction]) -> fractions.Fraction:
|
|
433
433
|
"""Finds the least common integer multiple of some fractions.
|
|
434
434
|
|
|
435
435
|
The solution is the smallest positive integer c such that there
|
cirq/ops/eigen_gate_test.py
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
from
|
|
15
|
+
from __future__ import annotations
|
|
16
16
|
|
|
17
17
|
import numpy as np
|
|
18
18
|
import pytest
|
|
@@ -41,7 +41,7 @@ class CExpZinGate(cirq.EigenGate, cirq.testing.TwoQubitGate):
|
|
|
41
41
|
def _with_exponent(self, exponent):
|
|
42
42
|
return CExpZinGate(exponent)
|
|
43
43
|
|
|
44
|
-
def _eigen_components(self) ->
|
|
44
|
+
def _eigen_components(self) -> list[tuple[float, np.ndarray]]:
|
|
45
45
|
return [
|
|
46
46
|
(0, np.diag([1, 1, 0, 0])),
|
|
47
47
|
(0.5, np.diag([0, 0, 1, 0])),
|
|
@@ -54,11 +54,11 @@ class ZGateDef(cirq.EigenGate, cirq.testing.SingleQubitGate):
|
|
|
54
54
|
def exponent(self):
|
|
55
55
|
return self._exponent
|
|
56
56
|
|
|
57
|
-
def _eigen_components(self) ->
|
|
57
|
+
def _eigen_components(self) -> list[tuple[float, np.ndarray]]:
|
|
58
58
|
return [(0, np.diag([1, 0])), (1, np.diag([0, 1]))]
|
|
59
59
|
|
|
60
60
|
|
|
61
|
-
def test_approximate_common_period():
|
|
61
|
+
def test_approximate_common_period() -> None:
|
|
62
62
|
from cirq.ops.eigen_gate import _approximate_common_period as f
|
|
63
63
|
|
|
64
64
|
assert f([]) is None
|
|
@@ -71,13 +71,19 @@ def test_approximate_common_period():
|
|
|
71
71
|
assert f([2.5]) == 2.5
|
|
72
72
|
assert f([1.5, 2]) == 6
|
|
73
73
|
assert f([2, 3]) == 6
|
|
74
|
-
|
|
75
|
-
assert
|
|
74
|
+
f_x = f([1 / 3, 2 / 3])
|
|
75
|
+
assert f_x is not None
|
|
76
|
+
assert abs(f_x - 2 / 3) < 1e-8
|
|
77
|
+
f_x = f([2 / 5, 3 / 5])
|
|
78
|
+
assert f_x is not None
|
|
79
|
+
assert abs(f_x - 6 / 5) < 1e-8
|
|
76
80
|
assert f([0.5, -0.5]) == 0.5
|
|
77
|
-
|
|
81
|
+
f_x = f([np.e])
|
|
82
|
+
assert f_x is not None
|
|
83
|
+
np.testing.assert_allclose(f_x, np.e, atol=1e-8)
|
|
78
84
|
|
|
79
85
|
|
|
80
|
-
def test_init():
|
|
86
|
+
def test_init() -> None:
|
|
81
87
|
assert CExpZinGate(1).exponent == 1
|
|
82
88
|
assert CExpZinGate(0.5).exponent == 0.5
|
|
83
89
|
assert CExpZinGate(4.5).exponent == 4.5
|
|
@@ -91,7 +97,7 @@ def test_init():
|
|
|
91
97
|
assert ZGateDef(exponent=0.5 + 0j).exponent == 0.5
|
|
92
98
|
|
|
93
99
|
|
|
94
|
-
def test_eq():
|
|
100
|
+
def test_eq() -> None:
|
|
95
101
|
eq = cirq.testing.EqualsTester()
|
|
96
102
|
eq.make_equality_group(lambda: CExpZinGate(quarter_turns=0.1))
|
|
97
103
|
eq.add_equality_group(CExpZinGate(0), CExpZinGate(4), CExpZinGate(-4))
|
|
@@ -166,7 +172,7 @@ def test_eq():
|
|
|
166
172
|
)
|
|
167
173
|
|
|
168
174
|
|
|
169
|
-
def test_approx_eq():
|
|
175
|
+
def test_approx_eq() -> None:
|
|
170
176
|
assert cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(1.5), atol=0.1)
|
|
171
177
|
assert cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(1.7), atol=0.3)
|
|
172
178
|
assert not cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(1.7), atol=0.1)
|
|
@@ -188,7 +194,7 @@ def test_approx_eq():
|
|
|
188
194
|
)
|
|
189
195
|
|
|
190
196
|
|
|
191
|
-
def test_approx_eq_periodic():
|
|
197
|
+
def test_approx_eq_periodic() -> None:
|
|
192
198
|
assert cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(5.5), atol=1e-9)
|
|
193
199
|
assert cirq.approx_eq(CExpZinGate(1.5), CExpZinGate(9.5), atol=1e-9)
|
|
194
200
|
assert cirq.approx_eq(CExpZinGate(-2.5), CExpZinGate(1.5), atol=1e-9)
|
|
@@ -199,7 +205,7 @@ def test_approx_eq_periodic():
|
|
|
199
205
|
assert cirq.approx_eq(CExpZinGate(0), CExpZinGate(4 - 1e-10), atol=1e-9)
|
|
200
206
|
|
|
201
207
|
|
|
202
|
-
def test_period():
|
|
208
|
+
def test_period() -> None:
|
|
203
209
|
class Components(cirq.EigenGate, cirq.testing.TwoQubitGate):
|
|
204
210
|
def __init__(self, a, b, c, d):
|
|
205
211
|
super().__init__()
|
|
@@ -208,7 +214,7 @@ def test_period():
|
|
|
208
214
|
self.c = c
|
|
209
215
|
self.d = d
|
|
210
216
|
|
|
211
|
-
def _eigen_components(self) ->
|
|
217
|
+
def _eigen_components(self) -> list[tuple[float, np.ndarray]]:
|
|
212
218
|
return [
|
|
213
219
|
(self.a, np.diag([1, 0, 0, 0])),
|
|
214
220
|
(self.b, np.diag([0, 1, 0, 0])),
|
|
@@ -224,14 +230,16 @@ def test_period():
|
|
|
224
230
|
assert Components(1 / 3, 1 / 2, 1 / 5, 0)._period() == 60
|
|
225
231
|
assert Components(1 / 6, 1 / 2, 1 / 5, 0)._period() == 60
|
|
226
232
|
assert Components(np.e, np.pi, 0, 0)._period() is None
|
|
227
|
-
|
|
233
|
+
period = Components(np.e, np.e, 0, 0)._period()
|
|
234
|
+
assert period is not None
|
|
235
|
+
np.testing.assert_allclose(period, 2 / np.e)
|
|
228
236
|
assert Components(-0.5, 0, 0, 0)._period() == 4
|
|
229
237
|
assert Components(-0.5, 0.5, 0, 0)._period() == 4
|
|
230
238
|
assert Components(-0.5, 0.5, 0.5, 0.5)._period() == 4
|
|
231
239
|
assert Components(1, 1, -1, 1)._period() == 2
|
|
232
240
|
|
|
233
241
|
|
|
234
|
-
def test_pow():
|
|
242
|
+
def test_pow() -> None:
|
|
235
243
|
assert CExpZinGate(0.25) ** 2 == CExpZinGate(0.5)
|
|
236
244
|
assert CExpZinGate(0.25) ** -1 == CExpZinGate(-0.25)
|
|
237
245
|
assert CExpZinGate(0.25) ** 0 == CExpZinGate(0)
|
|
@@ -240,17 +248,26 @@ def test_pow():
|
|
|
240
248
|
assert ZGateDef(exponent=0.25, global_shift=0.5) ** 2 == ZGateDef(
|
|
241
249
|
exponent=0.5, global_shift=0.5
|
|
242
250
|
)
|
|
243
|
-
with pytest.raises(ValueError, match="real"):
|
|
251
|
+
with pytest.raises(ValueError, match="Gate exponent must be real."):
|
|
244
252
|
assert ZGateDef(exponent=0.5) ** 0.5j
|
|
245
253
|
assert ZGateDef(exponent=0.5) ** (1 + 0j) == ZGateDef(exponent=0.5)
|
|
246
254
|
|
|
255
|
+
with pytest.raises(TypeError, match="Gate exponent must be a number or sympy expression."):
|
|
256
|
+
assert ZGateDef(exponent=0.5) ** "text"
|
|
257
|
+
|
|
258
|
+
with pytest.raises(TypeError, match="Gate exponent must be a number or sympy expression."):
|
|
259
|
+
assert ZGateDef(exponent="text")
|
|
260
|
+
|
|
261
|
+
with pytest.raises(TypeError, match="Gate exponent must be a number or sympy expression."):
|
|
262
|
+
assert ZGateDef(exponent=sympy.Symbol('a')) ** "text"
|
|
263
|
+
|
|
247
264
|
|
|
248
|
-
def test_inverse():
|
|
265
|
+
def test_inverse() -> None:
|
|
249
266
|
assert cirq.inverse(CExpZinGate(0.25)) == CExpZinGate(-0.25)
|
|
250
267
|
assert cirq.inverse(CExpZinGate(sympy.Symbol('a'))) == CExpZinGate(-sympy.Symbol('a'))
|
|
251
268
|
|
|
252
269
|
|
|
253
|
-
def test_trace_distance_bound():
|
|
270
|
+
def test_trace_distance_bound() -> None:
|
|
254
271
|
assert cirq.trace_distance_bound(CExpZinGate(0.001)) < 0.01
|
|
255
272
|
assert cirq.trace_distance_bound(CExpZinGate(sympy.Symbol('a'))) == 1
|
|
256
273
|
assert cirq.approx_eq(cirq.trace_distance_bound(CExpZinGate(2)), 1)
|
|
@@ -259,14 +276,14 @@ def test_trace_distance_bound():
|
|
|
259
276
|
def _num_qubits_(self): # pragma: no cover
|
|
260
277
|
return 1
|
|
261
278
|
|
|
262
|
-
def _eigen_components(self) ->
|
|
279
|
+
def _eigen_components(self) -> list[tuple[float, np.ndarray]]:
|
|
263
280
|
return [(0, np.array([[1, 0], [0, 0]])), (12, np.array([[0, 0], [0, 1]]))]
|
|
264
281
|
|
|
265
282
|
for numerator in range(13):
|
|
266
283
|
assert_has_consistent_trace_distance_bound(E() ** (numerator / 12))
|
|
267
284
|
|
|
268
285
|
|
|
269
|
-
def test_extrapolate():
|
|
286
|
+
def test_extrapolate() -> None:
|
|
270
287
|
h = CExpZinGate(2)
|
|
271
288
|
assert cirq.pow(h, 1.5) is not None
|
|
272
289
|
assert cirq.inverse(h, None) is not None
|
|
@@ -280,7 +297,7 @@ def test_extrapolate():
|
|
|
280
297
|
assert cirq.inverse(s) == CExpZinGate(-sympy.Symbol('a'))
|
|
281
298
|
|
|
282
299
|
|
|
283
|
-
def test_matrix():
|
|
300
|
+
def test_matrix() -> None:
|
|
284
301
|
for n in [1, 2, 3, 4, 0.0001, 3.9999]:
|
|
285
302
|
assert cirq.has_unitary(CExpZinGate(n))
|
|
286
303
|
|
|
@@ -326,11 +343,11 @@ def test_matrix():
|
|
|
326
343
|
)
|
|
327
344
|
|
|
328
345
|
|
|
329
|
-
def test_matrix_is_exact_for_quarter_turn():
|
|
346
|
+
def test_matrix_is_exact_for_quarter_turn() -> None:
|
|
330
347
|
np.testing.assert_equal(cirq.unitary(CExpZinGate(1)), np.diag([1, 1, 1j, -1j]))
|
|
331
348
|
|
|
332
349
|
|
|
333
|
-
def test_is_parameterized():
|
|
350
|
+
def test_is_parameterized() -> None:
|
|
334
351
|
assert not cirq.is_parameterized(CExpZinGate(0))
|
|
335
352
|
assert not cirq.is_parameterized(CExpZinGate(1))
|
|
336
353
|
assert not cirq.is_parameterized(CExpZinGate(3))
|
|
@@ -338,7 +355,7 @@ def test_is_parameterized():
|
|
|
338
355
|
|
|
339
356
|
|
|
340
357
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
341
|
-
def test_resolve_parameters(resolve_fn):
|
|
358
|
+
def test_resolve_parameters(resolve_fn) -> None:
|
|
342
359
|
assert resolve_fn(
|
|
343
360
|
CExpZinGate(sympy.Symbol('a')), cirq.ParamResolver({'a': 0.5})
|
|
344
361
|
) == CExpZinGate(0.5)
|
|
@@ -349,9 +366,9 @@ def test_resolve_parameters(resolve_fn):
|
|
|
349
366
|
resolve_fn(CExpZinGate(sympy.Symbol('a')), cirq.ParamResolver({'a': 0.5j}))
|
|
350
367
|
|
|
351
368
|
|
|
352
|
-
def test_diagram_period():
|
|
369
|
+
def test_diagram_period() -> None:
|
|
353
370
|
class ShiftyGate(cirq.EigenGate, cirq.testing.SingleQubitGate):
|
|
354
|
-
def _eigen_components(self) ->
|
|
371
|
+
def _eigen_components(self) -> list[tuple[float, np.ndarray]]:
|
|
355
372
|
raise NotImplementedError()
|
|
356
373
|
|
|
357
374
|
def __init__(self, e, *shifts):
|
|
@@ -388,7 +405,7 @@ class WeightedZPowGate(cirq.EigenGate, cirq.testing.SingleQubitGate):
|
|
|
388
405
|
self.weight = weight
|
|
389
406
|
super().__init__(**kwargs)
|
|
390
407
|
|
|
391
|
-
def _eigen_components(self) ->
|
|
408
|
+
def _eigen_components(self) -> list[tuple[float, np.ndarray]]:
|
|
392
409
|
return [(0, np.diag([1, 0])), (self.weight, np.diag([0, 1]))]
|
|
393
410
|
|
|
394
411
|
def _with_exponent(self, exponent):
|
|
@@ -419,5 +436,5 @@ class WeightedZPowGate(cirq.EigenGate, cirq.testing.SingleQubitGate):
|
|
|
419
436
|
(cirq.Z**2, cirq.Z ** (4 + 1e-10), True),
|
|
420
437
|
],
|
|
421
438
|
)
|
|
422
|
-
def test_equal_up_to_global_phase(gate1, gate2, eq_up_to_global_phase):
|
|
439
|
+
def test_equal_up_to_global_phase(gate1, gate2, eq_up_to_global_phase) -> None:
|
|
423
440
|
assert cirq.equal_up_to_global_phase(gate1, gate2) == eq_up_to_global_phase
|
cirq/ops/fourier_transform.py
CHANGED
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
from
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from typing import AbstractSet, Any
|
|
16
18
|
|
|
17
19
|
import numpy as np
|
|
18
20
|
import sympy
|
|
@@ -52,7 +54,7 @@ class QuantumFourierTransformGate(raw_types.Gate):
|
|
|
52
54
|
self._num_qubits = num_qubits
|
|
53
55
|
self._without_reverse = without_reverse
|
|
54
56
|
|
|
55
|
-
def _json_dict_(self) ->
|
|
57
|
+
def _json_dict_(self) -> dict[str, Any]:
|
|
56
58
|
return {'num_qubits': self._num_qubits, 'without_reverse': self._without_reverse}
|
|
57
59
|
|
|
58
60
|
def _value_equality_values_(self):
|
|
@@ -87,9 +89,7 @@ class QuantumFourierTransformGate(raw_types.Gate):
|
|
|
87
89
|
f'without_reverse={self._without_reverse!r})'
|
|
88
90
|
)
|
|
89
91
|
|
|
90
|
-
def _circuit_diagram_info_(
|
|
91
|
-
self, args: 'cirq.CircuitDiagramInfoArgs'
|
|
92
|
-
) -> 'cirq.CircuitDiagramInfo':
|
|
92
|
+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
|
|
93
93
|
return cirq.CircuitDiagramInfo(
|
|
94
94
|
wire_symbols=(str(self),) + tuple(f'#{k+1}' for k in range(1, self._num_qubits)),
|
|
95
95
|
exponent_qubit_index=0,
|
|
@@ -112,15 +112,15 @@ class PhaseGradientGate(raw_types.Gate):
|
|
|
112
112
|
This gate makes up a portion of the quantum fourier transform.
|
|
113
113
|
"""
|
|
114
114
|
|
|
115
|
-
def __init__(self, *, num_qubits: int, exponent:
|
|
115
|
+
def __init__(self, *, num_qubits: int, exponent: float | sympy.Basic):
|
|
116
116
|
self._num_qubits = num_qubits
|
|
117
117
|
self._exponent = exponent
|
|
118
118
|
|
|
119
119
|
@property
|
|
120
|
-
def exponent(self) ->
|
|
120
|
+
def exponent(self) -> float | sympy.Basic:
|
|
121
121
|
return self._exponent
|
|
122
122
|
|
|
123
|
-
def _json_dict_(self) ->
|
|
123
|
+
def _json_dict_(self) -> dict[str, Any]:
|
|
124
124
|
return {'num_qubits': self._num_qubits, 'exponent': self.exponent}
|
|
125
125
|
|
|
126
126
|
def _value_equality_values_(self):
|
|
@@ -133,7 +133,7 @@ class PhaseGradientGate(raw_types.Gate):
|
|
|
133
133
|
for i, q in enumerate(qubits):
|
|
134
134
|
yield cirq.Z(q) ** (self.exponent / 2**i)
|
|
135
135
|
|
|
136
|
-
def _apply_unitary_(self, args:
|
|
136
|
+
def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs):
|
|
137
137
|
if isinstance(self.exponent, sympy.Basic):
|
|
138
138
|
return NotImplemented
|
|
139
139
|
|
|
@@ -167,8 +167,8 @@ class PhaseGradientGate(raw_types.Gate):
|
|
|
167
167
|
return cirq.parameter_names(self.exponent)
|
|
168
168
|
|
|
169
169
|
def _resolve_parameters_(
|
|
170
|
-
self, resolver:
|
|
171
|
-
) ->
|
|
170
|
+
self, resolver: cirq.ParamResolver, recursive: bool
|
|
171
|
+
) -> PhaseGradientGate:
|
|
172
172
|
new_exponent = cirq.resolve_parameters(self.exponent, resolver, recursive)
|
|
173
173
|
return PhaseGradientGate(num_qubits=self._num_qubits, exponent=new_exponent)
|
|
174
174
|
|
|
@@ -182,9 +182,7 @@ class PhaseGradientGate(raw_types.Gate):
|
|
|
182
182
|
f'exponent={_compat.proper_repr(self.exponent)})'
|
|
183
183
|
)
|
|
184
184
|
|
|
185
|
-
def _circuit_diagram_info_(
|
|
186
|
-
self, args: 'cirq.CircuitDiagramInfoArgs'
|
|
187
|
-
) -> 'cirq.CircuitDiagramInfo':
|
|
185
|
+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> cirq.CircuitDiagramInfo:
|
|
188
186
|
return cirq.CircuitDiagramInfo(
|
|
189
187
|
wire_symbols=('Grad',) + tuple(f'#{k+1}' for k in range(1, self._num_qubits)),
|
|
190
188
|
exponent=self.exponent,
|
|
@@ -192,9 +190,7 @@ class PhaseGradientGate(raw_types.Gate):
|
|
|
192
190
|
)
|
|
193
191
|
|
|
194
192
|
|
|
195
|
-
def qft(
|
|
196
|
-
*qubits: 'cirq.Qid', without_reverse: bool = False, inverse: bool = False
|
|
197
|
-
) -> 'cirq.Operation':
|
|
193
|
+
def qft(*qubits: cirq.Qid, without_reverse: bool = False, inverse: bool = False) -> cirq.Operation:
|
|
198
194
|
"""The quantum Fourier transform.
|
|
199
195
|
|
|
200
196
|
Transforms a qubit register from the computational basis to the frequency
|
|
@@ -216,6 +212,4 @@ def qft(
|
|
|
216
212
|
A `cirq.Operation` applying the qft to the given qubits.
|
|
217
213
|
"""
|
|
218
214
|
result = QuantumFourierTransformGate(len(qubits), without_reverse=without_reverse).on(*qubits)
|
|
219
|
-
if inverse
|
|
220
|
-
result = cirq.inverse(result)
|
|
221
|
-
return result
|
|
215
|
+
return cirq.inverse(result) if inverse else result
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
15
17
|
import numpy as np
|
|
16
18
|
import pytest
|
|
17
19
|
import sympy
|
|
@@ -19,7 +21,7 @@ import sympy
|
|
|
19
21
|
import cirq
|
|
20
22
|
|
|
21
23
|
|
|
22
|
-
def test_phase_gradient():
|
|
24
|
+
def test_phase_gradient() -> None:
|
|
23
25
|
np.testing.assert_allclose(
|
|
24
26
|
cirq.unitary(cirq.PhaseGradientGate(num_qubits=2, exponent=1)), np.diag([1, 1j, -1, -1j])
|
|
25
27
|
)
|
|
@@ -31,7 +33,7 @@ def test_phase_gradient():
|
|
|
31
33
|
|
|
32
34
|
|
|
33
35
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
34
|
-
def test_phase_gradient_symbolic(resolve_fn):
|
|
36
|
+
def test_phase_gradient_symbolic(resolve_fn) -> None:
|
|
35
37
|
a = cirq.PhaseGradientGate(num_qubits=2, exponent=0.5)
|
|
36
38
|
b = cirq.PhaseGradientGate(num_qubits=2, exponent=sympy.Symbol('t'))
|
|
37
39
|
assert not cirq.is_parameterized(a)
|
|
@@ -43,22 +45,22 @@ def test_phase_gradient_symbolic(resolve_fn):
|
|
|
43
45
|
assert resolve_fn(b, {'t': 0.25}) == cirq.PhaseGradientGate(num_qubits=2, exponent=0.25)
|
|
44
46
|
|
|
45
47
|
|
|
46
|
-
def test_str():
|
|
48
|
+
def test_str() -> None:
|
|
47
49
|
assert str(cirq.PhaseGradientGate(num_qubits=2, exponent=0.5)) == 'Grad[2]^0.5'
|
|
48
50
|
assert str(cirq.PhaseGradientGate(num_qubits=2, exponent=1)) == 'Grad[2]'
|
|
49
51
|
|
|
50
52
|
|
|
51
|
-
def test_phase_gradient_gate_repr():
|
|
53
|
+
def test_phase_gradient_gate_repr() -> None:
|
|
52
54
|
a = cirq.PhaseGradientGate(num_qubits=2, exponent=0.5)
|
|
53
55
|
cirq.testing.assert_equivalent_repr(a)
|
|
54
56
|
|
|
55
57
|
|
|
56
|
-
def test_quantum_fourier_transform_gate_repr():
|
|
58
|
+
def test_quantum_fourier_transform_gate_repr() -> None:
|
|
57
59
|
b = cirq.QuantumFourierTransformGate(num_qubits=2, without_reverse=False)
|
|
58
60
|
cirq.testing.assert_equivalent_repr(b)
|
|
59
61
|
|
|
60
62
|
|
|
61
|
-
def test_pow():
|
|
63
|
+
def test_pow() -> None:
|
|
62
64
|
a = cirq.PhaseGradientGate(num_qubits=2, exponent=0.5)
|
|
63
65
|
assert a**0.5 == cirq.PhaseGradientGate(num_qubits=2, exponent=0.25)
|
|
64
66
|
assert a ** sympy.Symbol('t') == cirq.PhaseGradientGate(
|
|
@@ -66,7 +68,7 @@ def test_pow():
|
|
|
66
68
|
)
|
|
67
69
|
|
|
68
70
|
|
|
69
|
-
def test_qft():
|
|
71
|
+
def test_qft() -> None:
|
|
70
72
|
# fmt: off
|
|
71
73
|
np.testing.assert_allclose(
|
|
72
74
|
cirq.unitary(cirq.qft(*cirq.LineQubit.range(2))),
|
|
@@ -103,9 +105,10 @@ def test_qft():
|
|
|
103
105
|
atol=1e-8,
|
|
104
106
|
)
|
|
105
107
|
|
|
108
|
+
arr = np.array([[1, 1, 1, 1], [1, -1j, -1, 1j], [1, -1, 1, -1], [1, 1j, -1, -1j]]) / 2
|
|
106
109
|
np.testing.assert_allclose(
|
|
107
|
-
cirq.unitary(cirq.qft(*cirq.LineQubit.range(2)) ** -1),
|
|
108
|
-
|
|
110
|
+
cirq.unitary(cirq.qft(*cirq.LineQubit.range(2)) ** -1), # type: ignore[operator]
|
|
111
|
+
arr, # type: ignore[arg-type]
|
|
109
112
|
atol=1e-8,
|
|
110
113
|
)
|
|
111
114
|
|
|
@@ -116,15 +119,15 @@ def test_qft():
|
|
|
116
119
|
)
|
|
117
120
|
|
|
118
121
|
|
|
119
|
-
def test_inverse():
|
|
122
|
+
def test_inverse() -> None:
|
|
120
123
|
a, b, c = cirq.LineQubit.range(3)
|
|
121
|
-
assert cirq.qft(a, b, c, inverse=True) == cirq.qft(a, b, c) ** -1
|
|
124
|
+
assert cirq.qft(a, b, c, inverse=True) == cirq.qft(a, b, c) ** -1 # type: ignore[operator]
|
|
122
125
|
assert cirq.qft(a, b, c, inverse=True, without_reverse=True) == cirq.inverse(
|
|
123
126
|
cirq.qft(a, b, c, without_reverse=True)
|
|
124
127
|
)
|
|
125
128
|
|
|
126
129
|
|
|
127
|
-
def test_circuit_diagram():
|
|
130
|
+
def test_circuit_diagram() -> None:
|
|
128
131
|
cirq.testing.assert_has_diagram(
|
|
129
132
|
cirq.Circuit(cirq.decompose_once(cirq.qft(*cirq.LineQubit.range(4)))),
|
|
130
133
|
"""
|