Skip to content

Add async support in EngineClient, EngineSampler, etc. #5219

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

Merged
merged 14 commits into from
Jun 16, 2022

Conversation

maffoo
Copy link
Contributor

@maffoo maffoo commented Apr 7, 2022

This is WIP showing how we can add async support with duet to the various Engine classes that wrap the quantum engine API. In EngineClient we use an AsyncioExecutor to run the asyncio operations on the underlying gapic-generated code and expose them as duet-compatible futures. For EngineClient and classes that call it, we expose async methods, as well as synchronous versions wrapped with duet.sync. I have not yet fixed up test, but have tried this out and it seems to work fine.

@CirqBot CirqBot added the size: L 250< lines changed <1000 label Apr 7, 2022
@maffoo maffoo force-pushed the u/maffoo/engine-async branch from eaf0415 to 54b15b4 Compare April 9, 2022 00:16
@maffoo maffoo force-pushed the u/maffoo/engine-async branch 6 times, most recently from 9a297de to 4eb5fc6 Compare April 22, 2022 16:03
@maffoo
Copy link
Contributor Author

maffoo commented Apr 22, 2022

Tests are now passing for python 3.8+ but failing for 3.6 and 3.7 because they don't support AsyncMock which was added in 3.8. I'll mark them as xfail for now; we could consider revamping the tests to not rely so heavily on unittest.mock, but this would be a significant amount of work.

@maffoo
Copy link
Contributor Author

maffoo commented May 5, 2022

cc @verult

@maffoo maffoo force-pushed the u/maffoo/engine-async branch 2 times, most recently from 6541b86 to 0e515c4 Compare May 6, 2022 21:20
@maffoo maffoo force-pushed the u/maffoo/engine-async branch from 0e515c4 to 9c56246 Compare May 26, 2022 08:30
@maffoo maffoo force-pushed the u/maffoo/engine-async branch from 0ef9d84 to 62babd3 Compare May 26, 2022 17:34
@maffoo
Copy link
Contributor Author

maffoo commented May 26, 2022

@verult, this is working now that we've dropped support for py3.6. PTAL.

@maffoo maffoo marked this pull request as ready for review June 9, 2022 06:42
@maffoo maffoo requested review from wcourtney, a team, vtomole, cduck and verult as code owners June 9, 2022 06:42
@maffoo maffoo requested a review from viathor June 9, 2022 06:42
Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

A lot of these changes are "rote" (changing method name to _async, adding an await, adding a duet.sync(...) version). Would it be possible to split the "real" changes (like the executor and make_async_request stuff) into a PR and then the mechanical changes to follow? Could make reviewing easier.

The rote changes LGTM where they exist. I've put some comments about where they should exist; cc #5505 . In particular, I'd like to see some async methods show up in the Abstractxxx base classes so we can continue to rely on methods having local, simulator-backed implementations.

The executor stuff could use a little more documentation so future non-@maffoo devs can have some chance of developing with confidence.

@@ -49,26 +51,28 @@ def __init__(
self._processor_ids = [processor_id] if isinstance(processor_id, str) else processor_id
self._engine = engine

def run_sweep(
async def run_sweep_async(
Copy link
Collaborator

Choose a reason for hiding this comment

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

following #5361 #5432 QuantumEngineSampler is deprecated. These changes need to be applied to ProcessorSampler. Maybe don't bother adding them here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted changes to engine_sampler.py

@@ -38,6 +53,26 @@ def __init__(self, message):
RETRYABLE_ERROR_CODES = [500, 503]


class AsyncioExecutor:
Copy link
Collaborator

Choose a reason for hiding this comment

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

docstring for this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a docstring.

@@ -208,7 +209,7 @@ def __str__(self) -> str:
return f'Engine(project_id={self.project_id!r})'

@util.deprecated_gate_set_parameter
def run(
async def run_async(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we're moving towards encouraging people to use EngineProcessor.run methods instead of Engine because AbstractEngine doesn't actually have any of these methods so they don't get mocked by the simulated versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted changes to the run* methods on Engine.

@maffoo
Copy link
Contributor Author

maffoo commented Jun 15, 2022

@mpharrigan, I've create #5526 with the "real" changes to use async grpc with asyncio under the hood in EngineClient. Once that is merged the rest of the changes should be fairly mechanical, as you noted. PTAL.

Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

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

LGTM as written. Would love to see some more PRs that puts some async methods on @dstrain115 's base classes

@maffoo maffoo added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 16, 2022
@CirqBot
Copy link
Collaborator

CirqBot commented Jun 16, 2022

Automerge cancelled: A status check is failing.

@CirqBot CirqBot removed the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 16, 2022
@maffoo maffoo merged commit 1ed566b into master Jun 16, 2022
@maffoo maffoo deleted the u/maffoo/engine-async branch June 16, 2022 19:20
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants