Esempio n. 1
0
def test_results_to_proto():
    measurements = [
        v2.MeasureInfo('foo', [q(0, 0)], slot=0, invert_mask=[False], tags=[])
    ]
    trial_results = [
        [
            cirq.ResultDict(
                params=cirq.ParamResolver({'i': 0}),
                measurements={
                    'foo': np.array([[0], [1], [0], [1]], dtype=bool)
                },
            ),
            cirq.ResultDict(
                params=cirq.ParamResolver({'i': 1}),
                measurements={
                    'foo': np.array([[0], [1], [1], [0]], dtype=bool)
                },
            ),
        ],
        [
            cirq.ResultDict(
                params=cirq.ParamResolver({'i': 0}),
                measurements={
                    'foo': np.array([[0], [1], [0], [1]], dtype=bool)
                },
            ),
            cirq.ResultDict(
                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. 2
0
def test_results_to_proto_sweep_repetitions():
    measurements = [
        v2.MeasureInfo('foo', [q(0, 0)], slot=0, invert_mask=[False], tags=[])
    ]
    trial_results = [[
        cirq.ResultDict(
            params=cirq.ParamResolver({'i': 0}),
            measurements={'foo': np.array([[0]], dtype=bool)},
        ),
        cirq.ResultDict(
            params=cirq.ParamResolver({'i': 1}),
            measurements={'foo': np.array([[0], [1]], dtype=bool)},
        ),
    ]]
    with pytest.raises(ValueError, match='Different numbers of repetitions'):
        v2.results_to_proto(trial_results, measurements)
Esempio n. 3
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,
        ),
    )
Esempio n. 4
0
def test_results_from_proto_duplicate_qubit():
    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': 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 = v2.qubit_to_proto_id(qubit)
        qmr.results = bytes([results])
    with pytest.raises(ValueError, match='Qubit already exists'):
        v2.results_from_proto(proto, measurements)