Skip to main contentIBM Quantum Documentation

Session

Session(service=None, backend=None, max_time=None)

Class for creating a flexible Qiskit Runtime session.

A Qiskit Runtime session allows you to group a collection of iterative calls to the quantum computer. A session is started when the first job within the session is started. Subsequent jobs within the session are prioritized by the scheduler. Data used within a session, such as transpiled circuits, is also cached to avoid unnecessary overhead.

You can open a Qiskit Runtime session using this Session class and submit jobs to one or more primitives.

For example:

from qiskit.test.reference_circuits import ReferenceCircuits
from qiskit_ibm_runtime import Sampler, Session, Options
 
options = Options(optimization_level=3)
 
with Session(backend="ibmq_qasm_simulator") as session:
    sampler = Sampler(session=session, options=options)
    job = sampler.run(ReferenceCircuits.bell())
    print(f"Sampler job ID: {job.job_id()}")
    print(f"Sampler job result: {job.result()}")

Session constructor.

Parameters

  • service (Optional[QiskitRuntimeService]) – Optional instance of the QiskitRuntimeService class. If None, the service associated with the backend, if known, is used. Otherwise QiskitRuntimeService() is used to initialize your default saved account.
  • backend (Union[str, IBMBackend, None]) – Optional instance of qiskit_ibm_runtime.IBMBackend class or string name of backend. An instance of qiskit_ibm_provider.IBMBackend will not work. If not specified, a backend will be selected automatically (IBM Cloud channel only).
  • max_time (Union[int, str, None]) – (EXPERIMENTAL setting, can break between releases without warning) Maximum amount of time, a runtime session can be open before being forcibly closed. Can be specified as seconds (int) or a string like “2h 30m 40s”. This value must be less than the system imposed maximum (opens in a new tab).

Raises

ValueError – If an input value is invalid.


Attributes

service

Return service associated with this session.

Return type

QiskitRuntimeService

Returns

qiskit_ibm_runtime.QiskitRuntimeService associated with this session.

session_id

Return the session ID.

Return type

str

Returns

Session ID. None until a job runs in the session.


Methods

backend

backend()

Return backend for this session.

Return type

Optional[str]

Returns

Backend for this session. None if unknown.

cancel

cancel()

Cancel all pending jobs in a session.

Return type

None

close

close()

Close the session so new jobs will no longer be accepted, but existing queued or running jobs will run to completion. The session will be terminated once there are no more pending jobs.

Return type

None

details

details()

Return session details.

Returns

id: id of the session. backend_name: backend used for the session. interactive_timeout: The maximum idle time (in seconds) between jobs that is allowed to occur before the session is deactivated. max_time: Maximum allowed time (in seconds) for the session, subject to plan limits. active_timeout: The maximum time (in seconds) a session can stay active. state: State of the session - open, active, inactive, or closed. accepting_jobs: Whether or not the session is accepting jobs. last_job_started: Timestamp of when the last job in the session started. last_job_completed: Timestamp of when the last job in the session completed. started_at: Timestamp of when the session was started. closed_at: Timestamp of when the session was closed.

Return type

A dictionary with the sessions details, including

from_id

classmethod from_id(session_id, service, backend=None)

Construct a Session object with a given session_id

Parameters

  • session_id (str) – the id of the session to be created. This must be an already existing session id.
  • service (QiskitRuntimeService) – instance of the QiskitRuntimeService class.
  • backend (Union[str, IBMBackend, None]) – instance of qiskit_ibm_runtime.IBMBackend class or string name of backend.

Raises

IBMInputValueError – If given session_id does not exist. or the backend passed in does not match the original session backend.

Return type

Session

Returns

A new Session with the given session_id

run

run(program_id, inputs, options=None, callback=None, result_decoder=None)

Run a program in the session.

Parameters

  • program_id (str) – Program ID.
  • inputs (Union[Dict, ParameterNamespace]) – Program input parameters. These input values are passed to the runtime program.
  • options (Optional[Dict]) – Runtime options that control the execution environment. See qiskit_ibm_runtime.RuntimeOptions for all available options.
  • callback (Optional[Callable]) – Callback function to be invoked for any interim results and final result.

Return type

RuntimeJob

Returns

Submitted job.

status

status()

Return current session status.

Returns

Pending: Session is created but not active. It will become active when the next job of this session is dequeued. In progress, accepting new jobs: session is active and accepting new jobs. In progress, not accepting new jobs: session is active and not accepting new jobs. Closed: max_time expired or session was explicitly closed. None: status details are not available.

Return type

The current status of the session, including

Was this page helpful?