How to execute several pennylane circuits in one qiskit session?

Hello, I am trying to train a variational quantum algorithm on ibm’s hardware. To avoid huge delays, it would be most useful to be able to exploit the new QiskitRuntimeService feature that allows you to temporarily block a QPU by creating an interactive session. I tried to do it in a couple of different ways via the pennylane-qiskit plugin but did not succeed.

Any help in this regard would be much appreciated. Let me explain what I tried and how it failed.

Naive way

If you instantiate a qiskit.remote device in pennylane and submit jobs in the usual way (by means of creating a qnode), then each job will create its own session.

from qiskit_ibm_runtime import QiskitRuntimeService, Session
import numpy as np
import pennylane as qml

service = QiskitRuntimeService()

backend = service.least_busy(operational=True, simulator=False)
dev = qml.device('qiskit.remote', wires=127, backend=backend)

@qml.qnode(dev)
def circuit(input):
    qml.templates.AmplitudeEmbedding(features=input, wires=range(6),pad_with=0.0,normalize=True)
    return qml.expval(qml.PauliZ(0))

# run circuit
inputs = np.random.ramdom((2,2**6))
results = []
for i in range(2):
    results.append(circuit(inputs[i]))

Less naive way

Apparently, when instantiating a qiskit.remote device, one can provide a previously created session as a keyword argument. This creates a session that remains open until one submits the first job, once this job is completed, the session automatically closes and no more circuits can be submitted.

# same as before but we change the line where we instantiate the device
session = Session(backend=backend)
dev = qml.device('qiskit.remote', wires=127, backend=backend,session=session)

Third way

According to this tutorial, there is a qiskit_session class within pennylane_qiskit. However, I can’t import it (I tried with versions 0.37 and 0.38 for pennylane_qiskit):

from pennylane_qiskit import qiskit_session

Error:

----> 1 from pennylane_qiskit import qiskit_session

ImportError: cannot import name 'qiskit_session' from 'pennylane_qiskit' (path_to_env\venv\Lib\site-packages\pennylane_qiskit\__init__.py)

Fourth way

Naturally, one possibility would be to transform each QuantumTape generated in pennylane to a Qiskit’s QuantumCircuit and then submit all that within a session.

However, I would like to keep this as a last resort, since then I would lose pennylane’s automatic differentiation capabilities.

Conclusion

Is there a way in which I can submit several jobs to the same QiskitRuntimeService session from pennylane?

Thanks in advance for your help!

qml.about

Name: PennyLane
Version: 0.37.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: 
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-qiskit, PennyLane_Lightning

Platform info:           Windows-11-10.0.22621-SP0
Python version:          3.12.6
Numpy version:           1.26.4
Scipy version:           1.14.1
Installed devices:
- default.clifford (PennyLane-0.37.0)
- default.gaussian (PennyLane-0.37.0)
- default.mixed (PennyLane-0.37.0)
- default.qubit (PennyLane-0.37.0)
- default.qubit.autograd (PennyLane-0.37.0)
- default.qubit.jax (PennyLane-0.37.0)
- default.qubit.legacy (PennyLane-0.37.0)
- default.qubit.tf (PennyLane-0.37.0)
- default.qubit.torch (PennyLane-0.37.0)
- default.qutrit (PennyLane-0.37.0)
- default.qutrit.mixed (PennyLane-0.37.0)
- default.tensor (PennyLane-0.37.0)
- null.qubit (PennyLane-0.37.0)
- lightning.qubit (PennyLane_Lightning-0.38.0)
- qiskit.aer (PennyLane-qiskit-0.37.0)
- qiskit.basicaer (PennyLane-qiskit-0.37.0)
- qiskit.basicsim (PennyLane-qiskit-0.37.0)
- qiskit.ibmq (PennyLane-qiskit-0.37.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.37.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.37.0)
- qiskit.remote (PennyLane-qiskit-0.37.0)

Hi @ARO-GZ ,

Actually Qiskit seems to have removed their ‘Session’ keyword in their 0.30.0 dev release so any suggestion that I give you now may stop working very soon as soon as Qiskit makes another release. I’ll check with the team here who works on the plugin to see what the best alternative is. For now make sure to keep using qiskit_ibm_runtime v0.29.0. I hope to come back next week with more info.

Thanks @CatalinaAlbornoz, looking forward to your reply with further info!

Hi @ARO-GZ,

So unfortunately I found this note in a section of the Qiskit docs:

Session execution mode is not supported in the Open Plan. Jobs will run in job mode instead.

This means that there’s not really anything we can do on the PennyLane side.

If you have a Premium IBM plan you can probably open a context manager with a session (I can show you a code example if needed) but it’s not guaranteed to work since we cannot test it.

Hi @CatalinaAlbornoz , I am also trying to open a session with IBM hardware to run some QNN codes and my team has a premium plan for a few of IBM QPU devices. Would you mind sharing the code example so we can have a go at it?
Also, I get the same error as @ARO-GZ when I tried the method from the tutorial (this was after updating pennylane-qiskit package) I think the tutorial is outdated…

----> 1 from pennylane_qiskit import qiskit_session

ImportError: cannot import name 'qiskit_session' from 'pennylane_qiskit' (path_to_env\venv\Lib\site-packages\pennylane_qiskit\__init__.py)

Thanks,
Hoon

Hi @hoonjkim ,

I believe the answer to your ImportError should be solved with the update I suggested in this other thread. Let’s continue the conversation there in case you’re still facing issues.

1 Like