diff --git a/cirq-core/cirq/sim/simulator.py b/cirq-core/cirq/sim/simulator.py index 3e09a1e8cc2..6d5ad00d7e5 100644 --- a/cirq-core/cirq/sim/simulator.py +++ b/cirq-core/cirq/sim/simulator.py @@ -789,7 +789,19 @@ class StepResult(Generic[TSimulatorState], metaclass=abc.ABCMeta): def __init__(self, sim_state: TSimulatorState) -> None: self._sim_state = sim_state - self.measurements = sim_state.log_of_measurement_results + self._measurements = sim_state.log_of_measurement_results + + @property + def measurements(self) -> Mapping[str, Sequence[int]]: + return self._measurements + + @measurements.setter # type: ignore + @_compat.deprecated( + deadline="v0.16", + fix="The mutators of this class are deprecated, instantiate a new object instead.", + ) + def measurements(self, measurements: Mapping[str, Sequence[int]]): + self._measurements = measurements def _simulator_state(self) -> TSimulatorState: """Returns the simulator state of the simulator after this step. @@ -986,23 +998,47 @@ class SimulationTrialResult(Generic[TSimulatorState]): def __init__( self, params: 'cirq.ParamResolver', - measurements: Dict[str, np.ndarray], + measurements: Mapping[str, np.ndarray], final_simulator_state: TSimulatorState, ) -> None: """Initializes the `SimulationTrialResult` class. Args: params: A ParamResolver of settings used for this result. - measurements: A dictionary from measurement gate key to measurement + measurements: A mapping from measurement gate key to measurement results. Measurement results are a numpy ndarray of actual boolean measurement results (ordered by the qubits acted on by the measurement gate.) final_simulator_state: The final simulator state. """ - self.params = params - self.measurements = measurements + self._params = params + self._measurements = measurements self._final_simulator_state = final_simulator_state + @property + def params(self) -> 'cirq.ParamResolver': + return self._params + + @params.setter # type: ignore + @_compat.deprecated( + deadline='v0.16', + fix='The mutators of this class are deprecated, instantiate a new object instead.', + ) + def params(self, params: 'cirq.ParamResolver'): + self._params = params + + @property + def measurements(self) -> Mapping[str, np.ndarray]: + return self._measurements + + @measurements.setter # type: ignore + @_compat.deprecated( + deadline="v0.16", + fix="The mutators of this class are deprecated, instantiate a new object instead.", + ) + def measurements(self, measurements: Mapping[str, np.ndarray]): + self._measurements = measurements + def __repr__(self) -> str: return ( f'cirq.SimulationTrialResult(params={self.params!r}, ' diff --git a/cirq-core/cirq/sim/simulator_base.py b/cirq-core/cirq/sim/simulator_base.py index c4316ccf111..a8bae957bfd 100644 --- a/cirq-core/cirq/sim/simulator_base.py +++ b/cirq-core/cirq/sim/simulator_base.py @@ -110,9 +110,21 @@ def __init__( """ self._dtype = dtype self._prng = value.parse_random_state(seed) - self.noise = devices.NoiseModel.from_noise_model_like(noise) + self._noise = devices.NoiseModel.from_noise_model_like(noise) self._split_untangled_states = split_untangled_states + @property + def noise(self) -> 'cirq.NoiseModel': + return self._noise + + @noise.setter # type: ignore + @_compat.deprecated( + deadline="v0.16", + fix="The mutators of this class are deprecated, instantiate a new object instead.", + ) + def noise(self, noise: 'cirq.NoiseModel'): + self._noise = noise + def _create_partial_act_on_args( self, initial_state: Any, diff --git a/cirq-core/cirq/sim/simulator_base_test.py b/cirq-core/cirq/sim/simulator_base_test.py index b9318b442d3..1b976e74be5 100644 --- a/cirq-core/cirq/sim/simulator_base_test.py +++ b/cirq-core/cirq/sim/simulator_base_test.py @@ -441,3 +441,9 @@ def _create_simulator_trial_result(self): sim = DeprecatedSim() with cirq.testing.assert_deprecated(deadline='v0.16'): sim.simulate_moment_steps(cirq.Circuit()) + + +def test_deprecated_setters(): + sim = CountingSimulator() + with cirq.testing.assert_deprecated(deadline='v0.16'): + sim.noise = cirq.ConstantQubitNoiseModel(cirq.Z) diff --git a/cirq-core/cirq/sim/simulator_test.py b/cirq-core/cirq/sim/simulator_test.py index 59b4d99e5d9..5c010df9708 100644 --- a/cirq-core/cirq/sim/simulator_test.py +++ b/cirq-core/cirq/sim/simulator_test.py @@ -555,3 +555,14 @@ def _create_simulator_trial_result(self): sim = DeprecatedSim() with cirq.testing.assert_deprecated(deadline='v0.16'): sim.simulate_moment_steps(cirq.Circuit()) + + +def test_deprecated_setters(): + step = FakeStepResult() + result = cirq.SimulationTrialResult(cirq.ParamResolver(), {}, 0) + with cirq.testing.assert_deprecated(deadline='v0.16'): + step.measurements = {} + with cirq.testing.assert_deprecated(deadline='v0.16'): + result.measurements = {} + with cirq.testing.assert_deprecated(deadline='v0.16'): + result.params = cirq.ParamResolver()