English
Languages
English
Japanese
Spanish

Session

class Session(service=None, backend=None, max_time=None)[source]

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()}")
    # Close the session only if all jobs are finished and
    # you don't need to run more in the session.
    session.close()

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.

Raises:

ValueError – If an input value is invalid.

Attributes

Session.service

Return service associated with this session.

Session.session_id

Return the session ID.

Methods

Session.backend()

Return backend for this session.

Session.close()

Close the session.

Session.from_id(session_id[, service, ...])

Construct a Session object with a given session_id

Session.run(program_id, inputs[, options, ...])

Run a program in the session.