Skip to content

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
merged 7 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions cirq-google/cirq_google/engine/qcs_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
Expand All @@ -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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unintended deletion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed..

# 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
Expand All @@ -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))
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 instead use is_virtual to determine the default project_id?

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 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
20 changes: 16 additions & 4 deletions cirq-google/cirq_google/engine/qcs_notebook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@


def test_get_device_sampler():
result = get_qcs_objects_for_notebook()
assert result.device is cg.Sycamore
result = get_qcs_objects_for_notebook(virtual=True)
assert isinstance(result.device, cg.GridDevice)
assert not result.signed_in
assert isinstance(result.sampler, cg.PhasedFSimEngineSimulator)
assert isinstance(result.sampler, cg.ProcessorSampler)
assert result.is_simulator
assert result.processor_id == 'rainbow'
assert result.project_id == 'fake_project'

result = get_qcs_objects_for_notebook("", "")
result = get_qcs_objects_for_notebook(processor_id='weber', virtual=True)
assert isinstance(result.device, cg.GridDevice)
assert not result.signed_in
assert isinstance(result.sampler, cg.ProcessorSampler)
assert result.is_simulator
assert result.processor_id == 'weber'
assert result.project_id == 'fake_project'

# Note: if running locally with application default credentials,
# you actually will be signed_in, so it may not be a simulator
result = get_qcs_objects_for_notebook()
assert isinstance(result.device, cg.GridDevice)