Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argnums parameter of catalyst.grad is ignored #1477

Open
lasradorohan opened this issue Jan 21, 2025 · 1 comment
Open

argnums parameter of catalyst.grad is ignored #1477

lasradorohan opened this issue Jan 21, 2025 · 1 comment

Comments

@lasradorohan
Copy link

Parameter argnums of catalyst.grad() and catalyst.value_and_grad() is getting ignored. The gradients returned are of the first parameter regardless of argnums.

Code to reproduce:

import catalyst
import jax.numpy as jnp
import jax.random
import pennylane as qml
from pennylane import qjit, qnode

device = qml.device("lightning.qubit", wires=4)

@qjit
@qnode(device, interface="jax")
def circuit(inputs, weights):
    qml.AngleEmbedding(features=inputs, wires=range(4), rotation="X")
    for i in range(1, 4):
        qml.CRX(weights[i - 1], wires=[i, 0])
    return qml.expval(qml.PauliZ(wires=0))


if __name__ == '__main__':
    weights = jax.random.uniform(jax.random.key(0), shape=(3,), minval=0, maxval=jnp.pi)
    inputs = jax.random.uniform(jax.random.key(1), shape=(3,))
    print(catalyst.grad(circuit, argnums=0)(weights, inputs))
    print(catalyst.grad(circuit, argnums=1)(weights, inputs))
    print(catalyst.grad(circuit, argnums=(0, 1))(weights, inputs))
    print(catalyst.value_and_grad(circuit, argnums=0)(weights, inputs))
    print(catalyst.value_and_grad(circuit, argnums=1)(weights, inputs))
    print(catalyst.value_and_grad(circuit, argnums=(0, 1))(weights, inputs))

Output:

[-0.91515096 -0.0873711  -0.05631352]
[-0.91515096 -0.0873711  -0.05631352]
[-0.91515096 -0.0873711  -0.05631352]
(Array(-0.37879515, dtype=float64), Array([-0.91515096, -0.0873711 , -0.05631352], dtype=float64))
(Array(-0.37879515, dtype=float64), Array([-0.91515096, -0.0873711 , -0.05631352], dtype=float64))
(Array(-0.37879515, dtype=float64), Array([-0.91515096, -0.0873711 , -0.05631352], dtype=float64))

Issue with output: all the gradients are from the first input and therefore the same.

@lasradorohan
Copy link
Author

PR: #1478

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant