1616from tsim .noise .dem import get_detector_error_model
1717from tsim .utils .clifford import parametric_to_clifford_gates
1818from tsim .utils .diagram import render_svg
19- from tsim .utils .program_text import shorthand_to_stim , stim_to_shorthand
19+ from tsim .utils .program_text import (
20+ enriched_stim_error ,
21+ shorthand_to_stim ,
22+ stim_to_shorthand ,
23+ )
2024
2125
2226class Circuit :
@@ -42,7 +46,11 @@ def __init__(self, stim_program_text: str = ""):
4246 empty circuit.
4347
4448 """
45- self ._stim_circ = stim .Circuit (shorthand_to_stim (stim_program_text ))
49+ converted = shorthand_to_stim (stim_program_text )
50+ try :
51+ self ._stim_circ = stim .Circuit (converted )
52+ except ValueError as exc :
53+ raise enriched_stim_error (exc , converted ) from None
4654
4755 @classmethod
4856 def from_stim_program (cls , stim_circuit : stim .Circuit ) -> Circuit :
@@ -64,9 +72,11 @@ def append_from_stim_program_text(self, stim_program_text: str) -> None:
6472
6573 Supports the same shorthand syntax as the constructor.
6674 """
67- self ._stim_circ .append_from_stim_program_text (
68- shorthand_to_stim (stim_program_text )
69- )
75+ converted = shorthand_to_stim (stim_program_text )
76+ try :
77+ self ._stim_circ .append_from_stim_program_text (converted )
78+ except ValueError as exc :
79+ raise enriched_stim_error (exc , converted ) from None
7080
7181 @overload
7282 def append (
@@ -141,18 +151,21 @@ def append(
141151 name = "S_DAG"
142152 tag = "T"
143153 elif name in ("R_X" , "R_Y" , "R_Z" ):
144- assert arg is not None , f"For { name } gates, an angle must be provided."
154+ if arg is None :
155+ raise ValueError (f"For { name } gates, an angle must be provided." )
145156 args = list (arg ) if isinstance (arg , Iterable ) else [arg ]
146- assert (
147- len (args ) == 1
148- ), f"For { name } gates, a single angle must be provided."
157+ if len (args ) != 1 :
158+ raise ValueError (
159+ f"For { name } gates, a single angle must be provided."
160+ )
149161 tag = f"{ name } (theta={ args [0 ]} *pi)"
150162 name = "I"
151163 arg = None
152164 elif name == "U3" :
153- assert arg is not None and (
154- isinstance (arg , Iterable ) and len (list (arg )) == 3
155- ), f"For U3 gates, three rotation angles must be provided."
165+ if arg is None or not isinstance (arg , Iterable ) or len (list (arg )) != 3 :
166+ raise ValueError (
167+ "For U3 gates, three rotation angles must be provided."
168+ )
156169 theta , phi , lam = list (arg )
157170 tag = f"U3(theta={ theta } *pi, phi={ phi } *pi, lambda={ lam } *pi)"
158171 name = "I"
@@ -175,7 +188,11 @@ def from_file(cls, filename: str) -> Circuit:
175188 """
176189 with open (filename , "r" , encoding = "utf-8" ) as f :
177190 stim_program_text = f .read ()
178- stim_circ = stim .Circuit (shorthand_to_stim (stim_program_text ))
191+ converted = shorthand_to_stim (stim_program_text )
192+ try :
193+ stim_circ = stim .Circuit (converted )
194+ except ValueError as exc :
195+ raise enriched_stim_error (exc , converted ) from None
179196 return cls .from_stim_program (stim_circ )
180197
181198 def __repr__ (self ) -> str :
0 commit comments