# MLIR dialects and passes

This reference describes the MQT Compiler Collection’s MLIR infrastructure. For
compilation, simulation, and device execution, start with
[compilation and execution](../compilation/index.html.md). Compiler contributions
follow the [development policy](../development.html.md#mlir).

A dialect defines related operations and types within MLIR. MQT Core uses
several dialects so that importing a circuit, optimizing it, and managing its
classical data can each use a suitable representation:

- The [MQT dialect](MQT.html.md) stores frontend-neutral program metadata that
  remains meaningful across dialect conversions.
- The [QC dialect](QC.html.md) uses reference semantics and is designed as a
  compatibility dialect that simplifies translations from and to existing
  representations such as Qiskit circuits, OpenQASM, or QIR.
- The [QCO dialect](QCO.html.md) uses value semantics and is mainly designed for
  running optimizations.
- The [QTensor dialect](QTensor.html.md) adds support for one-dimensional tensors
  of qubits with linear semantics and is used in the QCO dialect to represent
  collections of qubits such as registers.
- The [CBit dialect](CBit.html.md) represents initialized classical-bit registers
  shared by QC and QCO.

These dialects define various canonicalization patterns and transformation
passes that enable the compilation of quantum programs to native quantum
hardware. Passes that are not tied to a single dialect are documented on the
[passes](Transforms.html.md) page. For interoperability, we provide
[conversions](Conversions.html.md) between dialects.

QC and QCO entry functions return classical program data and cannot return
qubits or quantum registers. Helper functions may pass and return quantum state.
Programs without results use a void entry function; a returned integer zero is
still data. The C++ program builders infer the entry signature in `finalize()`
from its return values. Scalar results survive OpenQASM, QC, QCO, and jeff
interchange. Each exporter defines its supported result types;
[QIR](../qir/index.html.md) accepts direct measurements and CBit registers and
creates its runtime exit status during conversion.

<a id="quantum-resource-contract"></a>

## Quantum resource rules

The compiler must track which qubit each operation acts on, even as its state
changes. QC, QCO, and QTensor use different representations for this
information, but share the rules below so that conversion between them preserves
behavior. The [register tutorial](../tutorials/control_flow.html.md) introduces
these rules with an executable example.

Quantum SSA representations have prior examples such as
[QSSA](https://arxiv.org/abs/2109.02409). The rules below specify Core’s
representation: exactly-one-use and positional wire correspondence are distinct
requirements. `WireOpInterface` pairs the first quantum input with the first
quantum output, and so on; use counts alone do not establish that relation.

When writing IR or a transformation, observe these requirements:

- Quantum operands of a gate or helper call denote disjoint resources. A
  register and one of its elements cannot be separate operands of the same call.
  Multiple references to a qubit are allowed when used sequentially.
- Each register slot retains the same qubit identity throughout its lifetime.
  `qtensor.extract` borrows that qubit; `qtensor.insert` restores it to the same
  slot. Gates change quantum states, including `swap`, which exchanges states
  without changing the identities stored in the register. QC register stores may
  initialize a physical reference buffer or write back the same reference; they
  cannot move, replace, or copy qubits between slots.
- A register passed to a helper, carried through a structured region boundary,
  returned, or dynamically deallocated must be complete: every extracted qubit
  has been restored. A scalar helper may use an extracted qubit while the
  remaining tensor stays in its caller.
- Quantum results of helpers and structured control flow correspond to quantum
  inputs in the same order. Updated quantum arguments form a suffix after the
  ordinary function results. Quantum values in QCO have exactly one use.
  `qco::verifyLinearity` checks both use counts and positional correspondence
  after ordinary MLIR verification. Public QCO program APIs run this check;
  parsing MLIR alone does not establish it.

Indices must be in bounds. Distinct runtime indices used together must select
non-overlapping resources; extraction and reinsertion indices must select the
same slot. Bounds and non-overlap are program preconditions. QCO-to-QC proves
restoration from identical SSA values, equal constants, or arithmetic equality
supported by MLIR value-bounds analysis; unproved restoration fails before
rewriting. Other conversions diagnose known violations and unsupported IR
shapes, but do not prove arbitrary index relationships or insert runtime checks.
Type checking and use counts alone do not establish these index relationships.

Slot identity refers to the underlying register and index. A view or slice may
renumber those indices; it does not change the qubits they denote. Overlapping
views may be used sequentially, but their overlapping resources cannot be
separate quantum operands of one operation. Each conversion documents which view
representations it accepts.

For example, this helper borrows one qubit. The caller restores it before
passing the complete register to the next loop iteration:

```mlir
func.func private @flip(%q: !qco.qubit) -> !qco.qubit {
  %out = qco.x %q : !qco.qubit
  return %out : !qco.qubit
}
func.func @main() attributes {mqt.entry_point} {
  %zero = arith.constant 0 : index
  %one = arith.constant 1 : index
  %two = arith.constant 2 : index
  %reg = qtensor.alloc(%two) : tensor<2x!qco.qubit>
  %out = scf.for %i = %zero to %two step %one
      iter_args(%current = %reg) -> (tensor<2x!qco.qubit>) {
    %rest, %q = qtensor.extract %current[%i] : tensor<2x!qco.qubit>
    %flipped = func.call @flip(%q) : (!qco.qubit) -> !qco.qubit
    %full = qtensor.insert %flipped into %rest[%i] : tensor<2x!qco.qubit>
    scf.yield %full : tensor<2x!qco.qubit>
  }
  qtensor.dealloc %out : tensor<2x!qco.qubit>
  return
}
```

Calling a whole-register helper with `%rest` before reinsertion is invalid: that
argument still has an extracted slot. Yielding `%rest` and `%flipped` as
separate loop-carried values also violates these rules. Keep `%rest` in the
caller while a scalar helper processes `%q`, then restore `%full` before a
whole-register call or loop yield.

QC represents the same loop with `memref.load` and an in-place scalar helper; no
write-back is needed. A loaded QC reference remains valid across later gates,
helper calls, and identity stores because its register slot is stable. Round
trips preserve program behavior, not the number or placement of loads.

* [‘mqt’ Dialect](MQT.html.md)
  * [Attributes](MQT.html.md#attributes)
  * [Enums](MQT.html.md#enums)
  * [C++ parameter helpers](MQT.html.md#c-parameter-helpers)
  * [Compilation seed](MQT.html.md#compilation-seed)
  * [Staged target compilation](MQT.html.md#staged-target-compilation)
  * [Passes](MQT.html.md#passes)
* [‘qc’ Dialect](QC.html.md)
  * [Operations](QC.html.md#operations)
  * [Types](QC.html.md#types)
  * [OpInterface definitions](QC.html.md#opinterface-definitions)
  * [Passes](QC.html.md#passes)
* [‘qco’ Dialect](QCO.html.md)
  * [Operations](QCO.html.md#operations)
  * [Types](QCO.html.md#types)
  * [OpInterface definitions](QCO.html.md#opinterface-definitions)
  * [Passes](QCO.html.md#passes)
* [‘qtensor’ Dialect](QTensor.html.md)
  * [Operations](QTensor.html.md#operations)
  * [Passes](QTensor.html.md#passes)
* [‘cbit’ Dialect](CBit.html.md)
  * [Operations](CBit.html.md#operations)
  * [Attributes](CBit.html.md#id6)
  * [Types](CBit.html.md#types)
  * [Enums](CBit.html.md#enums)
  * [Memory lowering](CBit.html.md#memory-lowering)
* [Passes](Transforms.html.md)
  * [Shared Passes](Transforms.html.md#shared-passes)
  * [QIR Passes](Transforms.html.md#qir-passes)
* [Conversions](Conversions.html.md)
  * [Classical Register Conversion](Conversions.html.md#classical-register-conversion)
  * [Internal Conversions](Conversions.html.md#internal-conversions)
  * [`jeff` Conversions](Conversions.html.md#jeff-conversions)
  * [QIR Conversions](Conversions.html.md#qir-conversions)
