Skip to main contentIBM Quantum Documentation

Save and retrieve jobs

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 Jobs dashboard(opens in a new tab).

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(opens in a new tab) object.

[1] :
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('ct1hmmanb5jg008f55kg', 'estimator')>,
 <RuntimeJob('ct18bvqnb5jg008f3bp0', 'sampler')>,
 <RuntimeJob('ct18bmenb5jg008f3bk0', '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.

[2] :
# 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:

ct18bvqnb5jg008f3bp0
[3] :
retrieved_job = service.job(job_id)
retrieved_job.result()

Output:

PrimitiveResult([PubResult(data=DataBin(meas=BitArray(<shape=(), num_shots=10000, num_bits=5>)), metadata={'circuit_metadata': {}})], metadata={'version': 2})

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 IBM Runtime's encoders.

[4] :
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.

[5] :
from qiskit_ibm_runtime import RuntimeDecoder
with open("result.json", "r") as file:
    result = json.load(file, cls=RuntimeDecoder)
 
result

Output:

PrimitiveResult([PubResult(data=DataBin(meas=BitArray(<shape=(), num_shots=10000, num_bits=5>)), metadata={'circuit_metadata': {}})], metadata={'version': 2})
Was this page helpful?
Report a bug or request content on GitHub.