Skip to content

Commit

Permalink
Encap public fields in cirq.sim (#5320)
Browse files Browse the repository at this point in the history
* Hide public fields from Step/Trial Reesult

* Also encap noise field
  • Loading branch information
daxfohl authored May 11, 2022
1 parent 4ba730b commit 21eb8f7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
46 changes: 41 additions & 5 deletions cirq-core/cirq/sim/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}, '
Expand Down
14 changes: 13 additions & 1 deletion cirq-core/cirq/sim/simulator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/sim/simulator_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions cirq-core/cirq/sim/simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 21eb8f7

Please sign in to comment.