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

Add asynchronous ability to simulated engine. #4811

Merged
merged 4 commits into from
Jan 31, 2022

Conversation

dstrain115
Copy link
Collaborator

Adds ASYNCHRONOUS type which starts the simulation
immediately in the background and then reports it
as RUNNING until it is completed. Once completed
or if results is called, the state will be SUCCESS.

Adds ASYNCHRONOUS type which starts the simulation
immediately in the background and then reports it
as RUNNING until it is completed.  Once completed
or if results is called, the state will be SUCCESS.
@dstrain115 dstrain115 requested review from cduck, vtomole, wcourtney and a team as code owners January 7, 2022 22:00
@dstrain115 dstrain115 requested a review from mpharrigan January 7, 2022 22:00
@CirqBot CirqBot added the size: M 50< lines changed <250 label Jan 7, 2022
@@ -55,6 +57,10 @@ def __init__(
self._type = simulation_type
self._failure_code = ''
self._failure_message = ''
if self._type == LocalSimulationType.ASYNCHRONOUS:
# If asynchronous mode, just kick off a new task and move on.
self._thread = concurrent.futures.ThreadPoolExecutor(max_workers=1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we be concerned about closing this threadpool? I usually see it used as a context manager which means there should be some sort of try..finally

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a call to shutdown immediately, since this will clean up the resources once the future is complete.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah clever

@@ -98,22 +104,31 @@ def batched_results(self) -> List[List[cirq.Result]]:
raise e
raise ValueError('Unsupported simulation type {self._type}')

def spawn_results(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

docstring? why is this called spawn?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added docstring and changed this to _execute_results since it should be private.

@dstrain115 dstrain115 requested a review from mpharrigan January 8, 2022 14:58
@dstrain115
Copy link
Collaborator Author

Any further comments on this?

@@ -98,22 +110,40 @@ def batched_results(self) -> Sequence[Sequence[cirq.Result]]:
raise e
raise ValueError('Unsupported simulation type {self._type}')

def _execute_results(self) -> Sequence[cirq.Result]:
Copy link
Contributor

Choose a reason for hiding this comment

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

This method may be called by a separate thread if running asynchronously, which introduces potential thread safety issues for other threads that may be polling the state. Is this a concern?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What thread safety issues are you worried about? Do you think State should be protected by a lock of some sort?Assignment here should be atomic.

@dstrain115 dstrain115 requested a review from maffoo January 17, 2022 14:13
@dstrain115 dstrain115 merged commit 8c8ee43 into quantumlib:master Jan 31, 2022
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
* Add asynchronous ability to simulated engine.

Adds ASYNCHRONOUS type which starts the simulation
immediately in the background and then reports it
as RUNNING until it is completed.  Once completed
or if results is called, the state will be SUCCESS.
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.

4 participants