`gridsynth` fails on controlled `RZ` inside uncalled decomposition-rule functions left behind by graph decomposition

Aperta
#3,386 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend, compilers

Direzione di ricerca

Start by reading mlir/lib/Quantum/Transforms/gridsynth.cpp and the graph-decomposition pass that leaves the unused rule functions in the module. Use the reproducer with qp.decompose followed by qp.transforms.gridsynth to confirm the failure, then inspect whether the pre-check and rewrite process unreachable functions. Done means the reproducer compiles without rejecting controlled rotations in uncalled rules.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description

When qp.transforms.gridsynth runs after qp.decompose with RZ in the gate set, it can fail with

error: Unsupported controlled gate for gridsynth. Only single-qubit RZ and PhaseShift gates can be discretized.

even though the decomposed circuit contains no controlled RZ/PhaseShift.

The offending ops are all inside decomposition-rule functions that are not called. These are the distributed control rule templates, for example __builtin__rx_to_rz_ry_2C(RX){0:[f64]}{wires:1}{}. The graph-decomposition pass puts them in the module, the solver doesn't choose them (or only calls numbered copies of chosen rules), and they stay there until a later dead-symbol cleanup. If GridsynthPass runs before that cleanup, its pre-check walks the whole module:

// mlir/lib/Quantum/Transforms/gridsynth.cpp
WalkResult controlledRotation = module->walk([](CustomOp op) {
    StringRef gateName = op.getGateName();
    if ((gateName == "RZ" || gateName == "PhaseShift") && !op.getInCtrlQubits().empty()) {
        op.emitError("Unsupported controlled gate for gridsynth. ...");
        return WalkResult::interrupt();
    }
    ...

So it rejects the dead code in the mentioned decomposition rule(s). Running symbol-dce between the two transforms fixes the failure.

Minimal reproducer
import pennylane as qp

gate_set = {"RZ", "Hadamard", "S", "CNOT", "GlobalPhase"}

@qp.qjit(capture=True)
@qp.transforms.gridsynth(epsilon=1e-3)
# @qp.transform(pass_name="symbol-dce")  # uncommenting this line makes it compile
@qp.decompose(gate_set=gate_set)
@qp.qnode(qp.device("null.qubit", wires=3))
def circuit(x):
    qp.ctrl(qp.RX(x, wires=0), control=[1, 2])
    return qp.expval(qp.Z(0))

circuit(0.3)

(Two control wires are needed so that the op stays a generic C(RX) instead of becoming CRX. Then the distributed control rule templates (*_2C(...)) are generated.)

Actual behaviour
circuit:222:9: error: Unsupported controlled gate for gridsynth. Only single-qubit RZ and PhaseShift gates can be discretized.
circuit:222:9: note: see current operation: %15:3 = "quantum.custom"(%1, %12, %13, %14, %2, %2) <{gate_name = "RZ", operandSegmentSizes = array<i32: 1, 1, 2, 2>, resultSegmentSizes = array<i32: 1, 2>}> : (f64, !quantum.bit, !quantum.bit, !quantum.bit, i1, i1) -> (!quantum.bit, !quantum.bit, !quantum.bit)
circuit:11:14: error: pass pipeline failed

In the IR dumped at the failure (keep_intermediate=True), all 26 functions that contain a controlled RZ/PhaseShift have zero callers. Some of them:

__builtin__rx_to_rz_ry_2C(RX){0:[f64]}{wires:1}{}
__builtin__rz_to_ps_2C(RZ){0:[f64]}{wires:1}{}
__builtin__phaseshift_to_rz_gp_2C(PhaseShift){0:[f64]}{wires:1}{}
__builtin__hadamard_to_rz_ry_2C(Hadamard){}{wires:1}{}
__builtin_adjoint_rotation_2C(Adjoint(RZ)){0:[f64]}{wires:1}{}
...

The functions the circuit actually calls (e.g. __builtin_flip_zero_ctrl_values(_controlled_rx_decomp)_2C(RX)..._0, __builtin__toffoli_Toffoli..._6) contain no controlled rotations.

Expected behaviour

The circuit compiles. Gridsynth should only care about ops that can be reached from the circuit, or graph decomposition shouldn't leave unused rule functions in the module. The former seems like the more local change, but maybe we want to have the graph decomposition clean up after itself more generally?

Possible fixes
  • Have graph-decomposition remove unused rule functions (e.g. run symbol-dce) before it returns.
  • And/or limit the GridsynthPass pre-check (and the rewrite) to functions reachable from the entry point(s).
Impact

This affects any pipeline of the form decompose(gate_set ⊇ {"RZ"}) → gridsynth on circuits with generic controlled ops.

Workaround
@qp.transforms.gridsynth(...)
@qp.transform(pass_name="symbol-dce")
@qp.decompose(gate_set=...)
Environment
  • PennyLane main @ 059c432d94 (0.46.0-dev134)
  • Catalyst main just now (22 Oct, 8am EST)
  • macOS (arm64)
Lingua principale
Python
Stelle
234
Fork
84
Merge medio
3g 11h
PR unite (30g)
78

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PennyLaneAI/catalyst

Tutte le issue di PennyLaneAI/catalyst

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.