Skip to content

Measure lower

measure_lower — validate + rewrite move.Measure to move.EndMeasure.

MeasureLower dataclass

MeasureLower(frame: ForwardFrame[MoveExecution])

Bases: RewriteRule


              flowchart TD
              bloqade.lanes.rewrite.measure_lower.MeasureLower[MeasureLower]

              

              click bloqade.lanes.rewrite.measure_lower.MeasureLower href "" "bloqade.lanes.rewrite.measure_lower.MeasureLower"
            

Lower move.Measure stmts to move.EndMeasure.

Driven by an AtomAnalysis frame: each move.Measure's future SSA result is looked up in the frame, and the associated MeasureFuture lattice element supplies both the measurement ordinal (measurement_count) and the set of zones actually observed (the keys of results, whose iteration order mirrors insertion order and therefore the original zone order).

The rewrite gives up silently (returns RewriteResult() with has_done_something=False) when:

  1. The frame has no MeasureFuture for the node's future SSA.
  2. measurement_count on that future != 1 — the move.Measure is not the first/only final measurement in the program.
  3. len(results) on that future != 1 — the measurement spans multiple zones.

Validating these invariants as hard errors is the job of dedicated validation passes that run between dialect transformations; rewrite rules just attempt the rewrite and back off when preconditions aren't met.

from_method classmethod

from_method(method: Method, arch_spec) -> 'MeasureLower'

Build a MeasureLower by running AtomAnalysis on the given method.

Stashes the returned ForwardFrame so that rewrite_Statement can look up each move.Measure's future to get both the measurement ordinal and zone set from the MeasureFuture lattice element.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/rewrite/measure_lower.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@classmethod
def from_method(cls, method: ir.Method, arch_spec) -> "MeasureLower":
    """Build a MeasureLower by running AtomAnalysis on the given method.

    Stashes the returned ForwardFrame so that ``rewrite_Statement``
    can look up each ``move.Measure``'s future to get both the
    measurement ordinal and zone set from the ``MeasureFuture``
    lattice element.
    """
    from bloqade.lanes.analysis.atom import AtomInterpreter

    interp = AtomInterpreter(method.dialects, arch_spec=arch_spec)
    frame, _ = interp.run(method)
    return cls(frame=frame)