Skip to content

Commit

Permalink
Raise RuntimeJobMaxTimeoutError if a program fails for running too lo…
Browse files Browse the repository at this point in the history
…ng (#432)

* raise RuntimeJobTimeoutError if a program fails for running too long

* reno

* update reno note

* rename error

* Update releasenotes/notes/exception-when-program-runs-too-long-cf80743eda83d601.yaml

Co-authored-by: Rathish Cholarajan <rathishc24@gmail.com>

* update integration test

Co-authored-by: Rathish Cholarajan <rathishc24@gmail.com>
Co-authored-by: Rathish Cholarajan <rathish.c@ibm.com>
  • Loading branch information
3 people committed Aug 2, 2022
1 parent 6181971 commit 297964e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions qiskit_ibm_runtime/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ class RuntimeJobTimeoutError(IBMRuntimeError):
"""Error raised when waiting for job times out."""

pass


class RuntimeJobMaxTimeoutError(IBMRuntimeError):
"""Error raised when a job times out."""

pass
8 changes: 6 additions & 2 deletions qiskit_ibm_runtime/runtime_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
RuntimeInvalidStateError,
IBMRuntimeError,
RuntimeJobTimeoutError,
RuntimeJobMaxTimeoutError,
)
from .program.result_decoder import ResultDecoder
from .api.clients import RuntimeClient, RuntimeWebsocketClient, WebsocketClientCloseCode
Expand Down Expand Up @@ -174,14 +175,17 @@ def result(
Raises:
RuntimeJobFailureError: If the job failed.
RuntimeJobTimeoutError: If the job does not complete within given timeout.
RuntimeJobMaxTimeoutError: If the job does not complete within given timeout.
"""
_decoder = decoder or self._result_decoder
if self._results is None or (_decoder != self._result_decoder):
self.wait_for_final_state(timeout=timeout)
if self._status == JobStatus.ERROR:
error_message = self.error_message()
if self._reason == "RAN TOO LONG":
raise RuntimeJobMaxTimeoutError(error_message)
raise RuntimeJobFailureError(
f"Unable to retrieve job result. " f"{self.error_message()}"
f"Unable to retrieve job result. " f"{error_message}"
)
result_raw = self._api_client.job_results(job_id=self.job_id)
self._results = _decoder.decode(result_raw) if result_raw else None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
upgrade:
- |
Raise ``RuntimeJobMaxTimeoutError`` when a job runs for too long so that it can be handled appropriately by programs.
3 changes: 2 additions & 1 deletion test/integration/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
RuntimeJobFailureError,
RuntimeInvalidStateError,
RuntimeJobNotFound,
RuntimeJobMaxTimeoutError,
)
from ..ibm_test_case import IBMIntegrationJobTestCase
from ..decorators import run_integration_test
Expand Down Expand Up @@ -115,7 +116,7 @@ def test_run_program_failed_ran_too_long(self, service):
),
job.error_message(),
)
with self.assertRaises(RuntimeJobFailureError):
with self.assertRaises(RuntimeJobMaxTimeoutError):
job.result()

@run_integration_test
Expand Down
3 changes: 2 additions & 1 deletion test/unit/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from qiskit_ibm_runtime.exceptions import (
RuntimeJobFailureError,
RuntimeJobNotFound,
RuntimeJobMaxTimeoutError,
RuntimeProgramNotFound,
IBMInputValueError,
)
Expand Down Expand Up @@ -192,7 +193,7 @@ def test_run_program_failed_ran_too_long(self, service):
),
job.error_message(),
)
with self.assertRaises(RuntimeJobFailureError):
with self.assertRaises(RuntimeJobMaxTimeoutError):
job.result()

@run_quantum_and_cloud_fake
Expand Down

0 comments on commit 297964e

Please sign in to comment.