Skip to content

Commit 4dc98ba

Browse files
dyatespavoljuhas
authored andcommitted
Add max_concurrent_jobs to Engine.get_sampler(). (quantumlib#7442)
Adds `max_concurrent_jobs` to the utility function [Engine.get_sampler](https://github.com/dyates/Cirq/blob/b8ca4844ef80102aa83ace1093552f25c02b101a/cirq-google/cirq_google/engine/engine.py#L580) to allow setting the [same field in the `ProcessSampler`]((https://github.com/quantumlib/Cirq/blob/4c7e393eb9fcefcba0c3c509e32cc354197cf014/cirq-google/cirq_google/engine/processor_sampler.py#L37)). This field throttles the number concurrent jobs that are sent to the backend (via `run_batch`). --------- Co-authored-by: Pavol Juhas <juhas@google.com>
1 parent 39d55bf commit 4dc98ba

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

cirq-google/cirq_google/engine/engine.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ def get_sampler(
583583
run_name: str = "",
584584
device_config_name: str = "",
585585
snapshot_id: str = "",
586+
max_concurrent_jobs: int = 10,
586587
) -> cirq_google.ProcessorSampler:
587588
"""Returns a sampler backed by the engine.
588589
@@ -596,6 +597,10 @@ def get_sampler(
596597
available qubits, couplers, and supported gates in the processor.
597598
snapshot_id: A unique identifier for an immutable snapshot reference. A
598599
snapshot contains a collection of device configurations for the processor.
600+
max_concurrent_jobs: The maximum number of jobs to be sent
601+
concurrently to the Engine. This client-side throttle can be
602+
used to proactively reduce load to the backends and avoid quota
603+
violations when pipelining circuit executions.
599604
600605
Returns:
601606
A `cirq.Sampler` instance (specifically a `engine_sampler.ProcessorSampler`
@@ -612,7 +617,10 @@ def get_sampler(
612617
'you need to specify a list.'
613618
)
614619
return self.get_processor(processor_id).get_sampler(
615-
run_name=run_name, device_config_name=device_config_name, snapshot_id=snapshot_id
620+
run_name=run_name,
621+
device_config_name=device_config_name,
622+
snapshot_id=snapshot_id,
623+
max_concurrent_jobs=max_concurrent_jobs,
616624
)
617625

618626

cirq-google/cirq_google/engine/engine_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,14 @@ def test_get_processor():
800800
assert cg.Engine(project_id='proj').get_processor('xmonsim').processor_id == 'xmonsim'
801801

802802

803+
def test_get_sampler_initializes_max_concurrent_jobs():
804+
max_concurrent_jobs = 5
805+
engine = cg.Engine(project_id='proj')
806+
sampler = engine.get_sampler(processor_id='tmp', max_concurrent_jobs=max_concurrent_jobs)
807+
808+
assert sampler.max_concurrent_jobs == max_concurrent_jobs
809+
810+
803811
@mock.patch('cirq_google.engine.engine_client.EngineClient', autospec=True)
804812
def test_sampler_with_unary_rpcs(client):
805813
setup_run_circuit_with_result_(client, _RESULTS)

cirq-google/cirq_google/engine/processor_sampler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,8 @@ def snapshot_id(self) -> str:
113113
@property
114114
def device_config_name(self) -> str:
115115
return self._device_config_name
116+
117+
@property
118+
def max_concurrent_jobs(self) -> int:
119+
assert self._concurrent_job_limiter.capacity is not None
120+
return self._concurrent_job_limiter.capacity

0 commit comments

Comments
 (0)