1+ from typing import Tuple
2+
13from kirin import ir , types , lowering
24from kirin .decl import info , statement
35from kirin .dialects import ilist
79from ._dialect import dialect
810
911
10- @statement (dialect = dialect )
11- class PauliChannel (ir .Statement ):
12-
12+ @statement
13+ class NativeNoiseStmt (ir .Statement ):
1314 traits = frozenset ({lowering .FromPythonCall ()})
1415
16+ @property
17+ def probabilities (self ) -> Tuple [Tuple [float , ...], ...]:
18+ raise NotImplementedError (f"Override the method in { type (self ).__name__ } " )
19+
20+ def check (self ):
21+ for probs in self .probabilities :
22+ self .check_probability (sum (probs ))
23+ for p in probs :
24+ self .check_probability (p )
25+
26+ def check_probability (self , p : float ):
27+ if not 0 <= p <= 1 :
28+ raise ValueError (
29+ f"Invalid noise probability encountered in { type (self ).__name__ } : { p } "
30+ )
31+
32+
33+ @statement (dialect = dialect )
34+ class PauliChannel (NativeNoiseStmt ):
1535 px : float = info .attribute (types .Float )
1636 py : float = info .attribute (types .Float )
1737 pz : float = info .attribute (types .Float )
1838 qargs : ir .SSAValue = info .argument (ilist .IListType [QubitType ])
1939
40+ @property
41+ def probabilities (self ) -> Tuple [Tuple [float , ...], ...]:
42+ return ((self .px , self .py , self .pz ),)
43+
2044
2145NumQubits = types .TypeVar ("NumQubits" )
2246
2347
2448@statement (dialect = dialect )
25- class CZPauliChannel (ir .Statement ):
26-
27- traits = frozenset ({lowering .FromPythonCall ()})
28-
49+ class CZPauliChannel (NativeNoiseStmt ):
2950 paired : bool = info .attribute (types .Bool )
3051 px_ctrl : float = info .attribute (types .Float )
3152 py_ctrl : float = info .attribute (types .Float )
@@ -36,11 +57,19 @@ class CZPauliChannel(ir.Statement):
3657 ctrls : ir .SSAValue = info .argument (ilist .IListType [QubitType , NumQubits ])
3758 qargs : ir .SSAValue = info .argument (ilist .IListType [QubitType , NumQubits ])
3859
60+ @property
61+ def probabilities (self ) -> Tuple [Tuple [float , ...], ...]:
62+ return (
63+ (self .px_ctrl , self .py_ctrl , self .pz_ctrl ),
64+ (self .px_qarg , self .py_qarg , self .pz_qarg ),
65+ )
3966
40- @statement (dialect = dialect )
41- class AtomLossChannel (ir .Statement ):
42-
43- traits = frozenset ({lowering .FromPythonCall ()})
4467
68+ @statement (dialect = dialect )
69+ class AtomLossChannel (NativeNoiseStmt ):
4570 prob : float = info .attribute (types .Float )
4671 qargs : ir .SSAValue = info .argument (ilist .IListType [QubitType ])
72+
73+ @property
74+ def probabilities (self ) -> Tuple [Tuple [float , ...], ...]:
75+ return ((self .prob ,),)
0 commit comments