Skip to content

Commit

Permalink
Use FromString classmethod for protos (#3066)
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo committed Jun 3, 2020
1 parent d9c0221 commit 369ed54
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
14 changes: 6 additions & 8 deletions cirq/google/engine/engine_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ def _deserialize_run_context(run_context: quantum.types.any_pb2.Any
raise ValueError('deserializing a v1 RunContext is not supported')
if (run_context_type == 'cirq.google.api.v2.RunContext' or
run_context_type == 'cirq.api.google.v2.RunContext'):
v2_run_context = v2.run_context_pb2.RunContext()
v2_run_context.ParseFromString(run_context.value)
v2_run_context = v2.run_context_pb2.RunContext.FromString(
run_context.value)
return v2_run_context.parameter_sweeps[0].repetitions, [
v2.sweep_from_proto(s.sweep)
for s in v2_run_context.parameter_sweeps
Expand Down Expand Up @@ -242,8 +242,7 @@ def get_calibration(self) -> Optional[calibration.Calibration]:
ids = self.context.client._ids_from_calibration_name(
status.calibration_name)
response = self.context.client.get_calibration(*ids)
metrics = v2.metrics_pb2.MetricsSnapshot()
metrics.ParseFromString(response.data.value)
metrics = v2.metrics_pb2.MetricsSnapshot.FromString(response.data.value)
return calibration.Calibration(metrics)

def cancel(self) -> None:
Expand Down Expand Up @@ -279,13 +278,12 @@ def results(self) -> List[study.TrialResult]:
result_type = result.type_url[len(engine_base.TYPE_PREFIX):]
if (result_type == 'cirq.google.api.v1.Result' or
result_type == 'cirq.api.google.v1.Result'):
v1_parsed_result = v1.program_pb2.Result()
v1_parsed_result.ParseFromString(result.value)
v1_parsed_result = v1.program_pb2.Result.FromString(
result.value)
self._results = self._get_job_results_v1(v1_parsed_result)
elif (result_type == 'cirq.google.api.v2.Result' or
result_type == 'cirq.api.google.v2.Result'):
v2_parsed_result = v2.result_pb2.Result()
v2_parsed_result.ParseFromString(result.value)
v2_parsed_result = v2.result_pb2.Result.FromString(result.value)
self._results = self._get_job_results_v2(v2_parsed_result)
else:
raise ValueError(
Expand Down
8 changes: 3 additions & 5 deletions cirq/google/engine/engine_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ def get_device_specification(
Device specification proto if present.
"""
if self._inner_processor().HasField('device_spec'):
device_spec = v2.device_pb2.DeviceSpecification()
device_spec.ParseFromString(
return v2.device_pb2.DeviceSpecification.FromString(
self._inner_processor().device_spec.value)
return device_spec
else:
return None

Expand All @@ -129,8 +127,8 @@ def get_device(
@staticmethod
def _to_calibration(calibration_any: qtypes.any_pb2.Any
) -> calibration.Calibration:
metrics = v2.metrics_pb2.MetricsSnapshot()
metrics.ParseFromString(calibration_any.value)
metrics = v2.metrics_pb2.MetricsSnapshot.FromString(
calibration_any.value)
return calibration.Calibration(metrics)

def list_calibrations(self,
Expand Down
3 changes: 1 addition & 2 deletions cirq/google/engine/engine_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ def _deserialize_program(code: qtypes.any_pb2.Any) -> 'Circuit':
raise ValueError('deserializing a v1 Program is not supported')
if (code_type == 'cirq.google.api.v2.Program' or
code_type == 'cirq.api.google.v2.Program'):
program = v2.program_pb2.Program()
program.ParseFromString(code.value)
program = v2.program_pb2.Program.FromString(code.value)
gate_set_map = {
g.gate_set_name: g for g in gate_sets.GOOGLE_GATESETS
}
Expand Down

0 comments on commit 369ed54

Please sign in to comment.