From 20160919df4ce25469d4f73aa16a7eb492c548bd Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Tue, 21 Jun 2022 20:02:25 -0700 Subject: [PATCH 1/3] Fix return value type error for measure_density_matrix Make returned value an unconditional np.ndarray. --- cirq-core/cirq/sim/density_matrix_utils.py | 33 ++++++++++------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/cirq-core/cirq/sim/density_matrix_utils.py b/cirq-core/cirq/sim/density_matrix_utils.py index c04d69f2c4d..8e0ea6ffca9 100644 --- a/cirq-core/cirq/sim/density_matrix_utils.py +++ b/cirq-core/cirq/sim/density_matrix_utils.py @@ -94,7 +94,7 @@ def measure_density_matrix( density_matrix: np.ndarray, indices: Sequence[int], qid_shape: Optional[Tuple[int, ...]] = None, - out: np.ndarray = None, + out: Optional[np.ndarray] = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> Tuple[List[int], np.ndarray]: """Performs a measurement of the density matrix in the computational basis. @@ -142,13 +142,16 @@ def measure_density_matrix( num_qubits = len(qid_shape) meas_shape = _indices_shape(qid_shape, indices) + arrout: np.ndarray = ( + np.copy(density_matrix) + if out is None + else density_matrix + if out is density_matrix + else (np.copyto(dst=out, src=density_matrix), out)[-1] + ) + if len(indices) == 0: - if out is None: - out = np.copy(density_matrix) - elif out is not density_matrix: - np.copyto(dst=out, src=density_matrix) - return ([], out) - # Final else: if out is matrix then matrix will be modified in place. + return ([], arrout) prng = value.parse_random_state(seed) @@ -169,21 +172,15 @@ def measure_density_matrix( # Remove ellipses from last element of mask[result_slice * 2] = False - if out is None: - out = np.copy(density_matrix) - elif out is not density_matrix: - np.copyto(dst=out, src=density_matrix) - # Final else: if out is matrix then matrix will be modified in place. - # Potentially reshape to tensor, and then set masked values to 0. - out.shape = qid_shape * 2 - out[mask] = 0 + arrout.shape = qid_shape * 2 + arrout[mask] = 0 # Restore original shape (if necessary) and renormalize. - out.shape = initial_shape - out /= probs[result] + arrout.shape = initial_shape + arrout /= probs[result] - return measurement_bits, out + return measurement_bits, arrout def _probs( From 31035c6dc7e29250981f9915c3161535502b5f57 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Wed, 22 Jun 2022 15:36:55 -0700 Subject: [PATCH 2/3] Replace type spec Type[np.number] --> np.typing.DTypeLike --- cirq-core/cirq/circuits/circuit.py | 7 +- .../sim/density_matrix_simulation_state.py | 2 +- .../cirq/sim/density_matrix_simulator_test.py | 90 ++++++++++--------- cirq-core/cirq/sim/mux.py | 9 +- cirq-core/cirq/sim/simulator_base.py | 3 +- cirq-core/cirq/sim/sparse_simulator.py | 2 +- cirq-core/cirq/sim/sparse_simulator_test.py | 75 ++++++++-------- .../cirq/sim/state_vector_simulation_state.py | 2 +- cirq-core/cirq/sim/state_vector_simulator.py | 3 +- 9 files changed, 100 insertions(+), 93 deletions(-) diff --git a/cirq-core/cirq/circuits/circuit.py b/cirq-core/cirq/circuits/circuit.py index c6b00726a2c..31c36da6191 100644 --- a/cirq-core/cirq/circuits/circuit.py +++ b/cirq-core/cirq/circuits/circuit.py @@ -64,6 +64,7 @@ if TYPE_CHECKING: import cirq + from numpy.typing import DTypeLike _TGate = TypeVar('_TGate', bound='cirq.Gate') @@ -998,7 +999,7 @@ def unitary( qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT, qubits_that_should_be_present: Iterable['cirq.Qid'] = (), ignore_terminal_measurements: bool = True, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, ) -> np.ndarray: """Converts the circuit into a unitary matrix, if possible. @@ -1088,7 +1089,7 @@ def final_state_vector( qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT, qubits_that_should_be_present: Iterable['cirq.Qid'] = (), ignore_terminal_measurements: Optional[bool] = None, - dtype: Optional[Type[np.number]] = None, + dtype: Optional['DTypeLike'] = None, param_resolver: 'cirq.ParamResolverOrSimilarType' = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> np.ndarray: @@ -2589,7 +2590,7 @@ def _apply_unitary_circuit( circuit: 'cirq.AbstractCircuit', state: np.ndarray, qubits: Tuple['cirq.Qid', ...], - dtype: Type[np.number], + dtype: 'DTypeLike', ) -> np.ndarray: """Applies a circuit's unitary effect to the given vector or matrix. diff --git a/cirq-core/cirq/sim/density_matrix_simulation_state.py b/cirq-core/cirq/sim/density_matrix_simulation_state.py index 1486be355b4..d31e5920762 100644 --- a/cirq-core/cirq/sim/density_matrix_simulation_state.py +++ b/cirq-core/cirq/sim/density_matrix_simulation_state.py @@ -252,7 +252,7 @@ def __init__( prng: Optional[np.random.RandomState] = None, qubits: Optional[Sequence['cirq.Qid']] = None, initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE'] = 0, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, classical_data: Optional['cirq.ClassicalDataStore'] = None, ): """Inits DensityMatrixSimulationState. diff --git a/cirq-core/cirq/sim/density_matrix_simulator_test.py b/cirq-core/cirq/sim/density_matrix_simulator_test.py index ffe3eabb58c..dc5a9a90998 100644 --- a/cirq-core/cirq/sim/density_matrix_simulator_test.py +++ b/cirq-core/cirq/sim/density_matrix_simulator_test.py @@ -20,6 +20,8 @@ import pytest import sympy +from numpy.typing import DTypeLike + import cirq import cirq.testing @@ -53,7 +55,7 @@ def test_invalid_dtype(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_no_measurements(dtype: Type[np.number], split: bool): +def test_run_no_measurements(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -64,7 +66,7 @@ def test_run_no_measurements(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_no_results(dtype: Type[np.number], split: bool): +def test_run_no_results(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -75,7 +77,7 @@ def test_run_no_results(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_empty_circuit(dtype: Type[np.number], split: bool): +def test_run_empty_circuit(dtype: DTypeLike, split: bool): simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with pytest.raises(ValueError, match="no measurements"): simulator.run(cirq.Circuit()) @@ -83,7 +85,7 @@ def test_run_empty_circuit(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_bit_flips(dtype: Type[np.number], split: bool): +def test_run_bit_flips(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -97,7 +99,7 @@ def test_run_bit_flips(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_bit_flips_with_dephasing(dtype: Type[np.number], split: bool): +def test_run_bit_flips_with_dephasing(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -111,7 +113,7 @@ def test_run_bit_flips_with_dephasing(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudit_increments(dtype: Type[np.number], split: bool): +def test_run_qudit_increments(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((3, 4)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1, 2]: @@ -130,7 +132,7 @@ def test_run_qudit_increments(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_not_channel_op(dtype: Type[np.number], split: bool): +def test_run_not_channel_op(dtype: DTypeLike, split: bool): class BadOp(cirq.Operation): def __init__(self, qubits): self._qubits = qubits @@ -152,7 +154,7 @@ def with_qubits(self, *new_qubits): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_mixture(dtype: Type[np.number], split: bool): +def test_run_mixture(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.bit_flip(0.5)(q0), cirq.measure(q0), cirq.measure(q1)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -166,7 +168,7 @@ def test_run_mixture(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudit_mixture(dtype: Type[np.number], split: bool): +def test_run_qudit_mixture(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((3, 2)) mixture = _TestMixture( [ @@ -187,7 +189,7 @@ def test_run_qudit_mixture(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_channel(dtype: Type[np.number], split: bool): +def test_run_channel(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit( cirq.X(q0), cirq.amplitude_damp(0.5)(q0), cirq.measure(q0), cirq.measure(q1) @@ -204,7 +206,7 @@ def test_run_channel(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_decomposable_channel(dtype: Type[np.number], split: bool): +def test_run_decomposable_channel(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit( @@ -225,7 +227,7 @@ def test_run_decomposable_channel(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudit_channel(dtype: Type[np.number], split: bool): +def test_run_qudit_channel(dtype: DTypeLike, split: bool): class TestChannel(cirq.Gate): def _qid_shape_(self): return (3,) @@ -257,7 +259,7 @@ def _kraus_(self): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_at_end_no_repetitions(dtype: Type[np.number], split: bool): +def test_run_measure_at_end_no_repetitions(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -276,7 +278,7 @@ def test_run_measure_at_end_no_repetitions(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_repetitions_measure_at_end(dtype: Type[np.number], split: bool): +def test_run_repetitions_measure_at_end(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -295,7 +297,7 @@ def test_run_repetitions_measure_at_end(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudits_repetitions_measure_at_end(dtype: Type[np.number], split: bool): +def test_run_qudits_repetitions_measure_at_end(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -317,7 +319,7 @@ def test_run_qudits_repetitions_measure_at_end(dtype: Type[np.number], split: bo @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measurement_not_terminal_no_repetitions(dtype: Type[np.number], split: bool): +def test_run_measurement_not_terminal_no_repetitions(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -341,7 +343,7 @@ def test_run_measurement_not_terminal_no_repetitions(dtype: Type[np.number], spl @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_repetitions_measurement_not_terminal(dtype: Type[np.number], split: bool): +def test_run_repetitions_measurement_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -365,7 +367,7 @@ def test_run_repetitions_measurement_not_terminal(dtype: Type[np.number], split: @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_qudits_repetitions_measurement_not_terminal(dtype: Type[np.number], split: bool): +def test_run_qudits_repetitions_measurement_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -389,7 +391,7 @@ def test_run_qudits_repetitions_measurement_not_terminal(dtype: Type[np.number], @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_param_resolver(dtype: Type[np.number], split: bool): +def test_run_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -409,7 +411,7 @@ def test_run_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_correlations(dtype: Type[np.number], split: bool): +def test_run_correlations(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1)) @@ -421,7 +423,7 @@ def test_run_correlations(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_multiple_qubits(dtype: Type[np.number], split: bool): +def test_run_measure_multiple_qubits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -433,7 +435,7 @@ def test_run_measure_multiple_qubits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_multiple_qudits(dtype: Type[np.number], split: bool): +def test_run_measure_multiple_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -447,7 +449,7 @@ def test_run_measure_multiple_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_sweeps_param_resolvers(dtype: Type[np.number], split: bool): +def test_run_sweeps_param_resolvers(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -473,7 +475,7 @@ def test_run_sweeps_param_resolvers(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_no_circuit(dtype: Type[np.number], split: bool): +def test_simulate_no_circuit(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit() @@ -486,7 +488,7 @@ def test_simulate_no_circuit(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate(dtype: Type[np.number], split: bool): +def test_simulate(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1)) @@ -497,7 +499,7 @@ def test_simulate(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qudits(dtype: Type[np.number], split: bool): +def test_simulate_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.XPowGate(dimension=3)(q1) ** 2) @@ -511,7 +513,7 @@ def test_simulate_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) def test_reset_one_qubit_does_not_affect_partial_trace_of_other_qubits( - dtype: Type[np.number], split: bool + dtype: DTypeLike, split: bool ): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -530,7 +532,7 @@ def test_reset_one_qubit_does_not_affect_partial_trace_of_other_qubits( [cirq.testing.random_circuit(cirq.LineQubit.range(4), 5, 0.9) for _ in range(20)], ), ) -def test_simulate_compare_to_state_vector_simulator(dtype: Type[np.number], circuit): +def test_simulate_compare_to_state_vector_simulator(dtype: DTypeLike, circuit): qubits = cirq.LineQubit.range(4) pure_result = ( cirq.Simulator(dtype=dtype).simulate(circuit, qubit_order=qubits).density_matrix_of() @@ -546,7 +548,7 @@ def test_simulate_compare_to_state_vector_simulator(dtype: Type[np.number], circ @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_bit_flips(dtype: Type[np.number], split: bool): +def test_simulate_bit_flips(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -563,7 +565,7 @@ def test_simulate_bit_flips(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qudit_increments(dtype: Type[np.number], split: bool): +def test_simulate_qudit_increments(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -583,7 +585,7 @@ def test_simulate_qudit_increments(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_initial_state(dtype: Type[np.number], split: bool): +def test_simulate_initial_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -597,7 +599,7 @@ def test_simulate_initial_state(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulation_state(dtype: Type[np.number], split: bool): +def test_simulation_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -624,7 +626,7 @@ def test_simulate_tps_initial_state(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_initial_qudit_state(dtype: Type[np.number], split: bool): +def test_simulate_initial_qudit_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((3, 4)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1, 2]: @@ -644,7 +646,7 @@ def test_simulate_initial_qudit_state(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qubit_order(dtype: Type[np.number], split: bool): +def test_simulate_qubit_order(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -658,7 +660,7 @@ def test_simulate_qubit_order(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_param_resolver(dtype: Type[np.number], split: bool): +def test_simulate_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -677,7 +679,7 @@ def test_simulate_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_measure_multiple_qubits(dtype: Type[np.number], split: bool): +def test_simulate_measure_multiple_qubits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -689,7 +691,7 @@ def test_simulate_measure_multiple_qubits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_measure_multiple_qudits(dtype: Type[np.number], split: bool): +def test_simulate_measure_multiple_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -703,7 +705,7 @@ def test_simulate_measure_multiple_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_sweeps_param_resolver(dtype: Type[np.number], split: bool): +def test_simulate_sweeps_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -730,7 +732,7 @@ def test_simulate_sweeps_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.H(q0), cirq.H(q1)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -744,7 +746,7 @@ def test_simulate_moment_steps(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_qudits(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) circuit = cirq.Circuit( cirq.XPowGate(dimension=2)(q0), @@ -765,7 +767,7 @@ def test_simulate_moment_steps_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_empty_circuit(dtype: DTypeLike, split: bool): circuit = cirq.Circuit() simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) step = None @@ -777,7 +779,7 @@ def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_sample(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_sample(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) @@ -798,7 +800,7 @@ def test_simulate_moment_steps_sample(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_sample_qudits(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_sample_qudits(dtype: DTypeLike, split: bool): class TestGate(cirq.Gate): """Swaps the 2nd qid |0> and |2> states when the 1st is |1>.""" @@ -827,7 +829,7 @@ def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_intermediate_measurement(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_intermediate_measurement(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0), cirq.H(q0)) simulator = cirq.DensityMatrixSimulator(dtype=dtype, split_untangled_states=split) diff --git a/cirq-core/cirq/sim/mux.py b/cirq-core/cirq/sim/mux.py index c0e357284de..0d33c744a60 100644 --- a/cirq-core/cirq/sim/mux.py +++ b/cirq-core/cirq/sim/mux.py @@ -29,6 +29,7 @@ if TYPE_CHECKING: import cirq + from numpy.typing import DTypeLike CIRCUIT_LIKE = Union[circuits.Circuit, ops.Gate, ops.OP_TREE] document( @@ -52,7 +53,7 @@ def sample( noise: 'cirq.NOISE_MODEL_LIKE' = None, param_resolver: Optional['cirq.ParamResolver'] = None, repetitions: int = 1, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> 'cirq.Result': """Simulates sampling from the given circuit. @@ -107,7 +108,7 @@ def final_state_vector( param_resolver: 'cirq.ParamResolverOrSimilarType' = None, qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT, ignore_terminal_measurements: bool = False, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> 'np.ndarray': """Returns the state vector resulting from acting operations on a state. @@ -177,7 +178,7 @@ def sample_sweep( *, noise: 'cirq.NOISE_MODEL_LIKE' = None, repetitions: int = 1, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> Sequence['cirq.Result']: """Runs the supplied Circuit, mimicking quantum hardware. @@ -223,7 +224,7 @@ def final_density_matrix( initial_state: 'cirq.STATE_VECTOR_LIKE' = 0, param_resolver: 'cirq.ParamResolverOrSimilarType' = None, qubit_order: 'cirq.QubitOrderOrList' = ops.QubitOrder.DEFAULT, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, seed: Optional[Union[int, np.random.RandomState]] = None, ignore_measurement_results: bool = True, ) -> 'np.ndarray': diff --git a/cirq-core/cirq/sim/simulator_base.py b/cirq-core/cirq/sim/simulator_base.py index 38a7941a0be..19fbfecb205 100644 --- a/cirq-core/cirq/sim/simulator_base.py +++ b/cirq-core/cirq/sim/simulator_base.py @@ -50,6 +50,7 @@ if TYPE_CHECKING: import cirq + from numpy.typing import DTypeLike TStepResultBase = TypeVar('TStepResultBase', bound='StepResultBase') @@ -93,7 +94,7 @@ class SimulatorBase( def __init__( self, *, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, noise: 'cirq.NOISE_MODEL_LIKE' = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, split_untangled_states: bool = False, diff --git a/cirq-core/cirq/sim/sparse_simulator.py b/cirq-core/cirq/sim/sparse_simulator.py index d3c32d2022e..2bdad9d03bc 100644 --- a/cirq-core/cirq/sim/sparse_simulator.py +++ b/cirq-core/cirq/sim/sparse_simulator.py @@ -127,7 +127,7 @@ class Simulator( def __init__( self, *, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, noise: 'cirq.NOISE_MODEL_LIKE' = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, split_untangled_states: bool = True, diff --git a/cirq-core/cirq/sim/sparse_simulator_test.py b/cirq-core/cirq/sim/sparse_simulator_test.py index 14966a4bdd2..5168f8f4c7b 100644 --- a/cirq-core/cirq/sim/sparse_simulator_test.py +++ b/cirq-core/cirq/sim/sparse_simulator_test.py @@ -16,6 +16,7 @@ from typing import Type from unittest import mock import numpy as np +from numpy.typing import DTypeLike import pytest import sympy @@ -29,7 +30,7 @@ def test_invalid_dtype(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_no_measurements(dtype: Type[np.number], split: bool): +def test_run_no_measurements(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -40,7 +41,7 @@ def test_run_no_measurements(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_no_results(dtype: Type[np.number], split: bool): +def test_run_no_results(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -51,7 +52,7 @@ def test_run_no_results(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_empty_circuit(dtype: Type[np.number], split: bool): +def test_run_empty_circuit(dtype: DTypeLike, split: bool): simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with pytest.raises(ValueError, match="no measurements"): simulator.run(cirq.Circuit()) @@ -59,7 +60,7 @@ def test_run_empty_circuit(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_reset(dtype: Type[np.number], split: bool): +def test_run_reset(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((2, 3)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit( @@ -79,7 +80,7 @@ def test_run_reset(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_bit_flips(dtype: Type[np.number], split: bool): +def test_run_bit_flips(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -93,7 +94,7 @@ def test_run_bit_flips(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_at_end_no_repetitions(dtype: Type[np.number], split: bool): +def test_run_measure_at_end_no_repetitions(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -119,7 +120,7 @@ def test_run_repetitions_terminal_measurement_stochastic(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_repetitions_measure_at_end(dtype: Type[np.number], split: bool): +def test_run_repetitions_measure_at_end(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -139,7 +140,7 @@ def test_run_repetitions_measure_at_end(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_invert_mask_measure_not_terminal(dtype: Type[np.number], split: bool): +def test_run_invert_mask_measure_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -160,7 +161,7 @@ def test_run_invert_mask_measure_not_terminal(dtype: Type[np.number], split: boo @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_partial_invert_mask_measure_not_terminal(dtype: Type[np.number], split: bool): +def test_run_partial_invert_mask_measure_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -181,7 +182,7 @@ def test_run_partial_invert_mask_measure_not_terminal(dtype: Type[np.number], sp @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measurement_not_terminal_no_repetitions(dtype: Type[np.number], split: bool): +def test_run_measurement_not_terminal_no_repetitions(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -205,7 +206,7 @@ def test_run_measurement_not_terminal_no_repetitions(dtype: Type[np.number], spl @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_repetitions_measurement_not_terminal(dtype: Type[np.number], split: bool): +def test_run_repetitions_measurement_not_terminal(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) with mock.patch.object(simulator, '_core_iterator', wraps=simulator._core_iterator) as mock_sim: @@ -230,7 +231,7 @@ def test_run_repetitions_measurement_not_terminal(dtype: Type[np.number], split: @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_param_resolver(dtype: Type[np.number], split: bool): +def test_run_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -249,7 +250,7 @@ def test_run_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_mixture(dtype: Type[np.number], split: bool): +def test_run_mixture(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.bit_flip(0.5)(q0), cirq.measure(q0)) @@ -259,7 +260,7 @@ def test_run_mixture(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_mixture_with_gates(dtype: Type[np.number], split: bool): +def test_run_mixture_with_gates(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split, seed=23) circuit = cirq.Circuit(cirq.H(q0), cirq.phase_flip(0.5)(q0), cirq.H(q0), cirq.measure(q0)) @@ -270,7 +271,7 @@ def test_run_mixture_with_gates(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_correlations(dtype: Type[np.number], split: bool): +def test_run_correlations(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1)) @@ -282,7 +283,7 @@ def test_run_correlations(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_measure_multiple_qubits(dtype: Type[np.number], split: bool): +def test_run_measure_multiple_qubits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -294,7 +295,7 @@ def test_run_measure_multiple_qubits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_run_sweeps_param_resolvers(dtype: Type[np.number], split: bool): +def test_run_sweeps_param_resolvers(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -320,7 +321,7 @@ def test_run_sweeps_param_resolvers(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_random_unitary(dtype: Type[np.number], split: bool): +def test_simulate_random_unitary(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for _ in range(10): @@ -336,7 +337,7 @@ def test_simulate_random_unitary(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_no_circuit(dtype: Type[np.number], split: bool): +def test_simulate_no_circuit(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit() @@ -347,7 +348,7 @@ def test_simulate_no_circuit(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate(dtype: Type[np.number], split: bool): +def test_simulate(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1)) @@ -369,7 +370,7 @@ def _mixture_(self): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qudits(dtype: Type[np.number], split: bool): +def test_simulate_qudits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQid.for_qid_shape((3, 4)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.XPowGate(dimension=3)(q0), cirq.XPowGate(dimension=4)(q1) ** 3) @@ -382,7 +383,7 @@ def test_simulate_qudits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_mixtures(dtype: Type[np.number], split: bool): +def test_simulate_mixtures(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) circuit = cirq.Circuit(cirq.bit_flip(0.5)(q0), cirq.measure(q0)) @@ -400,7 +401,7 @@ def test_simulate_mixtures(dtype: Type[np.number], split: bool): @pytest.mark.parametrize( 'dtype, split', itertools.product([np.complex64, np.complex128], [True, False]) ) -def test_simulate_qudit_mixtures(dtype: Type[np.number], split: bool): +def test_simulate_qudit_mixtures(dtype: DTypeLike, split: bool): q0 = cirq.LineQid(0, 3) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) mixture = _TestMixture( @@ -426,7 +427,7 @@ def test_simulate_qudit_mixtures(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_bit_flips(dtype: Type[np.number], split: bool): +def test_simulate_bit_flips(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -443,7 +444,7 @@ def test_simulate_bit_flips(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_initial_state(dtype: Type[np.number], split: bool): +def test_simulate_initial_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -457,7 +458,7 @@ def test_simulate_initial_state(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulation_state(dtype: Type[np.number], split: bool): +def test_simulation_state(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -472,7 +473,7 @@ def test_simulation_state(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_qubit_order(dtype: Type[np.number], split: bool): +def test_simulate_qubit_order(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -486,7 +487,7 @@ def test_simulate_qubit_order(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_param_resolver(dtype: Type[np.number], split: bool): +def test_simulate_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -505,7 +506,7 @@ def test_simulate_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_measure_multiple_qubits(dtype: Type[np.number], split: bool): +def test_simulate_measure_multiple_qubits(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -517,7 +518,7 @@ def test_simulate_measure_multiple_qubits(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_sweeps_param_resolver(dtype: Type[np.number], split: bool): +def test_simulate_sweeps_param_resolver(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) for b0 in [0, 1]: @@ -544,7 +545,7 @@ def test_simulate_sweeps_param_resolver(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.H(q0), cirq.H(q1)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -568,7 +569,7 @@ def test_simulate_moment_steps_implicit_copy_deprecated(): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_empty_circuit(dtype: DTypeLike, split: bool): circuit = cirq.Circuit() simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) step = None @@ -580,7 +581,7 @@ def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_sample(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_sample(dtype: DTypeLike, split: bool): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -601,7 +602,7 @@ def test_simulate_moment_steps_sample(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_moment_steps_intermediate_measurement(dtype: Type[np.number], split: bool): +def test_simulate_moment_steps_intermediate_measurement(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0), cirq.H(q0)) simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split) @@ -618,7 +619,7 @@ def test_simulate_moment_steps_intermediate_measurement(dtype: Type[np.number], @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_expectation_values(dtype: Type[np.number], split: bool): +def test_simulate_expectation_values(dtype: DTypeLike, split: bool): # Compare with test_expectation_from_state_vector_two_qubit_states # in file: cirq/ops/linear_combinations_test.py q0, q1 = cirq.LineQubit.range(2) @@ -643,7 +644,7 @@ def test_simulate_expectation_values(dtype: Type[np.number], split: bool): @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_expectation_values_terminal_measure(dtype: Type[np.number], split: bool): +def test_simulate_expectation_values_terminal_measure(dtype: DTypeLike, split: bool): q0 = cirq.LineQubit(0) circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0)) obs = cirq.Z(q0) @@ -681,7 +682,7 @@ def test_simulate_expectation_values_terminal_measure(dtype: Type[np.number], sp @pytest.mark.parametrize('dtype', [np.complex64, np.complex128]) @pytest.mark.parametrize('split', [True, False]) -def test_simulate_expectation_values_qubit_order(dtype: Type[np.number], split: bool): +def test_simulate_expectation_values_qubit_order(dtype: DTypeLike, split: bool): q0, q1, q2 = cirq.LineQubit.range(3) circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.X(q2)) obs = cirq.X(q0) + cirq.X(q1) - cirq.Z(q2) diff --git a/cirq-core/cirq/sim/state_vector_simulation_state.py b/cirq-core/cirq/sim/state_vector_simulation_state.py index 1111538f5ee..7cf36dba1f5 100644 --- a/cirq-core/cirq/sim/state_vector_simulation_state.py +++ b/cirq-core/cirq/sim/state_vector_simulation_state.py @@ -326,7 +326,7 @@ def __init__( prng: Optional[np.random.RandomState] = None, qubits: Optional[Sequence['cirq.Qid']] = None, initial_state: Union[np.ndarray, 'cirq.STATE_VECTOR_LIKE'] = 0, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, classical_data: Optional['cirq.ClassicalDataStore'] = None, ): """Inits StateVectorSimulationState. diff --git a/cirq-core/cirq/sim/state_vector_simulator.py b/cirq-core/cirq/sim/state_vector_simulator.py index 44c9aa1a10c..d02ea28616b 100644 --- a/cirq-core/cirq/sim/state_vector_simulator.py +++ b/cirq-core/cirq/sim/state_vector_simulator.py @@ -35,6 +35,7 @@ if TYPE_CHECKING: import cirq + from numpy.typing import DTypeLike TStateVectorStepResult = TypeVar('TStateVectorStepResult', bound='StateVectorStepResult') @@ -56,7 +57,7 @@ class SimulatesIntermediateStateVector( def __init__( self, *, - dtype: Type[np.number] = np.complex64, + dtype: 'DTypeLike' = np.complex64, noise: 'cirq.NOISE_MODEL_LIKE' = None, seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, split_untangled_states: bool = False, From 62951cdcb205e01dfa1630c4ff72a8a8dd7a5ce3 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Wed, 22 Jun 2022 17:35:37 -0700 Subject: [PATCH 3/3] pylint - clean unused imports --- .../cirq/sim/density_matrix_simulation_state.py | 2 +- cirq-core/cirq/sim/density_matrix_simulator_test.py | 1 - cirq-core/cirq/sim/mux.py | 2 +- cirq-core/cirq/sim/simulator_base.py | 1 - cirq-core/cirq/sim/sparse_simulator.py | 2 +- cirq-core/cirq/sim/sparse_simulator_test.py | 1 - cirq-core/cirq/sim/state_vector_simulation_state.py | 2 +- cirq-core/cirq/sim/state_vector_simulator.py | 13 +------------ 8 files changed, 5 insertions(+), 19 deletions(-) diff --git a/cirq-core/cirq/sim/density_matrix_simulation_state.py b/cirq-core/cirq/sim/density_matrix_simulation_state.py index d31e5920762..ad1c969364e 100644 --- a/cirq-core/cirq/sim/density_matrix_simulation_state.py +++ b/cirq-core/cirq/sim/density_matrix_simulation_state.py @@ -13,7 +13,7 @@ # limitations under the License. """Objects and methods for acting efficiently on a density matrix.""" -from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Type, Union +from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union import numpy as np diff --git a/cirq-core/cirq/sim/density_matrix_simulator_test.py b/cirq-core/cirq/sim/density_matrix_simulator_test.py index dc5a9a90998..42dffbf4d40 100644 --- a/cirq-core/cirq/sim/density_matrix_simulator_test.py +++ b/cirq-core/cirq/sim/density_matrix_simulator_test.py @@ -13,7 +13,6 @@ # limitations under the License. import itertools import random -from typing import Type from unittest import mock import numpy as np diff --git a/cirq-core/cirq/sim/mux.py b/cirq-core/cirq/sim/mux.py index 0d33c744a60..288e7454af5 100644 --- a/cirq-core/cirq/sim/mux.py +++ b/cirq-core/cirq/sim/mux.py @@ -17,7 +17,7 @@ Filename is a reference to multiplexing. """ -from typing import cast, List, Optional, Sequence, Type, TYPE_CHECKING, Union +from typing import cast, List, Optional, Sequence, TYPE_CHECKING, Union import numpy as np diff --git a/cirq-core/cirq/sim/simulator_base.py b/cirq-core/cirq/sim/simulator_base.py index 19fbfecb205..18a6b2d19d9 100644 --- a/cirq-core/cirq/sim/simulator_base.py +++ b/cirq-core/cirq/sim/simulator_base.py @@ -26,7 +26,6 @@ Optional, Sequence, Tuple, - Type, TypeVar, TYPE_CHECKING, ) diff --git a/cirq-core/cirq/sim/sparse_simulator.py b/cirq-core/cirq/sim/sparse_simulator.py index 2bdad9d03bc..932c231526e 100644 --- a/cirq-core/cirq/sim/sparse_simulator.py +++ b/cirq-core/cirq/sim/sparse_simulator.py @@ -14,7 +14,7 @@ """A simulator that uses numpy's einsum for sparse matrix operations.""" -from typing import Any, Iterator, List, Type, TYPE_CHECKING, Union, Sequence, Optional +from typing import Any, Iterator, List, TYPE_CHECKING, Union, Sequence, Optional import numpy as np diff --git a/cirq-core/cirq/sim/sparse_simulator_test.py b/cirq-core/cirq/sim/sparse_simulator_test.py index 5168f8f4c7b..6bfc7aa3005 100644 --- a/cirq-core/cirq/sim/sparse_simulator_test.py +++ b/cirq-core/cirq/sim/sparse_simulator_test.py @@ -13,7 +13,6 @@ # limitations under the License. import itertools import random -from typing import Type from unittest import mock import numpy as np from numpy.typing import DTypeLike diff --git a/cirq-core/cirq/sim/state_vector_simulation_state.py b/cirq-core/cirq/sim/state_vector_simulation_state.py index 7cf36dba1f5..1bfd7d62226 100644 --- a/cirq-core/cirq/sim/state_vector_simulation_state.py +++ b/cirq-core/cirq/sim/state_vector_simulation_state.py @@ -13,7 +13,7 @@ # limitations under the License. """Objects and methods for acting efficiently on a state vector.""" -from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Type, Union +from typing import Any, Callable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union import numpy as np diff --git a/cirq-core/cirq/sim/state_vector_simulator.py b/cirq-core/cirq/sim/state_vector_simulator.py index d02ea28616b..d2e9c86f0c8 100644 --- a/cirq-core/cirq/sim/state_vector_simulator.py +++ b/cirq-core/cirq/sim/state_vector_simulator.py @@ -14,18 +14,7 @@ """Abstract classes for simulations which keep track of state vector.""" import abc -from typing import ( - Any, - Dict, - Iterator, - Sequence, - TYPE_CHECKING, - Tuple, - Generic, - TypeVar, - Type, - Optional, -) +from typing import Any, Dict, Iterator, Sequence, TYPE_CHECKING, Tuple, Generic, TypeVar, Optional import numpy as np