Rule lowering depends on operator order in circuit

Open
#3,431 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@kipawaa is already working on this.

Since Nov 4, 2026.

Assessment

This issue has not been assessed yet.

Description

Observation
With the graph_decomposition decomposer, whether a circuit compiles can depend on the operator ordering in the circuit.
Less severely, but still problematically, the decomposition result can have reduced quality depending on the operator ordering.

Cause
An operator (node) that is pruned due to a control wire increasing cycle is cached as invalid, so that later visits to that node will not explore it again, even though in a different context, i.e. not as part of such a problematic cycle.
This caching "side effect" means that if the valid usage of the operator (node) comes first, we're fine, but if it follows the invalid usage as part of a cycle, the decomposition may fail (or produce worse outcomes if this causes worse decompositions to be chosen)

The PL graph decomposer has the same problem: https://github.com/PennyLaneAI/pennylane/issues/10424, which is fixed in https://github.com/PennyLaneAI/pennylane/pull/10423.

Minimal example

Only providing resources, no rule bodies because this is about graph construction.

import pennylane as qp
from pennylane.decomposition import add_decomps, local_decomps, register_resources
from pennylane.typing import Wire

from catalyst import qjit
from catalyst.passes import graph_decomposition


class B(qp.core.Operator2):
    def __init__(self, wires):
        super().__init__(wires=wires)


class X(qp.core.Operator2):
    def __init__(self, wires):
        super().__init__(wires=wires)


class Y(qp.core.Operator2):
    def __init__(self, wires):
        super().__init__(wires=wires)


@register_resources({})
def b_to_nothing(wires):
    del wires


@register_resources({X(Wire[1]): 1})
def cb_to_x(base, **_):
    del base


@register_resources({qp.ctrl(B(Wire[1]), control=[Wire[2], Wire[3]]): 1})
def x_to_ccb(wires):
    del wires


@register_resources({X(Wire[1]): 1})
def y_to_x(wires):
    del wires


def compile_circuit(controlled_b_first):
    with local_decomps():
        add_decomps(B, b_to_nothing)
        add_decomps("C(B)", cb_to_x)
        add_decomps(X, x_to_ccb)
        add_decomps(Y, y_to_x)

        @qjit(capture=True, target="mlir")
        @graph_decomposition(gate_set={qp.Z: 1})
        @qp.qnode(qp.device("null.qubit", wires=2))
        def circuit():
            if controlled_b_first:
                qp.ctrl(B(0), control=1)
                Y(0)
            else:
                Y(0)
                qp.ctrl(B(0), control=1)
            return qp.state()

        return qp.specs(circuit, level="all")().resources


compile_circuit(controlled_b_first=False)  # compiles
compile_circuit(controlled_b_first=True)   # fails

Technical cause (AI analysis)
In walk_reachable_decomp_rule_sets (frontend/catalyst/decomposition/decomposition_rules.py), whether a child gets pruned depends on the path taken (path_control_counts). But processed_states is keyed only by (base_id, modifier state) and shared across all paths:

previous_states = processed_states.setdefault(request.base_id, set())
missing_states = demanded_states - previous_states
if not missing_states:
    continue
...
if increases_control_cycle:
    continue

With C(B) first, the walk reaches X through C(B). On that path B already has one control, so X's child 2C(B) is pruned and X is marked as processed. The visit to X through Y, where nothing would be pruned, is then skipped. 2C(B) is never given rules, and X's only rule becomes unusable.

The roots are recorded in the order their operators first appear in the circuit (DecompositionScope.roots), so reordering independent gates changes the result. The C++ solver itself doesn't depend on order; it just receives an incomplete rule set. If X had a costlier fallback rule, the result would be a worse decomposition rather than an error.

Possible fix
Following PennyLane #10423: don't treat an operator state as fully explored when one of its children was pruned. For example, record the pruned children for each (base_id, state), and on a revisit through a different path, check them again against the new path instead of skipping the operator.

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.