Eliminate rz
EliminateRz: remove Rz from native-dialect programs by phase commutation.
Scans a flat block in order carrying a per-qubit phase frame. Each Rz is
absorbed into the frame and deleted; each R has its axis angle shifted by the
frame of the qubits it addresses; CZ and StarRz are diagonal and pass
through untouched. Whatever frame remains when the block ends is discarded --
sound because the residual is diagonal and the device's readout is in the Z
basis. flush_residual writes it back out instead, which makes the emitted
program exactly equal the input; see that field's docstring for why a test
wants it.
Two exact identities do all the work (angles in turns)::
R(phi, theta) . Rz(alpha) = Rz(alpha) . R(phi - alpha, theta)
CZ . Rz(alpha) = Rz(alpha) . CZ
The frame holds SSA values, not floats: a phase is whatever
rotation_angle operand the Rz carried, and a shifted axis is emitted as
py.Sub over those operands. That keeps the rule working on kernels whose
angles are function arguments rather than literals -- the pipeline already
carries non-constant angles end to end (RewriteStarRz feeds a
func.Invoke result straight into move.LocalRz). When both operands do
happen to be constants the arithmetic is folded to a literal, since nothing runs
constant folding after this rule and an unfolded py.Sub would otherwise
propagate all the way into the emitted program. Duplicate angle values are left
for the CommonSubexpressionElimination already in NativeToPlaceBase.emit
to merge -- both py.Constant and py.Sub are Pure, so it does, and a
memo here would only re-implement it while growing with circuit depth.
Dispatch is exhaustive: every statement type reachable in this window has a
registered handler, and the default raises. There is deliberately no "does this
statement touch a qubit?" heuristic -- an unrecognised statement is a statement
whose phase behaviour we cannot know, so it is an error rather than a guess.
Registering the unknown type as a no-op is not an available fix either:
func.Invoke may contain gates, so a handler claiming it is phase-neutral
would be false, and no handler can be written that is true.
no_raise selects what happens when the scan meets something it cannot
reason about. Under the default (False) it raises. Under True it
drains instead: the pending frame is re-emitted as real Rz statements
immediately before the offending statement, which is exact rather than merely
conservative. The rule moves each Rz later in the circuit, so a frame of
alpha pending on a qubit says precisely that the emitted program so far,
followed by Rz(alpha), equals the original program so far -- re-emitting it
restores the original exactly, and leaves the frame empty, which is the state
the scan starts in. The cost is that such a program reaches the backend with
Rz still in it and fails at the device rather than at compile time, which
is what best-effort mode buys everywhere else in this pipeline.
See docs/superpowers/specs/2026-09-14-rz-elimination-design.md.
EliminateRz
dataclass
EliminateRz(
no_raise: bool = False, flush_residual: bool = False
)
Bases: RewriteRule
flowchart TD
bloqade.lanes.rewrite.eliminate_rz.EliminateRz[EliminateRz]
click bloqade.lanes.rewrite.eliminate_rz.EliminateRz href "" "bloqade.lanes.rewrite.eliminate_rz.EliminateRz"
Remove every Rz from a flat native-dialect program.
Preconditions, both validated by NativeToPlaceBase.emit
immediately before this rule and neither re-checked here:
- A single-block callable region with no nested
func.Function(lanes.flat_block.validation). The frame is accumulated in the orderWalkoffers statements, which matches execution order within a block and nowhere else, so violating this yields a different circuit rather than an error. - Every
qubitsoperand is anilist.Newof distinct qubits (lanes.qubit_register.validation). That is why the handlers below readstmt.qubits.owneras anilist.Newwithout checking.
A caller driving this rule directly is responsible for both.
One Rz is deliberately left behind: the one the STAR gadget
introduces. It acts on a subset of a block's physical qubits, which this
rule's per-logical-qubit frame does not model, so it passes through and
reaches the backend as a physical local Rz. Removing it would need a
physical-domain pass, which is out of scope here.
That is stated here rather than raised as a warning at compile time: the
condition is static -- a StarRz is present in the program the caller
wrote -- and there is no action the caller could take in response, which
is usually the sign it belongs in documentation.
flush_residual
class-attribute
instance-attribute
flush_residual: bool = False
Write the leftover frame back out as terminal Rz rather than dropping it.
Off in production: the whole point of the pass is that the residual is
diagonal and the readout is in the Z basis, so emitting it would put back
an Rz the backend cannot execute for no gain.
On, the emitted program is exactly the input program -- every phase this
pass commuted forward is restored at the end of the wire it belongs to.
That makes the commutation itself checkable against an exact-state
reference, which discarding does not: a discarded residual is
indistinguishable from a sign error in the commutation, since both show up
only as a Z-type difference that a Z-basis measurement cannot see. See
test_logical_clifford_lowering.py, which runs the pass this way so its
signed stabilizer checks cover the real rule.
no_raise
class-attribute
instance-attribute
no_raise: bool = False
Give up gracefully instead of raising when a precondition fails.
Mirrors the flag the surrounding pipeline threads through its passes. See the module docstring for why giving up means draining the frame rather than skipping the statement: skipping would leave the program half commuted, valid, and silently wrong.
EliminateRzError
Bases: ValidationError
flowchart TD
bloqade.lanes.rewrite.eliminate_rz.EliminateRzError[EliminateRzError]
click bloqade.lanes.rewrite.eliminate_rz.EliminateRzError href "" "bloqade.lanes.rewrite.eliminate_rz.EliminateRzError"
The IR violates a precondition of the Rz elimination scan.
A kirin ValidationError rather than a bare Exception so it
carries the offending node and its source location, and renders with the
same caret hint as every other check reachable from
NativeToPlaceBase.emit. Construct it as
EliminateRzError(node, "message").