Skip to content

🔁 MLIR - Gate modifiers framework #1129

@burgholzer

Description

@burgholzer

Background

OpenQASM 3 introduces gate modifiers to concisely express transformations on quantum gates/blocks (see https://openqasm.com/versions/3.0/language/gates.html#quantum-gate-modifiers). Common modifiers include inv (adjoint), pow(r), and ctrl/negctrl.

In our stack today:

  • Controls: handled lazily via the unitary operation interface in both MQT Core IR and MLIR. No separate modifier op required right now.
  • Inversion: supported eagerly in MQT Core IR (see Operation::invert / Operation::get_inverted), but not yet modeled in MLIR and not available in a lazy evaluation scheme
  • Power: not supported in either MQT Core IR or MLIR, though the OQ3 parser recognizes the syntax. Support for adding that to the MQT Core IR is tracked in ✨ Support for the powering modifier of OpenQASM 3.0 #27

MLIR concept of “modifiers”

A quick LLM summary about the general concept:
In MLIR, gate modifiers can be represented using one or more of the following approaches:

  • Op attributes on the modified op (e.g., mqtref.gate {inv} or {pow = r}) with canonicalizations that eliminate attributes by rewriting to equivalent ops.
  • Wrapper ops that own a region containing the target op(s) (e.g., mqtref.inv { ... }), enabling region-level transforms like reversing order and inverting contained ops.
  • Function- or region-level modifiers for block-wise semantics (e.g., to invert a block of unitary ops), verified via traits (e.g., "unitary-only region").

We’ll introduce a single, consistent framework that:

  • Defines a common modifier abstraction.
  • Verifies legality via traits/interfaces (e.g., only unitary ops permit inv/pow).
  • Provides canonicalizations to eliminate modifiers when possible (e.g., inv(Rz(θ)) -> Rz(-θ)).

Problem

We lack a unified representation for gate modifiers in MLIR; inversion isn’t modeled yet, and pow is missing entirely. We need IR constructs, verifiers, and canonicalizations consistent with frontend semantics and downstream lowerings.

Proposal:

  • Introduce modifier attributes and wrapper ops in MQTRef:
    • Attributes: inv: unit, pow: <integer|float>, reserved control (deferred).
    • Wrapper ops: mqtref.inv (region-level), optional mqtref.power if region application is needed.
  • Define traits/interfaces that mark ops and regions as unitary-only and thus eligible for modifiers.
  • Add canonicalizations and legality checks (e.g., prohibit pow on non-unitary ops, allow inv only on unitary sequences).
  • Provide lowering guidance for inv and pow (see sub-issues below): eager rewrites where possible; otherwise preserve modifier until a later pass.

Examples:

  • Attribute form on a single op
    // Conceptual attribute usage (to be legalized by canonicalizations)
    %q = ... : !mqtref.qubit
    mqtref.rz %q, %theta {inv} : (!mqtref.qubit, f64) -> ()
  • Region form for inverse of a block
    mqtref.inv {
      // quantum-only region
      mqtref.h %q : !mqtref.qubit
      mqtref.cz %q0, %q1 : !mqtref.qubit, !mqtref.qubit
    }
    // Canonicalization: reverse order and invert each op

Acceptance criteria:

  • Common modifier abstraction exists (attributes + region wrapper), with verifiers and traits.
  • Canonicalizations for standard gates (e.g., adjoint of rotations, power-of-phase simplifications) are defined at framework level for reuse.
  • Documentation added to docs/mlir.md covering the modifier model.

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    MLIRAnything related to MLIRc++Anything related to C++ codefeatureNew feature or request

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions