Esempio n. 1
0
def test_results_from_proto_default_ordering():
    proto = result_pb2.Result()
    sr = proto.sweep_results.add()
    sr.repetitions = 8
    pr = sr.parameterized_results.add()
    pr.params.assignments.update({'i': 1})
    mr = pr.measurement_results.add()
    mr.key = 'foo'
    for qubit, results in [
        (q(0, 1), 0b1100_1100),
        (q(1, 1), 0b1010_1010),
        (q(0, 0), 0b1111_0000),
    ]:
        qmr = mr.qubit_measurement_results.add()
        qmr.qubit.id = qubit.proto_id()
        qmr.results = bytes([results])

    trial_results = v2.results_from_proto(proto)
    trial = trial_results[0][0]
    assert trial.params == cirq.ParamResolver({'i': 1})
    assert trial.repetitions == 8
    np.testing.assert_array_equal(
        trial.measurements['foo'],
        np.array([
            [0, 0, 0],
            [0, 1, 0],
            [1, 0, 0],
            [1, 1, 0],
            [0, 0, 1],
            [0, 1, 1],
            [1, 0, 1],
            [1, 1, 1],
        ],
                 dtype=bool))
Esempio n. 2
0
 def _get_job_results_v2(result: v2.result_pb2.Result) -> List[study.Result]:
     sweep_results = v2.results_from_proto(result)
     # Flatten to single list to match to sampler api.
     return [
         trial_result for sweep_result in sweep_results
         for trial_result in sweep_result
     ]
Esempio n. 3
0
def test_results_from_proto_duplicate_qubit():
    measurements = [v2.MeasureInfo('foo', [q(0, 0), q(0, 1), q(1, 1)], slot=0)]
    proto = result_pb2.Result()
    sr = proto.sweep_results.add()
    sr.repetitions = 8
    pr = sr.parameterized_results.add()
    pr.params.assignments.update({'i': 0})
    mr = pr.measurement_results.add()
    mr.key = 'foo'
    for qubit, results in [
        (q(0, 0), 0b1100_1100),
        (q(0, 1), 0b1010_1010),
        (q(0, 1), 0b1111_0000),
    ]:
        qmr = mr.qubit_measurement_results.add()
        qmr.qubit.id = qubit.proto_id()
        qmr.results = bytes([results])
    with pytest.raises(ValueError, match='qubit already exists'):
        v2.results_from_proto(proto, measurements)
Esempio n. 4
0
 def _get_job_results_v2(
         self, result_dict: Dict[str, Any]) -> List[study.TrialResult]:
     result_any = any_pb2.Any()
     gp.json_format.ParseDict(result_dict, result_any)
     result = v2.result_pb2.Result()
     result_any.Unpack(result)
     sweep_results = v2.results_from_proto(result)
     # Flatten to single list to match to sampler api.
     return [
         trial_result for sweep_result in sweep_results
         for trial_result in sweep_result
     ]
Esempio n. 5
0
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(
                params=cirq.ParamResolver({'i': 0}),
                measurements={
                    'foo': np.array([[0], [1], [0], [1]], dtype=bool),
                },
            ),
            cirq.Result.from_single_parameter_set(
                params=cirq.ParamResolver({'i': 1}),
                measurements={
                    'foo': np.array([[0], [1], [1], [0]], dtype=bool),
                },
            ),
        ],
        [
            cirq.Result.from_single_parameter_set(
                params=cirq.ParamResolver({'i': 0}),
                measurements={
                    'foo': np.array([[0], [1], [0], [1]], dtype=bool),
                },
            ),
            cirq.Result.from_single_parameter_set(
                params=cirq.ParamResolver({'i': 1}),
                measurements={
                    'foo': np.array([[0], [1], [1], [0]], dtype=bool),
                },
            ),
        ],
    ]
    proto = v2.results_to_proto(trial_results, measurements)
    assert isinstance(proto, v2.result_pb2.Result)
    assert len(proto.sweep_results) == 2
    deserialized = v2.results_from_proto(proto, measurements)
    assert len(deserialized) == 2
    for sweep_results, expected in zip(deserialized, trial_results):
        assert len(sweep_results) == len(expected)
        for trial_result, expected_trial_result in zip(sweep_results,
                                                       expected):
            assert trial_result.params == expected_trial_result.params
            assert trial_result.repetitions == expected_trial_result.repetitions
            np.testing.assert_array_equal(
                trial_result.measurements['foo'],
                expected_trial_result.measurements['foo'])
Esempio n. 6
0
def test_results_from_proto_qubit_ordering():
    measurements = [
        v2.MeasureInfo('foo', [q(0, 0), q(0, 1), q(1, 1)],
                       slot=0,
                       invert_mask=[False, False, False],
                       tags=[])
    ]
    proto = v2.result_pb2.Result()
    sr = proto.sweep_results.add()
    sr.repetitions = 8
    pr = sr.parameterized_results.add()
    pr.params.assignments.update({'i': 1})
    mr = pr.measurement_results.add()
    mr.key = 'foo'
    for qubit, results in [
        (q(0, 1), 0b1100_1100),
        (q(1, 1), 0b1010_1010),
        (q(0, 0), 0b1111_0000),
    ]:
        qmr = mr.qubit_measurement_results.add()
        qmr.qubit.id = v2.qubit_to_proto_id(qubit)
        qmr.results = bytes([results])

    trial_results = v2.results_from_proto(proto, measurements)
    trial = trial_results[0][0]
    assert trial.params == cirq.ParamResolver({'i': 1})
    assert trial.repetitions == 8
    np.testing.assert_array_equal(
        trial.measurements['foo'],
        np.array(
            [
                [0, 0, 0],
                [0, 0, 1],
                [0, 1, 0],
                [0, 1, 1],
                [1, 0, 0],
                [1, 0, 1],
                [1, 1, 0],
                [1, 1, 1],
            ],
            dtype=bool,
        ),
    )