Skip to content

Commit a0ceb17

Browse files
Cynocracyrht
authored andcommitted
Allow specifying timeout_seconds when constructing an ionq Sampler (quantumlib#5133)
* Allow specifying timeout_seconds when constructing an ionq Sampler This allows users of tensorflow quantum to use a high timeout and https://github.com/tensorflow/quantum/blob/f10d36b64761a003fd542392767fa079d57df0aa/tensorflow_quantum/python/quantum_context.py\#L82-L101 to run highly parallel workloads that may take very long times per-job without flakiness. * fmt * Python map * Coverage
1 parent 1b42a32 commit a0ceb17

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

cirq-ionq/cirq_ionq/sampler.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(
4444
self,
4545
service: 'cirq_ionq.Service',
4646
target: Optional[str],
47+
timeout_seconds: Optional[int] = None,
4748
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
4849
):
4950
"""Construct the sampler.
@@ -57,10 +58,12 @@ def __init__(
5758
seed: If the target is `simulation` the seed for generating results. If None, this
5859
will be `np.random`, if an int, will be `np.random.RandomState(int)`, otherwise
5960
must be a modulate similar to `np.random`.
61+
timeout_seconds: Length of time to wait for results. Default is specified in the job.
6062
"""
6163
self._service = service
6264
self._target = target
6365
self._seed = seed
66+
self._timeout_seconds = timeout_seconds
6467

6568
def run_sweep(
6669
self,
@@ -86,7 +89,10 @@ def run_sweep(
8689
)
8790
for resolver in resolvers
8891
]
89-
job_results = [job.results() for job in jobs]
92+
kwargs = {}
93+
if self._timeout_seconds is not None:
94+
kwargs = {"timeout_seconds": self._timeout_seconds}
95+
job_results = [job.results(**kwargs) for job in jobs]
9096
cirq_results = []
9197
for result, params in zip(job_results, resolvers):
9298
if isinstance(result, results.QPUResult):

cirq-ionq/cirq_ionq/sampler_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_sampler_multiple_jobs():
9494
job1 = ionq.Job(client=mock_service, job_dict=job_dict1)
9595
mock_service.create_job.side_effect = [job0, job1]
9696

97-
sampler = ionq.Sampler(service=mock_service, target='qpu')
97+
sampler = ionq.Sampler(service=mock_service, timeout_seconds=10, target='qpu')
9898
q0 = cirq.LineQubit(0)
9999
x = sp.Symbol('x')
100100
circuit = cirq.Circuit(cirq.X(q0) ** x, cirq.measure(q0, key='a'))

0 commit comments

Comments
 (0)