Session¶
- class Session(backend, max_time=None, *, create_new=True)[source]¶
Bases:
objectClass for creating a Qiskit Runtime session.
A Qiskit Runtime
sessionallows 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.You can open a Qiskit Runtime session using this
Sessionclass and submit jobs to one or more primitives.For example:
from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager from qiskit_ibm_runtime import QiskitRuntimeService, Session, SamplerV2 as Sampler service = QiskitRuntimeService() backend = service.least_busy(operational=True, simulator=False) # Bell Circuit qr = QuantumRegister(2, name="qr") cr = ClassicalRegister(2, name="cr") qc = QuantumCircuit(qr, cr, name="bell") qc.h(qr[0]) qc.cx(qr[0], qr[1]) qc.measure(qr, cr) pm = generate_preset_pass_manager(backend=backend, optimization_level=1) isa_circuit = pm.run(qc) with Session(backend=backend) as session: sampler = Sampler(mode=session) job = sampler.run([isa_circuit]) pub_result = job.result()[0] print(f"Sampler job ID: {job.job_id()}") print(f"Counts: {pub_result.data.cr.get_counts()}")
Session constructor.
- Parameters:
backend (BackendV2) – Instance of
Backendclass.max_time (Optional[Union[int, str]]) – 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.
create_new (Optional[bool]) – If True, the POST session API endpoint will be called to create a new session. Prevents creating a new session when
from_id()is called.
- Raises:
ValueError – If an input value is invalid.
Attributes
- service¶
Return service associated with this session.
- Returns:
qiskit_ibm_runtime.QiskitRuntimeServiceassociated with this session.
- session_id¶
Return the session ID.
- Returns:
Session ID. None if the backend is a simulator.
Methods
- backend()[source]¶
Return backend for this session.
- Returns:
Backend for this session. None if unknown.
- Return type:
str | None
- close()[source]¶
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()[source]¶
Return session details.
- Returns:
A dictionary with the sessions details.
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.activated_at: Timestamp of when the session state was changed to active.mode: Execution mode of the session.usage_time: The usage time, in seconds, of this Session or Batch. Usage is defined as the time a quantum system is committed to complete a job.
- Return type:
Dict[str, Any] | None
- classmethod from_id(session_id, service)[source]¶
Construct a Session object with a given
session_id. For example:from qiskit_ibm_runtime import QiskitRuntimeService, Session service = QiskitRuntimeService() job = service.job(<job_id>) existing_session_id = job.session_id new_session = Session.from_id(existing_session_id, service)
- 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
QiskitRuntimeServiceclass.- Raises:
IBMInputValueError: If given session_id does not exist. IBMRuntimeError: If the backend of the session is unknown.
- Returns:
A new Session with the given
session_id- Return type:
- status()[source]¶
Return current session status.
- Returns:
Session status as a string.
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:
str | None