1717from .quantum import QuOperator , identity
1818from .simplify import _full_light_cone_cancel
1919from .basecircuit import BaseCircuit
20+ from .pulse import DefcalBuilder , Param , Frame
2021
2122Gate = gates .Gate
2223Tensor = Any
@@ -137,21 +138,22 @@ def add_calibration(
137138 })
138139
139140
140- def to_tqasm (self , pragma : str ) -> str :
141+ def to_tqasm (self , pragma : Optional [ str ] = None ) -> str :
141142 qasm_lines = []
142- if pragma :
143- qasm_lines .append (pragma )
144143
145144 qasm_lines .append ("TQASM 0.2;" )
146145
146+ if pragma :
147+ qasm_lines .append (pragma )
148+
147149 qasm_lines .append (f"QREG q[{ self ._nqubits } ];" )
148150
149151 for cal in getattr (self , "calibrations" , []):
150152 pname = ", " .join (cal ["parameters" ])
151- qasm_lines .append (f"\n defcal { cal ['name' ]} { pname } {{" )
153+ qasm_lines .append (f"defcal { cal ['name' ]} { pname } {{" )
152154 for inst in cal ["instructions" ]:
153155 if inst ["type" ] == "frame" :
154- qasm_lines .append (f" frame { inst ['frame' ]} = newframe({ inst ['qubit' ]} );" )
156+ qasm_lines .append (f" frame { inst ['frame' ]. name } = newframe({ inst ['qubit' ]} );" )
155157 elif inst ["type" ] == "play" :
156158 args_str = ", " .join (str (x ) for x in inst ["args" ])
157159 wf_type = inst ["waveform_type" ]
@@ -171,7 +173,7 @@ def to_tqasm(self, pragma: str) -> str:
171173 for i , gate in enumerate (self ._qir ):
172174 for cal in cals_by_pos .get (i , []):
173175 # print(cal)
174- pname = ", " .join (cal .get ("parameters" , []))
176+ pname = ", " .join (f"q[ { x } ]" for x in cal .get ("parameters" , []))
175177 qasm_lines .append (f"{ cal ['name' ]} { pname } ;" )
176178
177179 # print(gate)
@@ -189,7 +191,7 @@ def to_tqasm(self, pragma: str) -> str:
189191 # 收尾:把 pos == len(self._qir) 的校准放在最后
190192 for cal in cals_by_pos .get (len (self ._qir ), []):
191193 # print(cal)
192- pname = ", " .join (cal .get ("parameters" , []))
194+ pname = ", " .join (f"q[ { x } ]" for x in cal .get ("parameters" , []))
193195 qasm_lines .append (f"{ cal ['name' ]} { pname } ;" )
194196
195197 return ("\n " .join (qasm_lines ))
@@ -1083,51 +1085,3 @@ def expectation(
10831085 else :
10841086 den = 1.0
10851087 return num / den
1086-
1087- class Param :
1088- def __init__ (self , name : str ):
1089- self .name = name
1090-
1091- class Frame :
1092- def __init__ (self , name : str ):
1093- self .name = name
1094-
1095- class DefcalBuilder :
1096- def __init__ (self , circuit , name : str , parameters : List ["Param" ]):
1097- self .circuit = circuit
1098- self .name = name
1099- self .parameters = parameters
1100- self .instructions = []
1101-
1102- def new_frame (self , frame_name : str , param : "Param" ):
1103- frame = Frame (frame_name )
1104- self .instructions .append ({
1105- "type" : "frame" ,
1106- "frame" : frame ,
1107- "qubit" : param .name ,
1108- })
1109- return frame
1110-
1111- def play (self , frame : Frame , waveform : Any , start_time : int = None ):
1112- if not hasattr (waveform , "__dataclass_fields__" ):
1113- raise TypeError ("Unsupported waveform type" )
1114-
1115- waveform_type = waveform .qasm_name ()
1116- args = waveform .to_args ()
1117- if start_time is not None :
1118- args = [start_time ] + args
1119-
1120- self .instructions .append ({
1121- "type" : "play" ,
1122- "frame" : frame .name ,
1123- "waveform_type" : waveform_type ,
1124- "args" : args ,
1125- })
1126- return self
1127-
1128- def build (self ):
1129- self .circuit .def_calibration (
1130- name = self .name ,
1131- parameters = [p .name for p in self .parameters ],
1132- instructions = self .instructions ,
1133- )
0 commit comments