diff --git a/cirq-google/cirq_google/calibration/workflow.py b/cirq-google/cirq_google/calibration/workflow.py index 2ec303d12c6..5ba99f102c8 100644 --- a/cirq-google/cirq_google/calibration/workflow.py +++ b/cirq-google/cirq_google/calibration/workflow.py @@ -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 @@ -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, ): @@ -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) @@ -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], @@ -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, ) @@ -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], @@ -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], @@ -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, ) diff --git a/cirq-google/cirq_google/calibration/workflow_test.py b/cirq-google/cirq_google/calibration/workflow_test.py index fad570e1403..6c0ffe14e1e 100644 --- a/cirq-google/cirq_google/calibration/workflow_test.py +++ b/cirq-google/cirq_google/calibration/workflow_test.py @@ -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 = [] @@ -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( @@ -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(): @@ -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 == [ @@ -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, ) @@ -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( diff --git a/cirq-google/cirq_google/engine/abstract_engine.py b/cirq-google/cirq_google/engine/abstract_engine.py index c331d01c27c..456cead9cf0 100644 --- a/cirq-google/cirq_google/engine/abstract_engine.py +++ b/cirq-google/cirq_google/engine/abstract_engine.py @@ -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] @@ -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: diff --git a/cirq-google/cirq_google/engine/abstract_local_engine.py b/cirq-google/cirq_google/engine/abstract_local_engine.py index efc36eef73b..d1f35d09853 100644 --- a/cirq-google/cirq_google/engine/abstract_local_engine.py +++ b/cirq-google/cirq_google/engine/abstract_local_engine.py @@ -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 @@ -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: @@ -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() diff --git a/cirq-google/cirq_google/engine/abstract_processor.py b/cirq-google/cirq_google/engine/abstract_processor.py index 15c508cb627..f7cbe6e933c 100644 --- a/cirq-google/cirq_google/engine/abstract_processor.py +++ b/cirq-google/cirq_google/engine/abstract_processor.py @@ -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 @@ -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, @@ -93,6 +94,7 @@ def run( """ @abc.abstractmethod + @util.deprecated_gate_set_parameter def run_sweep( self, program: cirq.Circuit, @@ -136,6 +138,7 @@ def run_sweep( """ @abc.abstractmethod + @util.deprecated_gate_set_parameter def run_batch( self, programs: Sequence[cirq.AbstractCircuit], @@ -190,6 +193,7 @@ def run_batch( """ @abc.abstractmethod + @util.deprecated_gate_set_parameter def run_calibration( self, layers: List['cirq_google.CalibrationLayer'], @@ -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. diff --git a/cirq-google/cirq_google/engine/engine.py b/cirq-google/cirq_google/engine/engine.py index a09a27e8cbb..edf86ea33cd 100644 --- a/cirq-google/cirq_google/engine/engine.py +++ b/cirq-google/cirq_google/engine/engine.py @@ -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, @@ -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, @@ -262,7 +263,6 @@ 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, @@ -270,6 +270,7 @@ def run( ) )[0] + @util.deprecated_gate_set_parameter def run_sweep( self, program: cirq.AbstractCircuit, @@ -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, @@ -332,6 +333,7 @@ def run_sweep( labels=job_labels, ) + @util.deprecated_gate_set_parameter def run_batch( self, programs: Sequence[cirq.AbstractCircuit], @@ -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, @@ -413,6 +415,7 @@ def run_batch( labels=job_labels, ) + @util.deprecated_gate_set_parameter def run_calibration( self, layers: List['cirq_google.CalibrationLayer'], @@ -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, @@ -490,6 +493,7 @@ def run_calibration( labels=job_labels, ) + @util.deprecated_gate_set_parameter def create_program( self, program: cirq.AbstractCircuit, @@ -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], @@ -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'], @@ -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: @@ -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: @@ -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: diff --git a/cirq-google/cirq_google/engine/engine_processor.py b/cirq-google/cirq_google/engine/engine_processor.py index ca57e7a0037..9edb77c038b 100644 --- a/cirq-google/cirq_google/engine/engine_processor.py +++ b/cirq-google/cirq_google/engine/engine_processor.py @@ -102,6 +102,7 @@ def engine(self) -> 'engine_base.Engine': return engine_base.Engine(self.project_id, context=self.context) + @util.deprecated_gate_set_parameter def get_sampler( self, gate_set: Optional[serializer.Serializer] = None ) -> engine_sampler.QuantumEngineSampler: @@ -117,9 +118,10 @@ def get_sampler( when sampled.1 """ return engine_sampler.QuantumEngineSampler( - engine=self.engine(), processor_id=self.processor_id, gate_set=gate_set + engine=self.engine(), processor_id=self.processor_id ) + @util.deprecated_gate_set_parameter def run_batch( self, programs: Sequence[cirq.AbstractCircuit], @@ -178,13 +180,13 @@ def run_batch( program_id=program_id, params_list=list(params_list) if params_list is not None else None, repetitions=repetitions, - gate_set=gate_set, program_description=program_description, program_labels=program_labels, job_description=job_description, job_labels=job_labels, ) + @util.deprecated_gate_set_parameter def run_calibration( self, layers: List[calibration_layer.CalibrationLayer], @@ -235,13 +237,13 @@ def run_calibration( processor_id=self.processor_id, program_id=program_id, job_id=job_id, - gate_set=gate_set, program_description=program_description, program_labels=program_labels, job_description=job_description, job_labels=job_labels, ) + @util.deprecated_gate_set_parameter def run_sweep( self, program: cirq.Circuit, @@ -291,7 +293,6 @@ def run_sweep( job_id=job_id, params=params, repetitions=repetitions, - gate_set=gate_set, program_description=program_description, program_labels=program_labels, job_description=job_description, diff --git a/cirq-google/cirq_google/engine/engine_processor_test.py b/cirq-google/cirq_google/engine/engine_processor_test.py index 66bdb9b2ad9..315703c7f80 100644 --- a/cirq-google/cirq_google/engine/engine_processor_test.py +++ b/cirq-google/cirq_google/engine/engine_processor_test.py @@ -842,7 +842,6 @@ def test_run_sweep_params(client): job = processor.run_sweep( program=_CIRCUIT, params=[cirq.ParamResolver({'a': 1}), cirq.ParamResolver({'a': 2})], - gate_set=cg.XMON, ) results = job.results() assert len(results) == 2 @@ -882,7 +881,6 @@ def test_run_batch(client): processor = cg.EngineProcessor('a', 'p', EngineContext()) job = processor.run_batch( - gate_set=cg.XMON, programs=[_CIRCUIT, _CIRCUIT], job_id='job-id', params_list=[cirq.Points('a', [1, 2]), cirq.Points('a', [3, 4])], @@ -932,9 +930,7 @@ def test_run_calibration(client): 'readout', cirq.Circuit(cirq.measure(q1, q2)), {'num_samples': 4242} ) processor = cg.EngineProcessor('proj', 'mysim', EngineContext()) - job = processor.run_calibration( - gate_set=cg.FSIM_GATESET, layers=[layer1, layer2], job_id='job-id' - ) + job = processor.run_calibration(layers=[layer1, layer2], job_id='job-id') results = job.calibration_results() assert len(results) == 2 assert results[0].code == v2.calibration_pb2.SUCCESS @@ -973,7 +969,7 @@ def test_sampler(client): client().get_job.return_value = quantum.QuantumJob(execution_status={'state': 'SUCCESS'}) client().get_job_results.return_value = quantum.QuantumResult(result=util.pack_any(_RESULTS_V2)) processor = cg.EngineProcessor('proj', 'mysim', EngineContext()) - sampler = processor.get_sampler(gate_set=cg.XMON) + sampler = processor.get_sampler() results = sampler.run_sweep( program=_CIRCUIT, params=[cirq.ParamResolver({'a': 1}), cirq.ParamResolver({'a': 2})] ) diff --git a/cirq-google/cirq_google/engine/engine_sampler.py b/cirq-google/cirq_google/engine/engine_sampler.py index e4921c36b79..4af074b5923 100644 --- a/cirq-google/cirq_google/engine/engine_sampler.py +++ b/cirq-google/cirq_google/engine/engine_sampler.py @@ -16,7 +16,7 @@ import cirq from cirq_google import engine -from cirq_google.serialization import gate_sets +from cirq_google.engine import util if TYPE_CHECKING: import cirq_google @@ -28,6 +28,7 @@ class QuantumEngineSampler(cirq.Sampler): Exposes a `cirq_google.Engine` instance as a `cirq.Sampler`. """ + @util.deprecated_gate_set_parameter def __init__( self, *, @@ -45,7 +46,6 @@ def __init__( samples. """ self._processor_ids = [processor_id] if isinstance(processor_id, str) else processor_id - self._gate_set = gate_set self._engine = engine def run_sweep( @@ -64,7 +64,6 @@ def run_sweep( params=params, repetitions=repetitions, processor_ids=self._processor_ids, - gate_set=self._gate_set, ) return job.results() @@ -97,7 +96,6 @@ def run_batch( params_list=params_list, repetitions=repetitions, processor_ids=self._processor_ids, - gate_set=self._gate_set, ) return job.batched_results() # Varying number of repetitions so no speedup @@ -108,8 +106,14 @@ def engine(self) -> 'cirq_google.Engine': return self._engine +@cirq._compat.deprecated_parameter( + deadline='v0.15', + fix='Remove the "gate_set_name" parameter.', + parameter_desc='gate_set_name', + match=lambda args, kwargs: 'gate_set_name' in kwargs or len(args) > 1, +) def get_engine_sampler( - processor_id: str, gate_set_name: str, project_id: Optional[str] = None + processor_id: str, gate_set_name: str = '', project_id: Optional[str] = None ) -> 'cirq_google.QuantumEngineSampler': """Get an EngineSampler assuming some sensible defaults. @@ -134,10 +138,4 @@ def get_engine_sampler( EnvironmentError: If no project_id is specified and the environment variable GOOGLE_CLOUD_PROJECT is not set. """ - if gate_set_name not in gate_sets.NAMED_GATESETS: - raise ValueError( - f"Unknown gateset {gate_set_name}. Please use one of: " - f"{sorted(gate_sets.NAMED_GATESETS.keys())}." - ) - gate_set = gate_sets.NAMED_GATESETS[gate_set_name] - return engine.get_engine(project_id).get_sampler(processor_id=processor_id, gate_set=gate_set) + return engine.get_engine(project_id).get_sampler(processor_id=processor_id) diff --git a/cirq-google/cirq_google/engine/engine_sampler_test.py b/cirq-google/cirq_google/engine/engine_sampler_test.py index 5285d6e1952..fe5c26b4f61 100644 --- a/cirq-google/cirq_google/engine/engine_sampler_test.py +++ b/cirq-google/cirq_google/engine/engine_sampler_test.py @@ -24,17 +24,17 @@ @pytest.mark.parametrize('circuit', [cirq.Circuit(), cirq.FrozenCircuit()]) def test_run_circuit(circuit): engine = mock.Mock() - sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp', gate_set=cg.XMON) + sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp') params = [cirq.ParamResolver({'a': 1})] sampler.run_sweep(circuit, params, 5) engine.run_sweep.assert_called_with( - gate_set=cg.XMON, params=params, processor_ids=['tmp'], program=circuit, repetitions=5 + params=params, processor_ids=['tmp'], program=circuit, repetitions=5 ) def test_run_engine_program(): engine = mock.Mock() - sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp', gate_set=cg.XMON) + sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp') program = mock.Mock(spec=cg.EngineProgram) params = [cirq.ParamResolver({'a': 1})] sampler.run_sweep(program, params, 5) @@ -44,7 +44,7 @@ def test_run_engine_program(): def test_run_batch(): engine = mock.Mock() - sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp', gate_set=cg.XMON) + sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp') a = cirq.LineQubit(0) circuit1 = cirq.Circuit(cirq.X(a)) circuit2 = cirq.Circuit(cirq.Y(a)) @@ -54,7 +54,6 @@ def test_run_batch(): params_list = [params1, params2] sampler.run_batch(circuits, params_list, 5) engine.run_batch.assert_called_with( - gate_set=cg.XMON, params_list=params_list, processor_ids=['tmp'], programs=circuits, @@ -64,7 +63,7 @@ def test_run_batch(): def test_run_batch_identical_repetitions(): engine = mock.Mock() - sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp', gate_set=cg.XMON) + sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp') a = cirq.LineQubit(0) circuit1 = cirq.Circuit(cirq.X(a)) circuit2 = cirq.Circuit(cirq.Y(a)) @@ -74,7 +73,6 @@ def test_run_batch_identical_repetitions(): params_list = [params1, params2] sampler.run_batch(circuits, params_list, [5, 5]) engine.run_batch.assert_called_with( - gate_set=cg.XMON, params_list=params_list, processor_ids=['tmp'], programs=circuits, @@ -84,7 +82,7 @@ def test_run_batch_identical_repetitions(): def test_run_batch_bad_number_of_repetitions(): engine = mock.Mock() - sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp', gate_set=cg.XMON) + sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp') a = cirq.LineQubit(0) circuit1 = cirq.Circuit(cirq.X(a)) circuit2 = cirq.Circuit(cirq.Y(a)) @@ -101,7 +99,7 @@ def test_run_batch_differing_repetitions(): job = mock.Mock() job.results.return_value = [] engine.run_sweep.return_value = job - sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp', gate_set=cg.XMON) + sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp') a = cirq.LineQubit(0) circuit1 = cirq.Circuit(cirq.X(a)) circuit2 = cirq.Circuit(cirq.Y(a)) @@ -112,33 +110,25 @@ def test_run_batch_differing_repetitions(): repetitions = [1, 2] sampler.run_batch(circuits, params_list, repetitions) engine.run_sweep.assert_called_with( - gate_set=cg.XMON, params=params2, processor_ids=['tmp'], program=circuit2, repetitions=2 + params=params2, processor_ids=['tmp'], program=circuit2, repetitions=2 ) engine.run_batch.assert_not_called() def test_engine_sampler_engine_property(): engine = mock.Mock() - sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp', gate_set=cg.XMON) + sampler = cg.QuantumEngineSampler(engine=engine, processor_id='tmp') assert sampler.engine is engine def test_get_engine_sampler_explicit_project_id(): with mock.patch.object(cirq_google.cloud.quantum, 'QuantumEngineServiceClient', autospec=True): - sampler = cg.get_engine_sampler( - processor_id='hi mom', gate_set_name='sqrt_iswap', project_id='myproj' - ) + sampler = cg.get_engine_sampler(processor_id='hi mom', project_id='myproj') assert hasattr(sampler, 'run_sweep') - with pytest.raises(ValueError): - sampler = cg.get_engine_sampler(processor_id='hi mom', gate_set_name='ccz') - def test_get_engine_sampler(): with mock.patch.object(cirq_google.cloud.quantum, 'QuantumEngineServiceClient', autospec=True): with mock.patch('google.auth.default', lambda: (None, 'myproj')): - sampler = cg.get_engine_sampler(processor_id='hi mom', gate_set_name='sqrt_iswap') + sampler = cg.get_engine_sampler(processor_id='hi mom') assert hasattr(sampler, 'run_sweep') - - with pytest.raises(ValueError): - sampler = cg.get_engine_sampler(processor_id='hi mom', gate_set_name='ccz') diff --git a/cirq-google/cirq_google/engine/simulated_local_processor.py b/cirq-google/cirq_google/engine/simulated_local_processor.py index 4522e8a1678..bf637b2d754 100644 --- a/cirq-google/cirq_google/engine/simulated_local_processor.py +++ b/cirq-google/cirq_google/engine/simulated_local_processor.py @@ -18,7 +18,7 @@ import cirq from cirq_google.api import v2 -from cirq_google.engine import calibration, validating_sampler +from cirq_google.engine import calibration, util, validating_sampler from cirq_google.engine.abstract_local_processor import AbstractLocalProcessor from cirq_google.engine.abstract_local_program import AbstractLocalProgram from cirq_google.engine.abstract_program import AbstractProgram @@ -160,6 +160,7 @@ def list_calibrations( if earliest_timestamp_seconds <= cal[0] <= latest_timestamp_seconds ] + @util.deprecated_gate_set_parameter def get_sampler(self, gate_set: Optional['Serializer'] = None) -> cirq.Sampler: return self._sampler @@ -203,6 +204,7 @@ def get_program(self, program_id: str) -> AbstractProgram: """ return self._programs[program_id] + @util.deprecated_gate_set_parameter def run_batch( self, programs: Sequence[cirq.AbstractCircuit], @@ -242,6 +244,7 @@ def run_batch( self._programs[program_id].add_job(job_id, job) return job + @util.deprecated_gate_set_parameter def run( self, program: cirq.Circuit, @@ -286,13 +289,13 @@ def run( job_id=job_id, params=[param_resolver or cirq.ParamResolver({})], repetitions=repetitions, - gate_set=gate_set, program_description=program_description, program_labels=program_labels, job_description=job_description, job_labels=job_labels, ).results()[0] + @util.deprecated_gate_set_parameter def run_sweep( self, program: cirq.Circuit, diff --git a/cirq-google/cirq_google/engine/util.py b/cirq-google/cirq_google/engine/util.py index 3867ab0144c..4a381b87fb7 100644 --- a/cirq-google/cirq_google/engine/util.py +++ b/cirq-google/cirq_google/engine/util.py @@ -11,11 +11,15 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +import inspect from typing import TypeVar from google.protobuf import any_pb2 from google.protobuf.message import Message +import cirq + M = TypeVar('M', bound=Message) @@ -32,3 +36,20 @@ def pack_any(message: Message) -> any_pb2.Any: def unpack_any(message: any_pb2.Any, out: M) -> M: message.Unpack(out) return out + + +def deprecated_gate_set_parameter(func): + """Decorates a function that takes a deprecated 'gate_set' parameter.""" + signature = inspect.signature(func) + gate_set_param = signature.parameters['gate_set'] + assert gate_set_param.default is None # Must be optional and default to None. + idx = list(signature.parameters).index('gate_set') + + decorator = cirq._compat.deprecated_parameter( + deadline='v0.15', + fix='Remove the gate_set parameter.', + parameter_desc='gate_set', + match=lambda args, kwargs: 'gate_set' in kwargs + or (gate_set_param.kind != inspect.Parameter.KEYWORD_ONLY and len(args) > idx), + ) + return decorator(func)