Problem with Pennylane-Qiskit

Hello!

I’ve been trying to implement certain pennylane tutorials on qiskit backend. A week ago, i.e., when I had Pennylane v27, everything worked fine with just

dev = qml.device('qiskit.aer',wires=tot_qubits)
@qml.qnode(dev)

The tutorial I’m trying to implement is the VQLS tutorial. However, as I updated to v31 and tried to run this command again, I get a problem in the calculation of cost function (Variational Quantum Linear Solver | PennyLane Demos), the error originates at the calculation of cost and is an attribute error.

 w, cost = opt.step_and_cost(cost_loc, w)

 g, forward = self.compute_grad(objective_fn, args, kwargs, grad_fn=grad_fn)



AttributeError: 'list' object has no attribute 'measurements'

And, finally, here is the output of qml.about() :

Name: PennyLane
Version: 0.31.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /p/bv/bin/soft/Python-3.8.6/lib/python3.8/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info:           Linux-3.10.0-1160.90.1.el7.x86_64-x86_64-with-glibc2.2.5
Python version:          3.8.6
Numpy version:           1.23.2
Scipy version:           1.9.3
Installed devices:
- default.gaussian (PennyLane-0.31.1)
- default.mixed (PennyLane-0.31.1)
- default.qubit (PennyLane-0.31.1)
- default.qubit.autograd (PennyLane-0.31.1)
- default.qubit.jax (PennyLane-0.31.1)
- default.qubit.tf (PennyLane-0.31.1)
- default.qubit.torch (PennyLane-0.31.1)
- default.qutrit (PennyLane-0.31.1)
- null.qubit (PennyLane-0.31.1)
- lightning.qubit (PennyLane-Lightning-0.31.0)
- qiskit.aer (PennyLane-qiskit-0.27.0)
- qiskit.basicaer (PennyLane-qiskit-0.27.0)
- qiskit.ibmq (PennyLane-qiskit-0.27.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.27.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.27.0)

Is it possible that the Pennylane-qiskit needs to go from v27 to v31 so this error is circumvented?

Thanks!

Hey @Kalyani!

Can you post the full code example that replicates this behaviour? I know you said that you’re running the VQLS demo with the pennylane-qiskit backend, but it would help tremendously if we could see the exact code you’re running so that we can try to replicate the error :slight_smile:

Hello @Kalyani,

Perhaps defining a backend would help:
dev = qml.device(‘qiskit.aer’,wires=tot_qubits,backend=‘aer_simulator_statevector’)

There are more examples in documentation: The Aer device — PennyLane-Qiskit 0.32.0-dev documentation

Hi @Kalyani,

I know that Qiskit made a release recently so your issue may be related to this. You could try updating your versions of the pennylane-qiskit plugin and qiskit-aer. Alternatively you could create a new virtual environment and pip install pennylane pennylane-qiskit qiskit-aer.

Here’s a small code example that works well on the latest version of these 3 packages.

import qiskit
import pennylane as qml
from pennylane import numpy as np

dev = qml.device('qiskit.aer',wires=2, shots=10)

@qml.qnode(dev)
def circuit(w):
    qml.Hadamard(wires=0)
    qml.RX(w,wires=0)
    return qml.expval(qml.PauliZ(0))

w=np.array(0.1,requires_grad=True)

opt = qml.GradientDescentOptimizer(stepsize=0.1)
w, cost = opt.step_and_cost(circuit, w)
print(cost)

If the example above works but your own example doesn’t, you could try installing the development version of the pennylane-qiskit plugin and check if this solves your issue.

Please feel free to add more details here if none of these options help.

Thanks for your suggestion @kevinkawchak ! In this case you shouldn’t need to add the backend so it’s probably be something else causing the issue.

Hello!
Thanks a lot! Seems like updating solved the problem :smiley:

1 Like

That’s great to hear @Kalyani !