Skip to content

Commit 31df8ef

Browse files
authored
Adds option to compress the size of RunContext (#7532)
- This allows the option to compress the size of RunContext by gzipping the contents. - From arbitrary tests, this can reduce the size of the RunContext by about 10x. For instance, ``` sweeps = [ cirq.Points(f'symbol_{num}', [random.choice([0,-0.5,-1,0.5,1]) for _ in range(100)]) for num in range(100)] sweep = cirq.Zip(*sweeps) proto_compressed = v2.run_context_to_proto(sweep, 100, compress_proto=True) proto_uncompressed = v2.run_context_to_proto(sweep, 100, compress_proto=False) print(f'Compressed length: {len(proto_compressed.SerializeToString())}') print(f'Uncompressed length: {len(proto_uncompressed.SerializeToString())}') ``` Prints out: ``` Compressed length: 11807 Uncompressed length: 122606 ```
1 parent 862bc2f commit 31df8ef

File tree

5 files changed

+67
-32
lines changed

5 files changed

+67
-32
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ message RunContext {
2020
// If the same parameter is supplied in both places, the provision here in
2121
// device_parameters_override will have no effect.
2222
DeviceParametersDiff device_parameters_override = 2;
23+
24+
// If compression is desired, this field stores the gzipped bytes
25+
// of the RunContext
26+
bytes compressed_run_context = 3;
2327
}
2428

2529
// Specifies how to repeatedly sample a circuit, with or without sweeping over

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

Lines changed: 30 additions & 30 deletions
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
import gzip
1718
import numbers
1819
from typing import Any, Callable, cast, TYPE_CHECKING
1920

@@ -321,7 +322,11 @@ def metadata_from_proto(metadata_pb: run_context_pb2.Metadata) -> Metadata:
321322

322323

323324
def run_context_to_proto(
324-
sweepable: cirq.Sweepable, repetitions: int, *, out: run_context_pb2.RunContext | None = None
325+
sweepable: cirq.Sweepable,
326+
repetitions: int,
327+
*,
328+
out: run_context_pb2.RunContext | None = None,
329+
compress_proto: bool = False,
325330
) -> run_context_pb2.RunContext:
326331
"""Populates a RunContext protobuf message.
327332
@@ -330,14 +335,23 @@ def run_context_to_proto(
330335
repetitions: The number of repetitions for the run context.
331336
out: Optional message to be populated. If not given, a new message will
332337
be created.
338+
compress_proto: If set to `True` the function will gzip the proto and
339+
store the contents in the bytes field.
333340
334341
Returns:
335342
Populated RunContext protobuf message.
336343
"""
337344
if out is None:
338345
out = run_context_pb2.RunContext()
346+
if compress_proto:
347+
uncompressed_wrapper = out
348+
out = run_context_pb2.RunContext()
339349
for sweep in cirq.to_sweeps(sweepable):
340350
sweep_proto = out.parameter_sweeps.add()
341351
sweep_proto.repetitions = repetitions
342352
sweep_to_proto(sweep, out=sweep_proto.sweep)
353+
if compress_proto:
354+
raw_bytes = out.SerializeToString()
355+
uncompressed_wrapper.compressed_run_context = gzip.compress(raw_bytes)
356+
return uncompressed_wrapper
343357
return out

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
import gzip
1718
import math
1819
from copy import deepcopy
1920
from typing import Iterator
@@ -417,6 +418,16 @@ def test_run_context_to_proto(pass_out: bool) -> None:
417418
assert out.parameter_sweeps[0].repetitions == 100
418419

419420

421+
def test_run_context_to_proto_with_compression() -> None:
422+
sweep = cirq.Linspace('a', 0, 1, 21)
423+
out = v2.run_context_to_proto(sweep, 100, compress_proto=True)
424+
raw_bytes = gzip.decompress(out.compressed_run_context)
425+
msg = v2.run_context_pb2.RunContext()
426+
msg.ParseFromString(raw_bytes)
427+
assert v2.sweep_from_proto(msg.parameter_sweeps[0].sweep) == sweep
428+
assert msg.parameter_sweeps[0].repetitions == 100
429+
430+
420431
@pytest.mark.parametrize(
421432
'sweep',
422433
[

0 commit comments

Comments
 (0)