Skip to content

Commit 83609eb

Browse files
authored
Make device parameter units/idx optional (#6264)
- Currently, the device parameters are picking up zero as the index and failing. - Need to make them optional so we can set this to None when it is not an array index. - Yay for proto3!
1 parent 8bd2161 commit 83609eb

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

cirq-google/cirq_google/api/v2/run_context.proto

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ message DeviceParameter {
9090
repeated string path = 1;
9191

9292
// If the value is an array, the index of the array to change.
93-
int64 idx = 2;
93+
optional int64 idx = 2;
9494

9595
// String representation of the units, if any.
9696
// Examples: "GHz", "ns", etc.
97-
string units = 3;
97+
optional string units = 3;
9898

9999
// Note that the device parameter values will be populated
100100
// by the sweep values themselves.

cirq-google/cirq_google/api/v2/run_context_pb2.py

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cirq-google/cirq_google/api/v2/run_context_pb2.pyi

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cirq-google/cirq_google/api/v2/sweeps.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ def sweep_from_proto(msg: run_context_pb2.Sweep) -> cirq.Sweep:
105105
key = msg.single_sweep.parameter_key
106106
if msg.single_sweep.HasField("parameter"):
107107
metadata = DeviceParameter(
108-
path=msg.single_sweep.parameter.path, idx=msg.single_sweep.parameter.idx
108+
path=msg.single_sweep.parameter.path,
109+
idx=msg.single_sweep.parameter.idx
110+
if msg.single_sweep.parameter.HasField("idx")
111+
else None,
112+
units=msg.single_sweep.parameter.units
113+
if msg.single_sweep.parameter.HasField("units")
114+
else None,
109115
)
110116
else:
111117
metadata = None

cirq-google/cirq_google/api/v2/sweeps_test.py

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ def _values(self) -> Iterator[float]:
5252
[1, 1.5, 2, 2.5, 3],
5353
metadata=DeviceParameter(path=['path', 'to', 'parameter'], idx=2, units='GHz'),
5454
),
55+
cirq.Points(
56+
'b',
57+
[1, 1.5, 2, 2.5, 3],
58+
metadata=DeviceParameter(path=['path', 'to', 'parameter'], idx=None),
59+
),
5560
cirq.Linspace('a', 0, 1, 5) * cirq.Linspace('b', 0, 1, 5),
5661
cirq.Points('a', [1, 2, 3]) + cirq.Linspace('b', 0, 1, 3),
5762
(
@@ -69,6 +74,8 @@ def test_sweep_to_proto_roundtrip(sweep):
6974
msg = v2.sweep_to_proto(sweep)
7075
deserialized = v2.sweep_from_proto(msg)
7176
assert deserialized == sweep
77+
# Check that metadata is the same, if it exists.
78+
assert getattr(deserialized, 'metadata', None) == getattr(sweep, 'metadata', None)
7279

7380

7481
def test_sweep_to_proto_linspace():

0 commit comments

Comments
 (0)