|
| 1 | +import itertools |
| 2 | + |
| 3 | +from kirin import ir |
| 4 | +from kirin.passes import Pass |
| 5 | +from kirin.rewrite import Walk |
| 6 | +from kirin.dialects import ilist |
| 7 | +from kirin.rewrite.abc import RewriteRule, RewriteResult |
| 8 | + |
| 9 | +from .stmts import ( |
| 10 | + PPError, |
| 11 | + QubitLoss, |
| 12 | + Depolarize, |
| 13 | + PauliError, |
| 14 | + NoiseChannel, |
| 15 | + TwoQubitPauliChannel, |
| 16 | + SingleQubitPauliChannel, |
| 17 | + StochasticUnitaryChannel, |
| 18 | +) |
| 19 | +from ..op.stmts import X, Y, Z, Kron, Identity |
| 20 | + |
| 21 | + |
| 22 | +class _RewriteNoiseStmts(RewriteRule): |
| 23 | + """Rewrites squin noise statements to StochasticUnitaryChannel""" |
| 24 | + |
| 25 | + def rewrite_Statement(self, node: ir.Statement) -> RewriteResult: |
| 26 | + if not isinstance(node, NoiseChannel) or isinstance(node, QubitLoss): |
| 27 | + return RewriteResult() |
| 28 | + |
| 29 | + return getattr(self, "rewrite_" + node.name)(node) |
| 30 | + |
| 31 | + def rewrite_pauli_error(self, node: PauliError) -> RewriteResult: |
| 32 | + (operators := ilist.New(values=(node.basis,))).insert_before(node) |
| 33 | + (ps := ilist.New(values=(node.p,))).insert_before(node) |
| 34 | + stochastic_channel = StochasticUnitaryChannel( |
| 35 | + operators=operators.result, probabilities=ps.result |
| 36 | + ) |
| 37 | + |
| 38 | + node.replace_by(stochastic_channel) |
| 39 | + return RewriteResult(has_done_something=True) |
| 40 | + |
| 41 | + def rewrite_single_qubit_pauli_channel( |
| 42 | + self, node: SingleQubitPauliChannel |
| 43 | + ) -> RewriteResult: |
| 44 | + paulis = (X(), Y(), Z()) |
| 45 | + paulis_ssa: list[ir.SSAValue] = [] |
| 46 | + for op in paulis: |
| 47 | + op.insert_before(node) |
| 48 | + paulis_ssa.append(op.result) |
| 49 | + |
| 50 | + (pauli_ops := ilist.New(values=paulis_ssa)).insert_before(node) |
| 51 | + |
| 52 | + stochastic_unitary = StochasticUnitaryChannel( |
| 53 | + operators=pauli_ops.result, probabilities=node.params |
| 54 | + ) |
| 55 | + node.replace_by(stochastic_unitary) |
| 56 | + return RewriteResult(has_done_something=True) |
| 57 | + |
| 58 | + def rewrite_two_qubit_pauli_channel( |
| 59 | + self, node: TwoQubitPauliChannel |
| 60 | + ) -> RewriteResult: |
| 61 | + paulis = (X(), Y(), Z(), Identity(sites=1)) |
| 62 | + for op in paulis: |
| 63 | + op.insert_before(node) |
| 64 | + |
| 65 | + # NOTE: collect list so we can skip the last entry, which will be two identities |
| 66 | + combinations = list(itertools.product(paulis, repeat=2))[:-1] |
| 67 | + operators: list[ir.SSAValue] = [] |
| 68 | + for pauli_1, pauli_2 in combinations: |
| 69 | + op = Kron(pauli_1.result, pauli_2.result) |
| 70 | + op.insert_before(node) |
| 71 | + operators.append(op.result) |
| 72 | + |
| 73 | + (operator_list := ilist.New(values=operators)).insert_before(node) |
| 74 | + stochastic_unitary = StochasticUnitaryChannel( |
| 75 | + operators=operator_list.result, probabilities=node.params |
| 76 | + ) |
| 77 | + |
| 78 | + node.replace_by(stochastic_unitary) |
| 79 | + return RewriteResult(has_done_something=True) |
| 80 | + |
| 81 | + def rewrite_p_p_error(self, node: PPError) -> RewriteResult: |
| 82 | + (operators := ilist.New(values=(node.op,))).insert_before(node) |
| 83 | + (ps := ilist.New(values=(node.p,))).insert_before(node) |
| 84 | + stochastic_channel = StochasticUnitaryChannel( |
| 85 | + operators=operators.result, probabilities=ps.result |
| 86 | + ) |
| 87 | + |
| 88 | + node.replace_by(stochastic_channel) |
| 89 | + return RewriteResult(has_done_something=True) |
| 90 | + |
| 91 | + def rewrite_depolarize(self, node: Depolarize) -> RewriteResult: |
| 92 | + paulis = (X(), Y(), Z()) |
| 93 | + operators: list[ir.SSAValue] = [] |
| 94 | + for op in paulis: |
| 95 | + op.insert_before(node) |
| 96 | + operators.append(op.result) |
| 97 | + |
| 98 | + (operator_list := ilist.New(values=operators)).insert_before(node) |
| 99 | + (ps := ilist.New(values=[node.p for _ in range(3)])).insert_before(node) |
| 100 | + |
| 101 | + stochastic_unitary = StochasticUnitaryChannel( |
| 102 | + operators=operator_list.result, probabilities=ps.result |
| 103 | + ) |
| 104 | + node.replace_by(stochastic_unitary) |
| 105 | + |
| 106 | + return RewriteResult(has_done_something=True) |
| 107 | + |
| 108 | + |
| 109 | +class RewriteNoiseStmts(Pass): |
| 110 | + def unsafe_run(self, mt: ir.Method): |
| 111 | + return Walk(_RewriteNoiseStmts()).rewrite(mt.code) |
0 commit comments