Running Pennylane circuit in IBMQ backend with Qiskit Runtime Session

Hi, I have a hybrid quantum-classical machine learning program which I am running on IBMQ backends using the qiskit-pennylane plugin. For each classical data-point, I need to run the quantum circuit with classical post processing of the expectation values. When running on IBMQ backends, the execution of each circuit corresponding to each data-point is submitted as a separate job. I was wondering if it is possible to use Qiskit Runtime Session with Pennylane circuit so that the whole program is submitted to IBMQ backend as a single job, and I do not have to wait in queue.

I am initializing “dev” in the following way-

dev = qml.device("qiskit.ibmq.circuit_runner", wires=n_qubits, backend="ibmq_quito", ibmqx_token=token)

Before running the loop on data points, I add-

with Session(service = service, backend = "ibmq_quito"):

but this does not work as the jobs are still being submitted as separate jobs. Is there a way to submit it as a single job?

1 Like

Im having similar issue trying to fit a variational classifier for 10 epochs in ibmq_quito is taking days, dont know what to do

1 Like

Hey @sdas!

Great question. This sounds a little strange to me… I’m going to confirm a couple things with some of our development team and I will get back to you as soon as I can :slight_smile:

Thanks @isaacdevlugt . Looking forward to your reply. If this function does not already exist, it will be very useful to add.

Hey @sdas!

There’s a batch_execute method that each of the pennylane-qiskit devices have (e.g. IBMQDevice — PennyLane-Qiskit 0.32.0-dev documentation). Does that do the trick for you?

Hi @isaacdevlugt this function does not seem to be useful. I have only one circuit which needs to run once for each data point. For example, a variational quantum circuit is trained for a model, and now I predict each data-point from the test set. I want that for the whole test dataset, the IBMQ device will be reserved for me (which you can achieve using Qiskit Runtime session), and I do not have to wait in queue for every data point.
I cannot use Qiskit runtime service because my network is a mixture of classical and quantum layers, where the quantum layer is defined as a pytorch nn.Module.

@sdas I think what’s best at this point is to see a minimal code example (as small as possible :pray: :sweat_smile:). Look forward to hearing back!

Also have the same problem. Is there a way to run qiskit primitives such as Sampler or Estimator with Pennylane? This would likely work in a session.

Hey @joaofbravo! Welcome to the forum :slight_smile:.

It might be best to make a new forum post for this :sweat_smile:, but the answer to your question is yes to Sampler (see here) and I don’t think so for estimator.

1 Like

I was wondering what the status of Qiskit runtime session support is within Pennylane. Are there any updates? I can’t seem to find any news

Hey @Matteo_Argenton! Welcome to the forum :slight_smile:

Check this documentation out: Qiskit Runtime Programs — PennyLane-Qiskit 0.35.0-dev documentation

Also, be on the lookout for v0.35 of PennyLane coming out next week! We have some exciting features for importing Qiskit code into PennyLane :eyes:

1 Like

Hello,

@isaacdevlugt unfortunately the link does not work.

I currently have the same issue: Starting an IBMQ session in Pennylane to train a hybrid quantum machine learning model. Do you already have a solution?

Thank you very much in advance!

Hi @stefan, welcome to the Forum!

I think your issue might be similar to the one described here about unexpected keyword argument 'session'.

If that’s the case just make sure to pip install qiskit_ibm_runtime==0.29.0. Then you can create a device as follows:

import pennylane as qml
from qiskit_ibm_runtime import QiskitRuntimeService

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

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

The code above will choose the least busy available backend.

Let me know if this is what you were looking for!

Dear @ CatalinaAlbornoz, thany you very much for your answer.

Scince I am using Qiskit 1.0 your code works and I can start a session on the IBM Quantum Platform.

Unfortunately, I have now a new issue: I am training a hybrid Quantum Machine Learning Model and every data point of a batch becomes an own job.
Previously I used qiskit 0.9 and it was possible to run a whole batch in one job. With Qiskit 0.9 I used the plugin “qiskit.ibmq” instead of “qiskit.remote”, which is obviously not avaible with Qiskit 1.0.

So my question would be if you know how I can run a whole batch with multiple data points in one job.

Thank you very much in advance and best regards
Stefan

Hi @stefan ,

Unfortunately Qiskit is no longer allowing you to run jobs together in a session as seen here in 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.

Hello @CatalinaAlbornoz,

thank you very much for your reply!

Within a session every data point becomes an own job. This is fine for me.

I now have the following problem: After the first Epoch, which was evaluated on an ibm quantum computer (ibmq), I receive the following error massage and the code aborts:

qiskit_ibm_runtime.exceptions.IBMRuntimeError: ‘Exception encountered when calling layer “QVC_PS” (type KerasLayer).\n\nThe session is closed.\n\nCall arguments received:\n • inputs=tf.Tensor(shape=(32, 6), dtype=float64)’

When I run the exact same code with the “default.qubit” backend everything works fine. I checked that the output of the QVC has the same dimension regardless of the backend.

Do you have any idea so solve the problem?

Hi @stefan, could you please share your code here?

I believe the key is in the part of the error which says The session is closed.

By looking at the code we might be able to identify what’s going on.