Skip to content

Commit b70b2fc

Browse files
jurrutiJose Urruticoechea
andauthored
Add new processor selector parameters in cirq-google/engine run methods (#6267)
* Add new processor selector parameters to engine_program run methods * Add new processor selector parameters to engine run methods * Add new processor selector parameters to processor run methods * Add device configuration parameters to processor sampler class * small docstring fix * test sampler with incomplete device configuraion throws * Address cxing comments * fix abstract processor run --------- Co-authored-by: Jose Urruticoechea <[email protected]>
1 parent b28bfce commit b70b2fc

File tree

9 files changed

+374
-46
lines changed

9 files changed

+374
-46
lines changed

cirq-google/cirq_google/engine/abstract_processor.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ async def run_async(
6464
program_labels: Optional[Dict[str, str]] = None,
6565
job_description: Optional[str] = None,
6666
job_labels: Optional[Dict[str, str]] = None,
67+
run_name: str = "",
68+
device_config_name: str = "",
6769
) -> cirq.Result:
6870
"""Runs the supplied Circuit on this processor.
6971
@@ -85,6 +87,12 @@ async def run_async(
8587
program_labels: Optional set of labels to set on the program.
8688
job_description: An optional description to set on the job.
8789
job_labels: Optional set of labels to set on the job.
90+
run_name: A unique identifier representing an automation run for the
91+
processor. An Automation Run contains a collection of device
92+
configurations for the processor.
93+
device_config_name: An identifier used to select the processor configuration
94+
utilized to run the job. A configuration identifies the set of
95+
available qubits, couplers, and supported gates in the processor.
8896
Returns:
8997
A single Result for this run.
9098
"""
@@ -98,6 +106,8 @@ async def run_async(
98106
program_labels=program_labels,
99107
job_description=job_description,
100108
job_labels=job_labels,
109+
run_name=run_name,
110+
device_config_name=device_config_name,
101111
)
102112
return job.results()[0]
103113

@@ -115,6 +125,8 @@ async def run_sweep_async(
115125
program_labels: Optional[Dict[str, str]] = None,
116126
job_description: Optional[str] = None,
117127
job_labels: Optional[Dict[str, str]] = None,
128+
run_name: str = "",
129+
device_config_name: str = "",
118130
) -> 'abstract_job.AbstractJob':
119131
"""Runs the supplied Circuit on this processor.
120132
@@ -138,6 +150,12 @@ async def run_sweep_async(
138150
program_labels: Optional set of labels to set on the program.
139151
job_description: An optional description to set on the job.
140152
job_labels: Optional set of labels to set on the job.
153+
run_name: A unique identifier representing an automation run for the
154+
processor. An Automation Run contains a collection of device
155+
configurations for the processor.
156+
device_config_name: An identifier used to select the processor configuration
157+
utilized to run the job. A configuration identifies the set of
158+
available qubits, couplers, and supported gates in the processor.
141159
Returns:
142160
An AbstractJob. If this is iterated over it returns a list of
143161
`cirq.Result`, one for each parameter sweep.
@@ -157,6 +175,8 @@ async def run_batch_async(
157175
program_labels: Optional[Dict[str, str]] = None,
158176
job_description: Optional[str] = None,
159177
job_labels: Optional[Dict[str, str]] = None,
178+
run_name: str = "",
179+
device_config_name: str = "",
160180
) -> 'abstract_job.AbstractJob':
161181
"""Runs the supplied Circuits on this processor.
162182
@@ -188,6 +208,12 @@ async def run_batch_async(
188208
program_labels: Optional set of labels to set on the program.
189209
job_description: An optional description to set on the job.
190210
job_labels: Optional set of labels to set on the job.
211+
run_name: A unique identifier representing an automation run for the
212+
processor. An Automation Run contains a collection of device
213+
configurations for the processor.
214+
device_config_name: An identifier used to select the processor configuration
215+
utilized to run the job. A configuration identifies the set of
216+
available qubits, couplers, and supported gates in the processor.
191217
Returns:
192218
An AbstractJob. If this is iterated over it returns a list of
193219
`cirq.Result`. All Results for the first circuit are listed
@@ -244,8 +270,19 @@ async def run_calibration_async(
244270
run_calibration = duet.sync(run_calibration_async)
245271

246272
@abc.abstractmethod
247-
def get_sampler(self) -> 'cg.ProcessorSampler':
248-
"""Returns a sampler backed by the processor."""
273+
def get_sampler(
274+
self, run_name: str = "", device_config_name: str = ""
275+
) -> 'cg.ProcessorSampler':
276+
"""Returns a sampler backed by the processor.
277+
278+
Args:
279+
run_name: A unique identifier representing an automation run for the
280+
processor. An Automation Run contains a collection of device
281+
configurations for the processor.
282+
device_config_name: An identifier used to select the processor configuration
283+
utilized to run the job. A configuration identifies the set of
284+
available qubits, couplers, and supported gates in the processor.
285+
"""
249286

250287
@abc.abstractmethod
251288
def engine(self) -> Optional['abstract_engine.AbstractEngine']:

cirq-google/cirq_google/engine/engine.py

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def __init__(
207207
def __str__(self) -> str:
208208
return f'Engine(project_id={self.project_id!r})'
209209

210+
# TODO(#6271): Deprecate and remove processor_ids before v1.4
210211
def run(
211212
self,
212213
program: cirq.AbstractCircuit,
@@ -219,6 +220,10 @@ def run(
219220
program_labels: Optional[Dict[str, str]] = None,
220221
job_description: Optional[str] = None,
221222
job_labels: Optional[Dict[str, str]] = None,
223+
*,
224+
processor_id: str = "",
225+
run_name: str = "",
226+
device_config_name: str = "",
222227
) -> cirq.Result:
223228
"""Runs the supplied Circuit via Quantum Engine.
224229
@@ -236,19 +241,34 @@ def run(
236241
and day.
237242
param_resolver: Parameters to run with the program.
238243
repetitions: The number of repetitions to simulate.
239-
processor_ids: The engine processors that should be candidates
240-
to run the program. Only one of these will be scheduled for
241-
execution.
244+
processor_ids: Deprecated list of candidate processor ids to run the program.
245+
Only allowed to contain one processor_id. If the argument `processor_id`
246+
is non-empty, `processor_ids` will be ignored.
242247
program_description: An optional description to set on the program.
243248
program_labels: Optional set of labels to set on the program.
244249
job_description: An optional description to set on the job.
245250
job_labels: Optional set of labels to set on the job.
251+
processor_id: Processor id for running the program. If not set,
252+
`processor_ids` will be used.
253+
run_name: A unique identifier representing an automation run for the
254+
specified processor. An Automation Run contains a collection of
255+
device configurations for a processor. If specified, `processor_id`
256+
is required to be set.
257+
device_config_name: An identifier used to select the processor configuration
258+
utilized to run the job. A configuration identifies the set of
259+
available qubits, couplers, and supported gates in the processor.
260+
If specified, `processor_id` is required to be set.
246261
247262
Returns:
248263
A single Result for this run.
249264
250265
Raises:
251266
ValueError: If no gate set is provided.
267+
ValueError: If neither `processor_id` or `processor_ids` are set.
268+
ValueError: If only one of `run_name` and `device_config_name` are specified.
269+
ValueError: If `processor_ids` has more than one processor id.
270+
ValueError: If either `run_name` and `device_config_name` are set but
271+
`processor_id` is empty.
252272
"""
253273
return list(
254274
self.run_sweep(
@@ -262,9 +282,13 @@ def run(
262282
program_labels=program_labels,
263283
job_description=job_description,
264284
job_labels=job_labels,
285+
processor_id=processor_id,
286+
run_name=run_name,
287+
device_config_name=device_config_name,
265288
)
266289
)[0]
267290

291+
# TODO(#6271): Deprecate and remove processor_ids before v1.4
268292
async def run_sweep_async(
269293
self,
270294
program: cirq.AbstractCircuit,
@@ -277,6 +301,10 @@ async def run_sweep_async(
277301
program_labels: Optional[Dict[str, str]] = None,
278302
job_description: Optional[str] = None,
279303
job_labels: Optional[Dict[str, str]] = None,
304+
*,
305+
processor_id: str = "",
306+
run_name: str = "",
307+
device_config_name: str = "",
280308
) -> engine_job.EngineJob:
281309
"""Runs the supplied Circuit via Quantum Engine.Creates
282310
@@ -297,20 +325,35 @@ async def run_sweep_async(
297325
and day.
298326
params: Parameters to run with the program.
299327
repetitions: The number of circuit repetitions to run.
300-
processor_ids: The engine processors that should be candidates
301-
to run the program. Only one of these will be scheduled for
302-
execution.
328+
processor_ids: Deprecated list of candidate processor ids to run the program.
329+
Only allowed to contain one processor_id. If the argument `processor_id`
330+
is non-empty, `processor_ids` will be ignored.
303331
program_description: An optional description to set on the program.
304332
program_labels: Optional set of labels to set on the program.
305333
job_description: An optional description to set on the job.
306334
job_labels: Optional set of labels to set on the job.
335+
processor_id: Processor id for running the program. If not set,
336+
`processor_ids` will be used.
337+
run_name: A unique identifier representing an automation run for the
338+
specified processor. An Automation Run contains a collection of
339+
device configurations for a processor. If specified, `processor_id`
340+
is required to be set.
341+
device_config_name: An identifier used to select the processor configuration
342+
utilized to run the job. A configuration identifies the set of
343+
available qubits, couplers, and supported gates in the processor.
344+
If specified, `processor_id` is required to be set.
307345
308346
Returns:
309347
An EngineJob. If this is iterated over it returns a list of
310348
TrialResults, one for each parameter sweep.
311349
312350
Raises:
313351
ValueError: If no gate set is provided.
352+
ValueError: If neither `processor_id` or `processor_ids` are set.
353+
ValueError: If only one of `run_name` and `device_config_name` are specified.
354+
ValueError: If `processor_ids` has more than one processor id.
355+
ValueError: If either `run_name` and `device_config_name` are set but
356+
`processor_id` is empty.
314357
"""
315358
engine_program = await self.create_program_async(
316359
program, program_id, description=program_description, labels=program_labels
@@ -322,10 +365,14 @@ async def run_sweep_async(
322365
processor_ids=processor_ids,
323366
description=job_description,
324367
labels=job_labels,
368+
processor_id=processor_id,
369+
run_name=run_name,
370+
device_config_name=device_config_name,
325371
)
326372

327373
run_sweep = duet.sync(run_sweep_async)
328374

375+
# TODO(#6271): Deprecate and remove processor_ids before v1.4
329376
async def run_batch_async(
330377
self,
331378
programs: Sequence[cirq.AbstractCircuit],
@@ -338,6 +385,10 @@ async def run_batch_async(
338385
program_labels: Optional[Dict[str, str]] = None,
339386
job_description: Optional[str] = None,
340387
job_labels: Optional[Dict[str, str]] = None,
388+
*,
389+
processor_id: str = "",
390+
run_name: str = "",
391+
device_config_name: str = "",
341392
) -> engine_job.EngineJob:
342393
"""Runs the supplied Circuits via Quantum Engine.Creates
343394
@@ -367,13 +418,23 @@ async def run_batch_async(
367418
require sweeps.
368419
repetitions: Number of circuit repetitions to run. Each sweep value
369420
of each circuit in the batch will run with the same repetitions.
370-
processor_ids: The engine processors that should be candidates
371-
to run the program. Only one of these will be scheduled for
372-
execution.
421+
processor_ids: Deprecated list of candidate processor ids to run the program.
422+
Only allowed to contain one processor_id. If the argument `processor_id`
423+
is non-empty, `processor_ids` will be ignored.
373424
program_description: An optional description to set on the program.
374425
program_labels: Optional set of labels to set on the program.
375426
job_description: An optional description to set on the job.
376427
job_labels: Optional set of labels to set on the job.
428+
processor_id: Processor id for running the program. If not set,
429+
`processor_ids` will be used.
430+
run_name: A unique identifier representing an automation run for the
431+
specified processor. An Automation Run contains a collection of
432+
device configurations for a processor. If specified, `processor_id`
433+
is required to be set.
434+
device_config_name: An identifier used to select the processor configuration
435+
utilized to run the job. A configuration identifies the set of
436+
available qubits, couplers, and supported gates in the processor.
437+
If specified, `processor_id` is required to be set.
377438
378439
Returns:
379440
An EngineJob. If this is iterated over it returns a list of
@@ -385,12 +446,17 @@ async def run_batch_async(
385446
Raises:
386447
ValueError: If the length of programs mismatches that of params_list, or
387448
`processor_ids` is not supplied.
449+
ValueError: If neither `processor_id` or `processor_ids` are set.
450+
ValueError: If only one of `run_name` and `device_config_name` are specified.
451+
ValueError: If `processor_ids` has more than one processor id.
452+
ValueError: If either `run_name` and `device_config_name` are set but
453+
`processor_id` is empty.
388454
"""
389455
if params_list is None:
390456
params_list = [None] * len(programs)
391457
elif len(programs) != len(params_list):
392458
raise ValueError('Number of circuits and sweeps must match')
393-
if not processor_ids:
459+
if not processor_ids and not processor_id:
394460
raise ValueError('Processor id must be specified.')
395461
engine_program = await self.create_batch_program_async(
396462
programs, program_id, description=program_description, labels=program_labels
@@ -402,6 +468,9 @@ async def run_batch_async(
402468
processor_ids=processor_ids,
403469
description=job_description,
404470
labels=job_labels,
471+
processor_id=processor_id,
472+
run_name=run_name,
473+
device_config_name=device_config_name,
405474
)
406475

407476
run_batch = duet.sync(run_batch_async)
@@ -778,11 +847,19 @@ def sampler(self, processor_id: Union[str, List[str]]) -> 'cirq_google.Processor
778847
"""
779848
return self.get_sampler(processor_id)
780849

781-
def get_sampler(self, processor_id: Union[str, List[str]]) -> 'cirq_google.ProcessorSampler':
850+
def get_sampler(
851+
self, processor_id: Union[str, List[str]], run_name: str = "", device_config_name: str = ""
852+
) -> 'cirq_google.ProcessorSampler':
782853
"""Returns a sampler backed by the engine.
783854
784855
Args:
785856
processor_id: String identifier of which processor should be used to sample.
857+
run_name: A unique identifier representing an automation run for the
858+
processor. An Automation Run contains a collection of device
859+
configurations for the processor.
860+
device_config_name: An identifier used to select the processor configuration
861+
utilized to run the job. A configuration identifies the set of
862+
available qubits, couplers, and supported gates in the processor.
786863
787864
Returns:
788865
A `cirq.Sampler` instance (specifically a `engine_sampler.ProcessorSampler`
@@ -798,7 +875,9 @@ def get_sampler(self, processor_id: Union[str, List[str]]) -> 'cirq_google.Proce
798875
'to get_sampler() no longer supported. Use Engine.run() instead if '
799876
'you need to specify a list.'
800877
)
801-
return self.get_processor(processor_id).get_sampler()
878+
return self.get_processor(processor_id).get_sampler(
879+
run_name=run_name, device_config_name=device_config_name
880+
)
802881

803882

804883
def get_engine(project_id: Optional[str] = None) -> Engine:

cirq-google/cirq_google/engine/engine_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,16 @@ async def create_job_async(
397397
) -> Tuple[str, quantum.QuantumJob]:
398398
"""Creates and runs a job on Quantum Engine.
399399
400+
Either both `run_name` and `device_config_name` must be set, or neither
401+
of them must be set. If none of them are set, a default internal device
402+
configuration will be used.
403+
400404
Args:
401405
project_id: A project_id of the parent Google Cloud Project.
402406
program_id: Unique ID of the program within the parent project.
403407
job_id: Unique ID of the job within the parent program.
404408
run_context: Properly serialized run context.
405-
processor_ids: Deprecated list of processor ids for running the program.
409+
processor_ids: Deprecated list of candidate processor ids to run the program.
406410
Only allowed to contain one processor_id. If the argument `processor_id`
407411
is non-empty, `processor_ids` will be ignored. Otherwise the deprecated
408412
decorator will fix the arguments and call create_job_async using

0 commit comments

Comments
 (0)