cirq-core 1.7.0.dev20250825174419__py3-none-any.whl → 1.7.0.dev20251203004401__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cirq/__init__.py +1 -0
- cirq/_compat.py +3 -2
- cirq/_compat_test.py +16 -15
- cirq/_doc.py +4 -3
- cirq/_import.py +2 -1
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/circuits/_bucket_priority_queue.py +2 -1
- cirq/circuits/circuit.py +19 -17
- cirq/circuits/circuit_operation.py +2 -1
- cirq/circuits/circuit_operation_test.py +19 -0
- cirq/circuits/circuit_test.py +31 -12
- cirq/circuits/frozen_circuit.py +3 -2
- cirq/circuits/moment.py +3 -15
- cirq/circuits/optimization_pass.py +2 -1
- cirq/circuits/qasm_output.py +39 -10
- cirq/circuits/qasm_output_test.py +51 -2
- cirq/circuits/text_diagram_drawer.py +2 -1
- cirq/contrib/acquaintance/bipartite.py +2 -1
- cirq/contrib/acquaintance/devices.py +1 -1
- cirq/contrib/acquaintance/executor.py +4 -5
- cirq/contrib/acquaintance/executor_test.py +2 -1
- cirq/contrib/acquaintance/gates.py +2 -1
- cirq/contrib/acquaintance/gates_test.py +1 -1
- cirq/contrib/acquaintance/inspection_utils.py +2 -1
- cirq/contrib/acquaintance/mutation_utils.py +2 -1
- cirq/contrib/acquaintance/optimizers.py +2 -1
- cirq/contrib/acquaintance/permutation.py +2 -1
- cirq/contrib/acquaintance/permutation_test.py +1 -1
- cirq/contrib/acquaintance/shift.py +2 -1
- cirq/contrib/acquaintance/shift_swap_network.py +2 -1
- cirq/contrib/acquaintance/strategies/complete.py +3 -2
- cirq/contrib/acquaintance/strategies/cubic.py +2 -1
- cirq/contrib/acquaintance/strategies/quartic_paired.py +2 -1
- cirq/contrib/acquaintance/strategies/quartic_paired_test.py +1 -1
- cirq/contrib/acquaintance/testing.py +2 -1
- cirq/contrib/acquaintance/topological_sort.py +2 -1
- cirq/contrib/bayesian_network/bayesian_network_gate.py +3 -2
- cirq/contrib/circuitdag/circuit_dag.py +4 -2
- cirq/contrib/custom_simulators/custom_state_simulator.py +2 -1
- cirq/contrib/custom_simulators/custom_state_simulator_test.py +1 -1
- cirq/contrib/graph_device/graph_device.py +2 -1
- cirq/contrib/graph_device/graph_device_test.py +2 -1
- cirq/contrib/graph_device/hypergraph.py +2 -1
- cirq/contrib/graph_device/uniform_graph_device.py +2 -1
- cirq/contrib/json.py +14 -2
- cirq/contrib/json_test_data/BayesianNetworkGate.json +10 -0
- cirq/contrib/json_test_data/BayesianNetworkGate.repr +3 -0
- cirq/contrib/json_test_data/QuantumVolumeResult.json +169 -0
- cirq/contrib/json_test_data/QuantumVolumeResult.repr +22 -0
- cirq/contrib/json_test_data/SwapPermutationGate.json +3 -0
- cirq/contrib/json_test_data/SwapPermutationGate.repr +1 -0
- cirq/contrib/json_test_data/spec.py +0 -2
- cirq/contrib/noise_models/noise_models.py +2 -1
- cirq/contrib/paulistring/clifford_optimize.py +20 -2
- cirq/contrib/paulistring/optimize.py +1 -1
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py +146 -35
- cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py +81 -178
- cirq/contrib/paulistring/recombine.py +5 -2
- cirq/contrib/paulistring/separate.py +1 -1
- cirq/contrib/qasm_import/_lexer.py +6 -1
- cirq/contrib/qasm_import/_lexer_test.py +1 -1
- cirq/contrib/qasm_import/_parser.py +24 -8
- cirq/contrib/qasm_import/_parser_test.py +44 -6
- cirq/contrib/qcircuit/qcircuit_pdf_test.py +6 -9
- cirq/contrib/quantikz/__init__.py +21 -0
- cirq/contrib/quantikz/circuit_to_latex_quantikz.py +680 -0
- cirq/contrib/quantikz/circuit_to_latex_quantikz_test.py +253 -0
- cirq/contrib/quantikz/circuit_to_latex_render.py +424 -0
- cirq/contrib/quantikz/circuit_to_latex_render_test.py +44 -0
- cirq/contrib/quantum_volume/quantum_volume.py +2 -1
- cirq/contrib/quimb/density_matrix.py +1 -1
- cirq/contrib/quimb/grid_circuits.py +2 -1
- cirq/contrib/quimb/grid_circuits_test.py +1 -1
- cirq/contrib/quimb/mps_simulator.py +4 -3
- cirq/contrib/quimb/state_vector.py +2 -1
- cirq/contrib/quirk/export_to_quirk.py +2 -1
- cirq/contrib/quirk/linearize_circuit.py +1 -1
- cirq/contrib/quirk/quirk_gate.py +2 -1
- cirq/contrib/routing/device.py +1 -1
- cirq/contrib/routing/greedy.py +2 -1
- cirq/contrib/routing/initialization.py +2 -1
- cirq/contrib/routing/router.py +2 -1
- cirq/contrib/routing/swap_network.py +2 -1
- cirq/contrib/routing/utils.py +2 -1
- cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking.py +7 -5
- cirq/contrib/shuffle_circuits/shuffle_circuits_with_readout_benchmarking_test.py +6 -6
- cirq/devices/device.py +2 -1
- cirq/devices/grid_device_metadata.py +2 -1
- cirq/devices/grid_qubit.py +7 -6
- cirq/devices/insertion_noise_model.py +2 -1
- cirq/devices/line_qubit.py +2 -1
- cirq/devices/named_topologies.py +2 -1
- cirq/devices/noise_model.py +2 -1
- cirq/devices/noise_model_test.py +1 -1
- cirq/devices/noise_properties.py +2 -1
- cirq/devices/superconducting_qubits_noise_properties_test.py +2 -1
- cirq/devices/thermal_noise_model.py +2 -1
- cirq/experiments/__init__.py +2 -0
- cirq/experiments/benchmarking/parallel_xeb.py +2 -1
- cirq/experiments/benchmarking/parallel_xeb_test.py +1 -1
- cirq/experiments/fidelity_estimation.py +2 -1
- cirq/experiments/fidelity_estimation_test.py +1 -1
- cirq/experiments/ghz_2d.py +150 -0
- cirq/experiments/ghz_2d_test.py +155 -0
- cirq/experiments/n_qubit_tomography.py +2 -1
- cirq/experiments/n_qubit_tomography_test.py +1 -1
- cirq/experiments/purity_estimation.py +1 -1
- cirq/experiments/qubit_characterizations.py +33 -4
- cirq/experiments/qubit_characterizations_test.py +16 -0
- cirq/experiments/random_quantum_circuit_generation.py +2 -1
- cirq/experiments/random_quantum_circuit_generation_test.py +2 -1
- cirq/experiments/readout_confusion_matrix.py +2 -1
- cirq/experiments/readout_confusion_matrix_test.py +1 -1
- cirq/experiments/single_qubit_readout_calibration.py +2 -1
- cirq/experiments/single_qubit_readout_calibration_test.py +1 -1
- cirq/experiments/t1_decay_experiment.py +2 -1
- cirq/experiments/two_qubit_xeb.py +2 -1
- cirq/experiments/two_qubit_xeb_test.py +1 -1
- cirq/experiments/xeb_fitting.py +2 -1
- cirq/experiments/xeb_fitting_test.py +1 -1
- cirq/experiments/xeb_sampling.py +5 -3
- cirq/experiments/xeb_sampling_test.py +1 -1
- cirq/experiments/xeb_simulation.py +2 -1
- cirq/experiments/xeb_simulation_test.py +2 -1
- cirq/experiments/z_phase_calibration.py +2 -1
- cirq/experiments/z_phase_calibration_test.py +18 -3
- cirq/interop/quirk/cells/__init__.py +1 -2
- cirq/interop/quirk/cells/all_cells.py +2 -1
- cirq/interop/quirk/cells/arithmetic_cells.py +2 -1
- cirq/interop/quirk/cells/cell.py +2 -1
- cirq/interop/quirk/cells/composite_cell.py +2 -1
- cirq/interop/quirk/cells/composite_cell_test.py +1 -1
- cirq/interop/quirk/cells/control_cells.py +2 -1
- cirq/interop/quirk/cells/frequency_space_cells.py +1 -1
- cirq/interop/quirk/cells/ignored_cells.py +1 -1
- cirq/interop/quirk/cells/input_cells.py +2 -1
- cirq/interop/quirk/cells/input_rotation_cells.py +2 -1
- cirq/interop/quirk/cells/measurement_cells.py +2 -1
- cirq/interop/quirk/cells/parse.py +2 -11
- cirq/interop/quirk/cells/qubit_permutation_cells.py +2 -1
- cirq/interop/quirk/cells/scalar_cells.py +2 -1
- cirq/interop/quirk/cells/single_qubit_rotation_cells.py +2 -1
- cirq/interop/quirk/cells/swap_cell.py +2 -1
- cirq/interop/quirk/cells/unsupported_cells.py +1 -1
- cirq/interop/quirk/url_to_circuit.py +2 -1
- cirq/json_resolver_cache.py +0 -2
- cirq/linalg/decompositions.py +6 -2
- cirq/linalg/decompositions_test.py +1 -0
- cirq/linalg/diagonalize.py +1 -1
- cirq/linalg/predicates.py +2 -1
- cirq/linalg/tolerance.py +2 -1
- cirq/linalg/transformations.py +3 -2
- cirq/ops/arithmetic_operation.py +4 -3
- cirq/ops/arithmetic_operation_test.py +1 -1
- cirq/ops/boolean_hamiltonian.py +4 -3
- cirq/ops/classically_controlled_operation.py +11 -11
- cirq/ops/classically_controlled_operation_test.py +26 -2
- cirq/ops/clifford_gate.py +3 -2
- cirq/ops/clifford_gate_test.py +1 -2
- cirq/ops/common_channels.py +2 -1
- cirq/ops/common_gates.py +3 -2
- cirq/ops/control_values.py +2 -1
- cirq/ops/controlled_gate.py +3 -2
- cirq/ops/controlled_gate_test.py +2 -1
- cirq/ops/controlled_operation.py +3 -2
- cirq/ops/controlled_operation_test.py +2 -1
- cirq/ops/dense_pauli_string.py +44 -81
- cirq/ops/dense_pauli_string_test.py +21 -0
- cirq/ops/diagonal_gate.py +3 -2
- cirq/ops/eigen_gate.py +9 -7
- cirq/ops/fourier_transform.py +3 -2
- cirq/ops/fourier_transform_test.py +2 -4
- cirq/ops/fsim_gate.py +3 -2
- cirq/ops/gate_operation.py +23 -12
- cirq/ops/gateset.py +22 -2
- cirq/ops/global_phase_op.py +3 -2
- cirq/ops/greedy_qubit_manager.py +2 -1
- cirq/ops/identity.py +2 -1
- cirq/ops/kraus_channel.py +2 -1
- cirq/ops/linear_combinations.py +12 -17
- cirq/ops/linear_combinations_test.py +23 -1
- cirq/ops/matrix_gates.py +2 -1
- cirq/ops/measure_util.py +8 -6
- cirq/ops/measurement_gate.py +2 -1
- cirq/ops/mixed_unitary_channel.py +2 -1
- cirq/ops/named_qubit.py +2 -2
- cirq/ops/op_tree.py +2 -1
- cirq/ops/parallel_gate.py +3 -2
- cirq/ops/parity_gates.py +2 -1
- cirq/ops/parity_gates_test.py +35 -0
- cirq/ops/pauli_interaction_gate.py +2 -1
- cirq/ops/pauli_measurement_gate.py +2 -1
- cirq/ops/pauli_string.py +37 -57
- cirq/ops/pauli_string_phasor.py +6 -5
- cirq/ops/pauli_string_raw_types.py +2 -1
- cirq/ops/pauli_string_test.py +49 -6
- cirq/ops/pauli_sum_exponential.py +2 -1
- cirq/ops/permutation_gate.py +2 -1
- cirq/ops/phased_iswap_gate.py +3 -2
- cirq/ops/phased_x_gate.py +5 -4
- cirq/ops/phased_x_z_gate.py +12 -5
- cirq/ops/projector.py +2 -1
- cirq/ops/qubit_manager.py +2 -1
- cirq/ops/qubit_order.py +2 -1
- cirq/ops/qubit_order_or_list.py +1 -1
- cirq/ops/random_gate_channel.py +3 -2
- cirq/ops/raw_types.py +33 -16
- cirq/ops/raw_types_test.py +4 -3
- cirq/ops/state_preparation_channel.py +2 -1
- cirq/ops/three_qubit_gates.py +3 -2
- cirq/ops/two_qubit_diagonal_gate.py +3 -2
- cirq/ops/uniform_superposition_gate.py +2 -1
- cirq/ops/wait_gate.py +10 -4
- cirq/protocols/act_on_protocol.py +2 -1
- cirq/protocols/act_on_protocol_test.py +2 -1
- cirq/protocols/apply_channel_protocol.py +2 -1
- cirq/protocols/apply_mixture_protocol.py +2 -1
- cirq/protocols/apply_mixture_protocol_test.py +2 -1
- cirq/protocols/apply_unitary_protocol.py +2 -1
- cirq/protocols/apply_unitary_protocol_test.py +2 -0
- cirq/protocols/approximate_equality_protocol.py +2 -1
- cirq/protocols/circuit_diagram_info_protocol.py +2 -1
- cirq/protocols/control_key_protocol.py +7 -0
- cirq/protocols/decompose_protocol.py +2 -12
- cirq/protocols/has_stabilizer_effect_protocol.py +1 -1
- cirq/protocols/has_stabilizer_effect_protocol_test.py +11 -9
- cirq/protocols/has_unitary_protocol_test.py +3 -3
- cirq/protocols/hash_from_pickle_test.py +2 -2
- cirq/protocols/inverse_protocol.py +2 -1
- cirq/protocols/json_serialization.py +5 -4
- cirq/protocols/json_serialization_test.py +31 -31
- cirq/protocols/kraus_protocol.py +4 -3
- cirq/protocols/kraus_protocol_test.py +7 -7
- cirq/protocols/measurement_key_protocol.py +32 -8
- cirq/protocols/mixture_protocol.py +3 -2
- cirq/protocols/mixture_protocol_test.py +7 -7
- cirq/protocols/mul_protocol_test.py +4 -4
- cirq/protocols/phase_protocol.py +13 -4
- cirq/protocols/pow_protocol.py +2 -1
- cirq/protocols/pow_protocol_test.py +5 -5
- cirq/protocols/qasm.py +2 -1
- cirq/protocols/qid_shape_protocol.py +2 -1
- cirq/protocols/resolve_parameters.py +17 -15
- cirq/protocols/trace_distance_bound.py +2 -1
- cirq/protocols/unitary_protocol.py +21 -21
- cirq/protocols/unitary_protocol_test.py +31 -19
- cirq/qis/channels.py +1 -1
- cirq/qis/channels_test.py +1 -1
- cirq/qis/clifford_tableau.py +16 -15
- cirq/qis/clifford_tableau_test.py +17 -17
- cirq/qis/entropy.py +3 -3
- cirq/qis/entropy_test.py +1 -1
- cirq/qis/quantum_state_representation.py +2 -1
- cirq/qis/states.py +7 -2
- cirq/qis/states_test.py +54 -54
- cirq/sim/classical_simulator.py +25 -14
- cirq/sim/classical_simulator_test.py +85 -30
- cirq/sim/clifford/clifford_simulator.py +7 -6
- cirq/sim/clifford/clifford_simulator_test.py +51 -50
- cirq/sim/clifford/clifford_tableau_simulation_state.py +2 -1
- cirq/sim/clifford/stabilizer_ch_form_simulation_state.py +2 -1
- cirq/sim/clifford/stabilizer_sampler.py +1 -1
- cirq/sim/clifford/stabilizer_simulation_state.py +2 -1
- cirq/sim/clifford/stabilizer_state_ch_form.py +16 -15
- cirq/sim/clifford/stabilizer_state_ch_form_test.py +0 -1
- cirq/sim/density_matrix_simulation_state.py +7 -6
- cirq/sim/density_matrix_simulator.py +3 -2
- cirq/sim/density_matrix_simulator_test.py +94 -84
- cirq/sim/density_matrix_utils.py +2 -1
- cirq/sim/density_matrix_utils_test.py +1 -1
- cirq/sim/mux.py +35 -8
- cirq/sim/mux_test.py +39 -26
- cirq/sim/simulation_product_state.py +2 -1
- cirq/sim/simulation_product_state_test.py +8 -7
- cirq/sim/simulation_state.py +6 -5
- cirq/sim/simulation_state_base.py +3 -2
- cirq/sim/simulation_state_test.py +7 -6
- cirq/sim/simulation_utils.py +2 -1
- cirq/sim/simulator.py +4 -3
- cirq/sim/simulator_base.py +2 -1
- cirq/sim/simulator_base_test.py +51 -36
- cirq/sim/simulator_test.py +41 -36
- cirq/sim/sparse_simulator.py +3 -2
- cirq/sim/sparse_simulator_test.py +92 -82
- cirq/sim/state_vector.py +5 -6
- cirq/sim/state_vector_simulation_state.py +10 -9
- cirq/sim/state_vector_simulator.py +2 -1
- cirq/sim/state_vector_simulator_test.py +9 -9
- cirq/sim/state_vector_test.py +40 -39
- cirq/study/__init__.py +1 -0
- cirq/study/flatten_expressions.py +2 -1
- cirq/study/resolver.py +31 -18
- cirq/study/resolver_test.py +1 -1
- cirq/study/result.py +2 -1
- cirq/study/result_test.py +20 -20
- cirq/study/sweepable.py +2 -1
- cirq/study/sweepable_test.py +20 -20
- cirq/study/sweeps.py +26 -1
- cirq/study/sweeps_test.py +67 -43
- cirq/testing/_compat_test_data/__init__.py +3 -3
- cirq/testing/circuit_compare.py +2 -1
- cirq/testing/circuit_compare_test.py +16 -14
- cirq/testing/consistent_act_on_test.py +1 -1
- cirq/testing/consistent_channels.py +2 -2
- cirq/testing/consistent_controlled_gate_op.py +2 -2
- cirq/testing/consistent_controlled_gate_op_test.py +2 -1
- cirq/testing/consistent_decomposition.py +4 -2
- cirq/testing/consistent_phase_by.py +1 -1
- cirq/testing/consistent_protocols.py +2 -1
- cirq/testing/consistent_protocols_test.py +3 -3
- cirq/testing/consistent_qasm.py +4 -3
- cirq/testing/consistent_qasm_test.py +3 -3
- cirq/testing/consistent_resolve_parameters.py +1 -1
- cirq/testing/consistent_unitary.py +1 -1
- cirq/testing/consistent_unitary_test.py +1 -1
- cirq/testing/deprecation.py +1 -1
- cirq/testing/devices.py +3 -2
- cirq/testing/equals_tester.py +4 -3
- cirq/testing/equivalent_basis_map.py +4 -2
- cirq/testing/json.py +3 -2
- cirq/testing/lin_alg_utils.py +1 -1
- cirq/testing/logs.py +1 -1
- cirq/testing/op_tree.py +1 -1
- cirq/testing/order_tester.py +2 -2
- cirq/testing/pytest_utils.py +2 -1
- cirq/testing/random_circuit.py +2 -1
- cirq/testing/random_circuit_test.py +2 -1
- cirq/testing/repr_pretty_tester.py +3 -3
- cirq/transformers/__init__.py +1 -0
- cirq/transformers/_connected_component.py +231 -0
- cirq/transformers/_connected_component_test.py +200 -0
- cirq/transformers/align_test.py +13 -13
- cirq/transformers/analytical_decompositions/clifford_decomposition.py +8 -7
- cirq/transformers/analytical_decompositions/clifford_decomposition_test.py +5 -5
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +11 -10
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition_test.py +6 -6
- cirq/transformers/analytical_decompositions/cphase_to_fsim.py +3 -2
- cirq/transformers/analytical_decompositions/cphase_to_fsim_test.py +11 -10
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +8 -7
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition_test.py +17 -20
- cirq/transformers/analytical_decompositions/single_qubit_decompositions_test.py +33 -27
- cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry_test.py +1 -1
- cirq/transformers/analytical_decompositions/three_qubit_decomposition.py +1 -1
- cirq/transformers/analytical_decompositions/two_qubit_state_preparation_test.py +12 -11
- cirq/transformers/analytical_decompositions/two_qubit_to_cz.py +5 -2
- cirq/transformers/analytical_decompositions/two_qubit_to_cz_test.py +3 -3
- cirq/transformers/analytical_decompositions/two_qubit_to_fsim.py +2 -1
- cirq/transformers/analytical_decompositions/two_qubit_to_ms.py +2 -1
- cirq/transformers/analytical_decompositions/two_qubit_to_ms_test.py +2 -2
- cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap.py +2 -1
- cirq/transformers/analytical_decompositions/two_qubit_to_sqrt_iswap_test.py +32 -30
- cirq/transformers/drop_negligible_operations_test.py +7 -7
- cirq/transformers/dynamical_decoupling.py +185 -112
- cirq/transformers/dynamical_decoupling_test.py +195 -201
- cirq/transformers/eject_phased_paulis.py +2 -1
- cirq/transformers/eject_phased_paulis_test.py +3 -2
- cirq/transformers/eject_z.py +5 -3
- cirq/transformers/eject_z_test.py +23 -25
- cirq/transformers/expand_composite.py +3 -2
- cirq/transformers/expand_composite_test.py +14 -14
- cirq/transformers/gauge_compiling/__init__.py +13 -0
- cirq/transformers/gauge_compiling/gauge_compiling.py +3 -2
- cirq/transformers/gauge_compiling/gauge_compiling_test.py +14 -12
- cirq/transformers/gauge_compiling/gauge_compiling_test_utils.py +3 -3
- cirq/transformers/gauge_compiling/idle_moments_gauge.py +225 -0
- cirq/transformers/gauge_compiling/idle_moments_gauge_test.py +193 -0
- cirq/transformers/gauge_compiling/multi_moment_cphase_gauge.py +242 -0
- cirq/transformers/gauge_compiling/multi_moment_cphase_gauge_test.py +243 -0
- cirq/transformers/gauge_compiling/multi_moment_gauge_compiling.py +151 -0
- cirq/transformers/gauge_compiling/sqrt_cz_gauge.py +2 -1
- cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils.py +1 -1
- cirq/transformers/heuristic_decompositions/gate_tabulation_math_utils_test.py +6 -6
- cirq/transformers/heuristic_decompositions/two_qubit_gate_tabulation.py +3 -2
- cirq/transformers/measurement_transformers.py +2 -1
- cirq/transformers/measurement_transformers_test.py +45 -39
- cirq/transformers/merge_k_qubit_gates.py +2 -1
- cirq/transformers/merge_k_qubit_gates_test.py +1 -1
- cirq/transformers/merge_single_qubit_gates.py +9 -5
- cirq/transformers/merge_single_qubit_gates_test.py +22 -22
- cirq/transformers/noise_adding_test.py +2 -2
- cirq/transformers/optimize_for_target_gateset.py +2 -1
- cirq/transformers/optimize_for_target_gateset_test.py +11 -9
- cirq/transformers/qubit_management_transformers_test.py +6 -2
- cirq/transformers/routing/mapping_manager.py +2 -1
- cirq/transformers/routing/route_circuit_cqc.py +2 -1
- cirq/transformers/stratify.py +2 -1
- cirq/transformers/symbolize.py +2 -1
- cirq/transformers/tag_transformers.py +2 -1
- cirq/transformers/target_gatesets/compilation_target_gateset.py +2 -1
- cirq/transformers/target_gatesets/cz_gateset.py +2 -1
- cirq/transformers/target_gatesets/cz_gateset_test.py +1 -1
- cirq/transformers/target_gatesets/sqrt_iswap_gateset.py +2 -1
- cirq/transformers/transformer_api.py +2 -1
- cirq/transformers/transformer_primitives.py +271 -145
- cirq/transformers/transformer_primitives_test.py +185 -1
- cirq/value/abc_alt.py +2 -1
- cirq/value/classical_data.py +2 -1
- cirq/value/condition.py +2 -1
- cirq/value/digits.py +9 -2
- cirq/value/duration.py +6 -5
- cirq/value/linear_dict.py +4 -9
- cirq/value/measurement_key.py +2 -1
- cirq/value/periodic_value.py +3 -2
- cirq/value/product_state.py +2 -1
- cirq/value/value_equality_attr.py +2 -1
- cirq/vis/density_matrix.py +1 -1
- cirq/vis/heatmap.py +2 -1
- cirq/vis/histogram.py +2 -1
- cirq/vis/state_histogram.py +2 -1
- cirq/work/collector.py +2 -1
- cirq/work/observable_grouping.py +2 -1
- cirq/work/observable_measurement.py +2 -1
- cirq/work/observable_measurement_data.py +2 -1
- cirq/work/observable_measurement_test.py +1 -1
- cirq/work/observable_readout_calibration.py +2 -1
- cirq/work/observable_readout_calibration_test.py +1 -1
- cirq/work/observable_settings.py +2 -1
- cirq/work/sampler.py +2 -1
- cirq/work/sampler_test.py +1 -1
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/METADATA +5 -6
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/RECORD +425 -406
- cirq/contrib/json_test.py +0 -33
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/WHEEL +0 -0
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.7.0.dev20250825174419.dist-info → cirq_core-1.7.0.dev20251203004401.dist-info}/top_level.txt +0 -0
cirq/sim/state_vector.py
CHANGED
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
19
|
import abc
|
|
20
|
-
from
|
|
20
|
+
from collections.abc import Mapping, Sequence
|
|
21
|
+
from typing import TYPE_CHECKING
|
|
21
22
|
|
|
22
23
|
import numpy as np
|
|
23
24
|
|
|
@@ -105,7 +106,7 @@ class StateVectorMixin:
|
|
|
105
106
|
and non-zero floats of the specified accuracy."""
|
|
106
107
|
return qis.dirac_notation(self.state_vector(), decimals, qid_shape=self._qid_shape)
|
|
107
108
|
|
|
108
|
-
def density_matrix_of(self, qubits:
|
|
109
|
+
def density_matrix_of(self, qubits: Sequence[cirq.Qid] | None = None) -> np.ndarray:
|
|
109
110
|
r"""Returns the density matrix of the state.
|
|
110
111
|
|
|
111
112
|
Calculate the density matrix for the system on the qubits provided.
|
|
@@ -135,8 +136,7 @@ class StateVectorMixin:
|
|
|
135
136
|
|
|
136
137
|
Raises:
|
|
137
138
|
ValueError: if the size of the state represents more than 25 qubits.
|
|
138
|
-
|
|
139
|
-
corresponding to the state.
|
|
139
|
+
KeyError: if some of the qubits provided are not in the quantum state.
|
|
140
140
|
"""
|
|
141
141
|
return qis.density_matrix_from_state_vector(
|
|
142
142
|
self.state_vector(),
|
|
@@ -160,8 +160,7 @@ class StateVectorMixin:
|
|
|
160
160
|
|
|
161
161
|
Raises:
|
|
162
162
|
ValueError: if the size of the state represents more than 25 qubits.
|
|
163
|
-
|
|
164
|
-
corresponding to the state.
|
|
163
|
+
KeyError: if the specified qubit is not in the quantum state.
|
|
165
164
|
"""
|
|
166
165
|
return qis.bloch_vector_from_state_vector(
|
|
167
166
|
self.state_vector(), self.qubit_map[qubit], qid_shape=self._qid_shape
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
|
-
from
|
|
19
|
+
from collections.abc import Callable, Sequence
|
|
20
|
+
from typing import Any, Self, TYPE_CHECKING
|
|
20
21
|
|
|
21
22
|
import numpy as np
|
|
22
23
|
|
|
@@ -55,7 +56,7 @@ class _BufferedStateVector(qis.QuantumStateRepresentation):
|
|
|
55
56
|
*,
|
|
56
57
|
initial_state: np.ndarray | cirq.STATE_VECTOR_LIKE = 0,
|
|
57
58
|
qid_shape: tuple[int, ...] | None = None,
|
|
58
|
-
dtype: type[np.complexfloating] | None = None,
|
|
59
|
+
dtype: type[np.complexfloating] | np.dtype[np.complexfloating] | None = None,
|
|
59
60
|
buffer: np.ndarray | None = None,
|
|
60
61
|
):
|
|
61
62
|
"""Initializes the object with the inputs.
|
|
@@ -324,7 +325,7 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
324
325
|
prng: np.random.RandomState | None = None,
|
|
325
326
|
qubits: Sequence[cirq.Qid] | None = None,
|
|
326
327
|
initial_state: np.ndarray | cirq.STATE_VECTOR_LIKE = 0,
|
|
327
|
-
dtype: type[np.complexfloating] = np.complex64,
|
|
328
|
+
dtype: type[np.complexfloating] | np.dtype[np.complexfloating] = np.complex64,
|
|
328
329
|
classical_data: cirq.ClassicalDataStore | None = None,
|
|
329
330
|
):
|
|
330
331
|
"""Inits StateVectorSimulationState.
|
|
@@ -356,7 +357,7 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
356
357
|
)
|
|
357
358
|
super().__init__(state=state, prng=prng, qubits=qubits, classical_data=classical_data)
|
|
358
359
|
|
|
359
|
-
def add_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
360
|
+
def add_qubits(self, qubits: Sequence[cirq.Qid]) -> Self:
|
|
360
361
|
ret = super().add_qubits(qubits)
|
|
361
362
|
return (
|
|
362
363
|
self.kronecker_product(type(self)(qubits=qubits), inplace=True)
|
|
@@ -364,7 +365,7 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
364
365
|
else ret
|
|
365
366
|
)
|
|
366
367
|
|
|
367
|
-
def remove_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
368
|
+
def remove_qubits(self, qubits: Sequence[cirq.Qid]) -> Self:
|
|
368
369
|
ret = super().remove_qubits(qubits)
|
|
369
370
|
if ret is not NotImplemented:
|
|
370
371
|
return ret
|
|
@@ -406,11 +407,11 @@ class StateVectorSimulationState(SimulationState[_BufferedStateVector]):
|
|
|
406
407
|
)
|
|
407
408
|
|
|
408
409
|
@property
|
|
409
|
-
def target_tensor(self):
|
|
410
|
+
def target_tensor(self) -> np.ndarray:
|
|
410
411
|
return self._state._state_vector
|
|
411
412
|
|
|
412
413
|
@property
|
|
413
|
-
def available_buffer(self):
|
|
414
|
+
def available_buffer(self) -> np.ndarray:
|
|
414
415
|
return self._state._buffer
|
|
415
416
|
|
|
416
417
|
|
|
@@ -429,7 +430,7 @@ def _strat_act_on_state_vector_from_mixture(
|
|
|
429
430
|
if index is None:
|
|
430
431
|
return NotImplemented
|
|
431
432
|
if protocols.is_measurement(action):
|
|
432
|
-
key = protocols.
|
|
433
|
+
key = protocols.measurement_key_obj(action)
|
|
433
434
|
args._classical_data.record_channel_measurement(key, index)
|
|
434
435
|
return True
|
|
435
436
|
|
|
@@ -441,6 +442,6 @@ def _strat_act_on_state_vector_from_channel(
|
|
|
441
442
|
if index is None:
|
|
442
443
|
return NotImplemented
|
|
443
444
|
if protocols.is_measurement(action):
|
|
444
|
-
key = protocols.
|
|
445
|
+
key = protocols.measurement_key_obj(action)
|
|
445
446
|
args._classical_data.record_channel_measurement(key, index)
|
|
446
447
|
return True
|
|
@@ -18,8 +18,9 @@ from __future__ import annotations
|
|
|
18
18
|
|
|
19
19
|
import abc
|
|
20
20
|
import warnings
|
|
21
|
+
from collections.abc import Iterator, Sequence
|
|
21
22
|
from functools import cached_property
|
|
22
|
-
from typing import Any, Generic,
|
|
23
|
+
from typing import Any, Generic, TYPE_CHECKING, TypeVar
|
|
23
24
|
|
|
24
25
|
import numpy as np
|
|
25
26
|
|
|
@@ -20,7 +20,7 @@ import cirq
|
|
|
20
20
|
import cirq.testing
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def test_state_vector_trial_result_repr():
|
|
23
|
+
def test_state_vector_trial_result_repr() -> None:
|
|
24
24
|
q0 = cirq.NamedQubit('a')
|
|
25
25
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
26
26
|
available_buffer=np.array([0, 1], dtype=np.complex64),
|
|
@@ -47,7 +47,7 @@ def test_state_vector_trial_result_repr():
|
|
|
47
47
|
assert eval(expected_repr) == trial_result
|
|
48
48
|
|
|
49
49
|
|
|
50
|
-
def test_state_vector_trial_result_equality():
|
|
50
|
+
def test_state_vector_trial_result_equality() -> None:
|
|
51
51
|
eq = cirq.testing.EqualsTester()
|
|
52
52
|
final_simulator_state = cirq.StateVectorSimulationState(initial_state=np.array([]))
|
|
53
53
|
eq.add_equality_group(
|
|
@@ -86,7 +86,7 @@ def test_state_vector_trial_result_equality():
|
|
|
86
86
|
)
|
|
87
87
|
|
|
88
88
|
|
|
89
|
-
def test_state_vector_trial_result_state_mixin():
|
|
89
|
+
def test_state_vector_trial_result_state_mixin() -> None:
|
|
90
90
|
qubits = cirq.LineQubit.range(2)
|
|
91
91
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
92
92
|
qubits=qubits, initial_state=np.array([0, 1, 0, 0])
|
|
@@ -103,7 +103,7 @@ def test_state_vector_trial_result_state_mixin():
|
|
|
103
103
|
assert result.dirac_notation() == '|01⟩'
|
|
104
104
|
|
|
105
105
|
|
|
106
|
-
def test_state_vector_trial_result_qid_shape():
|
|
106
|
+
def test_state_vector_trial_result_qid_shape() -> None:
|
|
107
107
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
108
108
|
qubits=[cirq.NamedQubit('a')], initial_state=np.array([0, 1])
|
|
109
109
|
)
|
|
@@ -125,7 +125,7 @@ def test_state_vector_trial_result_qid_shape():
|
|
|
125
125
|
assert cirq.qid_shape(trial_result) == (3, 2)
|
|
126
126
|
|
|
127
127
|
|
|
128
|
-
def test_state_vector_trial_state_vector_is_copy():
|
|
128
|
+
def test_state_vector_trial_state_vector_is_copy() -> None:
|
|
129
129
|
final_state_vector = np.array([0, 1], dtype=np.complex64)
|
|
130
130
|
qubit_map = {cirq.NamedQubit('a'): 0}
|
|
131
131
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
@@ -137,7 +137,7 @@ def test_state_vector_trial_state_vector_is_copy():
|
|
|
137
137
|
assert trial_result.state_vector(copy=True) is not final_simulator_state.target_tensor
|
|
138
138
|
|
|
139
139
|
|
|
140
|
-
def test_state_vector_trial_result_no_qubits():
|
|
140
|
+
def test_state_vector_trial_result_no_qubits() -> None:
|
|
141
141
|
initial_state_vector = np.array([1], dtype=np.complex64)
|
|
142
142
|
initial_state = initial_state_vector.reshape((2,) * 0) # reshape as tensor for 0 qubits
|
|
143
143
|
final_simulator_state = cirq.StateVectorSimulationState(qubits=[], initial_state=initial_state)
|
|
@@ -149,7 +149,7 @@ def test_state_vector_trial_result_no_qubits():
|
|
|
149
149
|
assert np.array_equal(state_vector, initial_state_vector)
|
|
150
150
|
|
|
151
151
|
|
|
152
|
-
def test_str_big():
|
|
152
|
+
def test_str_big() -> None:
|
|
153
153
|
qs = cirq.LineQubit.range(10)
|
|
154
154
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
155
155
|
prng=np.random.RandomState(0),
|
|
@@ -161,7 +161,7 @@ def test_str_big():
|
|
|
161
161
|
assert 'output vector: [0.03125+0.j 0.03125+0.j 0.03125+0.j ..' in str(result)
|
|
162
162
|
|
|
163
163
|
|
|
164
|
-
def test_str_qudit():
|
|
164
|
+
def test_str_qudit() -> None:
|
|
165
165
|
qutrit = cirq.LineQid(0, dimension=3)
|
|
166
166
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
167
167
|
prng=np.random.RandomState(0),
|
|
@@ -183,7 +183,7 @@ def test_str_qudit():
|
|
|
183
183
|
assert "|1⟩" in str(result)
|
|
184
184
|
|
|
185
185
|
|
|
186
|
-
def test_pretty_print():
|
|
186
|
+
def test_pretty_print() -> None:
|
|
187
187
|
final_simulator_state = cirq.StateVectorSimulationState(
|
|
188
188
|
available_buffer=np.array([1]),
|
|
189
189
|
prng=np.random.RandomState(0),
|
cirq/sim/state_vector_test.py
CHANGED
|
@@ -17,14 +17,13 @@
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
19
|
import itertools
|
|
20
|
-
from
|
|
20
|
+
from collections.abc import Iterator
|
|
21
21
|
from unittest import mock
|
|
22
22
|
|
|
23
23
|
import numpy as np
|
|
24
24
|
import pytest
|
|
25
25
|
|
|
26
26
|
import cirq
|
|
27
|
-
import cirq.testing
|
|
28
27
|
from cirq import linalg
|
|
29
28
|
|
|
30
29
|
|
|
@@ -35,7 +34,7 @@ def use_np_transpose(request) -> Iterator[bool]:
|
|
|
35
34
|
yield value
|
|
36
35
|
|
|
37
36
|
|
|
38
|
-
def test_state_mixin():
|
|
37
|
+
def test_state_mixin() -> None:
|
|
39
38
|
class TestClass(cirq.StateVectorMixin):
|
|
40
39
|
def state_vector(self, copy: bool | None = None) -> np.ndarray:
|
|
41
40
|
return np.array([0, 0, 1, 0])
|
|
@@ -60,7 +59,7 @@ def test_state_mixin():
|
|
|
60
59
|
_ = TestClass({qubits[0]: -1, qubits[1]: 1})
|
|
61
60
|
|
|
62
61
|
|
|
63
|
-
def test_sample_state_big_endian():
|
|
62
|
+
def test_sample_state_big_endian() -> None:
|
|
64
63
|
results = []
|
|
65
64
|
for x in range(8):
|
|
66
65
|
state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -71,7 +70,7 @@ def test_sample_state_big_endian():
|
|
|
71
70
|
np.testing.assert_equal(result, expected)
|
|
72
71
|
|
|
73
72
|
|
|
74
|
-
def test_sample_state_partial_indices():
|
|
73
|
+
def test_sample_state_partial_indices() -> None:
|
|
75
74
|
for index in range(3):
|
|
76
75
|
for x in range(8):
|
|
77
76
|
state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -80,14 +79,14 @@ def test_sample_state_partial_indices():
|
|
|
80
79
|
)
|
|
81
80
|
|
|
82
81
|
|
|
83
|
-
def test_sample_state_partial_indices_oder():
|
|
82
|
+
def test_sample_state_partial_indices_oder() -> None:
|
|
84
83
|
for x in range(8):
|
|
85
84
|
state = cirq.to_valid_state_vector(x, 3)
|
|
86
85
|
expected = [[bool(1 & (x >> 0)), bool(1 & (x >> 1))]]
|
|
87
86
|
np.testing.assert_equal(cirq.sample_state_vector(state, [2, 1]), expected)
|
|
88
87
|
|
|
89
88
|
|
|
90
|
-
def test_sample_state_partial_indices_all_orders():
|
|
89
|
+
def test_sample_state_partial_indices_all_orders() -> None:
|
|
91
90
|
for perm in itertools.permutations([0, 1, 2]):
|
|
92
91
|
for x in range(8):
|
|
93
92
|
state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -95,7 +94,7 @@ def test_sample_state_partial_indices_all_orders():
|
|
|
95
94
|
np.testing.assert_equal(cirq.sample_state_vector(state, perm), expected)
|
|
96
95
|
|
|
97
96
|
|
|
98
|
-
def test_sample_state():
|
|
97
|
+
def test_sample_state() -> None:
|
|
99
98
|
state = np.zeros(8, dtype=np.complex64)
|
|
100
99
|
state[0] = 1 / np.sqrt(2)
|
|
101
100
|
state[2] = 1 / np.sqrt(2)
|
|
@@ -110,12 +109,12 @@ def test_sample_state():
|
|
|
110
109
|
np.testing.assert_equal(cirq.sample_state_vector(state, [0]), [[False]])
|
|
111
110
|
|
|
112
111
|
|
|
113
|
-
def test_sample_empty_state():
|
|
112
|
+
def test_sample_empty_state() -> None:
|
|
114
113
|
state = np.array([1.0])
|
|
115
114
|
np.testing.assert_almost_equal(cirq.sample_state_vector(state, []), np.zeros(shape=(1, 0)))
|
|
116
115
|
|
|
117
116
|
|
|
118
|
-
def test_sample_no_repetitions():
|
|
117
|
+
def test_sample_no_repetitions() -> None:
|
|
119
118
|
state = cirq.to_valid_state_vector(0, 3)
|
|
120
119
|
np.testing.assert_almost_equal(
|
|
121
120
|
cirq.sample_state_vector(state, [1], repetitions=0), np.zeros(shape=(0, 1))
|
|
@@ -125,7 +124,7 @@ def test_sample_no_repetitions():
|
|
|
125
124
|
)
|
|
126
125
|
|
|
127
126
|
|
|
128
|
-
def test_sample_state_repetitions():
|
|
127
|
+
def test_sample_state_repetitions() -> None:
|
|
129
128
|
for perm in itertools.permutations([0, 1, 2]):
|
|
130
129
|
for x in range(8):
|
|
131
130
|
state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -135,7 +134,7 @@ def test_sample_state_repetitions():
|
|
|
135
134
|
np.testing.assert_equal(result, expected)
|
|
136
135
|
|
|
137
136
|
|
|
138
|
-
def test_sample_state_seed():
|
|
137
|
+
def test_sample_state_seed() -> None:
|
|
139
138
|
state = np.ones(2) / np.sqrt(2)
|
|
140
139
|
|
|
141
140
|
samples = cirq.sample_state_vector(state, [0], repetitions=10, seed=1234)
|
|
@@ -151,20 +150,20 @@ def test_sample_state_seed():
|
|
|
151
150
|
)
|
|
152
151
|
|
|
153
152
|
|
|
154
|
-
def test_sample_state_negative_repetitions():
|
|
153
|
+
def test_sample_state_negative_repetitions() -> None:
|
|
155
154
|
state = cirq.to_valid_state_vector(0, 3)
|
|
156
155
|
with pytest.raises(ValueError, match='-1'):
|
|
157
156
|
cirq.sample_state_vector(state, [1], repetitions=-1)
|
|
158
157
|
|
|
159
158
|
|
|
160
|
-
def test_sample_state_not_power_of_two():
|
|
159
|
+
def test_sample_state_not_power_of_two() -> None:
|
|
161
160
|
with pytest.raises(ValueError, match='3'):
|
|
162
161
|
cirq.sample_state_vector(np.array([1, 0, 0]), [1])
|
|
163
162
|
with pytest.raises(ValueError, match='5'):
|
|
164
163
|
cirq.sample_state_vector(np.array([0, 1, 0, 0, 0]), [1])
|
|
165
164
|
|
|
166
165
|
|
|
167
|
-
def test_sample_state_index_out_of_range():
|
|
166
|
+
def test_sample_state_index_out_of_range() -> None:
|
|
168
167
|
state = cirq.to_valid_state_vector(0, 3)
|
|
169
168
|
with pytest.raises(IndexError, match='-2'):
|
|
170
169
|
cirq.sample_state_vector(state, [-2])
|
|
@@ -172,12 +171,12 @@ def test_sample_state_index_out_of_range():
|
|
|
172
171
|
cirq.sample_state_vector(state, [3])
|
|
173
172
|
|
|
174
173
|
|
|
175
|
-
def test_sample_no_indices():
|
|
174
|
+
def test_sample_no_indices() -> None:
|
|
176
175
|
state = cirq.to_valid_state_vector(0, 3)
|
|
177
176
|
np.testing.assert_almost_equal(cirq.sample_state_vector(state, []), np.zeros(shape=(1, 0)))
|
|
178
177
|
|
|
179
178
|
|
|
180
|
-
def test_sample_no_indices_repetitions():
|
|
179
|
+
def test_sample_no_indices_repetitions() -> None:
|
|
181
180
|
state = cirq.to_valid_state_vector(0, 3)
|
|
182
181
|
np.testing.assert_almost_equal(
|
|
183
182
|
cirq.sample_state_vector(state, [], repetitions=2), np.zeros(shape=(2, 0))
|
|
@@ -185,7 +184,7 @@ def test_sample_no_indices_repetitions():
|
|
|
185
184
|
|
|
186
185
|
|
|
187
186
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
188
|
-
def test_measure_state_computational_basis(use_np_transpose: bool):
|
|
187
|
+
def test_measure_state_computational_basis(use_np_transpose: bool) -> None:
|
|
189
188
|
# verify patching of can_numpy_support_shape in the use_np_transpose fixture
|
|
190
189
|
assert linalg.can_numpy_support_shape([1]) is use_np_transpose
|
|
191
190
|
results = []
|
|
@@ -199,7 +198,7 @@ def test_measure_state_computational_basis(use_np_transpose: bool):
|
|
|
199
198
|
|
|
200
199
|
|
|
201
200
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
202
|
-
def test_measure_state_reshape(use_np_transpose: bool):
|
|
201
|
+
def test_measure_state_reshape(use_np_transpose: bool) -> None:
|
|
203
202
|
results = []
|
|
204
203
|
for x in range(8):
|
|
205
204
|
initial_state = np.reshape(cirq.to_valid_state_vector(x, 3), [2] * 3)
|
|
@@ -211,7 +210,7 @@ def test_measure_state_reshape(use_np_transpose: bool):
|
|
|
211
210
|
|
|
212
211
|
|
|
213
212
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
214
|
-
def test_measure_state_partial_indices(use_np_transpose: bool):
|
|
213
|
+
def test_measure_state_partial_indices(use_np_transpose: bool) -> None:
|
|
215
214
|
for index in range(3):
|
|
216
215
|
for x in range(8):
|
|
217
216
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -221,7 +220,7 @@ def test_measure_state_partial_indices(use_np_transpose: bool):
|
|
|
221
220
|
|
|
222
221
|
|
|
223
222
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
224
|
-
def test_measure_state_partial_indices_order(use_np_transpose: bool):
|
|
223
|
+
def test_measure_state_partial_indices_order(use_np_transpose: bool) -> None:
|
|
225
224
|
for x in range(8):
|
|
226
225
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
227
226
|
bits, state = cirq.measure_state_vector(initial_state, [2, 1])
|
|
@@ -230,7 +229,7 @@ def test_measure_state_partial_indices_order(use_np_transpose: bool):
|
|
|
230
229
|
|
|
231
230
|
|
|
232
231
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
233
|
-
def test_measure_state_partial_indices_all_orders(use_np_transpose: bool):
|
|
232
|
+
def test_measure_state_partial_indices_all_orders(use_np_transpose: bool) -> None:
|
|
234
233
|
for perm in itertools.permutations([0, 1, 2]):
|
|
235
234
|
for x in range(8):
|
|
236
235
|
initial_state = cirq.to_valid_state_vector(x, 3)
|
|
@@ -240,7 +239,7 @@ def test_measure_state_partial_indices_all_orders(use_np_transpose: bool):
|
|
|
240
239
|
|
|
241
240
|
|
|
242
241
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
243
|
-
def test_measure_state_collapse(use_np_transpose: bool):
|
|
242
|
+
def test_measure_state_collapse(use_np_transpose: bool) -> None:
|
|
244
243
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
245
244
|
initial_state[0] = 1 / np.sqrt(2)
|
|
246
245
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -264,7 +263,7 @@ def test_measure_state_collapse(use_np_transpose: bool):
|
|
|
264
263
|
|
|
265
264
|
|
|
266
265
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
267
|
-
def test_measure_state_seed(use_np_transpose: bool):
|
|
266
|
+
def test_measure_state_seed(use_np_transpose: bool) -> None:
|
|
268
267
|
n = 10
|
|
269
268
|
initial_state = np.ones(2**n) / 2 ** (n / 2)
|
|
270
269
|
|
|
@@ -284,7 +283,7 @@ def test_measure_state_seed(use_np_transpose: bool):
|
|
|
284
283
|
|
|
285
284
|
|
|
286
285
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
287
|
-
def test_measure_state_out_is_state(use_np_transpose: bool):
|
|
286
|
+
def test_measure_state_out_is_state(use_np_transpose: bool) -> None:
|
|
288
287
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
289
288
|
initial_state[0] = 1 / np.sqrt(2)
|
|
290
289
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -296,7 +295,7 @@ def test_measure_state_out_is_state(use_np_transpose: bool):
|
|
|
296
295
|
|
|
297
296
|
|
|
298
297
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
299
|
-
def test_measure_state_out_is_not_state(use_np_transpose: bool):
|
|
298
|
+
def test_measure_state_out_is_not_state(use_np_transpose: bool) -> None:
|
|
300
299
|
initial_state = np.zeros(8, dtype=np.complex64)
|
|
301
300
|
initial_state[0] = 1 / np.sqrt(2)
|
|
302
301
|
initial_state[2] = 1 / np.sqrt(2)
|
|
@@ -307,7 +306,7 @@ def test_measure_state_out_is_not_state(use_np_transpose: bool):
|
|
|
307
306
|
|
|
308
307
|
|
|
309
308
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
310
|
-
def test_measure_state_not_power_of_two(use_np_transpose: bool):
|
|
309
|
+
def test_measure_state_not_power_of_two(use_np_transpose: bool) -> None:
|
|
311
310
|
with pytest.raises(ValueError, match='3'):
|
|
312
311
|
_, _ = cirq.measure_state_vector(np.array([1, 0, 0]), [1])
|
|
313
312
|
with pytest.raises(ValueError, match='5'):
|
|
@@ -315,7 +314,7 @@ def test_measure_state_not_power_of_two(use_np_transpose: bool):
|
|
|
315
314
|
|
|
316
315
|
|
|
317
316
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
318
|
-
def test_measure_state_index_out_of_range(use_np_transpose: bool):
|
|
317
|
+
def test_measure_state_index_out_of_range(use_np_transpose: bool) -> None:
|
|
319
318
|
state = cirq.to_valid_state_vector(0, 3)
|
|
320
319
|
with pytest.raises(IndexError, match='-2'):
|
|
321
320
|
cirq.measure_state_vector(state, [-2])
|
|
@@ -324,7 +323,7 @@ def test_measure_state_index_out_of_range(use_np_transpose: bool):
|
|
|
324
323
|
|
|
325
324
|
|
|
326
325
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
327
|
-
def test_measure_state_no_indices(use_np_transpose: bool):
|
|
326
|
+
def test_measure_state_no_indices(use_np_transpose: bool) -> None:
|
|
328
327
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
329
328
|
bits, state = cirq.measure_state_vector(initial_state, [])
|
|
330
329
|
assert [] == bits
|
|
@@ -332,7 +331,7 @@ def test_measure_state_no_indices(use_np_transpose: bool):
|
|
|
332
331
|
|
|
333
332
|
|
|
334
333
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
335
|
-
def test_measure_state_no_indices_out_is_state(use_np_transpose: bool):
|
|
334
|
+
def test_measure_state_no_indices_out_is_state(use_np_transpose: bool) -> None:
|
|
336
335
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
337
336
|
bits, state = cirq.measure_state_vector(initial_state, [], out=initial_state)
|
|
338
337
|
assert [] == bits
|
|
@@ -341,7 +340,7 @@ def test_measure_state_no_indices_out_is_state(use_np_transpose: bool):
|
|
|
341
340
|
|
|
342
341
|
|
|
343
342
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
344
|
-
def test_measure_state_no_indices_out_is_not_state(use_np_transpose: bool):
|
|
343
|
+
def test_measure_state_no_indices_out_is_not_state(use_np_transpose: bool) -> None:
|
|
345
344
|
initial_state = cirq.to_valid_state_vector(0, 3)
|
|
346
345
|
out = np.zeros_like(initial_state)
|
|
347
346
|
bits, state = cirq.measure_state_vector(initial_state, [], out=out)
|
|
@@ -352,7 +351,7 @@ def test_measure_state_no_indices_out_is_not_state(use_np_transpose: bool):
|
|
|
352
351
|
|
|
353
352
|
|
|
354
353
|
@pytest.mark.parametrize('use_np_transpose', [False, True], indirect=True)
|
|
355
|
-
def test_measure_state_empty_state(use_np_transpose: bool):
|
|
354
|
+
def test_measure_state_empty_state(use_np_transpose: bool) -> None:
|
|
356
355
|
initial_state = np.array([1.0])
|
|
357
356
|
bits, state = cirq.measure_state_vector(initial_state, [])
|
|
358
357
|
assert [] == bits
|
|
@@ -364,12 +363,12 @@ class BasicStateVector(cirq.StateVectorMixin):
|
|
|
364
363
|
return np.array([0, 1, 0, 0])
|
|
365
364
|
|
|
366
365
|
|
|
367
|
-
def test_step_result_pretty_state():
|
|
366
|
+
def test_step_result_pretty_state() -> None:
|
|
368
367
|
step_result = BasicStateVector()
|
|
369
368
|
assert step_result.dirac_notation() == '|01⟩'
|
|
370
369
|
|
|
371
370
|
|
|
372
|
-
def test_step_result_density_matrix():
|
|
371
|
+
def test_step_result_density_matrix() -> None:
|
|
373
372
|
q0, q1 = cirq.LineQubit.range(2)
|
|
374
373
|
|
|
375
374
|
step_result = BasicStateVector({q0: 0, q1: 1})
|
|
@@ -385,7 +384,7 @@ def test_step_result_density_matrix():
|
|
|
385
384
|
np.testing.assert_array_almost_equal(single_rho, step_result.density_matrix_of([q1]))
|
|
386
385
|
|
|
387
386
|
|
|
388
|
-
def test_step_result_density_matrix_invalid():
|
|
387
|
+
def test_step_result_density_matrix_invalid() -> None:
|
|
389
388
|
q0, q1 = cirq.LineQubit.range(2)
|
|
390
389
|
|
|
391
390
|
step_result = BasicStateVector({q0: 0})
|
|
@@ -393,21 +392,23 @@ def test_step_result_density_matrix_invalid():
|
|
|
393
392
|
with pytest.raises(KeyError):
|
|
394
393
|
step_result.density_matrix_of([q1])
|
|
395
394
|
with pytest.raises(KeyError):
|
|
396
|
-
step_result.density_matrix_of('junk')
|
|
395
|
+
step_result.density_matrix_of('junk') # type: ignore[arg-type]
|
|
397
396
|
with pytest.raises(TypeError):
|
|
398
|
-
step_result.density_matrix_of(0)
|
|
397
|
+
step_result.density_matrix_of(0) # type: ignore[arg-type]
|
|
399
398
|
|
|
400
399
|
|
|
401
|
-
def test_step_result_bloch_vector():
|
|
400
|
+
def test_step_result_bloch_vector() -> None:
|
|
402
401
|
q0, q1 = cirq.LineQubit.range(2)
|
|
403
402
|
step_result = BasicStateVector({q0: 0, q1: 1})
|
|
404
403
|
bloch1 = np.array([0, 0, -1])
|
|
405
404
|
bloch0 = np.array([0, 0, 1])
|
|
406
405
|
np.testing.assert_array_almost_equal(bloch1, step_result.bloch_vector_of(q1))
|
|
407
406
|
np.testing.assert_array_almost_equal(bloch0, step_result.bloch_vector_of(q0))
|
|
407
|
+
with pytest.raises(KeyError):
|
|
408
|
+
step_result.bloch_vector_of(cirq.LineQubit(2))
|
|
408
409
|
|
|
409
410
|
|
|
410
|
-
def test_factor_validation():
|
|
411
|
+
def test_factor_validation() -> None:
|
|
411
412
|
args = cirq.Simulator()._create_simulation_state(0, qubits=cirq.LineQubit.range(2))
|
|
412
413
|
args.apply_operation(cirq.H(cirq.LineQubit(0)) ** 0.7)
|
|
413
414
|
t = args.create_merged_state().target_tensor
|
cirq/study/__init__.py
CHANGED
cirq/study/resolver.py
CHANGED
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
from __future__ import annotations
|
|
18
18
|
|
|
19
19
|
import numbers
|
|
20
|
-
from
|
|
20
|
+
from collections.abc import Iterator, Mapping
|
|
21
|
+
from typing import Any, cast, TYPE_CHECKING, Union
|
|
21
22
|
|
|
22
23
|
import numpy as np
|
|
23
24
|
import sympy
|
|
@@ -75,9 +76,20 @@ class ParamResolver:
|
|
|
75
76
|
|
|
76
77
|
self._param_hash: int | None = None
|
|
77
78
|
self._param_dict = cast(ParamDictType, {} if param_dict is None else param_dict)
|
|
79
|
+
self._param_dict_with_str_keys = self._param_dict
|
|
80
|
+
generate_str_keys = False
|
|
78
81
|
for key in self._param_dict:
|
|
79
|
-
if isinstance(key, sympy.Expr)
|
|
80
|
-
|
|
82
|
+
if isinstance(key, sympy.Expr):
|
|
83
|
+
if isinstance(key, sympy.Symbol):
|
|
84
|
+
generate_str_keys = True
|
|
85
|
+
else:
|
|
86
|
+
raise TypeError(f'ParamResolver keys cannot be (non-symbol) formulas ({key})')
|
|
87
|
+
if generate_str_keys:
|
|
88
|
+
# Remake dictionary with string keys for faster access
|
|
89
|
+
self._param_dict_with_str_keys = {
|
|
90
|
+
(key.name if isinstance(key, sympy.Symbol) else key): value
|
|
91
|
+
for key, value in self._param_dict.items()
|
|
92
|
+
}
|
|
81
93
|
self._deep_eval_map: ParamDictType = {}
|
|
82
94
|
|
|
83
95
|
@property
|
|
@@ -118,32 +130,33 @@ class ParamResolver:
|
|
|
118
130
|
sympy.SympifyError: If the resulting value cannot be interpreted.
|
|
119
131
|
"""
|
|
120
132
|
|
|
121
|
-
# Input is a pass through type, no resolution needed: return early
|
|
122
|
-
v = _resolve_value(value)
|
|
123
|
-
if v is not NotImplemented:
|
|
124
|
-
return v
|
|
125
|
-
|
|
126
133
|
# Handle string or symbol
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
134
|
+
original_value = value
|
|
135
|
+
if isinstance(value, sympy.Symbol):
|
|
136
|
+
value = value.name
|
|
137
|
+
if isinstance(value, str):
|
|
138
|
+
param_value = self._param_dict_with_str_keys.get(value, _NOT_FOUND)
|
|
139
|
+
if isinstance(param_value, float):
|
|
140
|
+
return param_value
|
|
133
141
|
if param_value is _NOT_FOUND:
|
|
134
142
|
# Symbol or string cannot be resolved if not in param dict; return as symbol.
|
|
135
|
-
return
|
|
143
|
+
return sympy.Symbol(value)
|
|
136
144
|
v = _resolve_value(param_value)
|
|
137
145
|
if v is not NotImplemented:
|
|
138
146
|
return v
|
|
139
147
|
if isinstance(param_value, str):
|
|
140
148
|
param_value = sympy.Symbol(param_value)
|
|
141
149
|
elif not isinstance(param_value, sympy.Basic):
|
|
142
|
-
return
|
|
150
|
+
return original_value
|
|
143
151
|
if recursive:
|
|
144
152
|
param_value = self._value_of_recursive(value)
|
|
145
153
|
return param_value
|
|
146
154
|
|
|
155
|
+
# Input is a pass through type, no resolution needed: return early
|
|
156
|
+
v = _resolve_value(value)
|
|
157
|
+
if v is not NotImplemented:
|
|
158
|
+
return v
|
|
159
|
+
|
|
147
160
|
if not isinstance(value, sympy.Basic):
|
|
148
161
|
# No known way to resolve this variable, return unchanged.
|
|
149
162
|
return value
|
|
@@ -210,7 +223,7 @@ class ParamResolver:
|
|
|
210
223
|
self._deep_eval_map[value] = _RECURSION_FLAG
|
|
211
224
|
|
|
212
225
|
v = self.value_of(value, recursive=False)
|
|
213
|
-
if v == value:
|
|
226
|
+
if v == value or (isinstance(v, sympy.Symbol) and v.name == value):
|
|
214
227
|
self._deep_eval_map[value] = v
|
|
215
228
|
else:
|
|
216
229
|
self._deep_eval_map[value] = self.value_of(v, recursive=True)
|
|
@@ -278,7 +291,7 @@ class ParamResolver:
|
|
|
278
291
|
|
|
279
292
|
|
|
280
293
|
def _resolve_value(val: Any) -> Any:
|
|
281
|
-
if val is None:
|
|
294
|
+
if isinstance(val, float) or val is None:
|
|
282
295
|
return val
|
|
283
296
|
if isinstance(val, numbers.Number) and not isinstance(val, sympy.Basic):
|
|
284
297
|
return val
|
cirq/study/resolver_test.py
CHANGED
cirq/study/result.py
CHANGED
|
@@ -19,7 +19,8 @@ from __future__ import annotations
|
|
|
19
19
|
import abc
|
|
20
20
|
import collections
|
|
21
21
|
import io
|
|
22
|
-
from
|
|
22
|
+
from collections.abc import Callable, Iterable, Mapping, Sequence
|
|
23
|
+
from typing import Any, cast, TYPE_CHECKING, TypeVar, Union
|
|
23
24
|
|
|
24
25
|
import numpy as np
|
|
25
26
|
import pandas as pd
|