-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Return processor_id and project_id in get_qcs_objects_for_notebooks #5759
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
CirqBot
merged 7 commits into
quantumlib:master
from
dstrain115:get_qcs_objects_for_notebook
Jul 14, 2022
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e82893
Return processor_id and project_id in get_qcs_objects_for_notebooks
dstrain115 94c156a
Formatting
dstrain115 d93f54e
Add virtual option to ensure deterministic test behavior.
dstrain115 c716dd1
Streamline get_qcs_for_notebooks to use only virtual or prod engine.
dstrain115 e946b37
Fix typing
dstrain115 883583b
Typing fixed for realsies.
dstrain115 9357099
Merge branch 'master' into get_qcs_objects_for_notebook
CirqBot 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 hidden or 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 |
---|---|---|
|
@@ -25,24 +25,24 @@ | |
PhasedFSimCharacterization, | ||
get_engine, | ||
) | ||
from cirq_google.engine import create_noiseless_virtual_engine_from_latest_templates | ||
|
||
|
||
@dataclasses.dataclass | ||
class QCSObjectsForNotebook: | ||
device: cirq.Device | ||
sampler: Union[PhasedFSimEngineSimulator, ProcessorSampler] | ||
signed_in: bool | ||
|
||
@property | ||
def is_simulator(self): | ||
return isinstance(self.sampler, PhasedFSimEngineSimulator) | ||
processor_id: Optional[str] | ||
project_id: Optional[str] | ||
is_simulator: bool | ||
|
||
|
||
# Disable missing-raises-doc lint check, since pylint gets confused | ||
# by exceptions that are raised and caught within this function. | ||
# pylint: disable=missing-raises-doc | ||
def get_qcs_objects_for_notebook( | ||
project_id: Optional[str] = None, processor_id: Optional[str] = None | ||
project_id: Optional[str] = None, processor_id: Optional[str] = None, virtual=False | ||
) -> QCSObjectsForNotebook: # pragma: nocover | ||
"""Authenticates on Google Cloud, can return a Device and Simulator. | ||
|
||
|
@@ -53,6 +53,8 @@ def get_qcs_objects_for_notebook( | |
personal project IDs in shared code. | ||
processor_id: Engine processor ID (from Cloud console or | ||
``Engine.list_processors``). | ||
virtual: If set to True, will create a noisy virtual Engine instead. | ||
This is useful for testing and simulation. | ||
|
||
Returns: | ||
An instance of DeviceSamplerInfo. | ||
|
@@ -61,8 +63,7 @@ def get_qcs_objects_for_notebook( | |
# Check for Google Application Default Credentials and run | ||
# interactive login if the notebook is executed in Colab. In | ||
# case the notebook is executed in Jupyter notebook or other | ||
# IPython runtimes, no interactive login is provided, it is | ||
# assumed that the `GOOGLE_APPLICATION_CREDENTIALS` env var is | ||
|
||
# set or `gcloud auth application-default login` was executed | ||
# already. For more information on using Application Default Credentials | ||
# see https://cloud.google.com/docs/authentication/production | ||
|
@@ -82,30 +83,46 @@ def get_qcs_objects_for_notebook( | |
# Attempt to connect to the Quantum Engine API, and use a simulator if unable to connect. | ||
sampler: Union[PhasedFSimEngineSimulator, ProcessorSampler] | ||
try: | ||
engine = get_engine(project_id) | ||
if virtual: | ||
engine = create_noiseless_virtual_engine_from_latest_templates() | ||
is_simulator = True | ||
else: | ||
engine = get_engine(project_id) | ||
is_simulator = False | ||
if processor_id: | ||
processor = engine.get_processor(processor_id) | ||
else: | ||
processors = engine.list_processors() | ||
if not processors: | ||
raise ValueError("No processors available.") | ||
processor = processors[0] | ||
processor_id = processor.processor_id | ||
print(f"Available processors: {[p.processor_id for p in processors]}") | ||
print(f"Using processor: {processor.processor_id}") | ||
if not project_id: | ||
project_id = getattr(processor, 'project_id', getattr(processor, '_project_name', None)) | ||
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. Should this instead use 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 this to AbstractLocalProcessor instead. |
||
device = processor.get_device() | ||
sampler = processor.get_sampler() | ||
signed_in = True | ||
signed_in = not virtual | ||
except Exception as exc: | ||
print(f"Unable to connect to quantum engine: {exc}") | ||
print("Using a noisy simulator.") | ||
sampler = PhasedFSimEngineSimulator.create_with_random_gaussian_sqrt_iswap( | ||
mean=SQRT_ISWAP_INV_PARAMETERS, | ||
sigma=PhasedFSimCharacterization(theta=0.01, zeta=0.10, chi=0.01, gamma=0.10, phi=0.02), | ||
) | ||
is_simulator = True | ||
device = Sycamore | ||
signed_in = False | ||
|
||
return QCSObjectsForNotebook(device=device, sampler=sampler, signed_in=signed_in) | ||
return QCSObjectsForNotebook( | ||
device=device, | ||
sampler=sampler, | ||
signed_in=signed_in, | ||
project_id=project_id, | ||
processor_id=processor_id, | ||
is_simulator=is_simulator, | ||
) | ||
|
||
|
||
# pylint: enable=missing-raises-doc |
This file contains hidden or 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.
Unintended deletion?
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.
Fixed..