|
30 | 30 | from cirq.study import sweeps |
31 | 31 |
|
32 | 32 |
|
33 | | -def _build_sweep_const(value: Any, use_float64: bool = False) -> run_context_pb2.ConstValue: |
| 33 | +def _add_sweep_const( |
| 34 | + sweep: run_context_pb2.SingleSweep, value: Any, use_float64: bool = False |
| 35 | +) -> None: |
34 | 36 | """Build the sweep const message from a value.""" |
35 | 37 | if isinstance(value, float): |
36 | 38 | # comparing to float is ~5x than testing numbers.Real |
37 | 39 | # if modifying the below, also modify the block below for numbers.Real |
38 | 40 | if use_float64: |
39 | | - return run_context_pb2.ConstValue(double_value=value) |
| 41 | + sweep.const_value.double_value = value |
40 | 42 | else: |
41 | 43 | # Note: A loss of precision for floating-point numbers may occur here. |
42 | | - return run_context_pb2.ConstValue(float_value=value) |
| 44 | + sweep.const_value.float_value = value |
43 | 45 | elif isinstance(value, int): |
44 | 46 | # comparing to int is ~5x than testing numbers.Integral |
45 | 47 | # if modifying the below, also modify the block below for numbers.Integral |
46 | | - return run_context_pb2.ConstValue(int_value=value) |
| 48 | + sweep.const_value.int_value = value |
47 | 49 | elif value is None: |
48 | | - return run_context_pb2.ConstValue(is_none=True) |
| 50 | + sweep.const_value.is_none = True |
49 | 51 | elif isinstance(value, str): |
50 | | - return run_context_pb2.ConstValue(string_value=value) |
| 52 | + sweep.const_value.string_value = value |
51 | 53 | elif isinstance(value, numbers.Integral): |
52 | 54 | # more general than isinstance(int) but also slower |
53 | | - return run_context_pb2.ConstValue(int_value=int(value)) |
| 55 | + sweep.const_value.int_value = int(value) |
54 | 56 | elif isinstance(value, numbers.Real): |
55 | 57 | # more general than isinstance(float) but also slower |
56 | 58 | if use_float64: |
57 | | - return run_context_pb2.ConstValue(double_value=float(value)) |
| 59 | + sweep.const_value.double_value = float(value) # pragma: no cover |
58 | 60 | else: |
59 | 61 | # Note: A loss of precision for floating-point numbers may occur here. |
60 | | - return run_context_pb2.ConstValue(float_value=float(value)) |
| 62 | + sweep.const_value.float_value = float(value) |
61 | 63 | elif isinstance(value, tunits.Value): |
62 | | - return run_context_pb2.ConstValue(with_unit_value=value.to_proto()) |
| 64 | + value.to_proto(sweep.const_value.with_unit_value) |
63 | 65 | else: |
64 | 66 | raise ValueError( |
65 | 67 | f"Unsupported type for serializing const sweep: {value=} and {type(value)=}" |
@@ -191,7 +193,7 @@ def sweep_to_proto( |
191 | 193 | sweep = cast(cirq.Points, sweep_transformer(sweep)) |
192 | 194 | out.single_sweep.parameter_key = sweep.key |
193 | 195 | if len(sweep.points) == 1: |
194 | | - out.single_sweep.const_value.MergeFrom(_build_sweep_const(sweep.points[0], use_float64)) |
| 196 | + _add_sweep_const(out.single_sweep, sweep.points[0], use_float64) |
195 | 197 | else: |
196 | 198 | if isinstance(sweep.points[0], tunits.Value): |
197 | 199 | unit = sweep.points[0].unit |
@@ -404,7 +406,7 @@ def sweepable_to_proto( |
404 | 406 | for key, val in sweepable.items(): |
405 | 407 | single_sweep = zip_proto.sweeps.add().single_sweep |
406 | 408 | single_sweep.parameter_key = key |
407 | | - single_sweep.const_value.MergeFrom(_build_sweep_const(val, use_float64)) |
| 409 | + _add_sweep_const(single_sweep, val, use_float64) |
408 | 410 | return out |
409 | 411 | if isinstance(sweepable, Iterable): |
410 | 412 | for sweepable_element in sweepable: |
|
0 commit comments