Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move serialize_program and serialize_run_context into EngineContext #5034

Merged
merged 5 commits into from
Mar 1, 2022

Conversation

maffoo
Copy link
Contributor

@maffoo maffoo commented Feb 27, 2022

It's convenient to be able to use these functions outside of having an EngineProgram object or calling Engine.create_program, for example when testing direct use of the streaming API with the combined CreateQuantumProgramAndJobRequest call.

@wcourtney, @verult

@maffoo maffoo requested review from wcourtney, a team, vtomole, cduck and verult as code owners February 27, 2022 15:52
@maffoo maffoo requested a review from viathor February 27, 2022 15:52
@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 27, 2022
program = serializer.serialize(program)
return _pack_any(program)

def serialize_run_context(self, sweeps: List[cirq.Sweep], repetitions: int) -> any_pb2.Any:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are now public, can you add unit tests to cirq-google/cirq_google/engine/engine_test.py?

@@ -126,6 +126,28 @@ def copy(self) -> 'EngineContext':
def _value_equality_values_(self):
return self.proto_version, self.client

def serialize_program(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the name "program" confusing here. Since it's moving to public visibility, what do you think of making it something like serialize_circuit and let the weird naming in create_program be its own problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The term "program" is fairly well-established, for example it is used in all the cirq.Sampler methods, so I'm inclined to leave this, but I could be persuaded.

program = serializer.serialize(program)
return _pack_any(program)

def serialize_run_context(self, sweeps: List[cirq.Sweep], repetitions: int) -> any_pb2.Any:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also accept a serializer for consistency with the serialize_program method above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The serializer is only used for circuits, so not needed for serializing the run context.

Also move pack_any into a utility module and use it in more places.
@maffoo
Copy link
Contributor Author

maffoo commented Feb 28, 2022

I reworked this a bit when thinking about testing:

  • moved the core logic of populating RunContext and BatchRunContext into new helper functions in v2/sweeps.py where we already have helpers for converting sweeps to protos. Added some tests for these new functions.
  • moved the code for packing messages into Any protos into a helper function in a new engine.util module. This replaces a bunch of duplicate versions of this logic in tests.
  • made the new helpers on EngineContext private. These really aren't meant to be "public" functions, just wanted them to be accessible in testing without having an engine program object.

Copy link
Collaborator

@wcourtney wcourtney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice rework! LGTM % additional test coverage.

@@ -134,3 +134,37 @@ def test_sweep_with_flattened_sweep():
param_sweep1 = cirq.Linspace('t', start=0, stop=1, length=20)
(_, param_sweep2) = cirq.flatten_with_sweep(circuit, param_sweep1)
assert v2.sweep_to_proto(param_sweep2) is not None


def test_run_context_to_proto():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test to demonstrate that specifying the out parameter is appended to? Same for the batch case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

from google.protobuf.message import Message


def pack_any(message: Message) -> any_pb2.Any:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shocking that there's not a standard utility for this, but it doesn't look like there is...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, right?

@maffoo maffoo merged commit d90b09c into master Mar 1, 2022
@maffoo maffoo deleted the u/maffoo/engine-refactor branch March 1, 2022 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants