diff --git a/cirq-core/cirq/contrib/quantum_volume/quantum_volume_test.py b/cirq-core/cirq/contrib/quantum_volume/quantum_volume_test.py index faa13046c40..b530b22b7c8 100644 --- a/cirq-core/cirq/contrib/quantum_volume/quantum_volume_test.py +++ b/cirq-core/cirq/contrib/quantum_volume/quantum_volume_test.py @@ -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]])}, ) @@ -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]]), diff --git a/cirq-core/cirq/sim/simulator.py b/cirq-core/cirq/sim/simulator.py index 25e0dd6f891..4750f621ce4 100644 --- a/cirq-core/cirq/sim/simulator.py +++ b/cirq-core/cirq/sim/simulator.py @@ -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( diff --git a/cirq-core/cirq/sim/simulator_test.py b/cirq-core/cirq/sim/simulator_test.py index 9054b89762c..35589b3fb17 100644 --- a/cirq-core/cirq/sim/simulator_test.py +++ b/cirq-core/cirq/sim/simulator_test.py @@ -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 ) @@ -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 diff --git a/cirq-core/cirq/study/result.py b/cirq-core/cirq/study/result.py index 646202e2f65..7aa75296818 100644 --- a/cirq-core/cirq/study/result.py +++ b/cirq-core/cirq/study/result.py @@ -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: @@ -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, diff --git a/cirq-core/cirq/study/result_test.py b/cirq-core/cirq/study/result_test.py index 7ea268a184c..94a3faba83b 100644 --- a/cirq-core/cirq/study/result_test.py +++ b/cirq-core/cirq/study/result_test.py @@ -19,6 +19,7 @@ import pytest import cirq +import cirq.testing from cirq.study.result import _pack_digits @@ -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]]), @@ -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]]), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), diff --git a/cirq-google/cirq_google/api/v2/results.py b/cirq-google/cirq_google/api/v2/results.py index 94e26ccf5c6..356ea6ba135 100644 --- a/cirq-google/cirq_google/api/v2/results.py +++ b/cirq-google/cirq_google/api/v2/results.py @@ -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, ) diff --git a/cirq-google/cirq_google/api/v2/results_test.py b/cirq-google/cirq_google/api/v2/results_test.py index 5a5a8b2d797..5f6bbc7c29b 100644 --- a/cirq-google/cirq_google/api/v2/results_test.py +++ b/cirq-google/cirq_google/api/v2/results_test.py @@ -140,13 +140,13 @@ 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), @@ -154,13 +154,13 @@ def test_results_to_proto(): ), ], [ - 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), @@ -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), diff --git a/cirq-google/cirq_google/engine/engine_job.py b/cirq-google/cirq_google/engine/engine_job.py index 6215f000b20..a08b4537f8a 100644 --- a/cirq-google/cirq_google/engine/engine_job.py +++ b/cirq-google/cirq_google/engine/engine_job.py @@ -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, )