Catalyst does not support QJIT-compiling a parameterized circuit with `qml.ctrl`
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Reproduce the example with qjit and qml.ctrl, then trace the failure through catalyst/api_extensions/quantum_operators.py and PennyLane's controlled.py. Determine how traced control_values reach Python control flow, and verify that the parameterized controlled circuit compiles and runs without TracerBoolConversionError.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 234
- Forks
- 84
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 66
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PennyLaneAI/catalyst
-
CI/Build enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
PennyLaneAI/catalyst#2907 · 2 comments ·
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
PennyLaneAI/catalyst#2653 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
PennyLaneAI/catalyst#3223 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
PennyLaneAI/catalyst#3217 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
PennyLaneAI/catalyst#3192 ·
All issues in PennyLaneAI/catalyst
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100