# Quantum gradients with backpropagation

**URL:** https://discuss.pennylane.ai/t/quantum-gradients-with-backpropagation/609
**Category:** PennyLane Help
**Created:** [October 6, 2020, 11:40am UTC](https://discuss.pennylane.ai/t/quantum-gradients-with-backpropagation/609 "2020-10-06T11:40:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![cubicgate](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/cubicgate/32/56_2.png) [@cubicgate](https://discuss.pennylane.ai/u/cubicgate)
#### Post date: [October 6, 2020, 11:40am UTC](https://discuss.pennylane.ai/t/quantum-gradients-with-backpropagation/609/1 "2020-10-06T11:40:31Z")

</div>

I am reading: [https://pennylane.ai/qml/demos/tutorial\_backprop.html](https://pennylane.ai/qml/demos/tutorial_backprop.html)  
The formula for gradient says: B(\theta + \pi/2) - B(\theta - \pi/2)  
but the code:

```auto
def parameter_shift_term(qnode, params, i):
    shifted = params.copy()
    shifted[i] += np.pi/2
    forward = qnode(shifted) # forward evaluation

    shifted[i] -= np.pi
    backward = qnode(shifted) # backward evaluation

    return 0.5 * (forward - backward)

```

I am wondering how:

```auto
shifted[i] += np.pi/2 
shifted[i] -= np.pi

```

work for this math formula.

Thanks!

---

<div class="post-metadata">

### Author: ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)
#### Post date: [October 6, 2020, 8:40pm UTC](https://discuss.pennylane.ai/t/quantum-gradients-with-backpropagation/609/2 "2020-10-06T20:40:53Z")

</div>

Hi @cubicgate,

Thank you so much for your question! 🙂

Indeed, in order to compute the derivative of \langle \hat{B} \rangle (\boldsymbol\theta) mentioned in the tutorial, we’d have two shifts:  
 \nabla\_{\theta\_i}\langle \hat{B} \rangle(\boldsymbol\theta) = \frac{1}{2} \left[\langle \hat{B} \rangle\left(\boldsymbol\theta + \frac{\pi}{2}\hat{\mathbf{e}}\_i\right) - \langle \hat{B} \rangle\left(\boldsymbol\theta - \frac{\pi}{2}\hat{\mathbf{e}}\_i\right) \right].

In the code, care should be taken as to what parameters the shifts are applied to. The reason is that the same shifted object is used for both forward and backward evaluations.

1. We initialize the shifted object to \boldsymbol\theta.
2. We then apply the shift such that shifted = \boldsymbol\theta + \frac{\pi}{2}\hat{\mathbf{e}}\_i and compute \langle \hat{B} \rangle\left(\boldsymbol\theta + \frac{\pi}{2}\hat{\mathbf{e}}\_i\right).
3. The value of shifted remained \boldsymbol\theta + \frac{\pi}{2}\hat{\mathbf{e}}\_i after the previous computation and it contains the shifted parameters.  
Therefore we have to apply a -\pi shift: -\pi/2 to obtain the initial parameters and -\pi/2 to get the parameters for the backward evaluation: shifted = \boldsymbol\theta + \frac{\pi}{2}\hat{\mathbf{e}}\_i - \pi\hat{\mathbf{e}}\_i = \boldsymbol\theta - \frac{\pi}{2}\hat{\mathbf{e}}\_i.  
We then use shifted to compute \langle \hat{B} \rangle\left(\boldsymbol\theta - \frac{\pi}{2}\hat{\mathbf{e}}\_i\right) for the backward evaluation.

Hope this helps, let us know if you have further questions!

---

<div class="post-metadata">

### Author: ![cubicgate](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/cubicgate/32/56_2.png) [@cubicgate](https://discuss.pennylane.ai/u/cubicgate)
#### Post date: [October 6, 2020, 11:45pm UTC](https://discuss.pennylane.ai/t/quantum-gradients-with-backpropagation/609/3 "2020-10-06T23:45:48Z")

</div>

Thanks @antalszava for your explanation! I did not see that you had pi (not pi/2) in the second line for shifted.
