@@ -66,6 +66,7 @@ def __init__(
6666 )
6767 self .calibrations = []
6868 self .calibration_invokes = []
69+ self .measz_invokes = []
6970
7071 self .inputs = inputs
7172 self .mps_inputs = mps_inputs
@@ -136,6 +137,14 @@ def add_calibration(
136137 "parameters" : parameters ,
137138 "pos" : len (self ._qir )
138139 })
140+
141+ def measz (self , * index : int ) -> None :
142+ self .measz_invokes .append ({
143+ "name" : "measz" ,
144+ "index" : index ,
145+ "pos" : len (self ._qir )
146+ })
147+ return
139148
140149
141150 def to_tqasm (self , pragma : Optional [str ]= None ) -> str :
@@ -169,8 +178,18 @@ def to_tqasm(self, pragma: Optional[str]= None) -> str:
169178 pos = cal .get ("pos" , len (self ._qir ))
170179 cals_by_pos [pos ].append (cal )
171180
181+ measz_by_pos = defaultdict (list )
182+ for m in getattr (self , "measz_invokes" , []):
183+ # pos 记录的是加入时的 len(self._qir)
184+ pos = m .get ("pos" , len (self ._qir ))
185+ measz_by_pos [pos ].append (m )
186+
172187 # 交错输出:在第 i 个门之前输出所有 pos == i 的校准
173188 for i , gate in enumerate (self ._qir ):
189+ for m in measz_by_pos .get (i , []):
190+ targets = ", " .join (f"q[{ idx } ]" for idx in m ["index" ])
191+ qasm_lines .append (f"MEASZ { targets } ;" )
192+
174193 for cal in cals_by_pos .get (i , []):
175194 # print(cal)
176195 pname = ", " .join (f"q[{ x } ]" for x in cal .get ("parameters" , []))
@@ -188,6 +207,10 @@ def to_tqasm(self, pragma: Optional[str]= None) -> str:
188207 else :
189208 qasm_lines .append (f"{ gname } { targets } ;" )
190209
210+ for m in measz_by_pos .get (len (self ._qir ), []):
211+ targets = ", " .join (f"q[{ idx } ]" for idx in m ["index" ])
212+ qasm_lines .append (f"MEASZ { targets } ;" )
213+
191214 # 收尾:把 pos == len(self._qir) 的校准放在最后
192215 for cal in cals_by_pos .get (len (self ._qir ), []):
193216 # print(cal)
0 commit comments