Skip to content

Commit

Permalink
Deprectate log_of_measurement_result input (#5100)
Browse files Browse the repository at this point in the history
Co-authored-by: Cirq Bot <craiggidney+github+cirqbot@google.com>
  • Loading branch information
daxfohl and CirqBot committed Mar 18, 2022
1 parent 848bfde commit caadb0c
Show file tree
Hide file tree
Showing 25 changed files with 53 additions and 95 deletions.
5 changes: 3 additions & 2 deletions cirq-core/cirq/contrib/quimb/mps_simulator_test.py
Expand Up @@ -550,7 +550,9 @@ def test_state_act_on_args_initializer():
s = ccq.mps_simulator.MPSState(
qubits=(cirq.LineQubit(0),),
prng=np.random.RandomState(0),
log_of_measurement_results={'test': [4]},
classical_data=cirq.ClassicalDataDictionaryStore(
_records={cirq.MeasurementKey('test'): [(4,)]}
),
)
assert s.qubits == (cirq.LineQubit(0),)
assert s.log_of_measurement_results == {'test': [4]}
Expand All @@ -560,7 +562,6 @@ def test_act_on_gate():
args = ccq.mps_simulator.MPSState(
qubits=cirq.LineQubit.range(3),
prng=np.random.RandomState(0),
log_of_measurement_results={},
)

cirq.act_on(cirq.X, args, [cirq.LineQubit(1)])
Expand Down
5 changes: 1 addition & 4 deletions cirq-core/cirq/ops/clifford_gate.py
Expand Up @@ -666,9 +666,7 @@ def _generate_clifford_from_known_gate(
) -> 'CliffordGate':
qubits = devices.LineQubit.range(num_qubits)
t = qis.CliffordTableau(num_qubits=num_qubits)
args = sim.ActOnCliffordTableauArgs(
tableau=t, qubits=qubits, prng=np.random.RandomState(), log_of_measurement_results={}
)
args = sim.ActOnCliffordTableauArgs(tableau=t, qubits=qubits)

protocols.act_on(gate, args, qubits, allow_decompose=False)
return CliffordGate.from_clifford_tableau(args.tableau)
Expand Down Expand Up @@ -732,7 +730,6 @@ def from_op_list(
tableau=base_tableau,
qubits=qubit_order,
prng=np.random.RandomState(0), # unused
log_of_measurement_results={}, # unused
)
for op in operations:
protocols.act_on(op, args, allow_decompose=True)
Expand Down
11 changes: 2 additions & 9 deletions cirq-core/cirq/ops/clifford_gate_test.py
Expand Up @@ -796,13 +796,11 @@ def test_clifford_gate_act_on_small_case():
tableau=cirq.CliffordTableau(num_qubits=5),
qubits=qubits,
prng=np.random.RandomState(),
log_of_measurement_results={},
)
expected_args = cirq.ActOnCliffordTableauArgs(
tableau=cirq.CliffordTableau(num_qubits=5),
qubits=qubits,
prng=np.random.RandomState(),
log_of_measurement_results={},
)
cirq.act_on(cirq.H, expected_args, qubits=[qubits[0]], allow_decompose=False)
cirq.act_on(cirq.CliffordGate.H, args, qubits=[qubits[0]], allow_decompose=False)
Expand Down Expand Up @@ -833,12 +831,8 @@ def test_clifford_gate_act_on_large_case():
t1 = cirq.CliffordTableau(num_qubits=n)
t2 = cirq.CliffordTableau(num_qubits=n)
qubits = cirq.LineQubit.range(n)
args1 = cirq.ActOnCliffordTableauArgs(
tableau=t1, qubits=qubits, prng=prng, log_of_measurement_results={}
)
args2 = cirq.ActOnCliffordTableauArgs(
tableau=t2, qubits=qubits, prng=prng, log_of_measurement_results={}
)
args1 = cirq.ActOnCliffordTableauArgs(tableau=t1, qubits=qubits, prng=prng)
args2 = cirq.ActOnCliffordTableauArgs(tableau=t2, qubits=qubits, prng=prng)
ops = []
for _ in range(num_ops):
g = prng.randint(len(gate_candidate))
Expand All @@ -861,7 +855,6 @@ def test_clifford_gate_act_on_ch_form():
initial_state=cirq.StabilizerStateChForm(num_qubits=2, initial_state=1),
qubits=cirq.LineQubit.range(2),
prng=np.random.RandomState(),
log_of_measurement_results={},
)
cirq.act_on(cirq.CliffordGate.X, args, qubits=cirq.LineQubit.range(1))
np.testing.assert_allclose(args.state.state_vector(), np.array([0, 0, 0, 1]))
Expand Down
1 change: 0 additions & 1 deletion cirq-core/cirq/ops/common_channels_test.py
Expand Up @@ -498,7 +498,6 @@ def test_reset_act_on():
available_buffer=np.empty(shape=(2, 2, 2, 2, 2), dtype=np.complex64),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=cirq.one_hot(
index=(1, 1, 1, 1, 1), shape=(2, 2, 2, 2, 2), dtype=np.complex64
),
Expand Down
8 changes: 0 additions & 8 deletions cirq-core/cirq/ops/common_gates_test.py
Expand Up @@ -309,7 +309,6 @@ def test_x_act_on_tableau():
tableau=original_tableau.copy(),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
)

cirq.act_on(cirq.X ** 0.5, args, [cirq.LineQubit(1)], allow_decompose=False)
Expand Down Expand Up @@ -359,7 +358,6 @@ def test_y_act_on_tableau():
tableau=original_tableau.copy(),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
)

cirq.act_on(cirq.Y ** 0.5, args, [cirq.LineQubit(1)], allow_decompose=False)
Expand Down Expand Up @@ -400,7 +398,6 @@ def test_z_h_act_on_tableau():
tableau=original_tableau.copy(),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
)

cirq.act_on(cirq.H, args, [cirq.LineQubit(1)], allow_decompose=False)
Expand Down Expand Up @@ -451,7 +448,6 @@ def test_cx_act_on_tableau():
tableau=original_tableau.copy(),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
)

cirq.act_on(cirq.CX, args, cirq.LineQubit.range(2), allow_decompose=False)
Expand Down Expand Up @@ -496,7 +492,6 @@ def test_cz_act_on_tableau():
tableau=original_tableau.copy(),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
)

cirq.act_on(cirq.CZ, args, cirq.LineQubit.range(2), allow_decompose=False)
Expand Down Expand Up @@ -537,13 +532,11 @@ def test_cz_act_on_equivalent_to_h_cx_h_tableau():
tableau=cirq.CliffordTableau(num_qubits=2),
qubits=cirq.LineQubit.range(2),
prng=np.random.RandomState(),
log_of_measurement_results={},
)
args2 = cirq.ActOnCliffordTableauArgs(
tableau=cirq.CliffordTableau(num_qubits=2),
qubits=cirq.LineQubit.range(2),
prng=np.random.RandomState(),
log_of_measurement_results={},
)
cirq.act_on(cirq.S, args=args1, qubits=[cirq.LineQubit(1)], allow_decompose=False)
cirq.act_on(cirq.S, args=args2, qubits=[cirq.LineQubit(1)], allow_decompose=False)
Expand Down Expand Up @@ -605,7 +598,6 @@ def test_act_on_ch_form(input_gate_sequence, outcome):
args = cirq.ActOnStabilizerCHFormArgs(
qubits=cirq.LineQubit.range(2),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=original_state.copy(),
)

Expand Down
6 changes: 2 additions & 4 deletions cirq-core/cirq/ops/global_phase_op_test.py
Expand Up @@ -43,7 +43,7 @@ def test_protocols():
@pytest.mark.parametrize('phase', [1, 1j, -1])
def test_act_on_tableau(phase):
original_tableau = cirq.CliffordTableau(0)
args = cirq.ActOnCliffordTableauArgs(original_tableau.copy(), np.random.RandomState(), {})
args = cirq.ActOnCliffordTableauArgs(original_tableau.copy(), np.random.RandomState())
cirq.act_on(cirq.global_phase_operation(phase), args, allow_decompose=False)
assert args.tableau == original_tableau

Expand All @@ -54,7 +54,6 @@ def test_act_on_ch_form(phase):
args = cirq.ActOnStabilizerCHFormArgs(
qubits=[],
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=state,
)
cirq.act_on(cirq.global_phase_operation(phase), args, allow_decompose=False)
Expand Down Expand Up @@ -309,7 +308,7 @@ def test_gate_protocols():
@pytest.mark.parametrize('phase', [1, 1j, -1])
def test_gate_act_on_tableau(phase):
original_tableau = cirq.CliffordTableau(0)
args = cirq.ActOnCliffordTableauArgs(original_tableau.copy(), np.random.RandomState(), {})
args = cirq.ActOnCliffordTableauArgs(original_tableau.copy(), np.random.RandomState())
cirq.act_on(cirq.GlobalPhaseGate(phase), args, qubits=(), allow_decompose=False)
assert args.tableau == original_tableau

Expand All @@ -320,7 +319,6 @@ def test_gate_act_on_ch_form(phase):
args = cirq.ActOnStabilizerCHFormArgs(
qubits=[],
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=state,
)
cirq.act_on(cirq.GlobalPhaseGate(phase), args, qubits=(), allow_decompose=False)
Expand Down
12 changes: 0 additions & 12 deletions cirq-core/cirq/ops/measurement_gate_test.py
Expand Up @@ -298,7 +298,6 @@ def test_act_on_state_vector():
available_buffer=np.empty(shape=(2, 2, 2, 2, 2)),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=cirq.one_hot(shape=(2, 2, 2, 2, 2), dtype=np.complex64),
dtype=np.complex64,
)
Expand All @@ -309,7 +308,6 @@ def test_act_on_state_vector():
available_buffer=np.empty(shape=(2, 2, 2, 2, 2)),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=cirq.one_hot(
index=(0, 1, 0, 0, 0), shape=(2, 2, 2, 2, 2), dtype=np.complex64
),
Expand All @@ -322,7 +320,6 @@ def test_act_on_state_vector():
available_buffer=np.empty(shape=(2, 2, 2, 2, 2)),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=cirq.one_hot(
index=(0, 1, 0, 1, 0), shape=(2, 2, 2, 2, 2), dtype=np.complex64
),
Expand All @@ -348,7 +345,6 @@ def test_act_on_clifford_tableau():
tableau=cirq.CliffordTableau(num_qubits=5, initial_state=0),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
)
cirq.act_on(m, args)
assert args.log_of_measurement_results == {'out': [1, 0]}
Expand All @@ -357,7 +353,6 @@ def test_act_on_clifford_tableau():
tableau=cirq.CliffordTableau(num_qubits=5, initial_state=8),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
)

cirq.act_on(m, args)
Expand All @@ -367,7 +362,6 @@ def test_act_on_clifford_tableau():
tableau=cirq.CliffordTableau(num_qubits=5, initial_state=10),
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
)
cirq.act_on(m, args)
datastore = cast(cirq.ClassicalDataDictionaryStore, args.classical_data)
Expand All @@ -388,7 +382,6 @@ def test_act_on_stabilizer_ch_form():
args = cirq.ActOnStabilizerCHFormArgs(
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=0,
)
cirq.act_on(m, args)
Expand All @@ -397,7 +390,6 @@ def test_act_on_stabilizer_ch_form():
args = cirq.ActOnStabilizerCHFormArgs(
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=8,
)

Expand All @@ -407,7 +399,6 @@ def test_act_on_stabilizer_ch_form():
args = cirq.ActOnStabilizerCHFormArgs(
qubits=cirq.LineQubit.range(5),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=10,
)
cirq.act_on(m, args)
Expand All @@ -428,7 +419,6 @@ def test_act_on_qutrit():
available_buffer=np.empty(shape=(3, 3, 3, 3, 3)),
qubits=cirq.LineQid.range(5, dimension=3),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=cirq.one_hot(
index=(0, 2, 0, 2, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64
),
Expand All @@ -441,7 +431,6 @@ def test_act_on_qutrit():
available_buffer=np.empty(shape=(3, 3, 3, 3, 3)),
qubits=cirq.LineQid.range(5, dimension=3),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=cirq.one_hot(
index=(0, 1, 0, 2, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64
),
Expand All @@ -454,7 +443,6 @@ def test_act_on_qutrit():
available_buffer=np.empty(shape=(3, 3, 3, 3, 3)),
qubits=cirq.LineQid.range(5, dimension=3),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=cirq.one_hot(
index=(0, 2, 0, 1, 0), shape=(3, 3, 3, 3, 3), dtype=np.complex64
),
Expand Down
8 changes: 7 additions & 1 deletion cirq-core/cirq/sim/act_on_args_container.py
Expand Up @@ -31,7 +31,7 @@
import numpy as np

from cirq import ops, protocols, value
from cirq._compat import deprecated
from cirq._compat import deprecated, deprecated_parameter
from cirq.sim.operation_target import OperationTarget
from cirq.sim.simulator import (
TActOnArgs,
Expand All @@ -48,6 +48,12 @@ class ActOnArgsContainer(
):
"""A container for a `Qid`-to-`ActOnArgs` dictionary."""

@deprecated_parameter(
deadline='v0.15',
fix='Use classical_data.',
parameter_desc='log_of_measurement_results and positional arguments',
match=lambda args, kwargs: 'log_of_measurement_results' in kwargs or len(args) > 4,
)
def __init__(
self,
args: Dict[Optional['cirq.Qid'], TActOnArgs],
Expand Down
12 changes: 6 additions & 6 deletions cirq-core/cirq/sim/act_on_args_container_test.py
Expand Up @@ -11,16 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Dict, List, Optional, Sequence, Union
from typing import Dict, List, Optional, Sequence, Union

import cirq


class EmptyActOnArgs(cirq.ActOnArgs):
def __init__(self, qubits, logs):
def __init__(self, qubits, classical_data):
super().__init__(
qubits=qubits,
log_of_measurement_results=logs,
classical_data=classical_data,
)

def _perform_measurement(self, qubits: Sequence[cirq.Qid]) -> List[int]:
Expand All @@ -30,7 +30,7 @@ def copy(self) -> 'EmptyActOnArgs': # type: ignore
"""The deep_copy_buffers parameter is omitted to trigger a deprecation warning test."""
return EmptyActOnArgs(
qubits=self.qubits,
logs=self.log_of_measurement_results.copy(),
classical_data=self.classical_data.copy(),
)

def _act_on_fallback_(
Expand Down Expand Up @@ -70,7 +70,7 @@ def create_container(
split_untangled_states=True,
) -> cirq.ActOnArgsContainer[EmptyActOnArgs]:
args_map: Dict[Optional['cirq.Qid'], EmptyActOnArgs] = {}
log: Dict[str, Any] = {}
log = cirq.ClassicalDataDictionaryStore()
if split_untangled_states:
for q in reversed(qubits):
args_map[q] = EmptyActOnArgs([q], log)
Expand All @@ -80,7 +80,7 @@ def create_container(
for q in qubits:
args_map[q] = args
args_map[None] = args if not split_untangled_states else EmptyActOnArgs((), log)
return cirq.ActOnArgsContainer(args_map, qubits, split_untangled_states, log)
return cirq.ActOnArgsContainer(args_map, qubits, split_untangled_states, classical_data=log)


def test_entanglement_causes_join():
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/sim/act_on_density_matrix_args.py
Expand Up @@ -33,6 +33,12 @@ class ActOnDensityMatrixArgs(ActOnArgs):
storing the density matrix of the quantum system with one axis per qubit.
"""

@_compat.deprecated_parameter(
deadline='v0.15',
fix='Use classical_data.',
parameter_desc='log_of_measurement_results and positional arguments',
match=lambda args, kwargs: 'log_of_measurement_results' in kwargs or len(args) > 5,
)
@_compat.deprecated_parameter(
deadline='v0.15',
fix='Use cirq.dephase_measurements to transform the circuit before simulating.',
Expand Down
2 changes: 0 additions & 2 deletions cirq-core/cirq/sim/act_on_density_matrix_args_test.py
Expand Up @@ -73,7 +73,6 @@ def _decompose_(self, qubits):
args = cirq.ActOnDensityMatrixArgs(
qubits=cirq.LineQubit.range(1),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=0,
dtype=np.complex64,
)
Expand All @@ -91,7 +90,6 @@ class NoDetails:
args = cirq.ActOnDensityMatrixArgs(
qubits=cirq.LineQubit.range(1),
prng=np.random.RandomState(),
log_of_measurement_results={},
initial_state=0,
dtype=np.complex64,
)
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/sim/act_on_state_vector_args.py
Expand Up @@ -37,6 +37,12 @@ class ActOnStateVectorArgs(ActOnArgs):
then pass `available_buffer` into `swap_target_tensor_for`.
"""

@_compat.deprecated_parameter(
deadline='v0.15',
fix='Use classical_data.',
parameter_desc='log_of_measurement_results and positional arguments',
match=lambda args, kwargs: 'log_of_measurement_results' in kwargs or len(args) > 4,
)
@_compat.deprecated_parameter(
deadline='v0.15',
fix='Use initial_state instead and specify all the arguments with keywords.',
Expand Down

0 comments on commit caadb0c

Please sign in to comment.