Skip to content

Classical control

ClassicalIfCondition dataclass

ClassicalIfCondition(
    measure: Measure, trigger_on_one: bool
)

Parsed measurement condition for cirq classical control emission.

classical_control_for_condition

classical_control_for_condition(
    key: str, condition: ClassicalIfCondition
) -> cirq.Condition

Build the cirq condition for condition using measurement key key.

Source code in .venv/lib/python3.12/site-packages/bloqade/cirq_utils/classical_control.py
165
166
167
168
169
170
171
172
173
174
def classical_control_for_condition(
    key: str, condition: ClassicalIfCondition
) -> cirq.Condition:
    """Build the cirq condition for ``condition`` using measurement key ``key``."""
    return cirq.BitMaskKeyCondition(
        key,
        target_value=int(condition.trigger_on_one),
        equal_target=True,
        bitmask=1,
    )

get_single_gate

get_single_gate(stmt: IfElse) -> gate.stmts.Gate | None

Return the sole gate in stmt's then-body, if present.

Source code in .venv/lib/python3.12/site-packages/bloqade/cirq_utils/classical_control.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def get_single_gate(stmt: scf.IfElse) -> gate.stmts.Gate | None:
    """Return the sole gate in ``stmt``'s then-body, if present."""
    if not stmt.then_body.blocks:
        return None

    then_stmts = list(stmt.then_body.blocks[0].stmts)
    if len(then_stmts) < 2:
        return None

    term = then_stmts[-1]
    if not isinstance(term, scf.Yield) or len(term.values) != 0:
        return None

    body_stmts = then_stmts[:-1]
    gates = [s for s in body_stmts if isinstance(s, gate.stmts.Gate)]
    if len(gates) != 1:
        return None

    allowed_non_gate = (
        ilist.New,
        py.Constant,
        py.GetItem,
        py.indexing.GetItem,
    )
    for s in body_stmts:
        if isinstance(s, gate.stmts.Gate):
            continue
        if not isinstance(s, allowed_non_gate):
            return None

    return gates[0]

is_empty_else

is_empty_else(stmt: IfElse) -> bool

Return whether stmt has no executable else body.

Source code in .venv/lib/python3.12/site-packages/bloqade/cirq_utils/classical_control.py
120
121
122
123
124
125
126
127
128
129
def is_empty_else(stmt: scf.IfElse) -> bool:
    """Return whether ``stmt`` has no executable else body."""
    if not stmt.else_body.blocks:
        return True
    else_stmts = list(stmt.else_body.blocks[0].stmts)
    return (
        len(else_stmts) == 1
        and isinstance(else_stmts[0], scf.Yield)
        and len(else_stmts[0].values) == 0
    )

is_is_lost_condition

is_is_lost_condition(cond: SSAValue) -> bool

Return whether cond is based on is_lost.

Source code in .venv/lib/python3.12/site-packages/bloqade/cirq_utils/classical_control.py
110
111
112
113
114
115
116
117
def is_is_lost_condition(cond: ir.SSAValue) -> bool:
    """Return whether ``cond`` is based on ``is_lost``."""
    if not isinstance(cond, ir.ResultValue):
        return False
    owner = cond.owner
    if isinstance(owner, (py.GetItem, py.indexing.GetItem)):
        return is_is_lost_condition(owner.obj)
    return isinstance(owner, qubit.IsLost)

is_single_qubit_measure

is_single_qubit_measure(measure: Measure) -> bool

Return whether measure reads exactly one qubit.

Source code in .venv/lib/python3.12/site-packages/bloqade/cirq_utils/classical_control.py
61
62
63
64
65
66
def is_single_qubit_measure(measure: qubit.Measure) -> bool:
    """Return whether ``measure`` reads exactly one qubit."""
    qubits_type = measure.qubits.type
    if not hasattr(qubits_type, "is_subseteq"):
        return False
    return qubits_type.is_subseteq(ilist.IListType[QubitType, types.Literal(1)])

parse_classical_if_condition

parse_classical_if_condition(
    cond: SSAValue,
) -> ClassicalIfCondition | None

Parse an IfElse condition supported by cirq classical control.

Source code in .venv/lib/python3.12/site-packages/bloqade/cirq_utils/classical_control.py
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def parse_classical_if_condition(cond: ir.SSAValue) -> ClassicalIfCondition | None:
    """Parse an ``IfElse`` condition supported by cirq classical control."""
    if not isinstance(cond, ir.ResultValue):
        return None
    owner = cond.owner

    if isinstance(owner, py.cmp.Eq):
        measure = _resolve_measure_stmt(owner.lhs)
        cmp_val = _unwrap_constant(owner.rhs)
        if measure is None:
            measure = _resolve_measure_stmt(owner.rhs)
            cmp_val = _unwrap_constant(owner.lhs)
        if measure is None or cmp_val not in {0, 1}:
            return None
        return ClassicalIfCondition(
            measure=measure,
            trigger_on_one=bool(cmp_val),
        )

    if isinstance(owner, py.cmp.NotEq):
        measure = _resolve_measure_stmt(owner.lhs)
        cmp_val = _unwrap_constant(owner.rhs)
        if measure is None:
            measure = _resolve_measure_stmt(owner.rhs)
            cmp_val = _unwrap_constant(owner.lhs)
        if measure is None or cmp_val not in {0, 1}:
            return None
        # != flips the polarity relative to ==
        return ClassicalIfCondition(
            measure=measure,
            trigger_on_one=not bool(cmp_val),
        )

    result = _resolve_predicate_chain(cond)
    if result is not None:
        measure, trigger_on_one = result
        return ClassicalIfCondition(measure=measure, trigger_on_one=trigger_on_one)

    return None