Skip to content

Analysis

CountStatementAnalysis dataclass

CountStatementAnalysis(
    predicate: Callable[
        [Statement], tuple[CounterKey, int] | None
    ],
    counter: Counter[CounterKey] = Counter(),
)

Bases: Forward[EmptyLattice], Generic[CounterKey]


              flowchart TD
              bloqade.analysis.count_statements.analysis.CountStatementAnalysis[CountStatementAnalysis]

              

              click bloqade.analysis.count_statements.analysis.CountStatementAnalysis href "" "bloqade.analysis.count_statements.analysis.CountStatementAnalysis"
            

Accumulate weighted counts for leaf statements in reachable IR.

The analysis enters resolvable callees of func.Invoke and func.Call once per call site. It also enters the callable of each supported higher-order ilist operation (Map, ForEach, Foldl, Foldr, and Scan) once per operation. Indirect calls whose callable cannot be resolved are skipped.

Control flow is visited as written in the IR, not as executed:

  • scf.For — the loop body is counted once, regardless of trip count.
  • scf.IfElse — both the then and else branches are counted.

A leaf statement is one handled through interpreter fallback rather than a dedicated traversal implementation. For each leaf, predicate returns either None or a (key, increment) pair. Each pair adds increment to counter[key]. The key is caller-defined, so multiple statements or statement types can contribute to the same bucket. Dedicated call, control-flow, and ilist traversal operations are not themselves passed to predicate.

Unused nested callables are not visited. Calling run resets the counter.

Usage examples

from bloqade import squin
from bloqade.analysis.count_statements import CountStatementAnalysis

@squin.kernel
def main():
    q = squin.qalloc(2)
    squin.broadcast.x(q)

def count_x(stmt):
    if isinstance(stmt, squin.gate.stmts.X):
        return "x", 1
    return None

analysis = CountStatementAnalysis(main.dialects, count_x)
analysis.run(main)
analysis.counter  # Counter({"x": 1})

counter class-attribute instance-attribute

counter: Counter[CounterKey] = field(
    default_factory=Counter
)

Weighted totals grouped by the keys returned from predicate.

predicate instance-attribute

predicate: Callable[
    [Statement], tuple[CounterKey, int] | None
]

Map a statement to a counter key and increment, or return None.

count_statement

count_statement(node: Statement) -> None

Add the contribution returned by predicate for node.

Source code in .venv/lib/python3.12/site-packages/bloqade/analysis/count_statements/analysis.py
80
81
82
83
84
85
def count_statement(self, node: Statement) -> None:
    """Add the contribution returned by ``predicate`` for ``node``."""
    result = self.predicate(node)
    if result is not None:
        key, increment = result
        self.counter[key] += increment

eval_fallback

eval_fallback(
    frame: ForwardFrame[EmptyLattice], node: Statement
) -> None

Count a statement without a dedicated traversal implementation.

Source code in .venv/lib/python3.12/site-packages/bloqade/analysis/count_statements/analysis.py
76
77
78
def eval_fallback(self, frame: ForwardFrame[EmptyLattice], node: Statement) -> None:
    """Count a statement without a dedicated traversal implementation."""
    self.count_statement(node)

initialize

initialize() -> Self

Reset interpreter state and discard counts from previous runs.

Source code in .venv/lib/python3.12/site-packages/bloqade/analysis/count_statements/analysis.py
70
71
72
73
74
def initialize(self) -> Self:
    """Reset interpreter state and discard counts from previous runs."""
    super().initialize()
    self.counter = Counter()
    return self

method_self

method_self(method: Method) -> EmptyLattice

Represent a method's implicit self value with lattice bottom.

Source code in .venv/lib/python3.12/site-packages/bloqade/analysis/count_statements/analysis.py
87
88
89
def method_self(self, method: Method) -> EmptyLattice:
    """Represent a method's implicit ``self`` value with lattice bottom."""
    return EmptyLattice.bottom()