Skip to content

Commit

Permalink
Add Concat to cirq-google sweep proto (#6821)
Browse files Browse the repository at this point in the history
* add Concat to sweeps

* json repr

* json testing

* add test for empty sweep

* comments

* coverage for keys property

* add concat

* compile proto

* compile proto

* fix test

* rm duplicate files
  • Loading branch information
senecameeks authored Dec 9, 2024
1 parent e9fbabc commit f07a4b5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
9 changes: 9 additions & 0 deletions cirq-google/cirq_google/api/v2/run_context.proto
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ message SweepFunction {
// the iterator will produce: {a: 1, b: 3} and {a: 2, b: 3}.
// The shorter sweeps will be filled by repeating their last value.
ZIP_LONGEST = 3;

// Concatenates multiple sweeps to a new sweep.
// All sweeps must share the same descriptors.
//
// Example of concat:
// If one sweep assigns 'a' to the values 0, 1, 2, and another sweep assigns
// 'a' to the values 3, 4, 5, the concatenation produces a sweep assigning
// 'a' to the values 0, 1, 2, 3, 4, 5 in sequence.
CONCAT = 4;
}

FunctionType function_type = 1;
Expand Down
42 changes: 21 additions & 21 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.

18 changes: 18 additions & 0 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.

6 changes: 6 additions & 0 deletions cirq-google/cirq_google/api/v2/sweeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def sweep_to_proto(
out.sweep_function.function_type = run_context_pb2.SweepFunction.ZIP
for s in sweep.sweeps:
sweep_to_proto(s, out=out.sweep_function.sweeps.add())
elif isinstance(sweep, cirq.Concat):
out.sweep_function.function_type = run_context_pb2.SweepFunction.CONCAT
for s in sweep.sweeps:
sweep_to_proto(s, out=out.sweep_function.sweeps.add())
elif isinstance(sweep, cirq.Linspace) and not isinstance(sweep.key, sympy.Expr):
out.single_sweep.parameter_key = sweep.key
out.single_sweep.linspace.first_point = sweep.start
Expand Down Expand Up @@ -135,6 +139,8 @@ def sweep_from_proto(msg: run_context_pb2.Sweep) -> cirq.Sweep:
return cirq.Zip(*factors)
if func_type == run_context_pb2.SweepFunction.ZIP_LONGEST:
return cirq.ZipLongest(*factors)
if func_type == run_context_pb2.SweepFunction.CONCAT:
return cirq.Concat(*factors)

raise ValueError(f'invalid sweep function type: {func_type}')
if which == 'single_sweep':
Expand Down
1 change: 1 addition & 0 deletions cirq-google/cirq_google/api/v2/sweeps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def _values(self) -> Iterator[float]:
cirq.Points('a', [1]) * cirq.Points('b', [1.0])
+ cirq.Points('c', ["abc"]) * cirq.Points("d", [1, 2, 3, 4]) # type: ignore[list-item]
),
cirq.Concat(cirq.Points('a', [1.0, 2.0, 3.0]), cirq.Points('a', [4.0])),
],
)
def test_sweep_to_proto_roundtrip(sweep):
Expand Down

0 comments on commit f07a4b5

Please sign in to comment.