Save and retrieve jobs
Package versions
The code on this page was developed using the following requirements. We recommend using these versions or newer.
qiskit[all]~=1.3.1
qiskit-ibm-runtime~=0.34.0
qiskit-aer~=0.15.1
qiskit-serverless~=0.18.0
qiskit-ibm-catalog~=0.2
qiskit-addon-sqd~=0.8.1
qiskit-addon-utils~=0.1.0
qiskit-addon-mpf~=0.2.0
scipy~=1.14.1
qiskit-addon-aqc-tensor~=0.1.2
qiskit-addon-obp~=0.1.0
scipy~=1.14.1
pyscf~=2.7.0
Quantum workflows often take a while to complete and can run over many sessions. Restarting your Python kernel means you'll lose any results stored in memory. To avoid loss of data, you can save results to file and retrieve results of past jobs from IBM Quantum™ so your next session can continue where you left off.
Retrieve jobs from IBM Quantum
IBM Quantum automatically stores results from every job for you to retrieve at a later date. Use this feature to continue quantum programs across kernel restarts and review past results. You can get the ID of a job programmatically through its job_id
method, or you can see all your submitted jobs and their IDs through the Workloads dashboard.
To find a job programatically, use the QiskitRuntimeService.jobs
method. By default, this returns the most recent jobs, but you can also filter jobs by backend name, creation date, and more. The following cell finds any jobs submitted in the last three months. The created_after
argument must be a datetime.datetime
object.
import datetime
from qiskit_ibm_runtime import QiskitRuntimeService
three_months_ago = datetime.datetime.now() - datetime.timedelta(days=90)
service = QiskitRuntimeService()
jobs_in_last_three_months = service.jobs(created_after=three_months_ago)
jobs_in_last_three_months[:3] # show first three jobs
Output:
[<RuntimeJob('cxc4kr7px23g0085r8v0', 'estimator')>,
<RuntimeJob('cxbdcxbpjw3000815df0', 'sampler')>,
<RuntimeJob('cxbcxexpx23g008mttr0', 'estimator')>]
You can also select by backend, job state, session, and more. For more information, see QiskitRuntimeService.jobs
in the API documentation.
Once you have the job ID, use the QiskitRuntimeService.job
method to retrieve it.
# Get ID of most recent successful job for demonstration.
# This will not work if you've never successfully run a job.
successful_job = next(
j for j in service.jobs(limit=1000) if j.status().name == "DONE"
)
job_id = successful_job.job_id()
print(job_id)
Output:
/tmp/ipykernel_1959/3090032923.py:4: DeprecationWarning: In a future release of qiskit-ibm-runtime no sooner than 3 months after the release date of 0.30.0, RuntimeJob.status() will be returned as a string instead of an instance of `JobStatus`. To prepare for this change, you can use the idiom `status.name if isinstance(status, JobStatus) else status`.
j for j in service.jobs(limit=1000) if j.status().name == "DONE"
cxbdcxbpjw3000815df0
retrieved_job = service.job(job_id)
retrieved_job.result()
Always forgetting how to retrieve a job? Try this prompt with Qiskit Code Assistant:
# Retrieve job JOB_ID from IBM Runtime and print the result
New to the code assistant? See Qiskit Code Assistant for installation and usage. Note this is an experimental feature and only available to IBM Quantum™ Premium Plan users.
Save results to disk
You may also want to save results to disk. To do this, use Python's built-in JSON library with Qiskit Runtime's encoders.
import json
from qiskit_ibm_runtime import RuntimeEncoder
with open("result.json", "w") as file:
json.dump(retrieved_job.result(), file, cls=RuntimeEncoder)
You can then load this array from disk in a separate kernel.
from qiskit_ibm_runtime import RuntimeDecoder
with open("result.json", "r") as file:
result = json.load(file, cls=RuntimeDecoder)
result