Skip to content

Commit

Permalink
Deprecate gate_set parameter on engine classes. (quantumlib#5207)
Browse files Browse the repository at this point in the history
Fixes quantumlib#4995

Review: @verult
  • Loading branch information
maffoo authored and rht committed May 1, 2023
1 parent dde0854 commit ab0fbdb
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 79 deletions.
20 changes: 8 additions & 12 deletions cirq-google/cirq_google/calibration/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
LocalXEBPhasedFSimCalibrationRequest,
)
from cirq_google.calibration.xeb_wrapper import run_local_xeb_calibration
from cirq_google.engine import Engine, QuantumEngineSampler
from cirq_google.engine import Engine, QuantumEngineSampler, util
from cirq_google.serialization.serializer import Serializer

_CALIBRATION_IRRELEVANT_GATES = cirq.MeasurementGate, cirq.SingleQubitGate, cirq.WaitGate
Expand Down Expand Up @@ -744,7 +744,6 @@ def _run_calibrations_via_engine(
calibration_requests: Sequence[PhasedFSimCalibrationRequest],
engine: Engine,
processor_id: str,
gate_set: Serializer,
max_layers_per_request: int = 1,
progress_func: Optional[Callable[[int, int], None]] = None,
):
Expand All @@ -763,7 +762,7 @@ def _run_calibrations_via_engine(
]

for cal_layers in nested_calibration_layers:
job = engine.run_calibration(cal_layers, processor_id=processor_id, gate_set=gate_set)
job = engine.run_calibration(cal_layers, processor_id=processor_id)
request_results = job.calibration_results()
results += [
calibration.parse_result(result, job)
Expand All @@ -787,6 +786,7 @@ def _run_local_calibrations_via_sampler(
]


@util.deprecated_gate_set_parameter
def run_calibrations(
calibrations: Sequence[PhasedFSimCalibrationRequest],
sampler: Union[Engine, cirq.Sampler],
Expand Down Expand Up @@ -842,25 +842,21 @@ def run_calibrations(
elif isinstance(sampler, QuantumEngineSampler):
engine = sampler.engine
(processor_id,) = sampler._processor_ids
gate_set = sampler._gate_set
else:
engine = None

if engine is not None:
if processor_id is None:
raise ValueError('processor_id must be provided.')
if gate_set is None:
raise ValueError('gate_set must be provided.')
raise ValueError('processor_id must be provided.') # coverage: ignore

if calibration_request_type == LocalXEBPhasedFSimCalibrationRequest:
sampler = engine.get_sampler(processor_id=processor_id, gate_set=gate_set)
return _run_local_calibrations_via_sampler(calibrations, sampler)
engine_sampler = engine.get_sampler(processor_id=processor_id)
return _run_local_calibrations_via_sampler(calibrations, engine_sampler)

return _run_calibrations_via_engine(
calibrations,
engine,
processor_id,
gate_set,
max_layers_per_request,
progress_func,
)
Expand Down Expand Up @@ -1186,6 +1182,7 @@ def as_circuit(self) -> cirq.Circuit:
return cirq.Circuit(self.operations)


@util.deprecated_gate_set_parameter
def run_floquet_characterization_for_moments(
circuit: cirq.Circuit,
sampler: Union[Engine, cirq.Sampler],
Expand Down Expand Up @@ -1250,13 +1247,13 @@ def run_floquet_characterization_for_moments(
requests,
sampler,
processor_id,
gate_set,
max_layers_per_request=max_layers_per_request,
progress_func=progress_func,
)
return circuit_calibration, results


@util.deprecated_gate_set_parameter
def run_zeta_chi_gamma_compensation_for_moments(
circuit: cirq.Circuit,
sampler: Union[Engine, cirq.Sampler],
Expand Down Expand Up @@ -1325,7 +1322,6 @@ def run_zeta_chi_gamma_compensation_for_moments(
calibrations=requests,
sampler=sampler,
processor_id=processor_id,
gate_set=gate_set,
max_layers_per_request=max_layers_per_request,
progress_func=progress_func,
)
Expand Down
15 changes: 5 additions & 10 deletions cirq-google/cirq_google/calibration/workflow_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,9 +1021,7 @@ def test_run_calibrations():
engine = mock.MagicMock(spec=cirq_google.Engine)
engine.run_calibration.return_value = job

sampler = cirq_google.QuantumEngineSampler(
engine=engine, processor_id='qproc', gate_set=cirq_google.FSIM_GATESET
)
sampler = cirq_google.QuantumEngineSampler(engine=engine, processor_id='qproc')

progress_calls = []

Expand Down Expand Up @@ -1127,9 +1125,7 @@ def test_run_characterization_with_engine():
def progress(step: int, steps: int) -> None:
progress_calls.append((step, steps))

actual = workflow.run_calibrations(
[request], engine, 'qproc', cirq_google.FSIM_GATESET, progress_func=progress
)
actual = workflow.run_calibrations([request], engine, 'qproc', progress_func=progress)

expected = [
PhasedFSimCalibrationResult(
Expand Down Expand Up @@ -1160,7 +1156,7 @@ def progress(step: int, steps: int) -> None:


def test_run_calibrations_empty():
assert workflow.run_calibrations([], None, 'qproc', cirq_google.FSIM_GATESET) == []
assert workflow.run_calibrations([], None, 'qproc') == []


def test_run_calibrations_fails_when_invalid_arguments():
Expand Down Expand Up @@ -1289,7 +1285,7 @@ def test_run_floquet_characterization_for_moments():
engine.run_calibration.return_value = job

circuit_with_calibration, requests = workflow.run_floquet_characterization_for_moments(
circuit, engine, 'qproc', cirq_google.FSIM_GATESET, options=options
circuit, engine, 'qproc', options=options
)

assert requests == [
Expand Down Expand Up @@ -1630,7 +1626,6 @@ def test_run_zeta_chi_gamma_calibration_for_moments() -> None:
circuit,
engine_simulator,
processor_id=None,
gate_set=cirq_google.FSIM_GATESET,
options=options,
)

Expand Down Expand Up @@ -1671,7 +1666,7 @@ def test_run_zeta_chi_gamma_calibration_for_moments_no_chi() -> None:
)

calibrated_circuit, *_ = workflow.run_zeta_chi_gamma_compensation_for_moments(
circuit, engine_simulator, processor_id=None, gate_set=cirq_google.SQRT_ISWAP_GATESET
circuit, engine_simulator, processor_id=None
)

assert cirq.allclose_up_to_global_phase(
Expand Down
3 changes: 2 additions & 1 deletion cirq-google/cirq_google/engine/abstract_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import cirq
from cirq_google.cloud import quantum
from cirq_google.engine import abstract_job, abstract_program, abstract_processor
from cirq_google.engine import abstract_job, abstract_program, abstract_processor, util
from cirq_google.serialization import Serializer

VALID_DATE_TYPE = Union[datetime.datetime, datetime.date]
Expand Down Expand Up @@ -125,6 +125,7 @@ def get_processor(self, processor_id: str) -> abstract_processor.AbstractProcess
"""

@abc.abstractmethod
@util.deprecated_gate_set_parameter
def get_sampler(
self, processor_id: Union[str, List[str]], gate_set: Optional[Serializer] = None
) -> cirq.Sampler:
Expand Down
4 changes: 3 additions & 1 deletion cirq-google/cirq_google/engine/abstract_local_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import Dict, List, Optional, Sequence, Set, Union, TYPE_CHECKING

import cirq
from cirq_google.engine import util
from cirq_google.engine.abstract_job import AbstractJob
from cirq_google.engine.abstract_program import AbstractProgram
from cirq_google.engine.abstract_local_processor import AbstractLocalProcessor
Expand Down Expand Up @@ -164,6 +165,7 @@ def get_processor(self, processor_id: str) -> AbstractLocalProcessor:
"""
return self._processors[processor_id]

@util.deprecated_gate_set_parameter
def get_sampler(
self, processor_id: Union[str, List[str]], gate_set: Optional[Serializer] = None
) -> cirq.Sampler:
Expand All @@ -180,4 +182,4 @@ def get_sampler(
"""
if not isinstance(processor_id, str):
raise ValueError(f'Invalid processor {processor_id}')
return self._processors[processor_id].get_sampler(gate_set=gate_set)
return self._processors[processor_id].get_sampler()
7 changes: 6 additions & 1 deletion cirq-google/cirq_google/engine/abstract_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import cirq

from cirq_google.api import v2
from cirq_google.engine import calibration
from cirq_google.cloud import quantum
from cirq_google.engine import calibration, util

if TYPE_CHECKING:
import cirq_google
Expand All @@ -53,6 +53,7 @@ class AbstractProcessor(abc.ABC):
This is an abstract class. Inheritors should implement abstract methods.
"""

@util.deprecated_gate_set_parameter
def run(
self,
program: cirq.Circuit,
Expand Down Expand Up @@ -93,6 +94,7 @@ def run(
"""

@abc.abstractmethod
@util.deprecated_gate_set_parameter
def run_sweep(
self,
program: cirq.Circuit,
Expand Down Expand Up @@ -136,6 +138,7 @@ def run_sweep(
"""

@abc.abstractmethod
@util.deprecated_gate_set_parameter
def run_batch(
self,
programs: Sequence[cirq.AbstractCircuit],
Expand Down Expand Up @@ -190,6 +193,7 @@ def run_batch(
"""

@abc.abstractmethod
@util.deprecated_gate_set_parameter
def run_calibration(
self,
layers: List['cirq_google.CalibrationLayer'],
Expand Down Expand Up @@ -236,6 +240,7 @@ def run_calibration(
"""

@abc.abstractmethod
@util.deprecated_gate_set_parameter
def get_sampler(self, gate_set: Optional['serializer.Serializer'] = None) -> cirq.Sampler:
"""Returns a sampler backed by the processor.
Expand Down
24 changes: 15 additions & 9 deletions cirq-google/cirq_google/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def __init__(
def __str__(self) -> str:
return f'Engine(project_id={self.project_id!r})'

@util.deprecated_gate_set_parameter
def run(
self,
program: cirq.AbstractCircuit,
Expand All @@ -216,7 +217,7 @@ def run(
param_resolver: cirq.ParamResolver = cirq.ParamResolver({}),
repetitions: int = 1,
processor_ids: Sequence[str] = ('xmonsim',),
gate_set: Serializer = None,
gate_set: Optional[Serializer] = None,
program_description: Optional[str] = None,
program_labels: Optional[Dict[str, str]] = None,
job_description: Optional[str] = None,
Expand Down Expand Up @@ -262,14 +263,14 @@ def run(
params=[param_resolver],
repetitions=repetitions,
processor_ids=processor_ids,
gate_set=gate_set,
program_description=program_description,
program_labels=program_labels,
job_description=job_description,
job_labels=job_labels,
)
)[0]

@util.deprecated_gate_set_parameter
def run_sweep(
self,
program: cirq.AbstractCircuit,
Expand Down Expand Up @@ -321,7 +322,7 @@ def run_sweep(
ValueError: If no gate set is provided.
"""
engine_program = self.create_program(
program, program_id, gate_set, program_description, program_labels
program, program_id, description=program_description, labels=program_labels
)
return engine_program.run_sweep(
job_id=job_id,
Expand All @@ -332,6 +333,7 @@ def run_sweep(
labels=job_labels,
)

@util.deprecated_gate_set_parameter
def run_batch(
self,
programs: Sequence[cirq.AbstractCircuit],
Expand Down Expand Up @@ -402,7 +404,7 @@ def run_batch(
if not processor_ids:
raise ValueError('Processor id must be specified.')
engine_program = self.create_batch_program(
programs, program_id, gate_set, program_description, program_labels
programs, program_id, description=program_description, labels=program_labels
)
return engine_program.run_batch(
job_id=job_id,
Expand All @@ -413,6 +415,7 @@ def run_batch(
labels=job_labels,
)

@util.deprecated_gate_set_parameter
def run_calibration(
self,
layers: List['cirq_google.CalibrationLayer'],
Expand Down Expand Up @@ -481,7 +484,7 @@ def run_calibration(
if job_labels is None:
job_labels = {'calibration': ''}
engine_program = self.create_calibration_program(
layers, program_id, gate_set, program_description, program_labels
layers, program_id, description=program_description, labels=program_labels
)
return engine_program.run_calibration(
job_id=job_id,
Expand All @@ -490,6 +493,7 @@ def run_calibration(
labels=job_labels,
)

@util.deprecated_gate_set_parameter
def create_program(
self,
program: cirq.AbstractCircuit,
Expand Down Expand Up @@ -533,6 +537,7 @@ def create_program(
self.project_id, new_program_id, self.context, new_program
)

@util.deprecated_gate_set_parameter
def create_batch_program(
self,
programs: Sequence[cirq.AbstractCircuit],
Expand Down Expand Up @@ -582,6 +587,7 @@ def create_batch_program(
self.project_id, new_program_id, self.context, new_program, result_type=ResultType.Batch
)

@util.deprecated_gate_set_parameter
def create_calibration_program(
self,
layers: List['cirq_google.CalibrationLayer'],
Expand Down Expand Up @@ -773,6 +779,7 @@ def get_processor(self, processor_id: str) -> engine_processor.EngineProcessor:
return engine_processor.EngineProcessor(self.project_id, processor_id, self.context)

@deprecated(deadline="v1.0", fix="Use get_sampler instead.")
@util.deprecated_gate_set_parameter
def sampler(
self, processor_id: Union[str, List[str]], gate_set: Optional[Serializer] = None
) -> engine_sampler.QuantumEngineSampler:
Expand All @@ -789,8 +796,9 @@ def sampler(
that will send circuits to the Quantum Computing Service
when sampled.
"""
return self.get_sampler(processor_id, gate_set)
return self.get_sampler(processor_id)

@util.deprecated_gate_set_parameter
def get_sampler(
self, processor_id: Union[str, List[str]], gate_set: Optional[Serializer] = None
) -> engine_sampler.QuantumEngineSampler:
Expand All @@ -807,9 +815,7 @@ def get_sampler(
that will send circuits to the Quantum Computing Service
when sampled.
"""
return engine_sampler.QuantumEngineSampler(
engine=self, processor_id=processor_id, gate_set=gate_set
)
return engine_sampler.QuantumEngineSampler(engine=self, processor_id=processor_id)


def get_engine(project_id: Optional[str] = None) -> Engine:
Expand Down
Loading

0 comments on commit ab0fbdb

Please sign in to comment.