The formula for gradient says: B(\theta + \pi/2) - B(\theta - \pi/2)
but the code:

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:

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


work for this math formula.

Thanks!

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!

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