Skip to main contentIBM Quantum Documentation Preview
This is a preview build of IBM Quantum™ documentation. Refer to docs.quantum.ibm.com for the official documentation.

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.2.4
qiskit-ibm-runtime~=0.33.2
qiskit-aer~=0.15.1
qiskit-serverless~=0.18.0
qiskit-ibm-catalog~=0.2
qiskit-addon-sqd~=0.8.0
qiskit-addon-utils~=0.1.0
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('cwxg5cdtdtng0087gwpg', 'estimator')>,
 <RuntimeJob('cwvgyah997wg008xvkgg', 'sampler')>,
 <RuntimeJob('cwvg6e95v39g008hm670', 'noise-learner')>]

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:

/var/folders/z_/6s4ntyps5lsb232v7f82201r0000gn/T/ipykernel_42305/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`.
  j for j in service.jobs(limit=1000) if j.status().name == "DONE"
cwvgyah997wg008xvkgg
retrieved_job = service.job(job_id)
retrieved_job.result()

Output:

PrimitiveResult([SamplerPubResult(data=DataBin(meas=BitArray(<shape=(100,), num_shots=4096, num_bits=2>), shape=(100,)), metadata={'circuit_metadata': {}})], metadata={'execution': {'execution_spans': ExecutionSpans([SliceSpan(<start='2024-11-15 09:06:56', stop='2024-11-15 09:09:21', size=409600>)])}, 'version': 2})
Qiskit Code Assistant

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

Output:

PrimitiveResult([SamplerPubResult(data=DataBin(meas=BitArray(<shape=(100,), num_shots=4096, num_bits=2>), shape=(100,)), metadata={'circuit_metadata': {}})], metadata={'execution': {'execution_spans': ExecutionSpans([SliceSpan(<start='2024-11-15 09:06:56', stop='2024-11-15 09:09:21', size=409600>)])}, 'version': 2})