Open poojithumeshrao opened 2 months ago
Hi @poojithumeshrao, thank you for opening this bug report! Our team took a look at your bug and found that this seems to be expected behaviour but with an error message that doesn't say much.
A ClassicalShadow
is a classical description of a quantum state that is capable of reproducing expectation values of local Pauli observables. In this case your error arises because the expected observable(s) aren't valid Pauli words. The Hermitian observable has no (obvious and a priori available) Pauli decomposition/representation.
Unfortunately it doesn't look like an easy fix from PennyLane's side but we will indeed improve the error message to make it easier to understand what's going on.
On the other hand we noticed that you're using PennyLane v0.32. If possible my recommendation would be to upgrade to the latest version. We will make a new release in two weeks so you can update your version now and again in two weeks to smooth out any possible issues.
If you want to learn more about classical shadows feel free to take a look at this demo.
Hi, @CatalinaAlbornoz, thank you for the timely response.
After going through the traceback, I was able to make it work (at least in my case) by manually decomposing the Hermitian matrix using qml.pauli_decompose(). The modified code is as follows:
import pennylane as qml
import pennylane.numpy as np
from matplotlib import pyplot as plt
from pennylane import classical_shadow, shadow_expval, ClassicalShadow, expval
np.random.seed(666)
def pqc(f1,wires=range(2)):
qml.ArbitraryStatePreparation(f1,wires=wires)
q = np.random.random((6))
mat = np.matrix(qml.matrix(pqc)(q))
mat = mat + mat.getH()
mat /= 2
H = qml.pauli_decompose(mat)
dev = qml.device("default.qubit", wires=range(2), shots=10000)
@qml.qnode(dev, interface="autograd")
def qnode(x, H):
qml.Hadamard(0)
qml.CNOT((0,1))
qml.RX(x, wires=0)
return shadow_expval([H])
x = np.array(0.5, requires_grad=True)
print(qnode(x, H), qml.grad(qnode)(x, H))
I am not sure if it works generally, but I thought it would also be useful for you.
Ah this is great! Thank you for sharing it @poojithumeshrao .
Hi @poojithumeshrao! Thanks for posting this. I tweaked the title of this issue to reflect that we still have a valid feature request to eventually take care of, i.e., support for qml.Hermitian
in classical shadows workflows.
Expected behavior
When the expectation value of a Hermitian observable is measured using qml.shadow_expval(), a float value has to be returned. Ideally, it must return a value close to qml.expval().
Actual behavior
"'NoneType' object is not iterable" error is thrown instead.
Additional information
No response
Source code
Tracebacks
System information
Existing GitHub issues