Skip to content

Commit

Permalink
Make device parameter units/idx optional (#6264)
Browse files Browse the repository at this point in the history
- 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!
  • Loading branch information
dstrain115 authored Aug 28, 2023
1 parent 8bd2161 commit 83609eb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cirq-google/cirq_google/api/v2/run_context.proto
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ message DeviceParameter {
repeated string path = 1;

// If the value is an array, the index of the array to change.
int64 idx = 2;
optional int64 idx = 2;

// String representation of the units, if any.
// Examples: "GHz", "ns", etc.
string units = 3;
optional string units = 3;

// Note that the device parameter values will be populated
// by the sweep values themselves.
Expand Down
16 changes: 8 additions & 8 deletions cirq-google/cirq_google/api/v2/run_context_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions cirq-google/cirq_google/api/v2/run_context_pb2.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion cirq-google/cirq_google/api/v2/sweeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ def sweep_from_proto(msg: run_context_pb2.Sweep) -> cirq.Sweep:
key = msg.single_sweep.parameter_key
if msg.single_sweep.HasField("parameter"):
metadata = DeviceParameter(
path=msg.single_sweep.parameter.path, idx=msg.single_sweep.parameter.idx
path=msg.single_sweep.parameter.path,
idx=msg.single_sweep.parameter.idx
if msg.single_sweep.parameter.HasField("idx")
else None,
units=msg.single_sweep.parameter.units
if msg.single_sweep.parameter.HasField("units")
else None,
)
else:
metadata = None
Expand Down
7 changes: 7 additions & 0 deletions cirq-google/cirq_google/api/v2/sweeps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def _values(self) -> Iterator[float]:
[1, 1.5, 2, 2.5, 3],
metadata=DeviceParameter(path=['path', 'to', 'parameter'], idx=2, units='GHz'),
),
cirq.Points(
'b',
[1, 1.5, 2, 2.5, 3],
metadata=DeviceParameter(path=['path', 'to', 'parameter'], idx=None),
),
cirq.Linspace('a', 0, 1, 5) * cirq.Linspace('b', 0, 1, 5),
cirq.Points('a', [1, 2, 3]) + cirq.Linspace('b', 0, 1, 3),
(
Expand All @@ -69,6 +74,8 @@ def test_sweep_to_proto_roundtrip(sweep):
msg = v2.sweep_to_proto(sweep)
deserialized = v2.sweep_from_proto(msg)
assert deserialized == sweep
# Check that metadata is the same, if it exists.
assert getattr(deserialized, 'metadata', None) == getattr(sweep, 'metadata', None)


def test_sweep_to_proto_linspace():
Expand Down

0 comments on commit 83609eb

Please sign in to comment.