Hi, I’m a beginner in Pennylane and I’m reading the documentation. In Numpy interface, I try to run the code below,
dev = qml.device('default.qubit', wires=5)
@qml.qnode(dev)
def circuit(data, weights):
qml.AmplitudeEmbedding(data, wires=[0, 1, 2], normalize=True)
qml.RX(weights[0], wires=0)
qml.RY(weights[1], wires=1)
qml.RZ(weights[2], wires=2)
qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[0, 2])
return qml.expval(qml.PauliZ(0))
rng = np.random.default_rng(seed=42) # make the results reproducable
data = rng.random([2 ** 3], requires_grad=True)
weights = np.array([0.1, 0.2, 0.3], requires_grad=False)
print(qml.grad(circuit)(data, weights=weights))
but get a TypeError:
TypeError: must be real number, not ArrayBox
I find that if I just change the wires
in qml.device
to 3, which is the number of wires used in qml.AmplitudeEmbedding
, then the code works with a UserWarning:
[ 0.38170533 0.21644929 0.42344963 0.34393308 -0.04337338 -0.44932293 -0.35054294 -0.36202196]
ComplexWarning: Casting complex values to real discards the imaginary part
return A.astype(dtype, order, casting, subok, copy)
It seems like the question is related to the qml.AmplitudeEmbedding
function, but in qml.AmplitudeEmbedding
’s documentation it is said that
At the moment, the features argument is not differentiable when using the template, and gradients with respect to the features cannot be computed by PennyLane.
Due to non-trivial classical processing to construct the state preparation circuit, the features argument is in general not differentiable.
So what’s the output above? I’m confused about them.
I was wondering if there’s something wrong with the code in “Numpy interface” documentation, or if there’s something I ignored. Could it be possible to explain these questions?
Thanks in advance for your help!
Best regards,
Henry