I noticed that calculating qml.jacobian(qml.grad(circuit)) using (lightning.qubit, diff_method=‘adjoint’) is not possible but using other diff methods is possible. But other diff methods are quite slow. Can someone please let me know if this is the case or am i making mistake.
Tagging @mlxd here because he’s the expert, but pennylane-lightning does support adjoint differentiation — there are caveats! For details, see here.
TLDR, adjoint differentiation has the following restrictions:
As it requires knowledge of the statevector, only statevector simulator devices can be used.
Only expectation values are supported as measurements.
Does not work for parametrized observables like Hamiltonian or Hermitian.
So, here is an example of pennylane-lightning in action with adjoint differentiation and the object you want calculated!
import pennylane as qml
from pennylane import numpy as np
dev_lightning = qml.device('lightning.qubit', wires=2)
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev_lightning, diff_method="adjoint")
def circuit_adjoint(a):
qml.RX(a[0], wires=0)
qml.CNOT(wires=(0,1))
qml.RY(a[1], wires=1)
qml.RZ(a[2], wires=1)
return qml.expval(qml.PauliX(wires=1))
x = np.array([0.1, 0.2, 0.3], requires_grad=True)
print(qml.jacobian(qml.grad(circuit_adjoint))(x))
I’m not sure if you actually meant to calculate qml.jacobian(qml.grad(circuit_adjoint))(x). In this example, the output seems to be independent of the input. Nevertheless, it runs! If you meant to just calculate, say, the Jacobian, it works as well :).
Hi @Abhishek
Just jumping in for a comment. The adjoint diff method in lightning.qubit only supports evaluation of Jacobian expectation values. If you would like to evaluate the Hessian, it is best to use the parameter shift method you have already discovered, since param-shift will have all the necessary backend support for the higher-order derivative.