Skip to content

Squin noise

SquinNoiseToStim dataclass

SquinNoiseToStim()

Bases: RewriteRule

rewrite_Apply_and_Broadcast

rewrite_Apply_and_Broadcast(
    stmt: Apply | Broadcast | Apply | Broadcast,
) -> RewriteResult

Rewrite Apply and Broadcast to their stim statements.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/squin_noise.py
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
def rewrite_Apply_and_Broadcast(
    self, stmt: qubit.Apply | qubit.Broadcast | wire.Apply | wire.Broadcast
) -> RewriteResult:
    """Rewrite Apply and Broadcast to their stim statements."""

    # this is an SSAValue, need it to be the actual operator
    applied_op = stmt.operator.owner

    if isinstance(applied_op, squin_noise.stmts.QubitLoss):
        return RewriteResult()

    if isinstance(applied_op, squin_noise.stmts.NoiseChannel):

        rewrite_method = getattr(self, f"rewrite_{type(applied_op).__name__}", None)
        # No rewrite method exists and the rewrite should stop
        if rewrite_method is None:
            return RewriteResult()

        qubit_idx_ssas = insert_qubit_idx_after_apply(stmt=stmt)
        if qubit_idx_ssas is None:
            return RewriteResult()

        stim_stmt = rewrite_method(stmt, qubit_idx_ssas)

        if isinstance(stmt, (wire.Apply, wire.Broadcast)):
            create_wire_passthrough(stmt)

        # guaranteed that you have a valid stim_stmt to plug in
        stmt.replace_by(stim_stmt)

        return RewriteResult(has_done_something=True)
    return RewriteResult()

rewrite_Depolarize

rewrite_Depolarize(
    stmt: Apply | Broadcast | Broadcast | Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement

Rewrite squin.noise.Depolarize to stim.Depolarize1.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/squin_noise.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def rewrite_Depolarize(
    self,
    stmt: qubit.Apply | qubit.Broadcast | wire.Broadcast | wire.Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement:
    """Rewrite squin.noise.Depolarize to stim.Depolarize1."""

    squin_channel = stmt.operator.owner
    assert isinstance(squin_channel, squin_noise.stmts.Depolarize)

    p = get_const_value(float, squin_channel.p)
    p_stmt = py.Constant(p)
    p_stmt.insert_before(stmt)

    stim_stmt = stim_noise.Depolarize1(targets=qubit_idx_ssas, p=p_stmt.result)
    return stim_stmt

rewrite_Depolarize2

rewrite_Depolarize2(
    stmt: Apply | Broadcast | Broadcast | Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement

Rewrite squin.noise.Depolarize2 to stim.Depolarize2.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/squin_noise.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def rewrite_Depolarize2(
    self,
    stmt: qubit.Apply | qubit.Broadcast | wire.Broadcast | wire.Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement:
    """Rewrite squin.noise.Depolarize2 to stim.Depolarize2."""

    squin_channel = stmt.operator.owner
    assert isinstance(squin_channel, squin_noise.stmts.Depolarize2)

    p = get_const_value(float, squin_channel.p)
    p_stmt = py.Constant(p)
    p_stmt.insert_before(stmt)

    stim_stmt = stim_noise.Depolarize2(targets=qubit_idx_ssas, p=p_stmt.result)
    return stim_stmt

rewrite_PauliError

rewrite_PauliError(
    stmt: Apply | Broadcast | Broadcast | Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement

Rewrite squin.noise.PauliError to XError, YError, ZError.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/squin_noise.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def rewrite_PauliError(
    self,
    stmt: qubit.Apply | qubit.Broadcast | wire.Broadcast | wire.Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement:
    """Rewrite squin.noise.PauliError to XError, YError, ZError."""
    squin_channel = stmt.operator.owner
    assert isinstance(squin_channel, squin_noise.stmts.PauliError)
    basis = squin_channel.basis.owner
    assert isinstance(basis, op.stmts.PauliOp)
    p = get_const_value(float, squin_channel.p)

    p_stmt = py.Constant(p)
    p_stmt.insert_before(stmt)

    if isinstance(basis, op.stmts.X):
        stim_stmt = stim_noise.XError(targets=qubit_idx_ssas, p=p_stmt.result)
    elif isinstance(basis, op.stmts.Y):
        stim_stmt = stim_noise.YError(targets=qubit_idx_ssas, p=p_stmt.result)
    else:
        stim_stmt = stim_noise.ZError(targets=qubit_idx_ssas, p=p_stmt.result)
    return stim_stmt

rewrite_SingleQubitPauliChannel

rewrite_SingleQubitPauliChannel(
    stmt: Apply | Broadcast | Broadcast | Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement

Rewrite squin.noise.SingleQubitPauliChannel to stim.PauliChannel1.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/squin_noise.py
 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 rewrite_SingleQubitPauliChannel(
    self,
    stmt: qubit.Apply | qubit.Broadcast | wire.Broadcast | wire.Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement:
    """Rewrite squin.noise.SingleQubitPauliChannel to stim.PauliChannel1."""

    squin_channel = stmt.operator.owner
    assert isinstance(squin_channel, squin_noise.stmts.SingleQubitPauliChannel)

    params = get_const_value(ilist.IList, squin_channel.params)
    new_stmts = [
        p_x := py.Constant(params[0]),
        p_y := py.Constant(params[1]),
        p_z := py.Constant(params[2]),
    ]
    for new_stmt in new_stmts:
        new_stmt.insert_before(stmt)

    stim_stmt = stim_noise.PauliChannel1(
        targets=qubit_idx_ssas,
        px=p_x.result,
        py=p_y.result,
        pz=p_z.result,
    )
    return stim_stmt

rewrite_TwoQubitPauliChannel

rewrite_TwoQubitPauliChannel(
    stmt: Apply | Broadcast | Broadcast | Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement

Rewrite squin.noise.SingleQubitPauliChannel to stim.PauliChannel1.

Source code in .venv/lib/python3.12/site-packages/bloqade/stim/rewrite/squin_noise.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def rewrite_TwoQubitPauliChannel(
    self,
    stmt: qubit.Apply | qubit.Broadcast | wire.Broadcast | wire.Apply,
    qubit_idx_ssas: Tuple[SSAValue],
) -> Statement:
    """Rewrite squin.noise.SingleQubitPauliChannel to stim.PauliChannel1."""

    squin_channel = stmt.operator.owner
    assert isinstance(squin_channel, squin_noise.stmts.TwoQubitPauliChannel)

    params = get_const_value(ilist.IList, squin_channel.params)
    param_stmts = [py.Constant(p) for p in params]
    for param_stmt in param_stmts:
        param_stmt.insert_before(stmt)

    stim_stmt = stim_noise.PauliChannel2(
        targets=qubit_idx_ssas,
        pix=param_stmts[0].result,
        piy=param_stmts[1].result,
        piz=param_stmts[2].result,
        pxi=param_stmts[3].result,
        pxx=param_stmts[4].result,
        pxy=param_stmts[5].result,
        pxz=param_stmts[6].result,
        pyi=param_stmts[7].result,
        pyx=param_stmts[8].result,
        pyy=param_stmts[9].result,
        pyz=param_stmts[10].result,
        pzi=param_stmts[11].result,
        pzx=param_stmts[12].result,
        pzy=param_stmts[13].result,
        pzz=param_stmts[14].result,
    )
    return stim_stmt