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

Important

The way sessions are started within Qiskit Runtime has changed. By 1 April 2024, upgrade to qiskit-ibm-runtime version 0.20.0 or later. In addition, ensure you are using Qiskit version 0.45.0 or later. Starting 1 April, session calls made in earlier versions of these packages will fail.

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.

How sessions work

The basic workflow for sessions is as follows:

  1. The first job in a session enters the normal queue.
  2. When the first job starts running, the maximum timeout clock starts.
  3. Subsequent jobs within the session are prioritized over other jobs, reducing wait times.
  4. The interactive timeout runs between the jobs in a session. Every session has an interactive timeout value (ITTL, or interactive time to live). If there are no session jobs queued within the ITTL window, the session is temporarily deactivated and normal job selection resumes. A job can reactivate the deactivated session if the session has not reached its maximum timeout value.
    Note

    The job must go through the normal queue to reactivate the session.

  5. If the maximum timeout value is reached, the session ends and any remaining queued jobs fail. However, any jobs already running will run to completion.

The following video illustrates the session basic workflow:

To find the session timeout values, follow the instructions in Determine session details. To set the session maximum timeout value, follow the instructions in Session length.

The default timeout settings are listed in the Maximum execution time guide.

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 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