|
| 1 | +from typing import Any |
1 | 2 | from dataclasses import field, dataclass |
2 | 3 |
|
3 | 4 | from kirin import ir, types, interp |
|
7 | 8 | from bloqade.types import QubitType |
8 | 9 | from bloqade.qasm2.parse import ast |
9 | 10 |
|
| 11 | +from bloqade.noise import native |
| 12 | + |
10 | 13 | from .base import EmitError, EmitQASM2Base, EmitQASM2Frame |
11 | 14 |
|
12 | 15 |
|
@@ -86,3 +89,84 @@ def emit_err(self, emit: EmitQASM2Gate, frame: EmitQASM2Frame, stmt): |
86 | 89 | @interp.impl(func.ConstantNone) |
87 | 90 | def ignore(self, emit: EmitQASM2Gate, frame: EmitQASM2Frame, stmt): |
88 | 91 | return () |
| 92 | + |
| 93 | + |
| 94 | +@native.dialect.register(key="emit.qasm2.gate") |
| 95 | +class NativeNoise(interp.MethodTable): |
| 96 | + |
| 97 | + def _convert(self, node: ast.Bit | ast.Name) -> str: |
| 98 | + if isinstance(node, ast.Bit): |
| 99 | + return f"{node.name.id}[{node.addr}]" |
| 100 | + else: |
| 101 | + return f"{node.id}" |
| 102 | + |
| 103 | + @interp.impl(native.CZPauliChannel) |
| 104 | + def emit_czp( |
| 105 | + self, |
| 106 | + emit: EmitQASM2Gate, |
| 107 | + frame: EmitQASM2Frame, |
| 108 | + stmt: native.CZPauliChannel, |
| 109 | + ): |
| 110 | + paired: bool = stmt.paired |
| 111 | + px_ctrl: float = stmt.px_ctrl |
| 112 | + py_ctrl: float = stmt.py_ctrl |
| 113 | + pz_ctrl: float = stmt.pz_ctrl |
| 114 | + px_qarg: float = stmt.pz_qarg |
| 115 | + py_qarg: float = stmt.py_qarg |
| 116 | + pz_qarg: float = stmt.pz_qarg |
| 117 | + ctrls: ilist.IList[ast.Bit, Any] = frame.get(stmt.ctrls) |
| 118 | + qargs: ilist.IList[ast.Bit, Any] = frame.get(stmt.qargs) |
| 119 | + frame.body.append( |
| 120 | + ast.Comment( |
| 121 | + text=f"native.CZPauliChannel(paired={paired}, p_ctrl[{px_ctrl}, {py_ctrl}, {pz_ctrl}], p_qarg[{px_qarg}, {py_qarg}, {pz_qarg}])" |
| 122 | + ) |
| 123 | + ) |
| 124 | + frame.body.append( |
| 125 | + ast.Comment( |
| 126 | + text=f" -: ctrls: {', '.join([self._convert(q) for q in ctrls])}" |
| 127 | + ) |
| 128 | + ) |
| 129 | + frame.body.append( |
| 130 | + ast.Comment( |
| 131 | + text=f" -: qargs: {', '.join([self._convert(q) for q in qargs])}" |
| 132 | + ) |
| 133 | + ) |
| 134 | + return () |
| 135 | + |
| 136 | + @interp.impl(native.AtomLossChannel) |
| 137 | + def emit_loss( |
| 138 | + self, |
| 139 | + emit: EmitQASM2Gate, |
| 140 | + frame: EmitQASM2Frame, |
| 141 | + stmt: native.AtomLossChannel, |
| 142 | + ): |
| 143 | + prob: float = stmt.prob |
| 144 | + qargs: ilist.IList[ast.Bit, Any] = frame.get(stmt.qargs) |
| 145 | + frame.body.append(ast.Comment(text=f"native.Atomloss(p={prob})")) |
| 146 | + frame.body.append( |
| 147 | + ast.Comment( |
| 148 | + text=f" -: qargs: {', '.join([self._convert(q) for q in qargs])}" |
| 149 | + ) |
| 150 | + ) |
| 151 | + return () |
| 152 | + |
| 153 | + @interp.impl(native.PauliChannel) |
| 154 | + def emit_pauli( |
| 155 | + self, |
| 156 | + emit: EmitQASM2Gate, |
| 157 | + frame: EmitQASM2Frame, |
| 158 | + stmt: native.PauliChannel, |
| 159 | + ): |
| 160 | + px: float = stmt.px |
| 161 | + py: float = stmt.py |
| 162 | + pz: float = stmt.pz |
| 163 | + qargs: ilist.IList[ast.Bit, Any] = frame.get(stmt.qargs) |
| 164 | + frame.body.append( |
| 165 | + ast.Comment(text=f"native.Atomloss(px={px}, py={py}, pz={pz})") |
| 166 | + ) |
| 167 | + frame.body.append( |
| 168 | + ast.Comment( |
| 169 | + text=f" -: qargs: {', '.join([self._convert(q) for q in qargs])}" |
| 170 | + ) |
| 171 | + ) |
| 172 | + return () |
0 commit comments