Differentiating quantum circuits using backprop

How does Pennylane compute gradients of quantum circuits using PyTorch for example? Does it turn a quantum circuit into a graph which PyTorch can backpropagate on?


Hi @Zohim_Chandani1 ,

A PyTorch-interfacing QNode acts like any other torch.autograd.Function. You can learn more about it here. backprop can be used to calculate gradients with PyTorch.

To optimize your hybrid classical-quantum model using the Torch interface, you must make use of the PyTorch provided optimizers, or your own custom PyTorch optimizer.

I hope this helps!

Thanks for your response.

I am unsure how the bridge between quantum cirucits in Pennylane and Pytorch happens.

I have been studying this file and it is unclear to me how Pytorch can take a quantum circuit and use autograd to differentiate it.

Can you please expand on this or link me the relevant files that enable this.


Hi @Zohim_Chandani1 ,

Actually the answer is quite complex. If you’re using default.qubit and backprop then everything should be a torch tensor and it should be Torch itself doing all of the work. However if you’re using a different device (eg. actual quantum hardware) then Torch cannot see what’s inside so everything will be a black box for Torch and it’s PennyLane who will calculate the gradients. Depending on the device you’re running on PennyLane will choose a different differentiation method.

That being said, not everything always works the way it should. Check out this thread to see how the inputs get transformed and how, in this particular case, something becomes a black box for Torch even though in theory it shouldn’t. I think seeing these Torchlens diagrams can help you answer your question.

Let me know if it does!