Trouble implementing parameter shift rule on a CV circuit

Hello,

I am finding it a little difficult to find the gradient of my simple circuit using parameter shift rule.

@qml.qnode(dev_gaussian)

def experiment(r, a, phi, rot):

    qml.Squeezing(r, 0, wires = 0)

    qml.Displacement(a, phi, wires = 0)

    qml.Rotation(rot, wires = 0)

    return qml.expval(qml.X(0))

It is a basic squeezed coherent state with a rotation gate applied.
I find that when I try to find the gradient with respect to the rotation gate, the results by parameter shift rule doesn’t match that by finite differences.
Can you explain why this behaviour?

Parameter shift rule used as described in documentation:

plus = experiment(r,a,phi,rot + np.pi / 2)
minus = experiment(r,a,phi,rot - np.pi / 2)

grad = (0.5 * (plus - minus))

Hi @Jane! The manual parameter-shift rule you have defined appears to work for me. For example, considering the same circuit and parameter, and comparing all three methods:

def experiment(r, a, phi, rot):
   qml.Squeezing(r, 0, wires=0)
   qml.Displacement(a, phi, wires=0)
   qml.Rotation(rot, wires=0)
   return qml.expval(qml.X(0))

rot = np.array(0.56, requires_grad=True)

PennyLane’s finite differences:

>>> qnode = qml.QNode(experiment, dev_gaussian, diff_method="finite-diff")
>>> qml.grad(qnode)(1.0, 0.6, 0.76, rot)
-1.162458135861577

PennyLane’s parameter-shift:

>>> qnode = qml.QNode(experiment, dev_gaussian, diff_method="parameter-shift")
>>> qml.grad(qnode)(1.0, 0.6, 0.76, rot)
-1.1624581201419182

Manual parameter-shift:

>>> plus = qnode(1.0, 0.6, 0.76, rot + np.pi / 2)
>>> minus = qnode(1.0, 0.6, 0.76, rot - np.pi / 2)
>>> (0.5 * (plus - minus))
tensor(-1.16245812, requires_grad=True)