Describe the bug
An error is raised when the gradient of a QNode with tensor observables is computed when using the Qiskit device. (Thanks @trbromley for catching this!)
To Reproduce
import pennylane as qml
import numpy as np
n_qubits = 2
depth = 2
noise = 0.1
def ansatz(weights):
weights = weights.reshape(depth, n_qubits, 3)
qml.templates.StronglyEntanglingLayers(weights, wires=list(range(n_qubits)))
# return qml.expval(qml.PauliZ(0)) # The error does NOT occur when using a single qubit observable
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
from qiskit.providers.aer.noise import NoiseModel
from qiskit.providers.aer.noise.errors.standard_errors import (
depolarizing_error,
amplitude_damping_error,
)
noise_model = NoiseModel()
noise_model.add_all_qubit_quantum_error(depolarizing_error(noise, 1), ["u1", "u2", "u3"])
dev_qsk = qml.device(
"qiskit.aer",
wires=n_qubits,
shots=10000,
noise_model=noise_model,
backend="qasm_simulator",
)
weights = np.random.random((depth, n_qubits, 3)).flatten()
exp_sampled = qml.QNode(ansatz, dev_qsk, diff_method="parameter-shift") # Want to get expectation value and gradient
grad_shift = qml.grad(exp_sampled, argnum=0)
print(exp_sampled(weights))
print(grad_shift(weights))
Expected behavior
The gradient is computed without any errors and the correct result is returned.
Screenshots
~/pennylane_qiskit/qiskit_device.py in expval(self, observable, wires, par)
353
354 # estimate the ev
--> 355 return np.mean(self.sample(observable, wires, par))
356
357 def var(self, observable, wires, par):
~/pennylane_qiskit/qiskit_device.py in sample(self, observable, wires, par)
404
405 for w, b in zip(eigvals, itertools.product([0, 1], repeat=len(wires))):
--> 406 samples = np.where(np.all(res == b, axis=1), w, samples)
407
408 return samples
<__array_function__ internals> in where(*args, **kwargs)
ValueError: operands could not be broadcast together with shapes (9999,) () (10000,)
Describe the bug An error is raised when the gradient of a
QNode
with tensor observables is computed when using theQiskit
device. (Thanks @trbromley for catching this!)To Reproduce
Expected behavior The gradient is computed without any errors and the correct result is returned.
Screenshots
qml.about() output
Additional context This bug is related to how the tensor observable part of
QiskitDevice.sample
is used: https://github.com/XanaduAI/pennylane-qiskit/blob/269802a7c58dd951c2a183e071e31ed93e3c906d/pennylane_qiskit/qiskit_device.py#L400When changing this to the logic used in
QubitDevice
, the error seems to be resolved.Also, on first look, it gave the impression that the error is raised upon consecutive calls to
sample
.Possible ways to resolve this bug:
QiskitDevice.sample
.QiskitDevice
toQubitDevice
(bigger undertaking).In either case, an integration test needs to be added testing gradient computation for the tensor observable case.