# Manually setting finite\_diff step size

**URL:** <https://discuss.pennylane.ai/t/manually-setting-finite-diff-step-size/1824>\
**Category:** PennyLane Help\
**Created:** [April 12, 2022, 5:04pm UTC](https://discuss.pennylane.ai/t/manually-setting-finite-diff-step-size/1824 "2022-04-12T17:04:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![QML](https://avatars.discourse-cdn.com/v4/letter/q/bc8723/32.png) [@QML](https://discuss.pennylane.ai/u/QML)\
**Post date:** [April 12, 2022, 5:04pm UTC](https://discuss.pennylane.ai/t/manually-setting-finite-diff-step-size/1824/1 "2022-04-12T17:04:15Z")

</div>

Hello!

I’m trying to manually set the finite\_diff step size in a defined operator.

As an example let’s take

```auto
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.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [April 12, 2022, 7:19pm UTC](https://discuss.pennylane.ai/t/manually-setting-finite-diff-step-size/1824/2 "2022-04-12T19:19:10Z")

</div>

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._

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [April 12, 2022, 7:21pm UTC](https://discuss.pennylane.ai/t/manually-setting-finite-diff-step-size/1824/3 "2022-04-12T19:21:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![QML](https://avatars.discourse-cdn.com/v4/letter/q/bc8723/32.png) [@QML](https://discuss.pennylane.ai/u/QML)\
**Post date:** [April 12, 2022, 7:28pm UTC](https://discuss.pennylane.ai/t/manually-setting-finite-diff-step-size/1824/4 "2022-04-12T19:28:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [April 12, 2022, 7:47pm UTC](https://discuss.pennylane.ai/t/manually-setting-finite-diff-step-size/1824/5 "2022-04-12T19:47:36Z")

</div>

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](https://pennylane.readthedocs.io/en/latest/code/api/pennylane.gradients.eigvals_to_frequencies.html) to obtain parameter frequencies. With this, all you’d add to your class is:

`parameter_frequencies = (...)`
