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

Fix FABLE template to return the correct result in JIT mode #6263

Merged
merged 15 commits into from
Sep 13, 2024
Merged
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
* The ``qml.Qubitization`` template now orders the ``control`` wires first and the ``hamiltonian`` wires second, which is the expected according to other templates.
[(#6229)](https://github.com/PennyLaneAI/pennylane/pull/6229)

* The ``qml.FABLE`` template now returns the correct value when JIT is enabled.
[(#6263)](https://github.com/PennyLaneAI/pennylane/pull/6263)

* <h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
Expand Down
6 changes: 5 additions & 1 deletion pennylane/templates/subroutines/fable.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
super().__init__(input_matrix, wires=wires, id=id)

@staticmethod
def compute_decomposition(input_matrix, wires, tol=0): # pylint:disable=arguments-differ

Check notice on line 132 in pennylane/templates/subroutines/fable.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/templates/subroutines/fable.py#L132

Too many branches (14/12) (too-many-branches)
r"""Sequence of gates that represents the efficient circuit produced by the FABLE technique

Args:
Expand Down Expand Up @@ -166,7 +166,11 @@
for c_wire in nots:
op_list.append(qml.CNOT(wires=[c_wire] + ancilla))
op_list.append(qml.RY(2 * theta, wires=ancilla))
nots[wire_map[control_index]] = 1
nots = {}
if wire_map[control_index] in nots:
del nots[wire_map[control_index]]
else:
nots[wire_map[control_index]] = 1
willjmax marked this conversation as resolved.
Show resolved Hide resolved
else:
if abs(2 * theta) > tol:
for c_wire in nots:
Expand Down
23 changes: 23 additions & 0 deletions tests/templates/test_subroutines/test_fable.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,29 @@ def circuit_jax(input_matrix):
gradient_jax = grad_fn(input_matrix_jax)
assert np.allclose(gradient_numeric, gradient_jax[0, 0], rtol=0.001)

@pytest.mark.jax
def test_jit_result(self):
willjmax marked this conversation as resolved.
Show resolved Hide resolved
"""Test that the value returned in JIT mode equals the value returned without JIT."""
import jax

def fable(input_matrix):
qml.FABLE(input_matrix, wires=range(5), tol=0)
return qml.expval(qml.PauliZ(wires=0))

input_matrix = np.array(
[
[-0.5, -0.4, 0.6, 0.7],
[0.9, 0.9, 0.8, 0.9],
[0.8, 0.7, 0.9, 0.8],
[0.9, 0.7, 0.8, 0.3],
]
)

device = qml.device("default.qubit", wires=5)
interpreted_fn = qml.QNode(fable, device)
jitted_fn = jax.jit(interpreted_fn)
assert np.allclose(jitted_fn(input_matrix), interpreted_fn(input_matrix))

@pytest.mark.jax
def test_fable_grad_jax_jit_error(self, input_matrix):
"""Test that FABLE is differentiable when using jax."""
Expand Down
Loading