17
17
from .quantum import QuOperator , identity
18
18
from .simplify import _full_light_cone_cancel
19
19
from .basecircuit import BaseCircuit
20
+ from .pulse import DefcalBuilder , Param , Frame
20
21
21
22
Gate = gates .Gate
22
23
Tensor = Any
@@ -137,21 +138,22 @@ def add_calibration(
137
138
})
138
139
139
140
140
- def to_tqasm (self , pragma : str ) -> str :
141
+ def to_tqasm (self , pragma : Optional [ str ] = None ) -> str :
141
142
qasm_lines = []
142
- if pragma :
143
- qasm_lines .append (pragma )
144
143
145
144
qasm_lines .append ("TQASM 0.2;" )
146
145
146
+ if pragma :
147
+ qasm_lines .append (pragma )
148
+
147
149
qasm_lines .append (f"QREG q[{ self ._nqubits } ];" )
148
150
149
151
for cal in getattr (self , "calibrations" , []):
150
152
pname = ", " .join (cal ["parameters" ])
151
- qasm_lines .append (f"\n defcal { cal ['name' ]} { pname } {{" )
153
+ qasm_lines .append (f"defcal { cal ['name' ]} { pname } {{" )
152
154
for inst in cal ["instructions" ]:
153
155
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' ]} );" )
155
157
elif inst ["type" ] == "play" :
156
158
args_str = ", " .join (str (x ) for x in inst ["args" ])
157
159
wf_type = inst ["waveform_type" ]
@@ -171,7 +173,7 @@ def to_tqasm(self, pragma: str) -> str:
171
173
for i , gate in enumerate (self ._qir ):
172
174
for cal in cals_by_pos .get (i , []):
173
175
# print(cal)
174
- pname = ", " .join (cal .get ("parameters" , []))
176
+ pname = ", " .join (f"q[ { x } ]" for x in cal .get ("parameters" , []))
175
177
qasm_lines .append (f"{ cal ['name' ]} { pname } ;" )
176
178
177
179
# print(gate)
@@ -189,7 +191,7 @@ def to_tqasm(self, pragma: str) -> str:
189
191
# 收尾:把 pos == len(self._qir) 的校准放在最后
190
192
for cal in cals_by_pos .get (len (self ._qir ), []):
191
193
# print(cal)
192
- pname = ", " .join (cal .get ("parameters" , []))
194
+ pname = ", " .join (f"q[ { x } ]" for x in cal .get ("parameters" , []))
193
195
qasm_lines .append (f"{ cal ['name' ]} { pname } ;" )
194
196
195
197
return ("\n " .join (qasm_lines ))
@@ -1083,51 +1085,3 @@ def expectation(
1083
1085
else :
1084
1086
den = 1.0
1085
1087
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