qiskit_session

pennylane_qiskit.qiskit_session(device, **kwargs)[source]

A context manager that creates a Qiskit Session and sets it as a session on the device while the context manager is active.

Warning

Currently, sessions cannot be used by IBM users on the Open plan. We recommend referring to the Qiskit Session documentation and opening a session with Qiskit’s session directly.

Using the context manager will ensure the Session closes properly and is removed from the device after completing the tasks. Any Session that was initialized and passed into the device will be overwritten by the Qiskit Session created by this context manager.

Parameters:
  • device (QiskitDevice2) – the device that will create remote tasks using the session

  • **kwargs – keyword arguments for session settings. Currently, the only relevant keyword argument is “max_time”, which allows setting the maximum amount of time the session is open.

Example:

import pennylane as qml
from pennylane_qiskit import qiskit_session
from qiskit_ibm_runtime import QiskitRuntimeService

# get backend
service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.least_busy(simulator=False, operational=True)

# initialize device
dev = qml.device('qiskit.remote', wires=2, backend=backend)

@qml.qnode(dev)
def circuit(x):
    qml.RX(x, 0)
    qml.CNOT([0, 1])
    return qml.expval(qml.PauliZ(1))

angle = 0.1

with qiskit_session(dev, max_time=60) as session:
    # queue for the first execution
    res = circuit(angle)[0]

    # then this loop executes immediately after without queueing again
    while res > 0:
        angle += 0.3
        res = circuit(angle)[0]

Note that if you passed in a session to your device, that session will be overwritten by qiskit_session.

import pennylane as qml
from pennylane_qiskit import qiskit_session
from qiskit_ibm_runtime import QiskitRuntimeService, Session

# get backend
service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.least_busy(simulator=False, operational=True)

# initialize device
dev = qml.device('qiskit.remote', wires=2, backend=backend, session=Session(backend=backend, max_time=30))

@qml.qnode(dev)
def circuit(x):
    qml.RX(x, 0)
    qml.CNOT([0, 1])
    return qml.expval(qml.PauliZ(1))

angle = 0.1

# This session will have the Qiskit default settings max_time=900
with qiskit_session(dev) as session:
    res = circuit(angle)[0]

    while res > 0:
        angle += 0.3
        res = circuit(angle)[0]