Skip to content

Commit

Permalink
Add timeout to engine object (#2996)
Browse files Browse the repository at this point in the history
* Add timeout to engine object

- Adds timeout to Engine object and makes it configurable
- This plumbs the timeout down to results object so that the
amount of polling is configurable.

* Moved timeout into EngineContext
  • Loading branch information
dstrain115 committed May 13, 2020
1 parent 8e30cf6 commit 17e27e6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
12 changes: 10 additions & 2 deletions cirq/google/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def __init__(self,
proto_version: Optional[ProtoVersion] = None,
service_args: Optional[Dict] = None,
verbose: Optional[bool] = None,
client: 'Optional[engine_client.EngineClient]' = None) -> None:
client: 'Optional[engine_client.EngineClient]' = None,
timeout: Optional[int] = None) -> None:
"""Context and client for using Quantum Engine.
Args:
Expand All @@ -80,6 +81,8 @@ def __init__(self,
configure options on the underlying client.
verbose: Suppresses stderr messages when set to False. Default is
true.
timeout: Timeout for polling for results, in seconds. Default is
to never timeout.
"""
if (service_args or verbose) and client:
raise ValueError(
Expand All @@ -91,6 +94,7 @@ def __init__(self,
client = engine_client.EngineClient(service_args=service_args,
verbose=verbose)
self.client = client
self.timeout = timeout

def copy(self) -> 'EngineContext':
return EngineContext(proto_version=self.proto_version,
Expand Down Expand Up @@ -124,6 +128,7 @@ def __init__(
service_args: Optional[Dict] = None,
verbose: Optional[bool] = None,
context: Optional[EngineContext] = None,
timeout: Optional[int] = None,
) -> None:
"""Supports creating and running programs against the Quantum Engine.
Expand All @@ -138,6 +143,8 @@ def __init__(
configure options on the underlying client.
verbose: Suppresses stderr messages when set to False. Default is
true.
timeout: Timeout for polling for results, in seconds. Default is
to never timeout.
"""
if context and (proto_version or service_args or verbose):
raise ValueError(
Expand All @@ -148,7 +155,8 @@ def __init__(
if not context:
context = EngineContext(proto_version=proto_version,
service_args=service_args,
verbose=verbose)
verbose=verbose,
timeout=timeout)
self.context = context

def run(
Expand Down
7 changes: 6 additions & 1 deletion cirq/google/engine/engine_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,15 @@ def results(self) -> List[study.TrialResult]:
import cirq.google.engine.engine as engine_base
if not self._results:
job = self._refresh_job()
for _ in range(1000):
total_seconds_waited = 0.0
timeout = self.context.timeout
while True:
if timeout and total_seconds_waited >= timeout:
break
if job.execution_status.state in TERMINAL_STATES:
break
time.sleep(0.5)
total_seconds_waited += 0.5
job = self._refresh_job()
self._raise_on_failure(job)
response = self.context.client.get_job_results(
Expand Down
2 changes: 1 addition & 1 deletion cirq/google/engine/engine_job_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def test_timeout(patched_time_sleep, get_job):
qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus(
state=qtypes.ExecutionStatus.State.RUNNING))
get_job.return_value = qjob
job = cg.EngineJob('a', 'b', 'steve', EngineContext())
job = cg.EngineJob('a', 'b', 'steve', EngineContext(timeout=500))
with pytest.raises(RuntimeError, match='Timed out'):
job.results()

Expand Down
2 changes: 1 addition & 1 deletion cirq/google/engine/engine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def test_run_circuit_timeout(patched_time_sleep, client):
'state': 'RUNNING',
})

engine = cg.Engine(project_id='project-id')
engine = cg.Engine(project_id='project-id', timeout=600)
with pytest.raises(RuntimeError, match='Timed out'):
engine.run(program=_CIRCUIT)

Expand Down

0 comments on commit 17e27e6

Please sign in to comment.