cirq-core 1.5.0.dev20250409225226__py3-none-any.whl → 1.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cirq-core might be problematic. Click here for more details.
- 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.dev20250409225226.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.dev20250409225226.dist-info → cirq_core-1.6.0.dist-info}/WHEEL +1 -1
- cirq_core-1.5.0.dev20250409225226.dist-info/RECORD +0 -1216
- {cirq_core-1.5.0.dev20250409225226.dist-info → cirq_core-1.6.0.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.5.0.dev20250409225226.dist-info → cirq_core-1.6.0.dist-info}/top_level.txt +0 -0
cirq/ops/phased_x_gate_test.py
CHANGED
|
@@ -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 itertools
|
|
16
18
|
|
|
17
19
|
import numpy as np
|
|
@@ -24,7 +26,7 @@ import cirq
|
|
|
24
26
|
@pytest.mark.parametrize(
|
|
25
27
|
'phase_exponent', [-0.5, 0, 0.5, 1, sympy.Symbol('p'), sympy.Symbol('p') + 1]
|
|
26
28
|
)
|
|
27
|
-
def test_phased_x_consistent_protocols(phase_exponent):
|
|
29
|
+
def test_phased_x_consistent_protocols(phase_exponent) -> None:
|
|
28
30
|
cirq.testing.assert_implements_consistent_protocols(
|
|
29
31
|
cirq.PhasedXPowGate(phase_exponent=phase_exponent, exponent=1.0)
|
|
30
32
|
)
|
|
@@ -33,7 +35,7 @@ def test_phased_x_consistent_protocols(phase_exponent):
|
|
|
33
35
|
)
|
|
34
36
|
|
|
35
37
|
|
|
36
|
-
def test_init():
|
|
38
|
+
def test_init() -> None:
|
|
37
39
|
g = cirq.PhasedXPowGate(phase_exponent=0.75, exponent=0.25, global_shift=0.1)
|
|
38
40
|
assert g.phase_exponent == 0.75
|
|
39
41
|
assert g.exponent == 0.25
|
|
@@ -51,13 +53,13 @@ def test_init():
|
|
|
51
53
|
|
|
52
54
|
|
|
53
55
|
@pytest.mark.parametrize('sym', [sympy.Symbol('a'), sympy.Symbol('a') + 1])
|
|
54
|
-
def test_no_symbolic_qasm_but_fails_gracefully(sym):
|
|
56
|
+
def test_no_symbolic_qasm_but_fails_gracefully(sym) -> None:
|
|
55
57
|
q = cirq.NamedQubit('q')
|
|
56
58
|
v = cirq.PhasedXPowGate(phase_exponent=sym).on(q)
|
|
57
59
|
assert cirq.qasm(v, args=cirq.QasmArgs(), default=None) is None
|
|
58
60
|
|
|
59
61
|
|
|
60
|
-
def test_extrapolate():
|
|
62
|
+
def test_extrapolate() -> None:
|
|
61
63
|
g = cirq.PhasedXPowGate(phase_exponent=0.25)
|
|
62
64
|
assert g**0.25 == (g**0.5) ** 0.5
|
|
63
65
|
|
|
@@ -71,7 +73,7 @@ def test_extrapolate():
|
|
|
71
73
|
assert g**0.5 != (g**3) ** 0.5 == g**-0.5
|
|
72
74
|
|
|
73
75
|
|
|
74
|
-
def test_eq():
|
|
76
|
+
def test_eq() -> None:
|
|
75
77
|
eq = cirq.testing.EqualsTester()
|
|
76
78
|
eq.add_equality_group(
|
|
77
79
|
cirq.PhasedXPowGate(phase_exponent=0),
|
|
@@ -122,7 +124,7 @@ def test_eq():
|
|
|
122
124
|
)
|
|
123
125
|
|
|
124
126
|
|
|
125
|
-
def test_approx_eq():
|
|
127
|
+
def test_approx_eq() -> None:
|
|
126
128
|
assert cirq.approx_eq(
|
|
127
129
|
cirq.PhasedXPowGate(phase_exponent=0.1, exponent=0.2, global_shift=0.3),
|
|
128
130
|
cirq.PhasedXPowGate(phase_exponent=0.1, exponent=0.2, global_shift=0.3),
|
|
@@ -140,7 +142,7 @@ def test_approx_eq():
|
|
|
140
142
|
)
|
|
141
143
|
|
|
142
144
|
|
|
143
|
-
def test_str_repr():
|
|
145
|
+
def test_str_repr() -> None:
|
|
144
146
|
assert str(cirq.PhasedXPowGate(phase_exponent=0.25)) == 'PhX(0.25)'
|
|
145
147
|
assert str(cirq.PhasedXPowGate(phase_exponent=0.25, exponent=0.5)) == 'PhX(0.25)**0.5'
|
|
146
148
|
assert repr(
|
|
@@ -156,7 +158,7 @@ def test_str_repr():
|
|
|
156
158
|
@pytest.mark.parametrize(
|
|
157
159
|
'resolve_fn, global_shift', [(cirq.resolve_parameters, 0), (cirq.resolve_parameters_once, 0.1)]
|
|
158
160
|
)
|
|
159
|
-
def test_parameterize(resolve_fn, global_shift):
|
|
161
|
+
def test_parameterize(resolve_fn, global_shift) -> None:
|
|
160
162
|
parameterized_gate = cirq.PhasedXPowGate(
|
|
161
163
|
exponent=sympy.Symbol('a'), phase_exponent=sympy.Symbol('b'), global_shift=global_shift
|
|
162
164
|
)
|
|
@@ -187,24 +189,24 @@ def test_parameterize(resolve_fn, global_shift):
|
|
|
187
189
|
assert cirq.is_parameterized(unparameterized_gate ** sympy.Symbol('a'))
|
|
188
190
|
assert cirq.is_parameterized(unparameterized_gate ** (sympy.Symbol('a') + 1))
|
|
189
191
|
|
|
190
|
-
|
|
192
|
+
resolver_dict = {'a': 0.5j}
|
|
191
193
|
with pytest.raises(ValueError, match='complex value'):
|
|
192
194
|
resolve_fn(
|
|
193
195
|
cirq.PhasedXPowGate(
|
|
194
196
|
exponent=sympy.Symbol('a'), phase_exponent=0.2, global_shift=global_shift
|
|
195
197
|
),
|
|
196
|
-
|
|
198
|
+
resolver_dict,
|
|
197
199
|
)
|
|
198
200
|
with pytest.raises(ValueError, match='complex value'):
|
|
199
201
|
resolve_fn(
|
|
200
202
|
cirq.PhasedXPowGate(
|
|
201
203
|
exponent=0.1, phase_exponent=sympy.Symbol('a'), global_shift=global_shift
|
|
202
204
|
),
|
|
203
|
-
|
|
205
|
+
resolver_dict,
|
|
204
206
|
)
|
|
205
207
|
|
|
206
208
|
|
|
207
|
-
def test_trace_bound():
|
|
209
|
+
def test_trace_bound() -> None:
|
|
208
210
|
assert (
|
|
209
211
|
cirq.trace_distance_bound(cirq.PhasedXPowGate(phase_exponent=0.25, exponent=0.001)) < 0.01
|
|
210
212
|
)
|
|
@@ -216,7 +218,7 @@ def test_trace_bound():
|
|
|
216
218
|
)
|
|
217
219
|
|
|
218
220
|
|
|
219
|
-
def test_diagram():
|
|
221
|
+
def test_diagram() -> None:
|
|
220
222
|
q = cirq.NamedQubit('q')
|
|
221
223
|
c = cirq.Circuit(
|
|
222
224
|
cirq.PhasedXPowGate(phase_exponent=sympy.Symbol('a'), exponent=sympy.Symbol('b')).on(q),
|
|
@@ -234,7 +236,7 @@ q: ───PhX(a)^b───PhX(2*a)^(b + 1)───PhX(0.25)───PhX(1)
|
|
|
234
236
|
)
|
|
235
237
|
|
|
236
238
|
|
|
237
|
-
def test_phase_by():
|
|
239
|
+
def test_phase_by() -> None:
|
|
238
240
|
g = cirq.PhasedXPowGate(phase_exponent=0.25)
|
|
239
241
|
g2 = cirq.phase_by(g, 0.25, 0)
|
|
240
242
|
assert g2 == cirq.PhasedXPowGate(phase_exponent=0.75)
|
|
@@ -255,7 +257,7 @@ def test_phase_by():
|
|
|
255
257
|
@pytest.mark.parametrize(
|
|
256
258
|
'exponent,phase_exponent', itertools.product(np.arange(-2.5, 2.75, 0.25), repeat=2)
|
|
257
259
|
)
|
|
258
|
-
def test_exponent_consistency(exponent, phase_exponent):
|
|
260
|
+
def test_exponent_consistency(exponent, phase_exponent) -> None:
|
|
259
261
|
"""Verifies that instances of PhasedX gate expose consistent exponents."""
|
|
260
262
|
g = cirq.PhasedXPowGate(exponent=exponent, phase_exponent=phase_exponent)
|
|
261
263
|
assert g.exponent in [exponent, -exponent]
|
|
@@ -269,7 +271,7 @@ def test_exponent_consistency(exponent, phase_exponent):
|
|
|
269
271
|
assert np.all(u == u2)
|
|
270
272
|
|
|
271
273
|
|
|
272
|
-
def test_approx_eq_for_close_phase_exponents():
|
|
274
|
+
def test_approx_eq_for_close_phase_exponents() -> None:
|
|
273
275
|
gate1 = cirq.PhasedXPowGate(phase_exponent=0)
|
|
274
276
|
gate2 = cirq.PhasedXPowGate(phase_exponent=1e-12)
|
|
275
277
|
gate3 = cirq.PhasedXPowGate(phase_exponent=2e-12)
|
cirq/ops/phased_x_z_gate.py
CHANGED
|
@@ -12,8 +12,10 @@
|
|
|
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 numbers
|
|
16
|
-
from typing import AbstractSet, Any,
|
|
18
|
+
from typing import AbstractSet, Any, Iterator, Sequence, TYPE_CHECKING
|
|
17
19
|
|
|
18
20
|
import numpy as np
|
|
19
21
|
import sympy
|
|
@@ -50,9 +52,9 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
50
52
|
def __init__(
|
|
51
53
|
self,
|
|
52
54
|
*,
|
|
53
|
-
x_exponent:
|
|
54
|
-
z_exponent:
|
|
55
|
-
axis_phase_exponent:
|
|
55
|
+
x_exponent: float | sympy.Expr,
|
|
56
|
+
z_exponent: float | sympy.Expr,
|
|
57
|
+
axis_phase_exponent: float | sympy.Expr,
|
|
56
58
|
) -> None:
|
|
57
59
|
"""Inits PhasedXZGate.
|
|
58
60
|
|
|
@@ -69,7 +71,7 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
69
71
|
self._axis_phase_exponent = axis_phase_exponent
|
|
70
72
|
|
|
71
73
|
@classmethod
|
|
72
|
-
def from_zyz_angles(cls, z0_rad: float, y_rad: float, z1_rad: float) ->
|
|
74
|
+
def from_zyz_angles(cls, z0_rad: float, y_rad: float, z1_rad: float) -> cirq.PhasedXZGate:
|
|
73
75
|
r"""Create a PhasedXZGate from ZYZ angles.
|
|
74
76
|
|
|
75
77
|
The returned gate is equivalent to $Rz(z0\_rad) Ry(y\_rad) Rz(z1\_rad)$ (in time order).
|
|
@@ -77,14 +79,14 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
77
79
|
return cls.from_zyz_exponents(z0=z0_rad / np.pi, y=y_rad / np.pi, z1=z1_rad / np.pi)
|
|
78
80
|
|
|
79
81
|
@classmethod
|
|
80
|
-
def from_zyz_exponents(cls, z0: float, y: float, z1: float) ->
|
|
82
|
+
def from_zyz_exponents(cls, z0: float, y: float, z1: float) -> cirq.PhasedXZGate:
|
|
81
83
|
"""Create a PhasedXZGate from ZYZ exponents.
|
|
82
84
|
|
|
83
85
|
The returned gate is equivalent to $Z^{z0} Y^y Z^{z1}$ (in time order).
|
|
84
86
|
"""
|
|
85
87
|
return PhasedXZGate(axis_phase_exponent=-z0 + 0.5, x_exponent=y, z_exponent=z0 + z1)
|
|
86
88
|
|
|
87
|
-
def _canonical(self) ->
|
|
89
|
+
def _canonical(self) -> cirq.PhasedXZGate:
|
|
88
90
|
x = self.x_exponent
|
|
89
91
|
z = self.z_exponent
|
|
90
92
|
a = self.axis_phase_exponent
|
|
@@ -126,15 +128,15 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
126
128
|
return PhasedXZGate(x_exponent=x, z_exponent=z, axis_phase_exponent=a)
|
|
127
129
|
|
|
128
130
|
@property
|
|
129
|
-
def x_exponent(self) ->
|
|
131
|
+
def x_exponent(self) -> float | sympy.Expr:
|
|
130
132
|
return self._x_exponent
|
|
131
133
|
|
|
132
134
|
@property
|
|
133
|
-
def z_exponent(self) ->
|
|
135
|
+
def z_exponent(self) -> float | sympy.Expr:
|
|
134
136
|
return self._z_exponent
|
|
135
137
|
|
|
136
138
|
@property
|
|
137
|
-
def axis_phase_exponent(self) ->
|
|
139
|
+
def axis_phase_exponent(self) -> float | sympy.Expr:
|
|
138
140
|
return self._axis_phase_exponent
|
|
139
141
|
|
|
140
142
|
def _value_equality_values_(self):
|
|
@@ -146,7 +148,7 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
146
148
|
)
|
|
147
149
|
|
|
148
150
|
@staticmethod
|
|
149
|
-
def from_matrix(mat: np.ndarray) ->
|
|
151
|
+
def from_matrix(mat: np.ndarray) -> cirq.PhasedXZGate:
|
|
150
152
|
pre_phase, rotation, post_phase = linalg.deconstruct_single_qubit_matrix_into_angles(mat)
|
|
151
153
|
pre_phase /= np.pi
|
|
152
154
|
post_phase /= np.pi
|
|
@@ -157,14 +159,14 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
157
159
|
x_exponent=rotation, axis_phase_exponent=-pre_phase, z_exponent=post_phase + pre_phase
|
|
158
160
|
)._canonical()
|
|
159
161
|
|
|
160
|
-
def with_z_exponent(self, z_exponent:
|
|
162
|
+
def with_z_exponent(self, z_exponent: float | sympy.Expr) -> cirq.PhasedXZGate:
|
|
161
163
|
return PhasedXZGate(
|
|
162
164
|
axis_phase_exponent=self._axis_phase_exponent,
|
|
163
165
|
x_exponent=self._x_exponent,
|
|
164
166
|
z_exponent=z_exponent,
|
|
165
167
|
)
|
|
166
168
|
|
|
167
|
-
def _qasm_(self, args:
|
|
169
|
+
def _qasm_(self, args: cirq.QasmArgs, qubits: tuple[cirq.Qid, ...]) -> str | None:
|
|
168
170
|
from cirq.circuits import qasm_output
|
|
169
171
|
|
|
170
172
|
qasm_gate = qasm_output.QasmUGate(
|
|
@@ -180,7 +182,7 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
180
182
|
def _has_unitary_(self) -> bool:
|
|
181
183
|
return not self._is_parameterized_()
|
|
182
184
|
|
|
183
|
-
def _unitary_(self) ->
|
|
185
|
+
def _unitary_(self) -> np.ndarray | None:
|
|
184
186
|
"""See `cirq.SupportsUnitary`."""
|
|
185
187
|
if self._is_parameterized_():
|
|
186
188
|
return None
|
|
@@ -189,13 +191,13 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
189
191
|
z_post = protocols.unitary(ops.Z ** (self._axis_phase_exponent + self._z_exponent))
|
|
190
192
|
return z_post @ x @ z_pre
|
|
191
193
|
|
|
192
|
-
def _decompose_(self, qubits: Sequence[
|
|
194
|
+
def _decompose_(self, qubits: Sequence[cirq.Qid]) -> Iterator[cirq.OP_TREE]:
|
|
193
195
|
q = qubits[0]
|
|
194
196
|
yield ops.Z(q) ** -self._axis_phase_exponent
|
|
195
197
|
yield ops.X(q) ** self._x_exponent
|
|
196
198
|
yield ops.Z(q) ** (self._axis_phase_exponent + self._z_exponent)
|
|
197
199
|
|
|
198
|
-
def __pow__(self, exponent: float) ->
|
|
200
|
+
def __pow__(self, exponent: float) -> PhasedXZGate:
|
|
199
201
|
if exponent == 1:
|
|
200
202
|
return self
|
|
201
203
|
if exponent == -1:
|
|
@@ -223,8 +225,8 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
223
225
|
)
|
|
224
226
|
|
|
225
227
|
def _resolve_parameters_(
|
|
226
|
-
self, resolver:
|
|
227
|
-
) ->
|
|
228
|
+
self, resolver: cirq.ParamResolver, recursive: bool
|
|
229
|
+
) -> cirq.PhasedXZGate:
|
|
228
230
|
"""See `cirq.SupportsParameterization`."""
|
|
229
231
|
z_exponent = resolver.value_of(self._z_exponent, recursive)
|
|
230
232
|
x_exponent = resolver.value_of(self._x_exponent, recursive)
|
|
@@ -250,7 +252,7 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
250
252
|
z_exponent=z_exponent, x_exponent=x_exponent, axis_phase_exponent=axis_phase_exponent
|
|
251
253
|
)
|
|
252
254
|
|
|
253
|
-
def _phase_by_(self, phase_turns, qubit_index) ->
|
|
255
|
+
def _phase_by_(self, phase_turns, qubit_index) -> cirq.PhasedXZGate:
|
|
254
256
|
"""See `cirq.SupportsPhase`."""
|
|
255
257
|
assert qubit_index == 0
|
|
256
258
|
return PhasedXZGate(
|
|
@@ -259,7 +261,7 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
259
261
|
axis_phase_exponent=self._axis_phase_exponent + phase_turns * 2,
|
|
260
262
|
)
|
|
261
263
|
|
|
262
|
-
def _pauli_expansion_(self) ->
|
|
264
|
+
def _pauli_expansion_(self) -> cirq.LinearDict[str]:
|
|
263
265
|
if protocols.is_parameterized(self):
|
|
264
266
|
return NotImplemented
|
|
265
267
|
x_angle = np.pi * self._x_exponent / 2
|
|
@@ -278,7 +280,7 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
278
280
|
}
|
|
279
281
|
) # yapf: disable
|
|
280
282
|
|
|
281
|
-
def _circuit_diagram_info_(self, args:
|
|
283
|
+
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> str:
|
|
282
284
|
"""See `cirq.SupportsCircuitDiagramInfo`."""
|
|
283
285
|
return (
|
|
284
286
|
f'PhXZ('
|
|
@@ -298,7 +300,7 @@ class PhasedXZGate(raw_types.Gate):
|
|
|
298
300
|
f'z_exponent={proper_repr(self._z_exponent)})'
|
|
299
301
|
)
|
|
300
302
|
|
|
301
|
-
def _json_dict_(self) ->
|
|
303
|
+
def _json_dict_(self) -> dict[str, Any]:
|
|
302
304
|
return protocols.obj_to_dict_helper(
|
|
303
305
|
self, ['axis_phase_exponent', 'x_exponent', 'z_exponent']
|
|
304
306
|
)
|
cirq/ops/phased_x_z_gate_test.py
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# pylint: disable=wrong-or-nonexistent-copyright-notice
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
2
5
|
import random
|
|
6
|
+
from typing import cast
|
|
3
7
|
|
|
4
8
|
import numpy as np
|
|
5
9
|
import pytest
|
|
@@ -8,14 +12,14 @@ import sympy
|
|
|
8
12
|
import cirq
|
|
9
13
|
|
|
10
14
|
|
|
11
|
-
def test_init_properties():
|
|
15
|
+
def test_init_properties() -> None:
|
|
12
16
|
g = cirq.PhasedXZGate(x_exponent=0.125, z_exponent=0.25, axis_phase_exponent=0.375)
|
|
13
17
|
assert g.x_exponent == 0.125
|
|
14
18
|
assert g.z_exponent == 0.25
|
|
15
19
|
assert g.axis_phase_exponent == 0.375
|
|
16
20
|
|
|
17
21
|
|
|
18
|
-
def test_eq():
|
|
22
|
+
def test_eq() -> None:
|
|
19
23
|
eq = cirq.testing.EqualsTester()
|
|
20
24
|
eq.make_equality_group(
|
|
21
25
|
lambda: cirq.PhasedXZGate(x_exponent=0.25, z_exponent=0.5, axis_phase_exponent=0.75)
|
|
@@ -58,7 +62,7 @@ def test_from_zyz_exponents(z0: float, y: float, z1: float) -> None:
|
|
|
58
62
|
)
|
|
59
63
|
|
|
60
64
|
|
|
61
|
-
def test_canonicalization():
|
|
65
|
+
def test_canonicalization() -> None:
|
|
62
66
|
def f(x, z, a):
|
|
63
67
|
return cirq.PhasedXZGate(x_exponent=x, z_exponent=z, axis_phase_exponent=a)
|
|
64
68
|
|
|
@@ -145,7 +149,7 @@ def test_canonicalization():
|
|
|
145
149
|
assert t.axis_phase_exponent == 0
|
|
146
150
|
|
|
147
151
|
|
|
148
|
-
def test_from_matrix():
|
|
152
|
+
def test_from_matrix() -> None:
|
|
149
153
|
# Axis rotations.
|
|
150
154
|
assert cirq.approx_eq(
|
|
151
155
|
cirq.PhasedXZGate.from_matrix(cirq.unitary(cirq.X**0.1)),
|
|
@@ -217,7 +221,7 @@ def test_from_matrix():
|
|
|
217
221
|
np.array([[0, 1], [1j, 0]]),
|
|
218
222
|
],
|
|
219
223
|
)
|
|
220
|
-
def test_from_matrix_close_unitary(unitary: np.ndarray):
|
|
224
|
+
def test_from_matrix_close_unitary(unitary: np.ndarray) -> None:
|
|
221
225
|
cirq.testing.assert_allclose_up_to_global_phase(
|
|
222
226
|
cirq.unitary(cirq.PhasedXZGate.from_matrix(unitary)), unitary, atol=1e-8
|
|
223
227
|
)
|
|
@@ -232,14 +236,14 @@ def test_from_matrix_close_unitary(unitary: np.ndarray):
|
|
|
232
236
|
np.array([[0, 1], [1j, 0]]),
|
|
233
237
|
],
|
|
234
238
|
)
|
|
235
|
-
def test_from_matrix_close_kraus(unitary: np.ndarray):
|
|
239
|
+
def test_from_matrix_close_kraus(unitary: np.ndarray) -> None:
|
|
236
240
|
gate = cirq.PhasedXZGate.from_matrix(unitary)
|
|
237
241
|
kraus = cirq.kraus(gate)
|
|
238
242
|
assert len(kraus) == 1
|
|
239
243
|
cirq.testing.assert_allclose_up_to_global_phase(kraus[0], unitary, atol=1e-8)
|
|
240
244
|
|
|
241
245
|
|
|
242
|
-
def test_protocols():
|
|
246
|
+
def test_protocols() -> None:
|
|
243
247
|
a = random.random()
|
|
244
248
|
b = random.random()
|
|
245
249
|
c = random.random()
|
|
@@ -256,7 +260,7 @@ def test_protocols():
|
|
|
256
260
|
cirq.testing.assert_implements_consistent_protocols(g)
|
|
257
261
|
|
|
258
262
|
|
|
259
|
-
def test_inverse():
|
|
263
|
+
def test_inverse() -> None:
|
|
260
264
|
a = random.random()
|
|
261
265
|
b = random.random()
|
|
262
266
|
c = random.random()
|
|
@@ -264,12 +268,14 @@ def test_inverse():
|
|
|
264
268
|
g = cirq.PhasedXZGate(x_exponent=a, z_exponent=b, axis_phase_exponent=c).on(q)
|
|
265
269
|
|
|
266
270
|
cirq.testing.assert_allclose_up_to_global_phase(
|
|
267
|
-
cirq.unitary(
|
|
271
|
+
cirq.unitary(cast(cirq.GateOperation, g) ** -1),
|
|
272
|
+
np.transpose(np.conjugate(cirq.unitary(g))),
|
|
273
|
+
atol=1e-8,
|
|
268
274
|
)
|
|
269
275
|
|
|
270
276
|
|
|
271
277
|
@pytest.mark.parametrize('resolve_fn', [cirq.resolve_parameters, cirq.resolve_parameters_once])
|
|
272
|
-
def test_parameterized(resolve_fn):
|
|
278
|
+
def test_parameterized(resolve_fn) -> None:
|
|
273
279
|
a = random.random()
|
|
274
280
|
b = random.random()
|
|
275
281
|
c = random.random()
|
|
@@ -296,7 +302,7 @@ def test_parameterized(resolve_fn):
|
|
|
296
302
|
resolve_fn(cirq.PhasedXZGate(x_exponent=a, z_exponent=b, axis_phase_exponent=t), resolver)
|
|
297
303
|
|
|
298
304
|
|
|
299
|
-
def test_str_diagram():
|
|
305
|
+
def test_str_diagram() -> None:
|
|
300
306
|
g = cirq.PhasedXZGate(x_exponent=0.5, z_exponent=0.25, axis_phase_exponent=0.125)
|
|
301
307
|
|
|
302
308
|
assert str(g) == "PhXZ(a=0.125,x=0.5,z=0.25)"
|
cirq/ops/projector.py
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
# pylint: disable=wrong-or-nonexistent-copyright-notice
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
2
5
|
import itertools
|
|
3
6
|
import math
|
|
4
|
-
from typing import Any,
|
|
7
|
+
from typing import Any, Iterable, Mapping, TYPE_CHECKING
|
|
5
8
|
|
|
6
9
|
import numpy as np
|
|
7
10
|
from scipy.sparse import csr_matrix
|
|
8
11
|
|
|
9
12
|
from cirq import value
|
|
10
|
-
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
import cirq
|
|
11
16
|
|
|
12
17
|
|
|
13
18
|
def _check_qids_dimension(qids):
|
|
@@ -21,7 +26,7 @@ def _check_qids_dimension(qids):
|
|
|
21
26
|
class ProjectorString:
|
|
22
27
|
"""Mapping of `cirq.Qid` to measurement values (with a coefficient) representing a projector."""
|
|
23
28
|
|
|
24
|
-
def __init__(self, projector_dict:
|
|
29
|
+
def __init__(self, projector_dict: dict[cirq.Qid, int], coefficient: complex = 1):
|
|
25
30
|
"""Constructor for ProjectorString
|
|
26
31
|
|
|
27
32
|
Args:
|
|
@@ -34,14 +39,14 @@ class ProjectorString:
|
|
|
34
39
|
self._coefficient = complex(coefficient)
|
|
35
40
|
|
|
36
41
|
@property
|
|
37
|
-
def projector_dict(self) ->
|
|
42
|
+
def projector_dict(self) -> dict[cirq.Qid, int]:
|
|
38
43
|
return self._projector_dict
|
|
39
44
|
|
|
40
45
|
@property
|
|
41
46
|
def coefficient(self) -> complex:
|
|
42
47
|
return self._coefficient
|
|
43
48
|
|
|
44
|
-
def matrix(self, projector_qids:
|
|
49
|
+
def matrix(self, projector_qids: Iterable[cirq.Qid] | None = None) -> csr_matrix:
|
|
45
50
|
"""Returns the matrix of self in computational basis of qubits.
|
|
46
51
|
|
|
47
52
|
Args:
|
|
@@ -75,15 +80,15 @@ class ProjectorString:
|
|
|
75
80
|
([self._coefficient] * len(ones_idx), (ones_idx, ones_idx)), shape=(total_d, total_d)
|
|
76
81
|
)
|
|
77
82
|
|
|
78
|
-
def _get_idx_to_keep(self, qid_map: Mapping[
|
|
83
|
+
def _get_idx_to_keep(self, qid_map: Mapping[cirq.Qid, int]):
|
|
79
84
|
num_qubits = len(qid_map)
|
|
80
|
-
idx_to_keep:
|
|
85
|
+
idx_to_keep: list[Any] = [slice(0, 2)] * num_qubits
|
|
81
86
|
for q in self.projector_dict.keys():
|
|
82
87
|
idx_to_keep[qid_map[q]] = self.projector_dict[q]
|
|
83
88
|
return tuple(idx_to_keep)
|
|
84
89
|
|
|
85
90
|
def expectation_from_state_vector(
|
|
86
|
-
self, state_vector: np.ndarray, qid_map: Mapping[
|
|
91
|
+
self, state_vector: np.ndarray, qid_map: Mapping[cirq.Qid, int]
|
|
87
92
|
) -> complex:
|
|
88
93
|
"""Expectation of the projection from a state vector.
|
|
89
94
|
|
|
@@ -105,7 +110,7 @@ class ProjectorString:
|
|
|
105
110
|
)
|
|
106
111
|
|
|
107
112
|
def expectation_from_density_matrix(
|
|
108
|
-
self, state: np.ndarray, qid_map: Mapping[
|
|
113
|
+
self, state: np.ndarray, qid_map: Mapping[cirq.Qid, int]
|
|
109
114
|
) -> complex:
|
|
110
115
|
"""Expectation of the projection from a density matrix.
|
|
111
116
|
|
|
@@ -130,10 +135,10 @@ class ProjectorString:
|
|
|
130
135
|
def __repr__(self) -> str:
|
|
131
136
|
return (
|
|
132
137
|
f"cirq.ProjectorString(projector_dict={self._projector_dict},"
|
|
133
|
-
|
|
138
|
+
f"coefficient={self._coefficient})"
|
|
134
139
|
)
|
|
135
140
|
|
|
136
|
-
def _json_dict_(self) ->
|
|
141
|
+
def _json_dict_(self) -> dict[str, Any]:
|
|
137
142
|
return {
|
|
138
143
|
'projector_dict': list(self._projector_dict.items()),
|
|
139
144
|
'coefficient': self._coefficient,
|
cirq/ops/projector_test.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
# pylint: disable=wrong-or-nonexistent-copyright-notice
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
2
5
|
import numpy as np
|
|
3
6
|
import pytest
|
|
4
7
|
|
|
5
8
|
import cirq
|
|
6
9
|
|
|
7
10
|
|
|
8
|
-
def test_projector_matrix():
|
|
11
|
+
def test_projector_matrix() -> None:
|
|
9
12
|
q0 = cirq.NamedQubit('q0')
|
|
10
13
|
|
|
11
14
|
zero_projector = cirq.ProjectorString({q0: 0})
|
|
@@ -15,11 +18,11 @@ def test_projector_matrix():
|
|
|
15
18
|
np.testing.assert_allclose(zero_projector.matrix().toarray(), [[1.0, 0.0], [0.0, 0.0]])
|
|
16
19
|
np.testing.assert_allclose(one_projector.matrix().toarray(), [[0.0, 0.0], [0.0, 1.0]])
|
|
17
20
|
np.testing.assert_allclose(
|
|
18
|
-
coeff_projector.matrix().toarray(), [[1.23 + 4.56j, 0.0], [0.0, 0.0]]
|
|
21
|
+
coeff_projector.matrix().toarray(), np.asarray([[1.23 + 4.56j, 0.0], [0.0, 0.0]])
|
|
19
22
|
)
|
|
20
23
|
|
|
21
24
|
|
|
22
|
-
def test_projector_repr():
|
|
25
|
+
def test_projector_repr() -> None:
|
|
23
26
|
q0 = cirq.NamedQubit('q0')
|
|
24
27
|
|
|
25
28
|
assert (
|
|
@@ -28,14 +31,14 @@ def test_projector_repr():
|
|
|
28
31
|
)
|
|
29
32
|
|
|
30
33
|
|
|
31
|
-
def test_projector_from_np_array():
|
|
34
|
+
def test_projector_from_np_array() -> None:
|
|
32
35
|
q0 = cirq.NamedQubit('q0')
|
|
33
36
|
|
|
34
37
|
zero_projector = cirq.ProjectorString({q0: 0})
|
|
35
38
|
np.testing.assert_allclose(zero_projector.matrix().toarray(), [[1.0, 0.0], [0.0, 0.0]])
|
|
36
39
|
|
|
37
40
|
|
|
38
|
-
def test_projector_matrix_missing_qid():
|
|
41
|
+
def test_projector_matrix_missing_qid() -> None:
|
|
39
42
|
q0, q1 = cirq.LineQubit.range(2)
|
|
40
43
|
proj = cirq.ProjectorString({q0: 0})
|
|
41
44
|
proj_with_coefficient = cirq.ProjectorString({q0: 0}, 1.23 + 4.56j)
|
|
@@ -53,7 +56,7 @@ def test_projector_matrix_missing_qid():
|
|
|
53
56
|
)
|
|
54
57
|
|
|
55
58
|
|
|
56
|
-
def test_equality():
|
|
59
|
+
def test_equality() -> None:
|
|
57
60
|
q0 = cirq.NamedQubit('q0')
|
|
58
61
|
|
|
59
62
|
obj1a = cirq.ProjectorString({q0: 0})
|
|
@@ -67,7 +70,7 @@ def test_equality():
|
|
|
67
70
|
eq.add_equality_group(obj3)
|
|
68
71
|
|
|
69
72
|
|
|
70
|
-
def test_get_values():
|
|
73
|
+
def test_get_values() -> None:
|
|
71
74
|
q0 = cirq.NamedQubit('q0')
|
|
72
75
|
d = cirq.ProjectorString({q0: 0}, 1.23 + 4.56j)
|
|
73
76
|
|
|
@@ -76,14 +79,14 @@ def test_get_values():
|
|
|
76
79
|
assert d.coefficient == 1.23 + 4.56j
|
|
77
80
|
|
|
78
81
|
|
|
79
|
-
def test_expectation_from_state_vector_basis_states_empty():
|
|
82
|
+
def test_expectation_from_state_vector_basis_states_empty() -> None:
|
|
80
83
|
q0 = cirq.NamedQubit('q0')
|
|
81
84
|
d = cirq.ProjectorString({})
|
|
82
85
|
|
|
83
86
|
np.testing.assert_allclose(d.expectation_from_state_vector(np.array([1.0, 0.0]), {q0: 0}), 1.0)
|
|
84
87
|
|
|
85
88
|
|
|
86
|
-
def test_expectation_from_state_vector_basis_states_single_qubits():
|
|
89
|
+
def test_expectation_from_state_vector_basis_states_single_qubits() -> None:
|
|
87
90
|
q0 = cirq.NamedQubit('q0')
|
|
88
91
|
d = cirq.ProjectorString({q0: 0})
|
|
89
92
|
|
|
@@ -91,7 +94,7 @@ def test_expectation_from_state_vector_basis_states_single_qubits():
|
|
|
91
94
|
np.testing.assert_allclose(d.expectation_from_state_vector(np.array([0.0, 1.0]), {q0: 0}), 0.0)
|
|
92
95
|
|
|
93
96
|
|
|
94
|
-
def test_expectation_from_state_vector_basis_states_three_qubits():
|
|
97
|
+
def test_expectation_from_state_vector_basis_states_three_qubits() -> None:
|
|
95
98
|
q0 = cirq.NamedQubit('q0')
|
|
96
99
|
q1 = cirq.NamedQubit('q1')
|
|
97
100
|
q2 = cirq.NamedQubit('q2')
|
|
@@ -141,7 +144,7 @@ def test_expectation_from_state_vector_basis_states_three_qubits():
|
|
|
141
144
|
)
|
|
142
145
|
|
|
143
146
|
|
|
144
|
-
def test_expectation_from_density_matrix_three_qubits():
|
|
147
|
+
def test_expectation_from_density_matrix_three_qubits() -> None:
|
|
145
148
|
q0 = cirq.NamedQubit('q0')
|
|
146
149
|
q1 = cirq.NamedQubit('q1')
|
|
147
150
|
q2 = cirq.NamedQubit('q2')
|
|
@@ -191,7 +194,7 @@ def test_expectation_from_density_matrix_three_qubits():
|
|
|
191
194
|
)
|
|
192
195
|
|
|
193
196
|
|
|
194
|
-
def test_consistency_state_vector_and_density_matrix():
|
|
197
|
+
def test_consistency_state_vector_and_density_matrix() -> None:
|
|
195
198
|
q0 = cirq.NamedQubit('q0')
|
|
196
199
|
q1 = cirq.NamedQubit('q1')
|
|
197
200
|
q2 = cirq.NamedQubit('q2')
|
|
@@ -209,7 +212,7 @@ def test_consistency_state_vector_and_density_matrix():
|
|
|
209
212
|
)
|
|
210
213
|
|
|
211
214
|
|
|
212
|
-
def test_expectation_higher_dims():
|
|
215
|
+
def test_expectation_higher_dims() -> None:
|
|
213
216
|
qubit = cirq.NamedQid('q0', dimension=2)
|
|
214
217
|
qutrit = cirq.NamedQid('q1', dimension=3)
|
|
215
218
|
|
|
@@ -221,7 +224,7 @@ def test_expectation_higher_dims():
|
|
|
221
224
|
_ = (d.expectation_from_state_vector(np.zeros(2 * 3), {qubit: 0, qutrit: 0}),)
|
|
222
225
|
|
|
223
226
|
|
|
224
|
-
def test_expectation_with_coefficient():
|
|
227
|
+
def test_expectation_with_coefficient() -> None:
|
|
225
228
|
q0 = cirq.NamedQubit('q0')
|
|
226
229
|
d = cirq.ProjectorString({q0: 0}, coefficient=(0.6 + 0.4j))
|
|
227
230
|
|
|
@@ -234,7 +237,7 @@ def test_expectation_with_coefficient():
|
|
|
234
237
|
)
|
|
235
238
|
|
|
236
239
|
|
|
237
|
-
def test_expectation_from_density_matrix_basis_states_empty():
|
|
240
|
+
def test_expectation_from_density_matrix_basis_states_empty() -> None:
|
|
238
241
|
q0 = cirq.NamedQubit('q0')
|
|
239
242
|
d = cirq.ProjectorString({})
|
|
240
243
|
|
|
@@ -243,7 +246,7 @@ def test_expectation_from_density_matrix_basis_states_empty():
|
|
|
243
246
|
)
|
|
244
247
|
|
|
245
248
|
|
|
246
|
-
def test_expectation_from_density_matrix_basis_states_single_qubits():
|
|
249
|
+
def test_expectation_from_density_matrix_basis_states_single_qubits() -> None:
|
|
247
250
|
q0 = cirq.NamedQubit('q0')
|
|
248
251
|
d = cirq.ProjectorString({q0: 0})
|
|
249
252
|
|
cirq/ops/qid_util.py
CHANGED
|
@@ -12,25 +12,27 @@
|
|
|
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 overload, TYPE_CHECKING
|
|
16
18
|
|
|
17
19
|
if TYPE_CHECKING:
|
|
18
20
|
import cirq
|
|
19
21
|
|
|
20
22
|
|
|
21
23
|
@overload
|
|
22
|
-
def q(__x: int) ->
|
|
24
|
+
def q(__x: int) -> cirq.LineQubit: ...
|
|
23
25
|
|
|
24
26
|
|
|
25
27
|
@overload
|
|
26
|
-
def q(__row: int, __col: int) ->
|
|
28
|
+
def q(__row: int, __col: int) -> cirq.GridQubit: ...
|
|
27
29
|
|
|
28
30
|
|
|
29
31
|
@overload
|
|
30
|
-
def q(__name: str) ->
|
|
32
|
+
def q(__name: str) -> cirq.NamedQubit: ...
|
|
31
33
|
|
|
32
34
|
|
|
33
|
-
def q(*args:
|
|
35
|
+
def q(*args: int | str) -> cirq.LineQubit | cirq.GridQubit | cirq.NamedQubit:
|
|
34
36
|
"""Constructs a qubit id of the appropriate type based on args.
|
|
35
37
|
|
|
36
38
|
This is shorthand for constructing qubit ids of common types:
|