Skip to content

[BUG] QubitUnitary decomposition fails with qjit for the 2 CNOT case #9016

@dime10

Description

@dime10

Originally reported in the forum (https://discuss.pennylane.ai/t/error-when-using-catalyst-grad-with-blockencode/9234), the following code will fail in the latest release:

import pennylane as qml
import catalyst
from jax import numpy as jnp

A = jnp.array([[1, 0], [0, 1]])

dev = qml.device("lightning.qubit", wires=2)
@qml.qnode(dev)
def loss_fn(angle):
    qml.BlockEncode(A, wires=[0, 1])
    qml.RZ(angle, wires=0)
    return qml.expval(qml.Z(0))

angle = jnp.array(0.5)

grad_fn = catalyst.qjit(catalyst.grad(loss_fn))
grads = grad_fn(angle)
The numpy.ndarray conversion method __array__() was called on traced array with shape float64[4]

The issue is improper detection of the abstract array case for U with the current Catalyst frontend, located here:

# The 2-CNOT decomposition relies on sorting eigenvalues, which is not supported
# with abstract tracers when capture is enabled. In that case, we fall back
# to the 3-CNOT decomposition.
if not capture.enabled():

I believe the bug was introduced in PR #8717

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions