[BUG] qp.Barrier is dropped under qjit, so merge_rotations and cancel_inverses optimize across it
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 43/100
Research direction
Start with get_qjit_device_capabilities in frontend/catalyst/device/qjit_device.py and catalyst_decompose in frontend/catalyst/device/decomposition.py to trace why Barrier is removed. Then inspect how the quantum dialect and optimization passes represent operations; the issue notes there is no barrier op and that runtime lowering cannot link a custom Barrier. Done means barriers preserve optimization boundaries under qjit and are removed before runtime lowering, while only_visual barriers may still be dropped.
Written by the indexing model from the issue text.
Description
Issue description
Let's assume we have the following circuit:
def with_barrier() -> qml.measurements.ExpectationMP:
"""Apply two rotations that the barrier should keep apart."""
qml.RX(0.5, wires=0)
qml.Barrier(wires=0)
qml.RX(0.5, wires=0)
return qml.expval(qml.PauliZ(0))
and we compile the circuit with merge_rotations.
-
Expected behavior: RX gates are not merged and stay separated with a
Barrierop/gate in-between. -
Actual behavior: The traced MLIR merges the RX gates into a single
RX(1.0)and the barrier is ignored.
The same holds true for cancel_inverses. I have tried using a custom device that lists Barrier, however it is still dropped. Patching the catalyst internal gate list to contain Barrier with said custom device compiles as expected, with no merged gates.
-
Reproduces how often: Always reproduces
-
System information:
Name: pennylane
Version: 0.46.0.dev107
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page:
Author:
License:
Location: /Users/filip/src/catalyst/.venv/lib/python3.12/site-packages
Platform info: macOS-15.1-arm64-arm-64bit
Python version: 3.12.14
Numpy version: 2.5.3
Scipy version: 1.18.1
JAX version: 0.7.1
Catalyst version: 0.16.0.dev109
Installed devices:
- lightning.qubit (pennylane_lightning-0.46.0.dev27)
- lightning.kokkos (pennylane_lightning_kokkos-0.46.0.dev27)
- default.clifford (pennylane-0.46.0.dev107)
- default.mixed (pennylane-0.46.0.dev107)
- default.qubit (pennylane-0.46.0.dev107)
- default.tensor (pennylane-0.46.0.dev107)
- null.qubit (pennylane-0.46.0.dev107)
- reference.qubit (pennylane-0.46.0.dev107)
- braket.aws.ahs (amazon-braket-pennylane-plugin-1.35.2)
- braket.aws.qubit (amazon-braket-pennylane-plugin-1.35.2)
- braket.local.ahs (amazon-braket-pennylane-plugin-1.35.2)
- braket.local.qubit (amazon-braket-pennylane-plugin-1.35.2)
- oqc.cloud (pennylane_catalyst-0.16.0.dev109)
- qiskit.aer (PennyLane-qiskit-0.45.0)
- qiskit.basicaer (PennyLane-qiskit-0.45.0)
- qiskit.basicsim (PennyLane-qiskit-0.45.0)
- qiskit.remote (PennyLane-qiskit-0.45.0)
Source code and tracebacks
import pennylane as qp
from catalyst import qjit
from catalyst.debug import get_compilation_stage
dev = qp.device("lightning.qubit", wires=1)
def circuit():
qp.RX(0.5, wires=0)
qp.Barrier(wires=0)
qp.RX(0.5, wires=0)
return qp.expval(qp.PauliZ(0))
# Without qjit, PennyLane's merge_rotations stops at the barrier.
pl = qp.transforms.merge_rotations(qp.QNode(circuit, dev))
print(qp.workflow.construct_tape(pl)().operations)
# With qjit, the barrier is already missing from the traced program...
jitted = qjit(qp.transforms.merge_rotations(qp.QNode(circuit, dev)), keep_intermediate=True)
print("\n".join(line for line in jitted.mlir.splitlines() if "quantum.custom" in line))
# ...so merge-rotations merges across it.
jitted()
after = get_compilation_stage(jitted, "QuantumCompilationStage")
print("\n".join(line for line in after.splitlines()
if "quantum.custom" in line or ("arith.constant" in line and "f64" in line)))
Output:
[RX(0.5, wires=[0]), Barrier(wires=[0]), RX(0.5, wires=[0])]
%out_qubits = quantum.custom "RX"(%cst) %1 : !quantum.bit
%out_qubits_0 = quantum.custom "RX"(%cst) %out_qubits : !quantum.bit
%cst = arith.constant 1.000000e+00 : f64
%out_qubits = quantum.custom "RX"(%cst) %1 : !quantum.bit
The same happens with cancel_inverses on H, Barrier, H: without qjit all three ops stay. Under qjit no gate is left after QuantumCompilationStage.
Additional information
Where the barrier is lost:
get_qjit_device_capabilitiesintersects the device's operations withRUNTIME_OPERATIONS
(qjit_device.py#L63-L101, intersected at L232), which does not containBarrier. This also holds for a device whose capability file listsBarrier: alightning.qubitsubclass withBarrieradded to its TOML still drops it.catalyst_decompose(decomposition.py#L176) therefore decomposes it throughcatalyst_decomposer(L109), andBarrier.decomposition()is[].- The
quantumdialect has no barrier op it could become.
For reference: with Barrier added to RUNTIME_OPERATIONS (monkeypatched), it appears as
quantum.custom "Barrier"() %q0, %q1, and merge-rotations keeps both rotations, since the
second RX consumes the barrier's result. Running the program then fails to link, because the
runtime has no __catalyst__qis__Barrier. So a fix probably needs a barrier op that passes treat
as a fence and that is removed before the runtime lowering. Barriers with only_visual=True can
keep being dropped.
@rmoyard and I hit this when compiling OpenQASM circuits with qjit: pennylane-qiskit converts barrier to
qp.Barrier, so the barriers a circuit author placed are ignored by the optimization passes.
- Dominant language
- Python
- Stars
- 234
- Forks
- 84
- Avg merge
- 3d 11h
- Merged PRs (30d)
- 78
Getting set up
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
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
PennyLaneAI/catalyst#3489 ·
Maintainers usually reply within 1 day
-
Fix platform tag for macOS wheelsMay be free again @sarehaskari17 claimed this 107 days ago, and no pull request is open. OpenCI/Build enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
PennyLaneAI/catalyst#2907 · 2 comments ·
Maintainers usually reply within 1 day
-
The docs page for `CompileOptions` is missing a description for the `target` attributePossibly taken @daxsh2803 claimed this 68 days ago. Opendocumentation
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
PennyLaneAI/catalyst#2653 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 68/100
PennyLaneAI/catalyst#3550 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 57/100
PennyLaneAI/catalyst#3497 ·
Maintainers usually reply within 1 day
All issues in PennyLaneAI/catalyst
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
521xueweihan/HelloGitHub#4729 ·
-
auto-reported bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
needs-triage type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
pollen-robotics/reachy_mini_conversation_app#631 ·
Maintainers usually reply within 7 days
-
stix-atlas.json: the same relationship id is used for several different mitigates relationshipsOpen
Difficulty 2/5 1-3 hours Newbie friendliness 76/100