Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate set_state_vector and set_density_matrix #4906

Merged
merged 3 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cirq-core/cirq/sim/density_matrix_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np

from cirq import ops, protocols, qis, study, value
from cirq._compat import proper_repr
from cirq._compat import deprecated, proper_repr
from cirq.sim import (
simulator,
act_on_density_matrix_args,
Expand Down Expand Up @@ -302,6 +302,11 @@ def __init__(
def _simulator_state(self) -> 'cirq.DensityMatrixSimulatorState':
return DensityMatrixSimulatorState(self.density_matrix(copy=False), self._qubit_mapping)

# TODO: When removing, also remove `simulator` from the constructor, and the line
# `sim_state = step_result._sim_state` from `SimulatorBase._core_iterator()`.
@deprecated(
deadline="v0.15", fix='Use `initial_state` to prepare a new simulation on the suffix.'
)
def set_density_matrix(self, density_matrix_repr: Union[int, np.ndarray]):
"""Set the density matrix to a new density matrix.

Expand Down
5 changes: 3 additions & 2 deletions cirq-core/cirq/sim/density_matrix_simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
def test_simulate_moment_steps_set_state(dtype: Type[np.number]):
def test_simulate_moment_steps_set_state_deprecated(dtype: Type[np.number]):
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)
Expand All @@ -850,7 +850,8 @@ def test_simulate_moment_steps_set_state(dtype: Type[np.number]):
if i == 0:
zero_zero = np.zeros((4, 4), dtype=dtype)
zero_zero[0, 0] = 1
step.set_density_matrix(zero_zero)
with cirq.testing.assert_deprecated('initial_state', deadline='v0.15'):
step.set_density_matrix(zero_zero)


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/sim/sparse_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import numpy as np

from cirq import ops, protocols, qis
from cirq._compat import deprecated
from cirq.sim import (
simulator,
state_vector,
Expand Down Expand Up @@ -316,6 +317,11 @@ def state_vector(self, copy: bool = True):
self._state_vector = np.reshape(vector, size)
return self._state_vector.copy() if copy else self._state_vector

# TODO: When removing, also remove `simulator` from the constructor, and the line
# `sim_state = step_result._sim_state` from `SimulatorBase._core_iterator()`.
@deprecated(
deadline="v0.15", fix='Use `initial_state` to prepare a new simulation on the suffix.'
)
def set_state_vector(self, state: 'cirq.STATE_VECTOR_LIKE'):
"""Set the state vector.

Expand Down
5 changes: 3 additions & 2 deletions cirq-core/cirq/sim/sparse_simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,14 +580,15 @@ def test_simulate_moment_steps_empty_circuit(dtype: Type[np.number], split: bool


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
def test_simulate_moment_steps_set_state(dtype):
def test_simulate_moment_steps_set_state_deprecated(dtype):
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)
for i, step in enumerate(simulator.simulate_moment_steps(circuit)):
np.testing.assert_almost_equal(step.state_vector(), np.array([0.5] * 4))
if i == 0:
step.set_state_vector(np.array([1, 0, 0, 0], dtype=dtype))
with cirq.testing.assert_deprecated('initial_state', deadline='v0.15'):
step.set_state_vector(np.array([1, 0, 0, 0], dtype=dtype))


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
Expand Down