Manually setting finite_diff step size

Hello!

I’m trying to manually set the finite_diff step size in a defined operator.

As an example let’s take

import pennylane as qml
from pennylane import numpy as np

class RXX(pennylane.operation.Operation):
    num_params = 1
    num_wires = 2
    par_domain = "R"

    grad_method = "F"

    @staticmethod
    def _matrix(*params):
        theta = params[0]
        c = np.cos(0.5 * theta)
        s = np.sin(0.5 * theta)
        return np.array(
            [
                [c, 0, 0, -s],
                [0, c, -s, 0],
                [0, -s, c, 0],
                [-s, 0, 0, c]
            ]
        )

How do I tell grad_method = "F" that I want a certain step size? I know that I can set the step size in Qnode but doing this sets the step size for the entire circuit. In this case I want to just set the step size for this particular class.

Hey @QML (nice username by the way lol)

Just so I understand your question, is this the classical analogue of what you want to do?

Given a function defined over two variables f(x, y), calculate its partial derivatives as follows:

\frac{\partial}{\partial x} f(x,y) \approx \frac{f(x + \delta, y) - f(x - \delta, y)}{2\delta}

and

\frac{\partial}{\partial y} f(x,y) \approx \frac{f(x, y + \epsilon) - f(x, y - \epsilon)}{2\epsilon}

for \delta \neq \epsilon.

I.e., this is what your eventual use case would look like. You would have your RXX gate in some larger circuit (f in the classical analogue) with other tunable parameters. But, for the parameters associated with RXX, use a different shift.

Yes, the equations given would be the effective results of the finite difference.

And yes, the parameters of RXX would have a different shift. I think the setup you’re proposing is the same one I’m intending to implement.

Glad I understand!

Unfortunately, this isn’t supported yet in PennyLane. As you stated, you can only access the step size for the entire circuit with:

@qml.qnode(dev, diff_method="finite-diff", h=<your step size>)

The PennyLane dev team is working on supporting this, though!

Out of curiosity, is there a reason why you’re not using parameter-shift? There is more operator-to-operator tweaking available in this case currently. From the generator of the RXX gate, you can use this to obtain parameter frequencies. With this, all you’d add to your class is:

parameter_frequencies = (...)

1 Like