Skip to content

Commit 1671207

Browse files
mergify[bot]LiranShirizlywshanks
authored
support RuntimeJobV2 job status (backport #1512) (#1521)
The job status definition in `RuntimeJobV2` ([JobStatus](https://github.com/Qiskit/qiskit-ibm-runtime/blob/main/qiskit_ibm_runtime/runtime_job_v2.py#L42-L48)) uses a str object compare to the previous definition in `RuntimeJob` which is an [enum ](https://github.com/Qiskit/qiskit/blob/stable/1.3/qiskit/providers/jobstatus.py#L18-L27). This cause qiskit-experiment to report `JobStatus.DONE` immediately after running an experiment, even if the job was queue/running and a wrong `ExperimentStatus`. This change fixes this issue by supporting both definitions, it converts the str `RuntimeJobV2` to the original enum (with the same names). <hr>This is an automatic backport of pull request #1512 done by [Mergify](https://mergify.com). --------- Co-authored-by: Liran Shirizly <[email protected]> Co-authored-by: Will Shanks <[email protected]>
1 parent b7d10c5 commit 1671207

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

qiskit_experiments/framework/experiment_data.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ def parse_utc_datetime(dt_str: str) -> datetime:
107107
return dt_utc
108108

109109

110+
def get_job_status(job: Job) -> JobStatus:
111+
"""Get job status in JobStatus format defined in:
112+
`from qiskit.providers.jobstatus import JobStatus`"""
113+
status = job.status()
114+
if isinstance(status, str):
115+
qe_job_status = getattr(JobStatus, status, None)
116+
if qe_job_status is None:
117+
raise QiskitError(
118+
f"The status of job {job.job_id()} is {status} "
119+
"which is not supported by qiskit-experiments."
120+
)
121+
status = qe_job_status
122+
return status
123+
124+
110125
class ExperimentData:
111126
"""Experiment data container class.
112127
@@ -910,7 +925,7 @@ def _add_job_data(
910925
return jid, True
911926
except Exception as ex: # pylint: disable=broad-except
912927
# Handle cancelled jobs
913-
status = job.status()
928+
status = get_job_status(job)
914929
if status == JobStatus.CANCELLED:
915930
LOG.warning("Job was cancelled before completion [Job ID: %s]", jid)
916931
return jid, False
@@ -1171,7 +1186,7 @@ def _retrieve_data(self):
11711186
# Add retrieved job objects to stored jobs and extract data
11721187
for jid, job in retrieved_jobs.items():
11731188
self._jobs[jid] = job
1174-
if job.status() in JOB_FINAL_STATES:
1189+
if get_job_status(job) in JOB_FINAL_STATES:
11751190
# Add job results synchronously
11761191
self._add_job_data(job)
11771192
else:
@@ -1982,7 +1997,7 @@ def cancel_jobs(
19821997
if ids and jid not in ids:
19831998
# Skip cancelling this callback
19841999
continue
1985-
if job and job.status() not in JOB_FINAL_STATES:
2000+
if job and get_job_status(job) not in JOB_FINAL_STATES:
19862001
try:
19872002
job.cancel()
19882003
LOG.warning("Cancelled job [Job ID: %s]", jid)
@@ -2259,7 +2274,7 @@ def job_status(self) -> JobStatus:
22592274
statuses = set()
22602275
for job in self._jobs.values():
22612276
if job:
2262-
statuses.add(job.status())
2277+
statuses.add(get_job_status(job))
22632278

22642279
# If any jobs are in non-DONE state return that state
22652280
for stat in [
@@ -2310,7 +2325,7 @@ def job_errors(self) -> str:
23102325

23112326
# Get any job errors
23122327
for job in self._jobs.values():
2313-
if job and job.status() == JobStatus.ERROR:
2328+
if job and get_job_status(job) == JobStatus.ERROR:
23142329
if hasattr(job, "error_message"):
23152330
error_msg = job.error_message()
23162331
else:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
fixes:
3+
- |
4+
This release contains a single fix for :class:`~.ExperimentData` job status handling.
5+
fixes_expdata:
6+
- |
7+
Fixed :class:`~.ExperimentData` not reporting the correct job status when
8+
the job is of type :class:`~qiskit_ibm_runtime.RuntimeJobV2`.
9+
developer:
10+
- |
11+
Added a function that returns the job status as :class:`qiskit.providers.jobstatus.JobStatus`:
12+
:meth:`~qiskit_experiments.framework.experiment_data.get_job_status`.

0 commit comments

Comments
 (0)