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/value/classical_data.py
CHANGED
|
@@ -12,9 +12,11 @@
|
|
|
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 abc
|
|
16
18
|
import enum
|
|
17
|
-
from typing import
|
|
19
|
+
from typing import Mapping, Sequence, TYPE_CHECKING
|
|
18
20
|
|
|
19
21
|
from typing_extensions import Self
|
|
20
22
|
|
|
@@ -45,21 +47,21 @@ class MeasurementType(enum.IntEnum):
|
|
|
45
47
|
|
|
46
48
|
class ClassicalDataStoreReader(abc.ABC):
|
|
47
49
|
@abc.abstractmethod
|
|
48
|
-
def keys(self) ->
|
|
50
|
+
def keys(self) -> tuple[cirq.MeasurementKey, ...]:
|
|
49
51
|
"""Gets the measurement keys in the order they were stored."""
|
|
50
52
|
|
|
51
53
|
@property
|
|
52
54
|
@abc.abstractmethod
|
|
53
|
-
def records(self) -> Mapping[
|
|
55
|
+
def records(self) -> Mapping[cirq.MeasurementKey, list[tuple[int, ...]]]:
|
|
54
56
|
"""Gets the a mapping from measurement key to measurement records."""
|
|
55
57
|
|
|
56
58
|
@property
|
|
57
59
|
@abc.abstractmethod
|
|
58
|
-
def channel_records(self) -> Mapping[
|
|
60
|
+
def channel_records(self) -> Mapping[cirq.MeasurementKey, list[int]]:
|
|
59
61
|
"""Gets the a mapping from measurement key to channel measurement records."""
|
|
60
62
|
|
|
61
63
|
@abc.abstractmethod
|
|
62
|
-
def get_int(self, key:
|
|
64
|
+
def get_int(self, key: cirq.MeasurementKey, index=-1) -> int:
|
|
63
65
|
"""Gets the integer corresponding to the measurement.
|
|
64
66
|
|
|
65
67
|
The integer is determined by summing the qubit-dimensional basis value
|
|
@@ -81,7 +83,7 @@ class ClassicalDataStoreReader(abc.ABC):
|
|
|
81
83
|
"""
|
|
82
84
|
|
|
83
85
|
@abc.abstractmethod
|
|
84
|
-
def get_digits(self, key:
|
|
86
|
+
def get_digits(self, key: cirq.MeasurementKey, index=-1) -> tuple[int, ...]:
|
|
85
87
|
"""Gets the values of the qubits that were measured into this key.
|
|
86
88
|
|
|
87
89
|
For example, if the measurement of qubits [q0, q1] produces [0, 1],
|
|
@@ -107,7 +109,7 @@ class ClassicalDataStoreReader(abc.ABC):
|
|
|
107
109
|
class ClassicalDataStore(ClassicalDataStoreReader, abc.ABC):
|
|
108
110
|
@abc.abstractmethod
|
|
109
111
|
def record_measurement(
|
|
110
|
-
self, key:
|
|
112
|
+
self, key: cirq.MeasurementKey, measurement: Sequence[int], qubits: Sequence[cirq.Qid]
|
|
111
113
|
):
|
|
112
114
|
"""Records a measurement.
|
|
113
115
|
|
|
@@ -122,7 +124,7 @@ class ClassicalDataStore(ClassicalDataStoreReader, abc.ABC):
|
|
|
122
124
|
"""
|
|
123
125
|
|
|
124
126
|
@abc.abstractmethod
|
|
125
|
-
def record_channel_measurement(self, key:
|
|
127
|
+
def record_channel_measurement(self, key: cirq.MeasurementKey, measurement: int):
|
|
126
128
|
"""Records a channel measurement.
|
|
127
129
|
|
|
128
130
|
Args:
|
|
@@ -141,12 +143,10 @@ class ClassicalDataDictionaryStore(ClassicalDataStore):
|
|
|
141
143
|
def __init__(
|
|
142
144
|
self,
|
|
143
145
|
*,
|
|
144
|
-
_records:
|
|
145
|
-
_measured_qubits:
|
|
146
|
-
|
|
147
|
-
] = None,
|
|
148
|
-
_channel_records: Optional[Dict['cirq.MeasurementKey', List[int]]] = None,
|
|
149
|
-
_measurement_types: Optional[Dict['cirq.MeasurementKey', 'cirq.MeasurementType']] = None,
|
|
146
|
+
_records: dict[cirq.MeasurementKey, list[tuple[int, ...]]] | None = None,
|
|
147
|
+
_measured_qubits: dict[cirq.MeasurementKey, list[tuple[cirq.Qid, ...]]] | None = None,
|
|
148
|
+
_channel_records: dict[cirq.MeasurementKey, list[int]] | None = None,
|
|
149
|
+
_measurement_types: dict[cirq.MeasurementKey, cirq.MeasurementType] | None = None,
|
|
150
150
|
):
|
|
151
151
|
"""Initializes a `ClassicalDataDictionaryStore` object."""
|
|
152
152
|
if not _measurement_types:
|
|
@@ -165,40 +165,40 @@ class ClassicalDataDictionaryStore(ClassicalDataStore):
|
|
|
165
165
|
_measured_qubits = {}
|
|
166
166
|
if _channel_records is None:
|
|
167
167
|
_channel_records = {}
|
|
168
|
-
self._records:
|
|
169
|
-
self._measured_qubits:
|
|
168
|
+
self._records: dict[cirq.MeasurementKey, list[tuple[int, ...]]] = _records
|
|
169
|
+
self._measured_qubits: dict[cirq.MeasurementKey, list[tuple[cirq.Qid, ...]]] = (
|
|
170
170
|
_measured_qubits
|
|
171
171
|
)
|
|
172
|
-
self._channel_records:
|
|
173
|
-
self._measurement_types:
|
|
172
|
+
self._channel_records: dict[cirq.MeasurementKey, list[int]] = _channel_records
|
|
173
|
+
self._measurement_types: dict[cirq.MeasurementKey, cirq.MeasurementType] = (
|
|
174
174
|
_measurement_types
|
|
175
175
|
)
|
|
176
176
|
|
|
177
177
|
@property
|
|
178
|
-
def records(self) -> Mapping[
|
|
178
|
+
def records(self) -> Mapping[cirq.MeasurementKey, list[tuple[int, ...]]]:
|
|
179
179
|
"""Gets the a mapping from measurement key to measurement records."""
|
|
180
180
|
return self._records
|
|
181
181
|
|
|
182
182
|
@property
|
|
183
|
-
def channel_records(self) -> Mapping[
|
|
183
|
+
def channel_records(self) -> Mapping[cirq.MeasurementKey, list[int]]:
|
|
184
184
|
"""Gets the a mapping from measurement key to channel measurement records."""
|
|
185
185
|
return self._channel_records
|
|
186
186
|
|
|
187
187
|
@property
|
|
188
|
-
def measured_qubits(self) -> Mapping[
|
|
188
|
+
def measured_qubits(self) -> Mapping[cirq.MeasurementKey, list[tuple[cirq.Qid, ...]]]:
|
|
189
189
|
"""Gets the a mapping from measurement key to the qubits measured."""
|
|
190
190
|
return self._measured_qubits
|
|
191
191
|
|
|
192
192
|
@property
|
|
193
|
-
def measurement_types(self) -> Mapping[
|
|
193
|
+
def measurement_types(self) -> Mapping[cirq.MeasurementKey, cirq.MeasurementType]:
|
|
194
194
|
"""Gets the a mapping from measurement key to the measurement type."""
|
|
195
195
|
return self._measurement_types
|
|
196
196
|
|
|
197
|
-
def keys(self) ->
|
|
197
|
+
def keys(self) -> tuple[cirq.MeasurementKey, ...]:
|
|
198
198
|
return tuple(self._measurement_types.keys())
|
|
199
199
|
|
|
200
200
|
def record_measurement(
|
|
201
|
-
self, key:
|
|
201
|
+
self, key: cirq.MeasurementKey, measurement: Sequence[int], qubits: Sequence[cirq.Qid]
|
|
202
202
|
):
|
|
203
203
|
if len(measurement) != len(qubits):
|
|
204
204
|
raise ValueError(f'{len(measurement)} measurements but {len(qubits)} qubits.')
|
|
@@ -217,7 +217,7 @@ class ClassicalDataDictionaryStore(ClassicalDataStore):
|
|
|
217
217
|
measured_qubits.append(tuple(qubits))
|
|
218
218
|
self._records[key].append(tuple(measurement))
|
|
219
219
|
|
|
220
|
-
def record_channel_measurement(self, key:
|
|
220
|
+
def record_channel_measurement(self, key: cirq.MeasurementKey, measurement: int):
|
|
221
221
|
if key not in self._measurement_types:
|
|
222
222
|
self._measurement_types[key] = MeasurementType.CHANNEL
|
|
223
223
|
self._channel_records[key] = []
|
|
@@ -225,14 +225,14 @@ class ClassicalDataDictionaryStore(ClassicalDataStore):
|
|
|
225
225
|
raise ValueError(f"Measurement already logged to key {key}")
|
|
226
226
|
self._channel_records[key].append(measurement)
|
|
227
227
|
|
|
228
|
-
def get_digits(self, key:
|
|
228
|
+
def get_digits(self, key: cirq.MeasurementKey, index=-1) -> tuple[int, ...]:
|
|
229
229
|
return (
|
|
230
230
|
self._records[key][index]
|
|
231
231
|
if self._measurement_types[key] == MeasurementType.MEASUREMENT
|
|
232
232
|
else (self._channel_records[key][index],)
|
|
233
233
|
)
|
|
234
234
|
|
|
235
|
-
def get_int(self, key:
|
|
235
|
+
def get_int(self, key: cirq.MeasurementKey, index=-1) -> int:
|
|
236
236
|
if key not in self._measurement_types:
|
|
237
237
|
raise KeyError(f'The measurement key {key} is not in {self._measurement_types}')
|
|
238
238
|
measurement_type = self._measurement_types[key]
|
|
@@ -11,6 +11,9 @@
|
|
|
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 re
|
|
15
18
|
|
|
16
19
|
import pytest
|
|
@@ -22,7 +25,7 @@ mkey_c = cirq.MeasurementKey('c')
|
|
|
22
25
|
two_qubits = tuple(cirq.LineQubit.range(2))
|
|
23
26
|
|
|
24
27
|
|
|
25
|
-
def test_init():
|
|
28
|
+
def test_init() -> None:
|
|
26
29
|
cd = cirq.ClassicalDataDictionaryStore()
|
|
27
30
|
assert cd.records == {}
|
|
28
31
|
assert cd.keys() == ()
|
|
@@ -44,7 +47,7 @@ def test_init():
|
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
|
|
47
|
-
def test_record_measurement():
|
|
50
|
+
def test_record_measurement() -> None:
|
|
48
51
|
cd = cirq.ClassicalDataDictionaryStore()
|
|
49
52
|
cd.record_measurement(mkey_m, (0, 1), two_qubits)
|
|
50
53
|
assert cd.records == {mkey_m: [(0, 1)]}
|
|
@@ -52,7 +55,7 @@ def test_record_measurement():
|
|
|
52
55
|
assert cd.measured_qubits == {mkey_m: [two_qubits]}
|
|
53
56
|
|
|
54
57
|
|
|
55
|
-
def test_record_measurement_errors():
|
|
58
|
+
def test_record_measurement_errors() -> None:
|
|
56
59
|
cd = cirq.ClassicalDataDictionaryStore()
|
|
57
60
|
with pytest.raises(ValueError, match='3 measurements but 2 qubits'):
|
|
58
61
|
cd.record_measurement(mkey_m, (0, 1, 2), two_qubits)
|
|
@@ -68,14 +71,14 @@ def test_record_measurement_errors():
|
|
|
68
71
|
cd.record_measurement(mkey_m, (1, 0), tuple(cirq.LineQid.range(2, dimension=3)))
|
|
69
72
|
|
|
70
73
|
|
|
71
|
-
def test_record_channel_measurement():
|
|
74
|
+
def test_record_channel_measurement() -> None:
|
|
72
75
|
cd = cirq.ClassicalDataDictionaryStore()
|
|
73
76
|
cd.record_channel_measurement(mkey_m, 1)
|
|
74
77
|
assert cd.channel_records == {mkey_m: [1]}
|
|
75
78
|
assert cd.keys() == (mkey_m,)
|
|
76
79
|
|
|
77
80
|
|
|
78
|
-
def test_record_channel_measurement_errors():
|
|
81
|
+
def test_record_channel_measurement_errors() -> None:
|
|
79
82
|
cd = cirq.ClassicalDataDictionaryStore()
|
|
80
83
|
cd.record_channel_measurement(mkey_m, 1)
|
|
81
84
|
cd.record_channel_measurement(mkey_m, 1)
|
|
@@ -88,7 +91,7 @@ def test_record_channel_measurement_errors():
|
|
|
88
91
|
cd.record_channel_measurement(mkey_m, 1)
|
|
89
92
|
|
|
90
93
|
|
|
91
|
-
def test_get_int():
|
|
94
|
+
def test_get_int() -> None:
|
|
92
95
|
cd = cirq.ClassicalDataDictionaryStore()
|
|
93
96
|
cd.record_measurement(mkey_m, (0, 1), two_qubits)
|
|
94
97
|
assert cd.get_int(mkey_m) == 1
|
|
@@ -106,7 +109,7 @@ def test_get_int():
|
|
|
106
109
|
cd.get_int(mkey_m)
|
|
107
110
|
|
|
108
111
|
|
|
109
|
-
def test_copy():
|
|
112
|
+
def test_copy() -> None:
|
|
110
113
|
cd = cirq.ClassicalDataDictionaryStore(
|
|
111
114
|
_records={mkey_m: [(0, 1)]},
|
|
112
115
|
_measured_qubits={mkey_m: [two_qubits]},
|
|
@@ -129,7 +132,7 @@ def test_copy():
|
|
|
129
132
|
assert cd1.measurement_types == cd.measurement_types
|
|
130
133
|
|
|
131
134
|
|
|
132
|
-
def test_repr():
|
|
135
|
+
def test_repr() -> None:
|
|
133
136
|
cd = cirq.ClassicalDataDictionaryStore(
|
|
134
137
|
_records={mkey_m: [(0, 1)]},
|
|
135
138
|
_measured_qubits={mkey_m: [two_qubits]},
|
cirq/value/condition.py
CHANGED
|
@@ -12,9 +12,11 @@
|
|
|
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 abc
|
|
16
18
|
import dataclasses
|
|
17
|
-
from typing import Any,
|
|
19
|
+
from typing import Any, Mapping, TYPE_CHECKING
|
|
18
20
|
|
|
19
21
|
import attrs
|
|
20
22
|
import sympy
|
|
@@ -32,15 +34,15 @@ class Condition(abc.ABC):
|
|
|
32
34
|
|
|
33
35
|
@property
|
|
34
36
|
@abc.abstractmethod
|
|
35
|
-
def keys(self) ->
|
|
37
|
+
def keys(self) -> tuple[cirq.MeasurementKey, ...]:
|
|
36
38
|
"""Gets the control keys."""
|
|
37
39
|
|
|
38
40
|
@abc.abstractmethod
|
|
39
|
-
def replace_key(self, current:
|
|
41
|
+
def replace_key(self, current: cirq.MeasurementKey, replacement: cirq.MeasurementKey):
|
|
40
42
|
"""Replaces the control keys."""
|
|
41
43
|
|
|
42
44
|
@abc.abstractmethod
|
|
43
|
-
def resolve(self, classical_data:
|
|
45
|
+
def resolve(self, classical_data: cirq.ClassicalDataStoreReader) -> bool:
|
|
44
46
|
"""Resolves the condition based on the measurements."""
|
|
45
47
|
|
|
46
48
|
@property
|
|
@@ -48,24 +50,24 @@ class Condition(abc.ABC):
|
|
|
48
50
|
def qasm(self):
|
|
49
51
|
"""Returns the qasm of this condition."""
|
|
50
52
|
|
|
51
|
-
def _qasm_(self, args:
|
|
53
|
+
def _qasm_(self, args: cirq.QasmArgs, **kwargs) -> str | None:
|
|
52
54
|
return self.qasm
|
|
53
55
|
|
|
54
|
-
def _with_measurement_key_mapping_(self, key_map: Mapping[str, str]) ->
|
|
56
|
+
def _with_measurement_key_mapping_(self, key_map: Mapping[str, str]) -> cirq.Condition:
|
|
55
57
|
condition = self
|
|
56
58
|
for k in self.keys:
|
|
57
59
|
condition = condition.replace_key(k, mkp.with_measurement_key_mapping(k, key_map))
|
|
58
60
|
return condition
|
|
59
61
|
|
|
60
|
-
def _with_key_path_prefix_(self, path:
|
|
62
|
+
def _with_key_path_prefix_(self, path: tuple[str, ...]) -> cirq.Condition:
|
|
61
63
|
condition = self
|
|
62
64
|
for k in self.keys:
|
|
63
65
|
condition = condition.replace_key(k, mkp.with_key_path_prefix(k, path))
|
|
64
66
|
return condition
|
|
65
67
|
|
|
66
68
|
def _with_rescoped_keys_(
|
|
67
|
-
self, path:
|
|
68
|
-
) ->
|
|
69
|
+
self, path: tuple[str, ...], bindable_keys: frozenset[cirq.MeasurementKey]
|
|
70
|
+
) -> cirq.Condition:
|
|
69
71
|
condition = self
|
|
70
72
|
for key in self.keys:
|
|
71
73
|
for i in range(len(path) + 1):
|
|
@@ -85,14 +87,14 @@ class KeyCondition(Condition):
|
|
|
85
87
|
time of resolution.
|
|
86
88
|
"""
|
|
87
89
|
|
|
88
|
-
key:
|
|
90
|
+
key: cirq.MeasurementKey
|
|
89
91
|
index: int = -1
|
|
90
92
|
|
|
91
93
|
@property
|
|
92
94
|
def keys(self):
|
|
93
95
|
return (self.key,)
|
|
94
96
|
|
|
95
|
-
def replace_key(self, current:
|
|
97
|
+
def replace_key(self, current: cirq.MeasurementKey, replacement: cirq.MeasurementKey):
|
|
96
98
|
return KeyCondition(replacement) if self.key == current else self
|
|
97
99
|
|
|
98
100
|
def __str__(self):
|
|
@@ -103,7 +105,7 @@ class KeyCondition(Condition):
|
|
|
103
105
|
return f'cirq.KeyCondition({self.key!r}, {self.index})'
|
|
104
106
|
return f'cirq.KeyCondition({self.key!r})'
|
|
105
107
|
|
|
106
|
-
def resolve(self, classical_data:
|
|
108
|
+
def resolve(self, classical_data: cirq.ClassicalDataStoreReader) -> bool:
|
|
107
109
|
if self.key not in classical_data.keys():
|
|
108
110
|
raise ValueError(f'Measurement key {self.key} missing when testing classical control')
|
|
109
111
|
return classical_data.get_int(self.key, self.index) != 0
|
|
@@ -119,7 +121,7 @@ class KeyCondition(Condition):
|
|
|
119
121
|
def qasm(self):
|
|
120
122
|
raise ValueError('QASM is defined only for SympyConditions of type key == constant.')
|
|
121
123
|
|
|
122
|
-
def _qasm_(self, args:
|
|
124
|
+
def _qasm_(self, args: cirq.QasmArgs, **kwargs) -> str | None:
|
|
123
125
|
args.validate_version('2.0', '3.0')
|
|
124
126
|
key_str = str(self.key)
|
|
125
127
|
if key_str not in args.meas_key_id_map:
|
|
@@ -162,7 +164,7 @@ class BitMaskKeyCondition(Condition):
|
|
|
162
164
|
- bitmask: Optional bitmask to apply before doing the comparison.
|
|
163
165
|
"""
|
|
164
166
|
|
|
165
|
-
key:
|
|
167
|
+
key: cirq.MeasurementKey = attrs.field(
|
|
166
168
|
converter=lambda x: (
|
|
167
169
|
x
|
|
168
170
|
if isinstance(x, measurement_key.MeasurementKey)
|
|
@@ -172,7 +174,7 @@ class BitMaskKeyCondition(Condition):
|
|
|
172
174
|
index: int = -1
|
|
173
175
|
target_value: int = 0
|
|
174
176
|
equal_target: bool = False
|
|
175
|
-
bitmask:
|
|
177
|
+
bitmask: int | None = None
|
|
176
178
|
|
|
177
179
|
@property
|
|
178
180
|
def keys(self):
|
|
@@ -180,8 +182,8 @@ class BitMaskKeyCondition(Condition):
|
|
|
180
182
|
|
|
181
183
|
@staticmethod
|
|
182
184
|
def create_equal_mask(
|
|
183
|
-
key:
|
|
184
|
-
) ->
|
|
185
|
+
key: cirq.MeasurementKey, bitmask: int, *, index: int = -1
|
|
186
|
+
) -> BitMaskKeyCondition:
|
|
185
187
|
"""Creates a condition that evaluates (meas & bitmask) == bitmask."""
|
|
186
188
|
return BitMaskKeyCondition(
|
|
187
189
|
key, index, target_value=bitmask, equal_target=True, bitmask=bitmask
|
|
@@ -189,14 +191,14 @@ class BitMaskKeyCondition(Condition):
|
|
|
189
191
|
|
|
190
192
|
@staticmethod
|
|
191
193
|
def create_not_equal_mask(
|
|
192
|
-
key:
|
|
193
|
-
) ->
|
|
194
|
+
key: cirq.MeasurementKey, bitmask: int, *, index: int = -1
|
|
195
|
+
) -> BitMaskKeyCondition:
|
|
194
196
|
"""Creates a condition that evaluates (meas & bitmask) != bitmask."""
|
|
195
197
|
return BitMaskKeyCondition(
|
|
196
198
|
key, index, target_value=bitmask, equal_target=False, bitmask=bitmask
|
|
197
199
|
)
|
|
198
200
|
|
|
199
|
-
def replace_key(self, current:
|
|
201
|
+
def replace_key(self, current: cirq.MeasurementKey, replacement: cirq.MeasurementKey):
|
|
200
202
|
return BitMaskKeyCondition(replacement) if self.key == current else self
|
|
201
203
|
|
|
202
204
|
def __str__(self):
|
|
@@ -218,7 +220,7 @@ class BitMaskKeyCondition(Condition):
|
|
|
218
220
|
parameters = ', '.join(f'{f.name}={repr(values[f.name])}' for f in attrs.fields(type(self)))
|
|
219
221
|
return f'cirq.BitMaskKeyCondition({parameters})'
|
|
220
222
|
|
|
221
|
-
def resolve(self, classical_data:
|
|
223
|
+
def resolve(self, classical_data: cirq.ClassicalDataStoreReader) -> bool:
|
|
222
224
|
if self.key not in classical_data.keys():
|
|
223
225
|
raise ValueError(f'Measurement key {self.key} missing when testing classical control')
|
|
224
226
|
value = classical_data.get_int(self.key, self.index)
|
|
@@ -269,7 +271,7 @@ class SympyCondition(Condition):
|
|
|
269
271
|
# keep the former here.
|
|
270
272
|
)
|
|
271
273
|
|
|
272
|
-
def replace_key(self, current:
|
|
274
|
+
def replace_key(self, current: cirq.MeasurementKey, replacement: cirq.MeasurementKey):
|
|
273
275
|
return SympyCondition(self.expr.subs({str(current): sympy.Symbol(str(replacement))}))
|
|
274
276
|
|
|
275
277
|
def __str__(self):
|
|
@@ -278,12 +280,12 @@ class SympyCondition(Condition):
|
|
|
278
280
|
def __repr__(self):
|
|
279
281
|
return f'cirq.SympyCondition({proper_repr(self.expr)})'
|
|
280
282
|
|
|
281
|
-
def resolve(self, classical_data:
|
|
283
|
+
def resolve(self, classical_data: cirq.ClassicalDataStoreReader) -> bool:
|
|
282
284
|
missing = [str(k) for k in self.keys if k not in classical_data.keys()]
|
|
283
285
|
if missing:
|
|
284
286
|
raise ValueError(f'Measurement keys {missing} missing when testing classical control')
|
|
285
287
|
|
|
286
|
-
replacements:
|
|
288
|
+
replacements: dict[str, Any] = {}
|
|
287
289
|
for symbol in self.expr.free_symbols:
|
|
288
290
|
if isinstance(symbol, sympy.Symbol):
|
|
289
291
|
name = symbol.name
|
cirq/value/condition_test.py
CHANGED
cirq/value/digits.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 Any, Iterable, overload
|
|
16
18
|
|
|
17
19
|
|
|
18
20
|
def big_endian_bits_to_int(bits: Iterable[Any]) -> int:
|
|
@@ -46,7 +48,7 @@ def big_endian_bits_to_int(bits: Iterable[Any]) -> int:
|
|
|
46
48
|
return result
|
|
47
49
|
|
|
48
50
|
|
|
49
|
-
def big_endian_int_to_bits(val: int, *, bit_count: int) ->
|
|
51
|
+
def big_endian_int_to_bits(val: int, *, bit_count: int) -> list[int]:
|
|
50
52
|
"""Returns the big-endian bits of an integer.
|
|
51
53
|
|
|
52
54
|
Args:
|
|
@@ -72,7 +74,7 @@ def big_endian_int_to_bits(val: int, *, bit_count: int) -> List[int]:
|
|
|
72
74
|
return [(val >> i) & 1 for i in range(bit_count)[::-1]]
|
|
73
75
|
|
|
74
76
|
|
|
75
|
-
def big_endian_digits_to_int(digits: Iterable[int], *, base:
|
|
77
|
+
def big_endian_digits_to_int(digits: Iterable[int], *, base: int | Iterable[int]) -> int:
|
|
76
78
|
"""Returns the big-endian integer specified by the given digits and base.
|
|
77
79
|
|
|
78
80
|
Args:
|
|
@@ -118,20 +120,24 @@ def big_endian_digits_to_int(digits: Iterable[int], *, base: Union[int, Iterable
|
|
|
118
120
|
return result
|
|
119
121
|
|
|
120
122
|
|
|
121
|
-
# pylint: disable=function-redefined
|
|
122
123
|
@overload
|
|
123
|
-
def big_endian_int_to_digits(val: int, *, digit_count: int, base: int) ->
|
|
124
|
+
def big_endian_int_to_digits(val: int, *, digit_count: int, base: int) -> list[int]:
|
|
124
125
|
pass
|
|
125
126
|
|
|
126
127
|
|
|
127
128
|
@overload
|
|
128
|
-
def big_endian_int_to_digits(val: int, *, base: Iterable[int]) ->
|
|
129
|
+
def big_endian_int_to_digits(val: int, *, digit_count: int, base: Iterable[int]) -> list[int]:
|
|
130
|
+
pass
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@overload
|
|
134
|
+
def big_endian_int_to_digits(val: int, *, base: Iterable[int]) -> list[int]:
|
|
129
135
|
pass
|
|
130
136
|
|
|
131
137
|
|
|
132
138
|
def big_endian_int_to_digits(
|
|
133
|
-
val: int, *, digit_count:
|
|
134
|
-
) ->
|
|
139
|
+
val: int, *, digit_count: int | None = None, base: int | Iterable[int]
|
|
140
|
+
) -> list[int]:
|
|
135
141
|
"""Separates an integer into big-endian digits.
|
|
136
142
|
|
|
137
143
|
Args:
|
|
@@ -187,6 +193,3 @@ def big_endian_int_to_digits(
|
|
|
187
193
|
)
|
|
188
194
|
|
|
189
195
|
return result[::-1]
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
# pylint: enable=function-redefined
|
cirq/value/digits_test.py
CHANGED
cirq/value/duration.py
CHANGED
|
@@ -11,10 +11,13 @@
|
|
|
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
|
+
|
|
14
15
|
"""A typed time delta that supports picosecond accuracy."""
|
|
15
16
|
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
16
19
|
import datetime
|
|
17
|
-
from typing import AbstractSet, Any,
|
|
20
|
+
from typing import AbstractSet, Any, TYPE_CHECKING, TypeAlias, Union
|
|
18
21
|
|
|
19
22
|
import numpy as np
|
|
20
23
|
import sympy
|
|
@@ -42,8 +45,8 @@ document(
|
|
|
42
45
|
)
|
|
43
46
|
|
|
44
47
|
|
|
45
|
-
_NUMERIC_INPUT_TYPE =
|
|
46
|
-
_NUMERIC_OUTPUT_TYPE =
|
|
48
|
+
_NUMERIC_INPUT_TYPE: TypeAlias = int | float | sympy.Expr | np.number
|
|
49
|
+
_NUMERIC_OUTPUT_TYPE: TypeAlias = int | float | sympy.Expr
|
|
47
50
|
|
|
48
51
|
|
|
49
52
|
class Duration:
|
|
@@ -51,7 +54,7 @@ class Duration:
|
|
|
51
54
|
|
|
52
55
|
def __init__(
|
|
53
56
|
self,
|
|
54
|
-
value: DURATION_LIKE = None,
|
|
57
|
+
value: DURATION_LIKE | int = None,
|
|
55
58
|
*, # Force keyword args.
|
|
56
59
|
picos: _NUMERIC_INPUT_TYPE = 0,
|
|
57
60
|
nanos: _NUMERIC_INPUT_TYPE = 0,
|
|
@@ -79,7 +82,7 @@ class Duration:
|
|
|
79
82
|
>>> print(cirq.Duration(micros=1.5 * sympy.Symbol('t')))
|
|
80
83
|
(1500.0*t) ns
|
|
81
84
|
"""
|
|
82
|
-
self._time_vals:
|
|
85
|
+
self._time_vals: list[_NUMERIC_INPUT_TYPE] = [0, 0, 0, 0]
|
|
83
86
|
self._multipliers = [1, 1000, 1000_000, 1000_000_000]
|
|
84
87
|
if value is not None and value != 0:
|
|
85
88
|
if isinstance(value, datetime.timedelta):
|
|
@@ -98,7 +101,7 @@ class Duration:
|
|
|
98
101
|
def _parameter_names_(self) -> AbstractSet[str]:
|
|
99
102
|
return protocols.parameter_names(self._time_vals)
|
|
100
103
|
|
|
101
|
-
def _resolve_parameters_(self, resolver:
|
|
104
|
+
def _resolve_parameters_(self, resolver: cirq.ParamResolver, recursive: bool) -> Duration:
|
|
102
105
|
return _duration_from_time_vals(
|
|
103
106
|
protocols.resolve_parameters(self._time_vals, resolver, recursive)
|
|
104
107
|
)
|
|
@@ -121,16 +124,16 @@ class Duration:
|
|
|
121
124
|
"""Returns the number of milliseconds that the duration spans."""
|
|
122
125
|
return self.total_picos() / 1000_000_000
|
|
123
126
|
|
|
124
|
-
def __add__(self, other) ->
|
|
127
|
+
def __add__(self, other) -> Duration:
|
|
125
128
|
other = _attempt_duration_like_to_duration(other)
|
|
126
129
|
if other is None:
|
|
127
130
|
return NotImplemented
|
|
128
131
|
return _duration_from_time_vals(_add_time_vals(self._time_vals, other._time_vals))
|
|
129
132
|
|
|
130
|
-
def __radd__(self, other) ->
|
|
133
|
+
def __radd__(self, other) -> Duration:
|
|
131
134
|
return self.__add__(other)
|
|
132
135
|
|
|
133
|
-
def __sub__(self, other) ->
|
|
136
|
+
def __sub__(self, other) -> Duration:
|
|
134
137
|
other = _attempt_duration_like_to_duration(other)
|
|
135
138
|
if other is None:
|
|
136
139
|
return NotImplemented
|
|
@@ -138,7 +141,7 @@ class Duration:
|
|
|
138
141
|
_add_time_vals(self._time_vals, [-x for x in other._time_vals])
|
|
139
142
|
)
|
|
140
143
|
|
|
141
|
-
def __rsub__(self, other) ->
|
|
144
|
+
def __rsub__(self, other) -> Duration:
|
|
142
145
|
other = _attempt_duration_like_to_duration(other)
|
|
143
146
|
if other is None:
|
|
144
147
|
return NotImplemented
|
|
@@ -146,17 +149,17 @@ class Duration:
|
|
|
146
149
|
_add_time_vals(other._time_vals, [-x for x in self._time_vals])
|
|
147
150
|
)
|
|
148
151
|
|
|
149
|
-
def __mul__(self, other) ->
|
|
152
|
+
def __mul__(self, other) -> Duration:
|
|
150
153
|
if not isinstance(other, (int, float, sympy.Expr)):
|
|
151
154
|
return NotImplemented
|
|
152
155
|
if other == 0:
|
|
153
156
|
return _duration_from_time_vals([0] * 4)
|
|
154
157
|
return _duration_from_time_vals([x * other for x in self._time_vals])
|
|
155
158
|
|
|
156
|
-
def __rmul__(self, other) ->
|
|
159
|
+
def __rmul__(self, other) -> Duration:
|
|
157
160
|
return self.__mul__(other)
|
|
158
161
|
|
|
159
|
-
def __truediv__(self, other) ->
|
|
162
|
+
def __truediv__(self, other) -> Duration | float:
|
|
160
163
|
if isinstance(other, (int, float, sympy.Expr)):
|
|
161
164
|
new_time_vals = [x / other for x in self._time_vals]
|
|
162
165
|
return _duration_from_time_vals(new_time_vals)
|
|
@@ -211,7 +214,7 @@ class Duration:
|
|
|
211
214
|
return hash(datetime.timedelta(microseconds=self.total_picos() / 1000000))
|
|
212
215
|
return hash((Duration, self.total_picos()))
|
|
213
216
|
|
|
214
|
-
def _decompose_into_amount_unit_suffix(self) ->
|
|
217
|
+
def _decompose_into_amount_unit_suffix(self) -> tuple[int, str, str]:
|
|
215
218
|
picos = self.total_picos()
|
|
216
219
|
if (
|
|
217
220
|
isinstance(picos, sympy.Mul)
|
|
@@ -258,11 +261,11 @@ class Duration:
|
|
|
258
261
|
amount, unit, _ = self._decompose_into_amount_unit_suffix()
|
|
259
262
|
return f'cirq.Duration({unit}={proper_repr(amount)})'
|
|
260
263
|
|
|
261
|
-
def _json_dict_(self) ->
|
|
264
|
+
def _json_dict_(self) -> dict[str, Any]:
|
|
262
265
|
return {'picos': self.total_picos()}
|
|
263
266
|
|
|
264
267
|
|
|
265
|
-
def _attempt_duration_like_to_duration(value: Any) ->
|
|
268
|
+
def _attempt_duration_like_to_duration(value: Any) -> Duration | None:
|
|
266
269
|
if isinstance(value, Duration):
|
|
267
270
|
return value
|
|
268
271
|
if isinstance(value, datetime.timedelta):
|
|
@@ -273,9 +276,9 @@ def _attempt_duration_like_to_duration(value: Any) -> Optional[Duration]:
|
|
|
273
276
|
|
|
274
277
|
|
|
275
278
|
def _add_time_vals(
|
|
276
|
-
val1:
|
|
277
|
-
) ->
|
|
278
|
-
ret:
|
|
279
|
+
val1: list[_NUMERIC_INPUT_TYPE], val2: list[_NUMERIC_INPUT_TYPE]
|
|
280
|
+
) -> list[_NUMERIC_INPUT_TYPE]:
|
|
281
|
+
ret: list[_NUMERIC_INPUT_TYPE] = []
|
|
279
282
|
for i in range(4):
|
|
280
283
|
if val1[i] and val2[i]:
|
|
281
284
|
ret.append(val1[i] + val2[i])
|
|
@@ -284,7 +287,7 @@ def _add_time_vals(
|
|
|
284
287
|
return ret
|
|
285
288
|
|
|
286
289
|
|
|
287
|
-
def _duration_from_time_vals(time_vals:
|
|
290
|
+
def _duration_from_time_vals(time_vals: list[_NUMERIC_INPUT_TYPE]):
|
|
288
291
|
ret = Duration()
|
|
289
292
|
ret._time_vals = time_vals
|
|
290
293
|
return ret
|