Skip to main contentIBM Quantum 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-aer~=0.15.1
qiskit-ibm-runtime~=0.31.0
qiskit-serverless~=0.17.1
qiskit-ibm-catalog~=0.1

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('cwj8a5r9r49g00893azg', 'sampler')>,
 <RuntimeJob('cwj82600r6b0008psmrg', 'noise-learner')>,
 <RuntimeJob('cwj7w400r6b0008psm4g', 'sampler')>]

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() if j.status().name == "DONE")
job_id = successful_job.job_id()
print(job_id)

Output:

/var/folders/z_/6s4ntyps5lsb232v7f82201r0000gn/T/ipykernel_5474/2704799770.py:3: 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`.
  successful_job = next(j for j in service.jobs() if j.status().name == "DONE")
cwj8a5r9r49g00893azg
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-01 07:37:02', stop='2024-11-01 07:39:03', 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-01 07:37:02', stop='2024-11-01 07:39:03', size=409600>)])}, 'version': 2})
Was this page helpful?
Report a bug or request content on GitHub.