Skip to content

dialects

auxiliary

stmts

const

ConstBool

Bases: Statement

IR Statement representing a constant float value.

result class-attribute instance-attribute
result: ResultValue = result(Bool)

result (Float): The result value.

value class-attribute instance-attribute
value: bool = attribute(Bool)

value (float): The constant float value.

ConstFloat

Bases: Statement

IR Statement representing a constant float value.

result class-attribute instance-attribute
result: ResultValue = result(Float)

result (Float): The result value.

value class-attribute instance-attribute
value: float = attribute(Float)

value (float): The constant float value.

ConstInt

Bases: Statement

IR Statement representing a constant integer value.

result class-attribute instance-attribute
result: ResultValue = result(Int)

result (Int): The result value.

value class-attribute instance-attribute
value: int = attribute(Int)

value (int): The constant integer value.

ConstStr

Bases: Statement

IR Statement representing a constant str value.

result class-attribute instance-attribute
result: ResultValue = result(String)

result (str): The result value.

value class-attribute instance-attribute
value: str = attribute(String)

value (str): The constant str value.

Neg

Bases: Statement

IR Statement representing a negation operation.

collapse

stmts

measure

Measurement

Bases: Statement

p class-attribute instance-attribute
p: SSAValue = argument(Float)

probability of noise introduced by measurement. For example 0.01 means 1% the measurement will be flipped

pp_measure

PPMeasurement

Bases: Statement

p class-attribute instance-attribute
p: SSAValue = argument(Float)

probability of noise introduced by measurement. For example 0.01 means 1% the measurement will be flipped

parse

lowering

One-to-one lowering routine from stim circuit to a stim-dialect kirin kernel.

loads

loads(
    stim_str: str,
    *,
    kernel_name: str = "main",
    ignore_unknown_stim: bool = False,
    error_unknown_nonstim: bool = False,
    nonstim_noise_ops: dict[str, type[Statement]] = {},
    dialects: DialectGroup | None = None,
    globals: dict[str, Any] | None = None,
    file: str | None = None,
    lineno_offset: int = 0,
    col_offset: int = 0,
    compactify: bool = True
) -> ir.Method[[], None]

Loads a STIM string and returns the corresponding kernel object.

Parameters:

Name Type Description Default
stim_str str

The string representation of a STIM circuit to load.

required

Other Parameters:

Name Type Description
kernel_name str

The name of the kernel to load. Defaults to "main".

ignore_unknown_stim bool

If True, don't throw a build error on an unimplemented stim instruction.

error_unknown_nonstim bool

If True, throw a build error if an unknown tag is used on the I_ERROR instruction.

nonstim_noise_ops dict[str, Statement]

Additional statements to represent non-standard stim operations. The dictionary key should match the tag used to identify it in stim (stim format I_ERROR[MY_NOISE](0.05) 0 1 2 3 or I_ERROR[MY_CORRELATED_NOISE:2417696374](0.03) 1 3 5).

dialects DialectGroup | None

The dialects to use. Defaults to stim.main.

globals dict[str, Any] | None

The global variables to use. Defaults to None.

file str | None

The file name for error reporting. Defaults to None.

lineno_offset int

The line number offset for error reporting. Defaults to 0.

col_offset int

The column number offset for error reporting. Defaults to 0.

compactify bool

Whether to compactify the output. Defaults to True.

Example:

from bloqade.stim.lowering import loads
method = loads('''
    X 0 2 4
    DEPOLARIZE1(0.01) 0
    I_ERROR[CUSTOM_ERROR](0.02) 2 4
    M 0 2 4
    DETECTOR rec[-1] rec[-2]
''')
Source code in .venv/lib/python3.12/site-packages/bloqade/stim/parse/lowering.py
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 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
108
def loads(
    stim_str: str,
    *,
    kernel_name: str = "main",
    ignore_unknown_stim: bool = False,
    error_unknown_nonstim: bool = False,
    nonstim_noise_ops: dict[str, type[kirin.ir.Statement]] = {},
    dialects: ir.DialectGroup | None = None,
    globals: dict[str, Any] | None = None,
    file: str | None = None,
    lineno_offset: int = 0,
    col_offset: int = 0,
    compactify: bool = True,
) -> ir.Method[[], None]:
    """Loads a STIM string and returns the corresponding kernel object.

    Args:
        stim_str: The string representation of a STIM circuit to load.

    Keyword Args:
        kernel_name (str): The name of the kernel to load. Defaults to "main".
        ignore_unknown_stim (bool): If True, don't throw a build error on an
            unimplemented stim instruction.
        error_unknown_nonstim (bool): If True, throw a build error if an unknown tag is
            used on the `I_ERROR` instruction.
        nonstim_noise_ops (dict[str, kirin.ir.Statement]): Additional statements to
            represent non-standard stim operations.  The dictionary key should match the
            tag used to identify it in stim (stim format
            `I_ERROR[MY_NOISE](0.05) 0 1 2 3` or
            `I_ERROR[MY_CORRELATED_NOISE:2417696374](0.03) 1 3 5`).
        dialects (ir.DialectGroup | None): The dialects to use. Defaults to `stim.main`.
        globals (dict[str, Any] | None): The global variables to use. Defaults to None.
        file (str | None): The file name for error reporting. Defaults to None.
        lineno_offset (int): The line number offset for error reporting. Defaults to 0.
        col_offset (int): The column number offset for error reporting. Defaults to 0.
        compactify (bool): Whether to compactify the output. Defaults to True.

    Example:

    ```python
    from bloqade.stim.lowering import loads
    method = loads('''
        X 0 2 4
        DEPOLARIZE1(0.01) 0
        I_ERROR[CUSTOM_ERROR](0.02) 2 4
        M 0 2 4
        DETECTOR rec[-1] rec[-2]
    ''')
    ```
    """
    import stim  # Optional dependency required to lower stim circuits

    circ = stim.Circuit(stim_str)
    stim_lowering = Stim(
        kstim.main if dialects is None else dialects,
        ignore_unknown_stim=ignore_unknown_stim,
        error_unknown_nonstim=error_unknown_nonstim,
        nonstim_noise_ops=nonstim_noise_ops,
    )
    frame = stim_lowering.get_frame(
        circ,
        source=stim_str,
        file=file,
        globals=globals,
        lineno_offset=lineno_offset,
        col_offset=col_offset,
        compactify=compactify,
    )

    return_value = func.ConstantNone()  # No return value
    frame.push(return_value)
    return_node = frame.push(func.Return(value_or_stmt=return_value))

    body = frame.curr_region
    code = func.Function(
        sym_name=kernel_name,
        signature=func.Signature((), return_node.value.type),
        body=body,
    )
    return ir.Method(
        mod=None,
        py_func=None,
        sym_name=kernel_name,
        arg_names=[],
        dialects=kstim.dialects,
        code=code,
    )