-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
dstrain115
merged 4 commits into
quantumlib:master
from
dstrain115:add_async_to_emulator
Jan 31, 2022
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
and a provided sampler to execute circuits.""" | ||
from typing import cast, List, Optional, Tuple | ||
|
||
import concurrent.futures | ||
|
||
import cirq | ||
from cirq_google.engine.client import quantum | ||
from cirq_google.engine.calibration_result import CalibrationResult | ||
|
@@ -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) | ||
self._future = self._thread.submit(self.spawn_results) | ||
|
||
def execution_status(self) -> quantum.enums.ExecutionStatus.State: | ||
"""Return the execution status of the job.""" | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docstring? why is this called spawn? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
reps, sweeps = self.get_repetitions_and_sweeps() | ||
program = self.program().get_circuit() | ||
try: | ||
self._state = quantum.enums.ExecutionStatus.State.RUNNING | ||
results = self._sampler.run_sweep( | ||
program=program, params=sweeps[0] if sweeps else None, repetitions=reps | ||
) | ||
self._state = quantum.enums.ExecutionStatus.State.SUCCESS | ||
return results | ||
except Exception as e: | ||
self._failure_code = '500' | ||
self._failure_message = str(e) | ||
self._state = quantum.enums.ExecutionStatus.State.FAILURE | ||
raise e | ||
|
||
def results(self) -> List[cirq.Result]: | ||
"""Returns the job results, blocking until the job is complete.""" | ||
if self._type == LocalSimulationType.SYNCHRONOUS: | ||
reps, sweeps = self.get_repetitions_and_sweeps() | ||
program = self.program().get_circuit() | ||
try: | ||
self._state = quantum.enums.ExecutionStatus.State.SUCCESS | ||
return self._sampler.run_sweep( | ||
program=program, params=sweeps[0] if sweeps else None, repetitions=reps | ||
) | ||
except Exception as e: | ||
self._failure_code = '500' | ||
self._failure_message = str(e) | ||
self._state = quantum.enums.ExecutionStatus.State.FAILURE | ||
raise e | ||
raise ValueError('Unsupported simulation type {self._type}') | ||
return self.spawn_results() | ||
elif self._type == LocalSimulationType.ASYNCHRONOUS: | ||
return self._future.result() | ||
|
||
else: | ||
raise ValueError('Unsupported simulation type {self._type}') | ||
|
||
def calibration_results(self) -> List[CalibrationResult]: | ||
"""Returns the results of a run_calibration() call. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah clever