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

Initialize processor sampler with the default device config key #6521

Merged
Show file tree
Hide file tree
Changes from 17 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
6 changes: 4 additions & 2 deletions cirq-google/cirq_google/cloud/quantum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
from cirq_google.cloud.quantum_v1alpha1.types.engine import UpdateQuantumJobRequest
from cirq_google.cloud.quantum_v1alpha1.types.engine import UpdateQuantumProgramRequest
from cirq_google.cloud.quantum_v1alpha1.types.engine import UpdateQuantumReservationRequest
from cirq_google.cloud.quantum_v1alpha1.types.quantum import DeviceConfigKey
from cirq_google.cloud.quantum_v1alpha1.types.quantum import DeviceConfigSelector
from cirq_google.cloud.quantum_v1alpha1.types.quantum import ExecutionStatus
from cirq_google.cloud.quantum_v1alpha1.types.quantum import GcsLocation
from cirq_google.cloud.quantum_v1alpha1.types.quantum import InlineData
Expand All @@ -77,6 +77,7 @@
from cirq_google.cloud.quantum_v1alpha1.types.quantum import QuantumResult
from cirq_google.cloud.quantum_v1alpha1.types.quantum import QuantumTimeSlot
from cirq_google.cloud.quantum_v1alpha1.types.quantum import SchedulingConfig
from cirq_google.cloud.quantum_v1alpha1.types.quantum import DeviceConfigKey

__all__ = (
'QuantumEngineServiceClient',
Expand Down Expand Up @@ -121,7 +122,7 @@
'UpdateQuantumJobRequest',
'UpdateQuantumProgramRequest',
'UpdateQuantumReservationRequest',
'DeviceConfigKey',
'DeviceConfigSelector',
'ExecutionStatus',
'GcsLocation',
'InlineData',
Expand All @@ -137,4 +138,5 @@
'QuantumResult',
'QuantumTimeSlot',
'SchedulingConfig',
'DeviceConfigKey',
)
6 changes: 4 additions & 2 deletions cirq-google/cirq_google/cloud/quantum_v1alpha1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
from .types.engine import UpdateQuantumJobRequest
from .types.engine import UpdateQuantumProgramRequest
from .types.engine import UpdateQuantumReservationRequest
from .types.quantum import DeviceConfigKey
from .types.quantum import DeviceConfigSelector
from .types.quantum import ExecutionStatus
from .types.quantum import GcsLocation
from .types.quantum import InlineData
Expand All @@ -73,6 +73,7 @@
from .types.quantum import QuantumResult
from .types.quantum import QuantumTimeSlot
from .types.quantum import SchedulingConfig
from .types.quantum import DeviceConfigKey

__all__ = (
'QuantumEngineServiceAsyncClient',
Expand All @@ -85,7 +86,7 @@
'DeleteQuantumJobRequest',
'DeleteQuantumProgramRequest',
'DeleteQuantumReservationRequest',
'DeviceConfigKey',
'DeviceConfigSelector',
'ExecutionStatus',
'GcsLocation',
'GetQuantumCalibrationRequest',
Expand Down Expand Up @@ -133,4 +134,5 @@
'UpdateQuantumJobRequest',
'UpdateQuantumProgramRequest',
'UpdateQuantumReservationRequest',
'DeviceConfigKey',
)
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
)
from .quantum import (
DeviceConfigKey,
DeviceConfigSelector,
ExecutionStatus,
GcsLocation,
InlineData,
Expand Down Expand Up @@ -115,7 +116,7 @@
'UpdateQuantumJobRequest',
'UpdateQuantumProgramRequest',
'UpdateQuantumReservationRequest',
'DeviceConfigKey',
'DeviceConfigSelector',
'ExecutionStatus',
'GcsLocation',
'InlineData',
Expand All @@ -131,4 +132,5 @@
'QuantumResult',
'QuantumTimeSlot',
'SchedulingConfig',
'DeviceConfigKey',
)
26 changes: 23 additions & 3 deletions cirq-google/cirq_google/cloud/quantum_v1alpha1/types/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
'QuantumReservationBudget',
'QuantumTimeSlot',
'QuantumReservation',
'DeviceConfigSelector',
'DeviceConfigKey',
},
)

Expand Down Expand Up @@ -152,7 +154,7 @@ class QuantumJob(proto.Message):
run_context = proto.Field(proto.MESSAGE, number=11, message=any_pb2.Any)


class DeviceConfigKey(proto.Message):
class DeviceConfigSelector(proto.Message):
r"""-
Attributes:
run_name (str):
Expand Down Expand Up @@ -185,13 +187,13 @@ class ProcessorSelector(proto.Message):
-
processor (str):
-
device_config_key ((google.cloud.quantum_v1alpha1.types.DeviceConfigKey):
device_config_selector ((google.cloud.quantum_v1alpha1.types.DeviceConfigSelector):
-
"""

processor_names = proto.RepeatedField(proto.STRING, number=1)
processor = proto.Field(proto.STRING, number=2)
device_config_key = proto.Field(proto.MESSAGE, number=3, message=DeviceConfigKey)
device_config_selector = proto.Field(proto.MESSAGE, number=3, message=DeviceConfigSelector)

target_route = proto.Field(proto.STRING, number=1)
processor_selector = proto.Field(proto.MESSAGE, number=3, message=ProcessorSelector)
Expand Down Expand Up @@ -378,6 +380,9 @@ class QuantumProcessor(proto.Message):
Output only. -
activity_stats (google.cloud.quantum_v1alpha1.types.QuantumProcessor.ActivityStats):
-
default_device_config_key (google.cloud.quantum_v1alpha1.types.DeviceConfigKey):
-

"""

class Health(proto.Enum):
Expand Down Expand Up @@ -413,6 +418,7 @@ class ActivityStats(proto.Message):
current_calibration = proto.Field(proto.STRING, number=10)
active_time_slot = proto.Field(proto.MESSAGE, number=11, message='QuantumTimeSlot')
activity_stats = proto.Field(proto.MESSAGE, number=12, message=ActivityStats)
default_device_config_key = proto.Field(proto.MESSAGE, number=13, message='DeviceConfigKey')


class QuantumCalibration(proto.Message):
Expand Down Expand Up @@ -605,4 +611,18 @@ class QuantumReservation(proto.Message):
whitelisted_users = proto.RepeatedField(proto.STRING, number=5)


class DeviceConfigKey(proto.Message):
r"""-

Attributes:
run (str):
-
config_alias (str):
-
"""

run = proto.Field(proto.STRING, number=1)
config_alias = proto.Field(proto.STRING, number=2)


__all__ = tuple(sorted(__protobuf__.manifest))
4 changes: 2 additions & 2 deletions cirq-google/cirq_google/engine/engine_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ async def create_job_async(
scheduling_config=quantum.SchedulingConfig(
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor=_processor_name_from_ids(project_id, processor_id),
device_config_key=quantum.DeviceConfigKey(
device_config_selector=quantum.DeviceConfigSelector(
run_name=run_name, config_alias=device_config_name
),
)
Expand Down Expand Up @@ -816,7 +816,7 @@ def run_job_over_stream(
scheduling_config=quantum.SchedulingConfig(
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor=_processor_name_from_ids(project_id, processor_id),
device_config_key=quantum.DeviceConfigKey(
device_config_selector=quantum.DeviceConfigSelector(
run_name=run_name, config_alias=device_config_name
),
)
Expand Down
34 changes: 22 additions & 12 deletions cirq-google/cirq_google/engine/engine_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ def test_create_job(client_constructor):
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(run_name="", config_alias=""),
device_config_selector=quantum.DeviceConfigSelector(
run_name="", config_alias=""
),
),
),
description='A job',
Expand All @@ -396,7 +398,9 @@ def test_create_job(client_constructor):
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(run_name="", config_alias=""),
device_config_selector=quantum.DeviceConfigSelector(
run_name="", config_alias=""
),
),
),
description='A job',
Expand All @@ -417,7 +421,9 @@ def test_create_job(client_constructor):
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(run_name="", config_alias=""),
device_config_selector=quantum.DeviceConfigSelector(
run_name="", config_alias=""
),
),
),
labels=labels,
Expand All @@ -439,7 +445,9 @@ def test_create_job(client_constructor):
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(run_name="", config_alias=""),
device_config_selector=quantum.DeviceConfigSelector(
run_name="", config_alias=""
),
),
),
),
Expand All @@ -463,7 +471,9 @@ def test_create_job(client_constructor):
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(run_name="", config_alias=""),
device_config_selector=quantum.DeviceConfigSelector(
run_name="", config_alias=""
),
),
),
),
Expand Down Expand Up @@ -573,7 +583,7 @@ def test_create_job_with_run_name_and_device_config_name(
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(
device_config_selector=quantum.DeviceConfigSelector(
run_name=run_name, config_alias=device_config_name
),
),
Expand Down Expand Up @@ -615,7 +625,7 @@ def test_create_job_with_run_name_and_device_config_name(
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(),
device_config_selector=quantum.DeviceConfigSelector(),
),
),
description='A job',
Expand Down Expand Up @@ -649,7 +659,7 @@ def test_create_job_with_run_name_and_device_config_name(
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(),
device_config_selector=quantum.DeviceConfigSelector(),
),
),
description='A job',
Expand Down Expand Up @@ -680,7 +690,7 @@ def test_create_job_with_run_name_and_device_config_name(
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(),
device_config_selector=quantum.DeviceConfigSelector(),
),
),
description='A job',
Expand Down Expand Up @@ -717,7 +727,7 @@ def test_create_job_with_run_name_and_device_config_name(
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(),
device_config_selector=quantum.DeviceConfigSelector(),
),
),
description='A job',
Expand Down Expand Up @@ -752,7 +762,7 @@ def test_create_job_with_run_name_and_device_config_name(
priority=10,
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(),
device_config_selector=quantum.DeviceConfigSelector(),
),
),
),
Expand Down Expand Up @@ -784,7 +794,7 @@ def test_create_job_with_run_name_and_device_config_name(
scheduling_config=quantum.SchedulingConfig(
processor_selector=quantum.SchedulingConfig.ProcessorSelector(
processor='projects/proj/processors/processor0',
device_config_key=quantum.DeviceConfigKey(),
device_config_selector=quantum.DeviceConfigSelector(),
)
),
),
Expand Down
21 changes: 19 additions & 2 deletions cirq-google/cirq_google/engine/engine_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

import datetime

import logging
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you know if there's a policy on logging in Cirq? I only see it used for tests in Google-managed code (cirq-core and cirq-google). I do see some examples of print statements coupled with a verbosity flag, e.g.:

from typing import Dict, List, Optional, TYPE_CHECKING, Union

from google.protobuf import any_pb2

import cirq
from cirq_google.cloud import quantum
from cirq_google.api import v2
from cirq_google.cloud import quantum
from cirq_google.devices import grid_device
from cirq_google.engine import abstract_processor, calibration, processor_sampler, util

Expand Down Expand Up @@ -80,6 +81,8 @@ def __init__(
self.context = context
self._processor = _processor

logging.basicConfig(level=logging.INFO)

def __repr__(self) -> str:
return (
f'<EngineProcessor: processor_id={self.processor_id!r}, '
Expand Down Expand Up @@ -110,8 +113,22 @@ def get_sampler(
Returns:
A `cirq.Sampler` instance (specifically a `engine_sampler.ProcessorSampler`
that will send circuits to the Quantum Computing Service
when sampled.1
when sampled.
"""
processor = self._inner_processor()
# If a run_name or config_alias is not provided, initialize them
# to the Processor's default values.
if not run_name or not device_config_name:
logging.info(
"Cannot specify only one of `run_name` and `device_config_name`. "
Copy link
Collaborator

Choose a reason for hiding this comment

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

If a user tried to specify a config then they probably didn't intend to use the default. If we automatically fix it for them, then they may not notice and wind up running the experiment with a different config than they expected. They should acknowledge the issue by fixing the incorrect config, so we should probably still raise an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I considered this, but thought logging that we're switching to the default values would be sufficient since there's precedent for switching to a simulator if the user is unable to connect to the quantum_engine. Example.

But, I think this is a good concern so I opted to only initialize with the default value if run_name and config_alias are not specified.

"Using the Processor's default values instead.\n"
"Default run_name: `%s`\n"
"Default device_config_name: `%s`\n",
processor.default_device_config_key.run,
processor.default_device_config_key.config_alias,
)
run_name = processor.default_device_config_key.run
device_config_name = processor.default_device_config_key.config_alias
return processor_sampler.ProcessorSampler(
processor=self, run_name=run_name, device_config_name=device_config_name
)
Expand Down
Loading