Skip to content

Commit e6544cf

Browse files
committed
Implement probability checks for native noise statements
1 parent 16d6f84 commit e6544cf

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/bloqade/noise/native/stmts.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class PauliChannel(ir.Statement):
1717
pz: float = info.attribute(types.Float)
1818
qargs: ir.SSAValue = info.argument(ilist.IListType[QubitType])
1919

20+
def check(self):
21+
probs = (self.px, self.py, self.pz)
22+
if not all(0 <= p <= 1 for p in probs) or not 0 <= sum(probs) <= 1:
23+
raise ValueError(f"Invalid Pauli error probabilities (px, py, pz): {probs}")
24+
2025

2126
NumQubits = types.TypeVar("NumQubits")
2227

@@ -36,6 +41,23 @@ class CZPauliChannel(ir.Statement):
3641
ctrls: ir.SSAValue = info.argument(ilist.IListType[QubitType, NumQubits])
3742
qargs: ir.SSAValue = info.argument(ilist.IListType[QubitType, NumQubits])
3843

44+
def check(self):
45+
probs_ctrl = (self.px_ctrl, self.py_ctrl, self.pz_ctrl)
46+
47+
def check_prob(p: float) -> bool:
48+
return 0 <= p <= 1
49+
50+
if not map(check_prob, probs_ctrl) or not check_prob(sum(probs_ctrl)):
51+
raise ValueError(
52+
f"Invalid control probabilities for CZ Pauli channel (px_ctrl, py_ctrl, pz_ctrl): {probs_ctrl}"
53+
)
54+
55+
probs_qarg = (self.px_qarg, self.py_qarg, self.pz_qarg)
56+
if not map(check_prob, probs_qarg) or not check_prob(sum(probs_qarg)):
57+
raise ValueError(
58+
f"Invalid probabilities for CZ Pauli channel (px_qarg, py_qarg, pz_qarg): {probs_qarg}"
59+
)
60+
3961

4062
@statement(dialect=dialect)
4163
class AtomLossChannel(ir.Statement):
@@ -44,3 +66,7 @@ class AtomLossChannel(ir.Statement):
4466

4567
prob: float = info.attribute(types.Float)
4668
qargs: ir.SSAValue = info.argument(ilist.IListType[QubitType])
69+
70+
def check(self):
71+
if not 0 <= self.prob <= 1:
72+
raise ValueError(f"Invalid atom loss probability {self.prob}")

0 commit comments

Comments
 (0)