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

Catalyst does not support QJIT-compiling a parameterized circuit with qml.ctrl #1266

Open
joeycarter opened this issue Nov 1, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@joeycarter
Copy link
Contributor

We discovered this issue when attempting to QJIT-compile a circuit implementing Grover's algorithm.

Consider the following PennyLane program that applies the qml.ctrl function to a Z gate, which results in an error only when @qjit is applied:

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

NUM_QUBITS = 2

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

@qjit
@qml.qnode(dev)
def circuit(basis_state):
    wires = list(range(NUM_QUBITS))
    qml.ctrl(qml.Z(wires[-1]), control=wires[:-1], control_values=basis_state[:-1])
    return qml.state()

basis_state = jnp.array([0.0, 0.0])
state = circuit(basis_state)
Traceback (most recent call last):
...
  File ".../venv/lib/python3.12/site-packages/pennylane/ops/op_math/controlled.py", line 144, in ctrl
    return ops_loader.ctrl(op, control, control_values=control_values, work_wires=work_wires)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.12/site-packages/catalyst/api_extensions/quantum_operators.py", line 319, in ctrl
    return res() if isinstance(f, Operator) else res
           ^^^^^
  File ".../venv/lib/python3.12/site-packages/catalyst/api_extensions/quantum_operators.py", line 532, in __call__
    return create_controlled_op(
           ^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.12/site-packages/pennylane/ops/op_math/controlled.py", line 161, in create_controlled_op
    ctrl_op = _try_wrap_in_custom_ctrl_op(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.12/site-packages/pennylane/ops/op_math/controlled.py", line 323, in _try_wrap_in_custom_ctrl_op
    if custom_key in ops_with_custom_ctrl_ops and all(control_values):
                                                  ^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.12/site-packages/jax/_src/core.py", line 712, in __bool__
    return self.aval._bool(self)
           ^^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.12/site-packages/jax/_src/core.py", line 1475, in error
    raise TracerBoolConversionError(arg)
jax.errors.TracerBoolConversionError: Attempted boolean conversion of traced array with shape float64[]..
See https://jax.readthedocs.io/en/latest/errors.html#jax.errors.TracerBoolConversionError

The issue is that the input to the circuit, basis_state, which is a traced JAX array during compilation, is being using in Python control flow, which is not allowed. Using AutoGraph does not resolve the issue.

The appropriate changes to Catalyst and/or PennyLane should be made to add support for controlled gates in QJIT-compiled circuits, where one of the input arguments to the parameterized circuit is used as input to qml.ctrl.

@joeycarter joeycarter added the bug Something isn't working label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant