Skip to content

Commit

Permalink
Add tests returning NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
rauletorresc committed Jan 7, 2025
1 parent 3c76a60 commit 661a187
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions frontend/test/pytest/test_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,5 +1780,46 @@ def fn(x):
assert np.allclose(res_pattern_partial, expected)


class TestDecompositionGradient:
"""Test usage Gradient functions over decomposed gates"""

def test_compute_decomposition(self):
"""Test usage over computed decomposition."""

dev = qml.device("lightning.qubit", wires=1)

@qml.qnode(dev)
def circuit(x):
U = jnp.array([[1, 0], [0, x]])
decomp = qml.QubitUnitary.compute_decomposition(U, wires=0)
for op in decomp:
qml.apply(op)
return qml.probs()

def f(x):
probs = circuit(x)
return probs[0] + probs[1]

assert np.isnan(grad(qjit(f), argnums=0)(0.0)) == True

def test_unitary_to_rot(self):
"""Test usage with unitary to rot transform."""

dev = qml.device("lightning.qubit", wires=1)

@qml.transforms.unitary_to_rot
@qml.qnode(dev)
def circuit(x):
U = jnp.array([[1, 0], [0, x]])
qml.QubitUnitary(U, wires=0)
return qml.probs()

def f(x):
probs = circuit(x)
return probs[0] + probs[1]

assert np.isnan(grad(qjit(f), argnums=0)(0.0)) == True


if __name__ == "__main__":
pytest.main(["-x", __file__])

0 comments on commit 661a187

Please sign in to comment.