Skip to content

Ifs to stim

IfElseSimplification dataclass

IfElseSimplification()

contains_ifelse

contains_ifelse(stmt: IfElse) -> bool

Check if the IfElse statement contains another IfElse statement.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/ifs_to_stim.py
35
36
37
38
39
40
def contains_ifelse(self, stmt: scf.IfElse) -> bool:
    """Check if the IfElse statement contains another IfElse statement."""
    for child in stmt.walk(include_self=False):
        if isinstance(child, scf.IfElse):
            return True
    return False

has_else_body

has_else_body(stmt: IfElse) -> bool

Check if the IfElse statement has an else body.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/ifs_to_stim.py
57
58
59
60
61
62
63
64
65
def has_else_body(self, stmt: scf.IfElse) -> bool:
    """Check if the IfElse statement has an else body."""
    if stmt.else_body.blocks and not (
        len(stmt.else_body.blocks[0].stmts) == 1
        and isinstance(stmt.else_body.blocks[0].last_stmt, scf.Yield)
    ):
        return True

    return False

is_nested_ifelse

is_nested_ifelse(stmt: IfElse) -> bool

Check if the IfElse statement is nested.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/ifs_to_stim.py
45
46
47
48
49
50
51
52
53
54
55
def is_nested_ifelse(self, stmt: scf.IfElse) -> bool:
    """Check if the IfElse statement is nested."""
    if stmt.parent_stmt is not None:
        if isinstance(stmt.parent_stmt, scf.IfElse) or isinstance(
            stmt.parent_stmt.parent_stmt, scf.IfElse
        ):
            return True
        else:
            return False
    else:
        return False

IfToStim dataclass

IfToStim(measure_frame: MeasureIDFrame)

Bases: IfElseSimplification, RewriteRule


              flowchart TD
              bloqade.stim.rewrite.ifs_to_stim.IfToStim[IfToStim]
              bloqade.stim.rewrite.ifs_to_stim.IfElseSimplification[IfElseSimplification]

                              bloqade.stim.rewrite.ifs_to_stim.IfElseSimplification --> bloqade.stim.rewrite.ifs_to_stim.IfToStim
                


              click bloqade.stim.rewrite.ifs_to_stim.IfToStim href "" "bloqade.stim.rewrite.ifs_to_stim.IfToStim"
              click bloqade.stim.rewrite.ifs_to_stim.IfElseSimplification href "" "bloqade.stim.rewrite.ifs_to_stim.IfElseSimplification"
            

Rewrite if statements to stim equivalent statements.

StimSplitIfStmts dataclass

StimSplitIfStmts()

Bases: IfElseSimplification, SplitIfStmts


              flowchart TD
              bloqade.stim.rewrite.ifs_to_stim.StimSplitIfStmts[StimSplitIfStmts]
              bloqade.stim.rewrite.ifs_to_stim.IfElseSimplification[IfElseSimplification]
              bloqade.rewrite.rules.split_ifs.SplitIfStmts[SplitIfStmts]

                              bloqade.stim.rewrite.ifs_to_stim.IfElseSimplification --> bloqade.stim.rewrite.ifs_to_stim.StimSplitIfStmts
                
                bloqade.rewrite.rules.split_ifs.SplitIfStmts --> bloqade.stim.rewrite.ifs_to_stim.StimSplitIfStmts
                


              click bloqade.stim.rewrite.ifs_to_stim.StimSplitIfStmts href "" "bloqade.stim.rewrite.ifs_to_stim.StimSplitIfStmts"
              click bloqade.stim.rewrite.ifs_to_stim.IfElseSimplification href "" "bloqade.stim.rewrite.ifs_to_stim.IfElseSimplification"
              click bloqade.rewrite.rules.split_ifs.SplitIfStmts href "" "bloqade.rewrite.rules.split_ifs.SplitIfStmts"
            

Splits the then body of an if-else statement into multiple if statements

Given an IfElse with multiple valid statements in the then-body:

if measure_result

squin.x(q0) squin.y(q1)

this should be rewritten to:

if measure_result

squin.x(q0)

if measure_result

squin.y(q1)