Skip to content

Commit

Permalink
Removed deprecated processor_ids (#6563)
Browse files Browse the repository at this point in the history
* removed deprecated processor_ids in Engine, EngineProgram and EngineClient

* Made processor_id required, engine.md updated

* Update engine.md

removed the `[]` for processor_id

* processor_id required in Engine, omitted a condition in EngineClient

* Format and lint changes made

---------

Co-authored-by: Pavol Juhas <juhas@google.com>
  • Loading branch information
JaShom and pavoljuhas authored Apr 26, 2024
1 parent 5828147 commit 2474d47
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 195 deletions.
40 changes: 7 additions & 33 deletions cirq-google/cirq_google/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import enum
import random
import string
from typing import Dict, List, Optional, Sequence, Set, TypeVar, Union, TYPE_CHECKING
from typing import Dict, List, Optional, Set, TypeVar, Union, TYPE_CHECKING

import duet
import google.auth
Expand Down Expand Up @@ -210,21 +210,19 @@ def __init__(
def __str__(self) -> str:
return f'Engine(project_id={self.project_id!r})'

# TODO(#6271): Deprecate and remove processor_ids before v1.4
def run(
self,
program: cirq.AbstractCircuit,
processor_id: str,
program_id: Optional[str] = None,
job_id: Optional[str] = None,
param_resolver: cirq.ParamResolver = cirq.ParamResolver({}),
repetitions: int = 1,
processor_ids: Sequence[str] = ('xmonsim',),
program_description: Optional[str] = None,
program_labels: Optional[Dict[str, str]] = None,
job_description: Optional[str] = None,
job_labels: Optional[Dict[str, str]] = None,
*,
processor_id: str = "",
run_name: str = "",
device_config_name: str = "",
) -> cirq.Result:
Expand All @@ -244,15 +242,11 @@ def run(
and day.
param_resolver: Parameters to run with the program.
repetitions: The number of repetitions to simulate.
processor_ids: Deprecated list of candidate processor ids to run the program.
Only allowed to contain one processor_id. If the argument `processor_id`
is non-empty, `processor_ids` will be ignored.
program_description: An optional description to set on the program.
program_labels: Optional set of labels to set on the program.
job_description: An optional description to set on the job.
job_labels: Optional set of labels to set on the job.
processor_id: Processor id for running the program. If not set,
`processor_ids` will be used.
processor_id: Processor id for running the program.
run_name: A unique identifier representing an automation run for the
specified processor. An Automation Run contains a collection of
device configurations for a processor. If specified, `processor_id`
Expand All @@ -267,9 +261,7 @@ def run(
Raises:
ValueError: If no gate set is provided.
ValueError: If neither `processor_id` or `processor_ids` are set.
ValueError: If only one of `run_name` and `device_config_name` are specified.
ValueError: If `processor_ids` has more than one processor id.
ValueError: If either `run_name` and `device_config_name` are set but
`processor_id` is empty.
"""
Expand All @@ -280,32 +272,29 @@ def run(
job_id=job_id,
params=[param_resolver],
repetitions=repetitions,
processor_ids=processor_ids,
processor_id=processor_id,
program_description=program_description,
program_labels=program_labels,
job_description=job_description,
job_labels=job_labels,
processor_id=processor_id,
run_name=run_name,
device_config_name=device_config_name,
)
)[0]

# TODO(#6271): Deprecate and remove processor_ids before v1.4
async def run_sweep_async(
self,
program: cirq.AbstractCircuit,
processor_id: str,
program_id: Optional[str] = None,
job_id: Optional[str] = None,
params: cirq.Sweepable = None,
repetitions: int = 1,
processor_ids: Sequence[str] = ('xmonsim',),
program_description: Optional[str] = None,
program_labels: Optional[Dict[str, str]] = None,
job_description: Optional[str] = None,
job_labels: Optional[Dict[str, str]] = None,
*,
processor_id: str = "",
run_name: str = "",
device_config_name: str = "",
) -> engine_job.EngineJob:
Expand All @@ -328,15 +317,11 @@ async def run_sweep_async(
and day.
params: Parameters to run with the program.
repetitions: The number of circuit repetitions to run.
processor_ids: Deprecated list of candidate processor ids to run the program.
Only allowed to contain one processor_id. If the argument `processor_id`
is non-empty, `processor_ids` will be ignored.
program_description: An optional description to set on the program.
program_labels: Optional set of labels to set on the program.
job_description: An optional description to set on the job.
job_labels: Optional set of labels to set on the job.
processor_id: Processor id for running the program. If not set,
`processor_ids` will be used.
processor_id: Processor id for running the program.
run_name: A unique identifier representing an automation run for the
specified processor. An Automation Run contains a collection of
device configurations for a processor. If specified, `processor_id`
Expand All @@ -352,22 +337,12 @@ async def run_sweep_async(
Raises:
ValueError: If no gate set is provided.
ValueError: If neither `processor_id` or `processor_ids` are set.
ValueError: If only one of `run_name` and `device_config_name` are specified.
ValueError: If `processor_ids` has more than one processor id.
ValueError: If either `run_name` and `device_config_name` are set but
`processor_id` is empty.
"""

if self.context.enable_streaming:
# This logic is temporary prior to deprecating the processor_ids parameter.
# TODO(#6271) Remove after deprecating processor_ids elsewhere prior to v1.4.
if processor_ids:
if len(processor_ids) > 1:
raise ValueError("The use of multiple processors is no longer supported.")
if len(processor_ids) == 1 and not processor_id:
processor_id = processor_ids[0]

if not program_id:
program_id = _make_random_id('prog-')
if not job_id:
Expand Down Expand Up @@ -403,10 +378,9 @@ async def run_sweep_async(
job_id=job_id,
params=params,
repetitions=repetitions,
processor_ids=processor_ids,
processor_id=processor_id,
description=job_description,
labels=job_labels,
processor_id=processor_id,
run_name=run_name,
device_config_name=device_config_name,
)
Expand Down
65 changes: 4 additions & 61 deletions cirq-google/cirq_google/engine/engine_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
Dict,
List,
Optional,
Sequence,
Set,
TypeVar,
Tuple,
Expand All @@ -36,7 +35,6 @@
from google.protobuf import any_pb2, field_mask_pb2
from google.protobuf.timestamp_pb2 import Timestamp

from cirq._compat import deprecated_parameter
from cirq_google.cloud import quantum
from cirq_google.engine.asyncio_executor import AsyncioExecutor
from cirq_google.engine import stream_manager
Expand Down Expand Up @@ -378,25 +376,17 @@ async def delete_program_async(

delete_program = duet.sync(delete_program_async)

@deprecated_parameter(
deadline='v1.4',
fix='Use `processor_id` instead of `processor_ids`.',
parameter_desc='processor_ids',
match=lambda args, kwargs: _match_deprecated_processor_ids(args, kwargs),
rewrite=lambda args, kwargs: rewrite_processor_ids_to_processor_id(args, kwargs),
)
async def create_job_async(
self,
project_id: str,
program_id: str,
job_id: Optional[str],
processor_ids: Optional[Sequence[str]] = None,
processor_id: str,
run_context: any_pb2.Any = any_pb2.Any(),
priority: Optional[int] = None,
description: Optional[str] = None,
labels: Optional[Dict[str, str]] = None,
*,
processor_id: str = "",
run_name: str = "",
device_config_name: str = "",
) -> Tuple[str, quantum.QuantumJob]:
Expand All @@ -411,16 +401,10 @@ async def create_job_async(
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
run_context: Properly serialized run context.
processor_ids: Deprecated list of candidate processor ids to run the program.
Only allowed to contain one processor_id. If the argument `processor_id`
is non-empty, `processor_ids` will be ignored. Otherwise the deprecated
decorator will fix the arguments and call create_job_async using
`processor_id` instead of `processor_ids`.
priority: Optional priority to run at, 0-1000.
description: Optional description to set on the job.
labels: Optional set of labels to set on the job.
processor_id: Processor id for running the program. If not set,
`processor_ids` will be used.
processor_id: Processor id for running the program.
run_name: A unique identifier representing an automation run for the
specified processor. An Automation Run contains a collection of
device configurations for a processor. If specified, `processor_id`
Expand All @@ -434,9 +418,7 @@ async def create_job_async(
Raises:
ValueError: If the priority is not between 0 and 1000.
ValueError: If neither `processor_id` or `processor_ids` are set.
ValueError: If only one of `run_name` and `device_config_name` are specified.
ValueError: If `processor_ids` has more than one processor id.
ValueError: If either `run_name` and `device_config_name` are set but
`processor_id` is empty.
"""
Expand Down Expand Up @@ -474,8 +456,7 @@ async def create_job_async(
job = await self._send_request_async(self.grpc_client.create_quantum_job, request)
return _ids_from_job_name(job.name)[2], job

# TODO(cxing): Remove type ignore once @deprecated_parameter decorator is removed
create_job = duet.sync(create_job_async) # type: ignore
create_job = duet.sync(create_job_async)

async def list_jobs_async(
self,
Expand Down Expand Up @@ -775,8 +756,7 @@ def run_job_over_stream(
priority: Optional priority to run at, 0-1000.
job_description: Optional description to set on the job.
job_labels: Optional set of labels to set on the job.
processor_id: Processor id for running the program. If not set,
`processor_ids` will be used.
processor_id: Processor id for running the program.
run_name: A unique identifier representing an automation run for the
specified processor. An Automation Run contains a collection of
device configurations for a processor. If specified, `processor_id`
Expand Down Expand Up @@ -1225,40 +1205,3 @@ def _date_or_time_to_filter_expr(param_name: str, param: Union[datetime.datetime
f"type {type(param)}. Supported types: datetime.datetime and"
f"datetime.date"
)


def rewrite_processor_ids_to_processor_id(args, kwargs):
"""Rewrites the create_job parameters so that `processor_id` is used instead of the deprecated
`processor_ids`.
Raises:
ValueError: If `processor_ids` has more than one processor id.
ValueError: If `run_name` or `device_config_name` are set but `processor_id` is not.
"""

# Use `processor_id` keyword argument instead of `processor_ids`
processor_ids = args[4] if len(args) > 4 else kwargs['processor_ids']
if len(processor_ids) > 1:
raise ValueError("The use of multiple processors is no longer supported.")
if 'processor_id' not in kwargs or not kwargs['processor_id']:
if ('run_name' in kwargs and kwargs['run_name']) or (
'device_config_name' in kwargs and kwargs['device_config_name']
):
raise ValueError(
'Cannot specify `run_name` or `device_config_name` if `processor_id` is empty.'
)
kwargs['processor_id'] = processor_ids[0]

# Erase `processor_ids` from args and kwargs
if len(args) > 4:
args_list = list(args)
args_list[4] = None
args = tuple(args_list)
else:
kwargs.pop('processor_ids')

return args, kwargs


def _match_deprecated_processor_ids(args, kwargs):
return ('processor_ids' in kwargs and kwargs['processor_ids']) or len(args) > 4
Loading

0 comments on commit 2474d47

Please sign in to comment.