Hey @ming, sorry I didn’t realize this earlier, but QNodes that return qml.sample
aren’t generally differentiable. In the documentation for qml.sample
it says:
QNodes that return samples cannot, in general, be differentiated, since the derivative with respect to a sample — a stochastic process — is ill-defined. The one exception is if the QNode uses the parameter-shift method (
diff_method="parameter-shift"
), in which caseqml.sample(obs)
is interpreted as a single-shot expectation value of the observableobs
.
The second half of leads me to believe that there should be a workaround to your issue, but I think there’s a bug! I’ve made an issue here: [BUG] QNodes that return `qml.sample(obs)` and use `parameter-shift` are not differentiable · Issue #4840 · PennyLaneAI/pennylane · GitHub
If there are any updates, I’ll let you know