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.

Introduction to Qiskit Runtime sessions

Note

Session execution mode is not supported in the Open Plan. Jobs will run in job mode instead.


What is a session?

A session is a Qiskit Runtime feature that lets you efficiently run multi-job iterative workloads on quantum computers. Using sessions helps avoid delays caused by queuing each job separately, which can be particularly useful for iterative tasks that require frequent communication between classical and quantum resources.

Note

The queuing time does not decrease for the first job submitted within a session. Therefore, a session does not provide any benefits when running a single job. Additionally, sessions do not work on simulators because simulators do not have a queue.


Advantages of using sessions

There are several benefits to using sessions:

  • Efficiency: Multiple jobs from a single algorithm run can be run sequentially without interruptions.
  • Flexibility: You can submit jobs, check results, and submit new jobs within an active session without needing to start a new one.

For instructions to start a session, see Run a job in a session.


End a session

A session ends in the following circumstances:

  • The maximum timeout (TTL) value is reached, resulting in the cancelation of all queued jobs.
  • The session is manually canceled, resulting in the cancelation of all queued jobs.
  • The session is manually closed. The session stops accepting new jobs but continues to run queued jobs with priority.
  • If you use Session as a context manager, that is, with Session(), the session is automatically closed when the context ends (the same behavior as using session.close()).
Note

When a session ends, the QPU finishes running any job that is currently running.


Usage patterns

Sessions are especially useful for algorithms that require classical post-processing, where jobs submitted within the interactive time-out are processed immediately. If you want to submit jobs in a batch instead, see Run jobs in a batch.

Example: Run an iterative workload that uses the classical SciPy optimizer to minimize a cost function. In this model, SciPy uses the output of the cost function to calculate its next input.

def cost_func(params, ansatz, hamiltonian, estimator):
    # Return estimate of energy from estimator
 
    energy = estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0]
    return energy
 
x0 = 2 * np.pi * np.random.random(num_params)
 
session = Session(backend=backend)
 
# If using qiskit-ibm-runtime earlier than 0.24.0, change `mode=` to `session=`
estimator = Estimator(mode=session, options={"shots": int(1e4)})
res = minimize(cost_func, x0, args=(ansatz, hamiltonian, estimator), method="cobyla")
 
# Close the session because we didn't use a context manager.
session.close()

Next steps

Recommendations