Skip to content

Commit

Permalink
Deprecate from_single_parameter_set on Result (#4680)
Browse files Browse the repository at this point in the history
Fixes #2582
  • Loading branch information
dabacon authored Nov 13, 2021
1 parent 8374b71 commit ca0401f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/quantum_volume/quantum_volume_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_sample_heavy_set():

sampler = Mock(spec=cirq.Simulator)
# Construct a result that returns "1", "2", "3", "0"
result = cirq.Result.from_single_parameter_set(
result = cirq.Result(
params=cirq.ParamResolver({}),
measurements={'mock': np.array([[0, 1], [1, 0], [1, 1], [0, 0]])},
)
Expand All @@ -94,7 +94,7 @@ def test_sample_heavy_set_with_parity():
# bitstring "10" is valid and heavy. The second "01" is valid and not
# heavy. The third and fourth bitstrings "11" and "00" are not valid and
# dropped.
result = cirq.Result.from_single_parameter_set(
result = cirq.Result(
params=cirq.ParamResolver({}),
measurements={
'0': np.array([[1], [0]]),
Expand Down
4 changes: 1 addition & 3 deletions cirq-core/cirq/sim/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def run_sweep_iter(
measurements = self._run(
circuit=program, param_resolver=param_resolver, repetitions=repetitions
)
yield study.Result.from_single_parameter_set(
params=param_resolver, measurements=measurements
)
yield study.Result(params=param_resolver, measurements=measurements)

@abc.abstractmethod
def _run(
Expand Down
12 changes: 3 additions & 9 deletions cirq-core/cirq/sim/simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def test_run_simulator_run():
circuit.__iter__ = mock.Mock(return_value=iter([]))
param_resolver = mock.Mock(cirq.ParamResolver)
param_resolver.param_dict = {}
expected_result = cirq.Result.from_single_parameter_set(
measurements=expected_measurements, params=param_resolver
)
expected_result = cirq.Result(measurements=expected_measurements, params=param_resolver)
assert expected_result == simulator.run(
program=circuit, repetitions=10, param_resolver=param_resolver
)
Expand All @@ -93,12 +91,8 @@ def test_run_simulator_sweeps():
for resolver in param_resolvers:
resolver.param_dict = {}
expected_results = [
cirq.Result.from_single_parameter_set(
measurements=expected_measurements, params=param_resolvers[0]
),
cirq.Result.from_single_parameter_set(
measurements=expected_measurements, params=param_resolvers[1]
),
cirq.Result(measurements=expected_measurements, params=param_resolvers[0]),
cirq.Result(measurements=expected_measurements, params=param_resolvers[1]),
]
assert expected_results == simulator.run_sweep(
program=circuit, repetitions=10, params=param_resolvers
Expand Down
7 changes: 6 additions & 1 deletion cirq-core/cirq/study/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import pandas as pd

from cirq import value, ops
from cirq._compat import proper_repr
from cirq._compat import deprecated, proper_repr
from cirq.study import resolver

if TYPE_CHECKING:
Expand Down Expand Up @@ -128,6 +128,11 @@ def data(self) -> pd.DataFrame:
return self._data

@staticmethod
@deprecated(
deadline="v0.15",
fix="The static method from_single_parameter_set is deprecated, "
"use the Result constructor instead.",
)
def from_single_parameter_set(
*, # Forces keyword args.
params: resolver.ParamResolver,
Expand Down
44 changes: 22 additions & 22 deletions cirq-core/cirq/study/result_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pytest

import cirq
import cirq.testing
from cirq.study.result import _pack_digits


Expand All @@ -28,14 +29,19 @@ def test_result_init():


def test_repr():
v = cirq.Result.from_single_parameter_set(
v = cirq.Result(
params=cirq.ParamResolver({'a': 2}), measurements={'xy': np.array([[1, 0], [0, 1]])}
)
cirq.testing.assert_equivalent_repr(v)


def test_from_single_parameter_set_deprecation():
with cirq.testing.assert_deprecated("use the Result constructor", deadline="v0.15"):
_ = cirq.Result.from_single_parameter_set(params=cirq.ParamResolver({}), measurements={})


def test_str():
result = cirq.Result.from_single_parameter_set(
result = cirq.Result(
params=cirq.ParamResolver({}),
measurements={
'ab': np.array([[0, 1], [0, 1], [0, 1], [1, 0], [0, 1]]),
Expand All @@ -44,7 +50,7 @@ def test_str():
)
assert str(result) == 'ab=00010, 11101\nc=00101'

result = cirq.Result.from_single_parameter_set(
result = cirq.Result(
params=cirq.ParamResolver({}),
measurements={
'ab': np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]),
Expand All @@ -55,7 +61,7 @@ def test_str():


def test_df():
result = cirq.Result.from_single_parameter_set(
result = cirq.Result(
params=cirq.ParamResolver({}),
measurements={
'ab': np.array([[0, 1], [0, 1], [0, 1], [1, 0], [0, 1]], dtype=bool),
Expand All @@ -73,7 +79,7 @@ def test_df():


def test_histogram():
result = cirq.Result.from_single_parameter_set(
result = cirq.Result(
params=cirq.ParamResolver({}),
measurements={
'ab': np.array([[0, 1], [0, 1], [0, 1], [1, 0], [0, 1]], dtype=bool),
Expand All @@ -94,7 +100,7 @@ def test_histogram():


def test_multi_measurement_histogram():
result = cirq.Result.from_single_parameter_set(
result = cirq.Result(
params=cirq.ParamResolver({}),
measurements={
'ab': np.array([[0, 1], [0, 1], [0, 1], [1, 0], [0, 1]], dtype=bool),
Expand Down Expand Up @@ -167,31 +173,25 @@ def test_multi_measurement_histogram():
def test_trial_result_equality():
et = cirq.testing.EqualsTester()
et.add_equality_group(
cirq.Result.from_single_parameter_set(
params=cirq.ParamResolver({}), measurements={'a': np.array([[0]] * 5)}
)
cirq.Result(params=cirq.ParamResolver({}), measurements={'a': np.array([[0]] * 5)})
)
et.add_equality_group(
cirq.Result.from_single_parameter_set(
params=cirq.ParamResolver({}), measurements={'a': np.array([[0]] * 6)}
)
cirq.Result(params=cirq.ParamResolver({}), measurements={'a': np.array([[0]] * 6)})
)
et.add_equality_group(
cirq.Result.from_single_parameter_set(
params=cirq.ParamResolver({}), measurements={'a': np.array([[1]] * 5)}
)
cirq.Result(params=cirq.ParamResolver({}), measurements={'a': np.array([[1]] * 5)})
)


def test_trial_result_addition_valid():
a = cirq.Result.from_single_parameter_set(
a = cirq.Result(
params=cirq.ParamResolver({'ax': 1}),
measurements={
'q0': np.array([[0, 1], [1, 0], [0, 1]], dtype=bool),
'q1': np.array([[0], [0], [1]], dtype=bool),
},
)
b = cirq.Result.from_single_parameter_set(
b = cirq.Result(
params=cirq.ParamResolver({'ax': 1}),
measurements={
'q0': np.array([[0, 1]], dtype=bool),
Expand All @@ -205,28 +205,28 @@ def test_trial_result_addition_valid():


def test_trial_result_addition_invalid():
a = cirq.Result.from_single_parameter_set(
a = cirq.Result(
params=cirq.ParamResolver({'ax': 1}),
measurements={
'q0': np.array([[0, 1], [1, 0], [0, 1]], dtype=bool),
'q1': np.array([[0], [0], [1]], dtype=bool),
},
)
b = cirq.Result.from_single_parameter_set(
b = cirq.Result(
params=cirq.ParamResolver({'bad': 1}),
measurements={
'q0': np.array([[0, 1], [1, 0], [0, 1]], dtype=bool),
'q1': np.array([[0], [0], [1]], dtype=bool),
},
)
c = cirq.Result.from_single_parameter_set(
c = cirq.Result(
params=cirq.ParamResolver({'ax': 1}),
measurements={
'bad': np.array([[0, 1], [1, 0], [0, 1]], dtype=bool),
'q1': np.array([[0], [0], [1]], dtype=bool),
},
)
d = cirq.Result.from_single_parameter_set(
d = cirq.Result(
params=cirq.ParamResolver({'ax': 1}),
measurements={
'q0': np.array([[0, 1], [1, 0], [0, 1]], dtype=bool),
Expand Down Expand Up @@ -261,7 +261,7 @@ def test_qubit_keys_for_histogram():


def test_text_diagram_jupyter():
result = cirq.Result.from_single_parameter_set(
result = cirq.Result(
params=cirq.ParamResolver({}),
measurements={
'ab': np.array([[0, 1], [0, 1], [0, 1], [1, 0], [0, 1]], dtype=bool),
Expand Down
2 changes: 1 addition & 1 deletion cirq-google/cirq_google/api/v2/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _trial_sweep_from_proto(
ordered_results = list(qubit_results.values())
m_data[mr.key] = np.array(ordered_results).transpose()
trial_sweep.append(
cirq.Result.from_single_parameter_set(
cirq.Result(
params=cirq.ParamResolver(dict(pr.params.assignments)),
measurements=m_data,
)
Expand Down
12 changes: 6 additions & 6 deletions cirq-google/cirq_google/api/v2/results_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,27 @@ def test_results_to_proto():
measurements = [v2.MeasureInfo('foo', [q(0, 0)], slot=0, invert_mask=[False], tags=[])]
trial_results = [
[
cirq.Result.from_single_parameter_set(
cirq.Result(
params=cirq.ParamResolver({'i': 0}),
measurements={
'foo': np.array([[0], [1], [0], [1]], dtype=bool),
},
),
cirq.Result.from_single_parameter_set(
cirq.Result(
params=cirq.ParamResolver({'i': 1}),
measurements={
'foo': np.array([[0], [1], [1], [0]], dtype=bool),
},
),
],
[
cirq.Result.from_single_parameter_set(
cirq.Result(
params=cirq.ParamResolver({'i': 0}),
measurements={
'foo': np.array([[0], [1], [0], [1]], dtype=bool),
},
),
cirq.Result.from_single_parameter_set(
cirq.Result(
params=cirq.ParamResolver({'i': 1}),
measurements={
'foo': np.array([[0], [1], [1], [0]], dtype=bool),
Expand All @@ -187,13 +187,13 @@ def test_results_to_proto_sweep_repetitions():
measurements = [v2.MeasureInfo('foo', [q(0, 0)], slot=0, invert_mask=[False], tags=[])]
trial_results = [
[
cirq.Result.from_single_parameter_set(
cirq.Result(
params=cirq.ParamResolver({'i': 0}),
measurements={
'foo': np.array([[0]], dtype=bool),
},
),
cirq.Result.from_single_parameter_set(
cirq.Result(
params=cirq.ParamResolver({'i': 1}),
measurements={
'foo': np.array([[0], [1]], dtype=bool),
Expand Down
2 changes: 1 addition & 1 deletion cirq-google/cirq_google/engine/engine_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def _get_job_results_v1(result: v1.program_pb2.Result) -> List[cirq.Result]:
measurements = v1.unpack_results(data, sweep_repetitions, key_sizes)

trial_results.append(
cirq.Result.from_single_parameter_set(
cirq.Result(
params=cirq.ParamResolver(result.params.assignments),
measurements=measurements,
)
Expand Down

0 comments on commit ca0401f

Please sign in to comment.