Skip to content

Set detector partial

SetDetectorPartial dataclass

SetDetectorPartial()

Bases: RewriteRule

Rewrite SetDetector using GetRecIdxFromMeasurement placeholders.

Instead of computing record indices from analysis results immediately, this injects GetRecIdxFromMeasurement statements that will be resolved post-analysis.

extract_coord_ssas

extract_coord_ssas(
    node: Statement,
) -> list[ir.SSAValue] | None

Extract coordinate values from a SetDetector's coordinates argument and insert corresponding stim constant statements before the node.

Returns the list of coord SSA values, or None if extraction fails.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/set_detector_partial.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def extract_coord_ssas(
    node: ir.Statement,
) -> list[ir.SSAValue] | None:
    """Extract coordinate values from a SetDetector's coordinates argument
    and insert corresponding stim constant statements before the node.

    Returns the list of coord SSA values, or None if extraction fails.
    """
    if not isinstance(node.coordinates.owner, py.Constant):
        return None

    coord_values = node.coordinates.owner.value.unwrap()

    if not isinstance(coord_values, Iterable):
        return None

    if any(not isinstance(value, (int, float)) for value in coord_values):
        return None

    coord_ssas: list[ir.SSAValue] = []
    for coord_value in coord_values:
        if isinstance(coord_value, float):
            coord_stmt = auxiliary.ConstFloat(value=coord_value)
        else:
            coord_stmt = auxiliary.ConstInt(value=coord_value)
        coord_ssas.append(coord_stmt.result)
        coord_stmt.insert_before(node)

    return coord_ssas