Skip to content

Commit f85e731

Browse files
daxfohlCirqBot
andauthored
Remove deprecated log_of_measurement_results parameters (#5233)
* deprecate log_of_measurement_results * Fix test * coverage * Add empty ClassicalDataDictionaryStore.repr test * Add ActOnArgs test * Add tests * Add ActOnStabilizerArgs tests * nits * coverGE * format Co-authored-by: Cirq Bot <[email protected]>
1 parent 32fa8b6 commit f85e731

17 files changed

+358
-399
lines changed

cirq-core/cirq/contrib/quimb/mps_simulator.py

+29-55
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import quimb.tensor as qtn
2626

2727
from cirq import devices, protocols, qis, value
28-
from cirq._compat import deprecated
28+
from cirq._compat import deprecated_parameter
2929
from cirq.sim import simulator_base
3030
from cirq.sim.act_on_args import ActOnArgs
3131

@@ -53,9 +53,7 @@ class MPSOptions:
5353

5454

5555
class MPSSimulator(
56-
simulator_base.SimulatorBase[
57-
'MPSSimulatorStepResult', 'MPSTrialResult', 'MPSState', 'MPSState'
58-
],
56+
simulator_base.SimulatorBase['MPSSimulatorStepResult', 'MPSTrialResult', 'MPSState', 'MPSState']
5957
):
6058
"""An efficient simulator for MPS circuits."""
6159

@@ -83,10 +81,7 @@ def __init__(
8381
raise ValueError(f'noise must be unitary or mixture but was {noise_model}')
8482
self.simulation_options = simulation_options
8583
self.grouping = grouping
86-
super().__init__(
87-
noise=noise,
88-
seed=seed,
89-
)
84+
super().__init__(noise=noise, seed=seed)
9085

9186
def _create_partial_act_on_args(
9287
self,
@@ -120,10 +115,7 @@ def _create_partial_act_on_args(
120115
classical_data=classical_data,
121116
)
122117

123-
def _create_step_result(
124-
self,
125-
sim_state: 'cirq.OperationTarget[MPSState]',
126-
):
118+
def _create_step_result(self, sim_state: 'cirq.OperationTarget[MPSState]'):
127119
return MPSSimulatorStepResult(sim_state)
128120

129121
def _create_simulator_trial_result(
@@ -182,10 +174,7 @@ def _repr_pretty_(self, p: Any, cycle: bool):
182174
class MPSSimulatorStepResult(simulator_base.StepResultBase['MPSState', 'MPSState']):
183175
"""A `StepResult` that can perform measurements."""
184176

185-
def __init__(
186-
self,
187-
sim_state: 'cirq.OperationTarget[MPSState]',
188-
):
177+
def __init__(self, sim_state: 'cirq.OperationTarget[MPSState]'):
189178
"""Results of a step of the simulator.
190179
Attributes:
191180
sim_state: The qubit:ActOnArgs lookup for this step.
@@ -576,6 +565,19 @@ def sample(
576565
class MPSState(ActOnArgs):
577566
"""A state of the MPS simulation."""
578567

568+
@deprecated_parameter(
569+
deadline='v0.16',
570+
fix='Use kwargs instead of positional args',
571+
parameter_desc='args',
572+
match=lambda args, kwargs: len(args) > 1,
573+
)
574+
@deprecated_parameter(
575+
deadline='v0.16',
576+
fix='Replace log_of_measurement_results with'
577+
' classical_data=cirq.ClassicalDataDictionaryStore(_records=logs).',
578+
parameter_desc='log_of_measurement_results',
579+
match=lambda args, kwargs: 'log_of_measurement_results' in kwargs,
580+
)
579581
def __init__(
580582
self,
581583
qubits: Sequence['cirq.Qid'],
@@ -614,13 +616,16 @@ def __init__(
614616
simulation_options=simulation_options,
615617
grouping={qubit_map[k]: v for k, v in final_grouping.items()},
616618
)
617-
super().__init__(
618-
state=state,
619-
prng=prng,
620-
qubits=qubits,
621-
log_of_measurement_results=log_of_measurement_results,
622-
classical_data=classical_data,
623-
)
619+
if log_of_measurement_results is not None:
620+
super().__init__(
621+
state=state,
622+
prng=prng,
623+
qubits=qubits,
624+
log_of_measurement_results=log_of_measurement_results,
625+
classical_data=classical_data,
626+
)
627+
else:
628+
super().__init__(state=state, prng=prng, qubits=qubits, classical_data=classical_data)
624629
self._state: _MPSHandler = state
625630

626631
def i_str(self, i: int) -> str:
@@ -665,21 +670,8 @@ def to_numpy(self) -> np.ndarray:
665670
"""An alias for the state vector."""
666671
return self._state.to_numpy()
667672

668-
@deprecated(deadline="v0.15", fix="Use cirq.act_on(op, mps_state)")
669-
def apply_op(self, op: 'cirq.Operation', prng: np.random.RandomState):
670-
"""Applies a unitary operation, mutating the object to represent the new state.
671-
672-
op:
673-
The operation that mutates the object. Note that currently, only 1-
674-
and 2- qubit operations are currently supported.
675-
"""
676-
return self._state.apply_op(op, self.get_axes(op.qubits), prng)
677-
678673
def _act_on_fallback_(
679-
self,
680-
action: Any,
681-
qubits: Sequence['cirq.Qid'],
682-
allow_decompose: bool = True,
674+
self, action: Any, qubits: Sequence['cirq.Qid'], allow_decompose: bool = True
683675
) -> bool:
684676
"""Delegates the action to self.apply_op"""
685677
return self._state.apply_op(action, self.get_axes(qubits), self.prng)
@@ -691,21 +683,3 @@ def estimation_stats(self):
691683
@property
692684
def M(self):
693685
return self._state._M
694-
695-
@deprecated(deadline="v0.15", fix="Use cirq.act_on(measurement, mps_state)")
696-
def perform_measurement(
697-
self, qubits: Sequence['cirq.Qid'], prng: np.random.RandomState, collapse_state_vector=True
698-
) -> List[int]:
699-
"""Performs a measurement over one or more qubits.
700-
701-
Args:
702-
qubits: The sequence of qids to measure, in that order.
703-
prng: A random number generator, used to simulate measurements.
704-
collapse_state_vector: A Boolean specifying whether we should mutate
705-
the state after the measurement.
706-
707-
Raises:
708-
ValueError: If the probabilities for the measurements differ too much from one for the
709-
tolerance specified in simulation options.
710-
"""
711-
return self._state._measure(self.get_axes(qubits), prng, collapse_state_vector)

cirq-core/cirq/contrib/quimb/mps_simulator_test.py

+10-23
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ def test_various_gates_1d():
4545
def test_various_gates_1d_flip():
4646
q0, q1 = cirq.LineQubit.range(2)
4747

48-
circuit = cirq.Circuit(
49-
cirq.H(q1),
50-
cirq.CNOT(q1, q0),
51-
)
48+
circuit = cirq.Circuit(cirq.H(q1), cirq.CNOT(q1, q0))
5249

5350
assert_same_output_as_dense(circuit=circuit, qubit_order=[q0, q1])
5451
assert_same_output_as_dense(circuit=circuit, qubit_order=[q1, q0])
@@ -378,10 +375,7 @@ def test_supremacy_equal_more_cols():
378375
def test_tensor_index_names():
379376
qubits = cirq.LineQubit.range(12)
380377
qubit_map = {qubit: i for i, qubit in enumerate(qubits)}
381-
state = ccq.mps_simulator.MPSState(
382-
qubit_map,
383-
prng=value.parse_random_state(0),
384-
)
378+
state = ccq.mps_simulator.MPSState(qubits=qubit_map, prng=value.parse_random_state(0))
385379

386380
assert state.i_str(0) == "i_00"
387381
assert state.i_str(11) == "i_11"
@@ -559,10 +553,7 @@ def test_state_act_on_args_initializer():
559553

560554

561555
def test_act_on_gate():
562-
args = ccq.mps_simulator.MPSState(
563-
qubits=cirq.LineQubit.range(3),
564-
prng=np.random.RandomState(0),
565-
)
556+
args = ccq.mps_simulator.MPSState(qubits=cirq.LineQubit.range(3), prng=np.random.RandomState(0))
566557

567558
cirq.act_on(cirq.X, args, [cirq.LineQubit(1)])
568559
np.testing.assert_allclose(
@@ -571,15 +562,11 @@ def test_act_on_gate():
571562
)
572563

573564

574-
def test_deprectated():
575-
q0 = cirq.LineQubit(0)
565+
def test_deprecated():
576566
prng = np.random.RandomState(0)
577-
args = ccq.mps_simulator.MPSState(
578-
qubits=cirq.LineQubit.range(3),
579-
prng=prng,
580-
log_of_measurement_results={},
581-
)
582-
with cirq.testing.assert_deprecated(deadline='0.15'):
583-
args.perform_measurement([q0], prng)
584-
with cirq.testing.assert_deprecated(deadline='0.15'):
585-
args.apply_op(cirq.X(q0), prng)
567+
with cirq.testing.assert_deprecated('log_of_measurement_results', deadline='0.16', count=2):
568+
_ = ccq.mps_simulator.MPSState(
569+
qubits=cirq.LineQubit.range(3), prng=prng, log_of_measurement_results={}
570+
)
571+
with cirq.testing.assert_deprecated('positional', deadline='0.16'):
572+
_ = ccq.mps_simulator.MPSState(cirq.LineQubit.range(3), prng=prng)

cirq-core/cirq/protocols/act_on_protocol_test.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ def measure(self, axes, seed=None):
3030

3131
class DummyActOnArgs(cirq.ActOnArgs):
3232
def __init__(self, fallback_result: Any = NotImplemented):
33-
super().__init__(np.random.RandomState(), state=DummyQuantumState())
33+
super().__init__(prng=np.random.RandomState(), state=DummyQuantumState())
3434
self.fallback_result = fallback_result
3535

3636
def _act_on_fallback_(
37-
self,
38-
action: Any,
39-
qubits: Sequence['cirq.Qid'],
40-
allow_decompose: bool = True,
37+
self, action: Any, qubits: Sequence['cirq.Qid'], allow_decompose: bool = True
4138
):
4239
return self.fallback_result
4340

Original file line numberDiff line numberDiff line change
@@ -1,60 +1,69 @@
1-
{
2-
"cirq_type": "ClassicalDataDictionaryStore",
3-
"records": [
4-
[
5-
{
6-
"cirq_type": "MeasurementKey",
7-
"name": "m",
8-
"path": []
9-
},
10-
[[0, 1]]
11-
]
12-
],
13-
"measured_qubits": [
14-
[
15-
{
16-
"cirq_type": "MeasurementKey",
17-
"name": "m",
18-
"path": []
19-
},
20-
[[
1+
[
2+
{
3+
"cirq_type": "ClassicalDataDictionaryStore",
4+
"records": [
5+
[
216
{
22-
"cirq_type": "LineQubit",
23-
"x": 0
7+
"cirq_type": "MeasurementKey",
8+
"name": "m",
9+
"path": []
2410
},
11+
[[0, 1]]
12+
]
13+
],
14+
"measured_qubits": [
15+
[
2516
{
26-
"cirq_type": "LineQubit",
27-
"x": 1
28-
}
29-
]]
30-
]
31-
],
32-
"channel_records": [
33-
[
34-
{
35-
"cirq_type": "MeasurementKey",
36-
"name": "c",
37-
"path": []
38-
},
39-
[3]
40-
]
41-
],
42-
"measurement_types": [
43-
[
44-
{
45-
"cirq_type": "MeasurementKey",
46-
"name": "m",
47-
"path": []
48-
},
49-
1
17+
"cirq_type": "MeasurementKey",
18+
"name": "m",
19+
"path": []
20+
},
21+
[[
22+
{
23+
"cirq_type": "LineQubit",
24+
"x": 0
25+
},
26+
{
27+
"cirq_type": "LineQubit",
28+
"x": 1
29+
}
30+
]]
31+
]
32+
],
33+
"channel_records": [
34+
[
35+
{
36+
"cirq_type": "MeasurementKey",
37+
"name": "c",
38+
"path": []
39+
},
40+
[3]
41+
]
5042
],
51-
[
52-
{
53-
"cirq_type": "MeasurementKey",
54-
"name": "c",
55-
"path": []
56-
},
57-
2
43+
"measurement_types": [
44+
[
45+
{
46+
"cirq_type": "MeasurementKey",
47+
"name": "m",
48+
"path": []
49+
},
50+
1
51+
],
52+
[
53+
{
54+
"cirq_type": "MeasurementKey",
55+
"name": "c",
56+
"path": []
57+
},
58+
2
59+
]
5860
]
59-
]
60-
}
61+
},
62+
{
63+
"cirq_type": "ClassicalDataDictionaryStore",
64+
"records": [],
65+
"measured_qubits": [],
66+
"channel_records": [],
67+
"measurement_types": []
68+
}
69+
]
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
cirq.ClassicalDataDictionaryStore(_records={cirq.MeasurementKey('m'): [[0, 1]]}, _measured_qubits={cirq.MeasurementKey('m'): [[cirq.LineQubit(0), cirq.LineQubit(1)]]}, _channel_records={cirq.MeasurementKey('c'): [3]}, _measurement_types={cirq.MeasurementKey('m'): cirq.MeasurementType.MEASUREMENT, cirq.MeasurementKey('c'): cirq.MeasurementType.CHANNEL})
1+
[
2+
cirq.ClassicalDataDictionaryStore(_records={cirq.MeasurementKey('m'): [[0, 1]]}, _measured_qubits={cirq.MeasurementKey('m'): [[cirq.LineQubit(0), cirq.LineQubit(1)]]}, _channel_records={cirq.MeasurementKey('c'): [3]}, _measurement_types={cirq.MeasurementKey('m'): cirq.MeasurementType.MEASUREMENT, cirq.MeasurementKey('c'): cirq.MeasurementType.CHANNEL}),
3+
cirq.ClassicalDataDictionaryStore(),
4+
]

0 commit comments

Comments
 (0)