[BUG] qp.Barrier is dropped under qjit, so merge_rotations and cancel_inverses optimize across it

Open
#3,298 2 comments 2 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
43/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
compilers

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

enhancement
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 Barrier op/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_capabilities intersects the device's operations with RUNTIME_OPERATIONS
    (qjit_device.py#L63-L101, intersected at L232), which does not contain Barrier. This also holds for a device whose capability file lists Barrier: a lightning.qubit subclass with Barrier added to its TOML still drops it.
  • catalyst_decompose (decomposition.py#L176) therefore decomposes it through catalyst_decomposer (L109), and Barrier.decomposition() is [].
  • The quantum dialect 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PennyLaneAI/catalyst

All issues in PennyLaneAI/catalyst

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.