def _get_job_results_v2(result: v2.result_pb2.Result) -> Sequence[cirq.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 ]
def test_results_from_proto_default_ordering(): 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) 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, ), )
def _get_job_results_v2(self, result: v2.result_pb2.Result) -> Sequence[EngineResult]: sweep_results = v2.results_from_proto(result) job_id = self.id() job_finished = self.update_time() # Flatten to single list to match to sampler api. return [ EngineResult.from_result(result, job_id=job_id, job_finished_time=job_finished) for sweep_result in sweep_results for result in sweep_result ]
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)
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'])