Skip to content

Commit

Permalink
Allow qml functions through autograph
Browse files Browse the repository at this point in the history
  • Loading branch information
dime10 committed Oct 16, 2024
1 parent 1e7914a commit ab53091
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
2 changes: 2 additions & 0 deletions frontend/catalyst/autograph/ag_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,9 @@ def converted_call(fn, args, kwargs, caller_fn_scope=None, options=None):
# HOTFIX: pass through calls of known Catalyst wrapper functions
if fn in (
catalyst.adjoint,
qml.adjoint,
catalyst.ctrl,
qml.ctrl,
catalyst.grad,
catalyst.value_and_grad,
catalyst.jacobian,
Expand Down
30 changes: 7 additions & 23 deletions frontend/test/pytest/test_autograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,7 @@
from jax.errors import TracerBoolConversionError
from numpy.testing import assert_allclose

from catalyst import (
AutoGraphError,
adjoint,
autograph_source,
cond,
ctrl,
debug,
disable_autograph,
for_loop,
grad,
jacobian,
jvp,
measure,
qjit,
run_autograph,
vjp,
vmap,
while_loop,
)
from catalyst import *
from catalyst.autograph.transformer import TRANSFORMER
from catalyst.utils.dummy import dummy_func
from catalyst.utils.exceptions import CompileError
Expand Down Expand Up @@ -295,7 +277,8 @@ def fn(x: float):
assert check_cache(inner.user_function.func)
assert fn(np.pi) == -1

def test_adjoint_wrapper(self):
@pytest.mark.parametrize("adjoint_fn", [adjoint, qml.adjoint])
def test_adjoint_wrapper(self, adjoint_fn):
"""Test conversion is happening succesfully on functions wrapped with 'adjoint'."""

def inner(x):
Expand All @@ -304,14 +287,15 @@ def inner(x):
@qjit(autograph=True)
@qml.qnode(qml.device("lightning.qubit", wires=1))
def fn(x: float):
adjoint(inner)(x)
adjoint_fn(inner)(x)
return qml.probs()

assert hasattr(fn.user_function, "ag_unconverted")
assert check_cache(inner)
assert np.allclose(fn(np.pi), [0.0, 1.0])

def test_ctrl_wrapper(self):
@pytest.mark.parametrize("ctrl_fn", [ctrl, qml.ctrl])
def test_ctrl_wrapper(self, ctrl_fn):
"""Test conversion is happening succesfully on functions wrapped with 'ctrl'."""

def inner(x):
Expand All @@ -320,7 +304,7 @@ def inner(x):
@qjit(autograph=True)
@qml.qnode(qml.device("lightning.qubit", wires=2))
def fn(x: float):
ctrl(inner, control=1)(x)
ctrl_fn(inner, control=1)(x)
return qml.probs()

assert hasattr(fn.user_function, "ag_unconverted")
Expand Down

0 comments on commit ab53091

Please sign in to comment.