pennylane icon indicating copy to clipboard operation
pennylane copied to clipboard

[BUG] TensorFlow + second order derivatives + the adjoint diff method is not compatible

Open trbromley opened this issue 1 year ago • 3 comments

Expected behavior

Second order derivative is calculated correctly when using TensorFlow and the adjoint differentiation method.

Actual behavior

Second order derivative is None

Additional information

Brought up in this forum post.

Source code

import pennylane as qml
import tensorflow as tf

dev = qml.device("default.qubit")

@qml.qnode(dev, diff_method="adjoint")
def f(x):
    qml.RX(x, 0)
    return qml.expval(qml.PauliZ(0))

x = tf.Variable(0.3, dtype=tf.float64)

with tf.GradientTape(persistent=True) as tape1:
    with tf.GradientTape(persistent=True) as tape0:
        res = f(x)

    grad0 = tape0.gradient(res, x)

grad1 = tape1.gradient(grad0, x)
assert grad1 is not None

Tracebacks

No response

System information

PennyLane v0.35.0 and 2.15.0.

Existing GitHub issues

  • [X] I have searched existing GitHub issues to make sure the issue does not already exist.

trbromley avatar Mar 07 '24 14:03 trbromley

So higher order derivatives aren't compatible with any (non-backprop) device derivative, and it would be a major project to get this to work.

I guess the only solution I can really think of would be to somehow detect when a user is trying to take second-order derivatives without requesting them, and raise a more informative error. This is also tied to issue #5234.

albi3ro avatar Mar 07 '24 15:03 albi3ro

Have we implemented 2nd order adjoint anywhere? Or is requiring this implementation the biggest blocker?

josh146 avatar Mar 07 '24 15:03 josh146

I implemented it few years ago, so I'm not too concerned with that part. But it might take a little bit of work to figure out how to register it with the ML framework.

albi3ro avatar Mar 07 '24 17:03 albi3ro

Closing this issue for now, since it is less of a bug, and more of a new feature we need to develop (adjoint differentiation + second order derivatives)

josh146 avatar Aug 27 '24 17:08 josh146