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.

- We initialize the shifted object to \boldsymbol\theta.
- 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).
- 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!