# How can we compute the gradient in a Quantum RNN?

I was looking into implementing a quantum recurrent neural network (QRNN) for a project, but I have some doubts about the computation of the gradient. There are a few papers that have implemented a QRNN (e.g. https://arxiv.org/abs/2006.14619, https://link.aps.org/accepted/10.1103/PhysRevA.103.052414, and https://proceedings.neurips.cc/paper/2020/file/0ec96be397dd6d3cf2fecb4a2d627c1c-Paper.pdf), but they are all running a simulation rather than using classical hardware.

The fact that they are running a simulation means that they can use the finite-difference method to compute their gradients, and in their papers that is what they are doing, using something like scipy's BFGS algorithm to find the steps in which the function can improve.

But I don't think this will work well on real hardware. Well, I am not qualified to say that all of these papers are fundamentally wrong because I have just started learning, but I really don't see how it makes sense.

https://www.youtube.com/watch?v=McgBeSVIGus explains how, for quantum neural networks, we prefer to use a parameter-shift rule over the finite-difference method, since the finite difference method is too numerically unstable to work when you have to deal with noisy quantum devices and the estimation of the expected value that you do during evaluation.

Except, I don't think the parameter shift rule will apply to RNNs. I tried to follow the proof at https://pennylane.ai/qml/glossary/parameter_shift.html, and it basically assumes you have a series of N quantum gates, then a gate you want to differentiate, and then another series of M quantum gates and then the loss function. This way you can mathematically collapse the first N gates into the input and the last M gates into the loss function and compute a gradient w.r.t. just the middle gate. However, in a recurrent network, the gate that we want to differentiate will appear many times, not just once, so if the parameter shift rule works, I don't think this proof will cover it. The linked papers do not address this concern; one of them mentions the parameter shift rule in passing and says it would work but doesn't explain why it would work and proceeds to use finite-differences in their implementation.

Am I missing something here or is there actually a huge numerical problem with computing the gradient recurrent quantum neural networks?

• Hi @alex-li, could you elaborate more about what you have in mind for a quantum RNN? What does the circuit/architecture of the model look like? Oct 26, 2022 at 18:09
• I am very generally with this question, but a general architecture could be something like figure 1 of arxiv.org/pdf/2207.00378.pdf. Essentially, you define a parameterized circuit by guessing, and then run inputs through that circuit many times in a row, doing a partial measurement after each input to get an output. Oct 27, 2022 at 17:05
• I don't have time to write a full answer, but if the same parameter/gate appears multiple times in your circuit, you just have to use the product rule. Your final gradient will be a sum of the (parameter-shift) gradient contributions from each place the gate appears. Oct 28, 2022 at 16:45