20
20
from bloqade .analog .builder .base import ParamType
21
21
22
22
23
-
24
23
class HTTPHandlerABC :
25
24
@abc .abstractmethod
26
25
def submit_task_via_zapier (task_ir : QuEraTaskSpecification , task_id : str ):
@@ -75,17 +74,21 @@ def convert_preview_to_download(preview_url):
75
74
76
75
77
76
class HTTPHandler (HTTPHandlerABC ):
78
- def __init__ (self , zapier_webhook_url : str = None ,
79
- zapier_webhook_key : str = None ,
80
- vercel_api_url : str = None ):
77
+ def __init__ (
78
+ self ,
79
+ zapier_webhook_url : str = None ,
80
+ zapier_webhook_key : str = None ,
81
+ vercel_api_url : str = None ,
82
+ ):
81
83
self .zapier_webhook_url = zapier_webhook_url or os .environ ["ZAPIER_WEBHOOK_URL" ]
82
84
self .zapier_webhook_key = zapier_webhook_key or os .environ ["ZAPIER_WEBHOOK_KEY" ]
83
85
self .verrcel_api_url = vercel_api_url or os .environ ["VERCEL_API_URL" ]
84
86
85
- def submit_task_via_zapier (self , task_ir : QuEraTaskSpecification , task_id : str , task_note : str ):
87
+ def submit_task_via_zapier (
88
+ self , task_ir : QuEraTaskSpecification , task_id : str , task_note : str
89
+ ):
86
90
# implement http request logic to submit task via Zapier
87
- request_options = dict (
88
- params = {"key" : self .zapier_webhook_key , "note" : task_id })
91
+ request_options = dict (params = {"key" : self .zapier_webhook_key , "note" : task_id })
89
92
90
93
# for metadata, task_ir in self._compile_single(shots, use_experimental, args):
91
94
json_request_body = task_ir .json (exclude_none = True , exclude_unset = True )
@@ -98,8 +101,7 @@ def submit_task_via_zapier(self, task_ir: QuEraTaskSpecification, task_id: str,
98
101
submit_status = response_data .get ("status" , None )
99
102
return submit_status
100
103
else :
101
- print (
102
- f"HTTP request failed with status code: { response .status_code } " )
104
+ print (f"HTTP request failed with status code: { response .status_code } " )
103
105
print ("HTTP responce: " , response .text )
104
106
return "Failed"
105
107
@@ -131,7 +133,6 @@ def query_task_status(self, task_id: str):
131
133
status = matches [0 ].get ("status" )
132
134
return status
133
135
134
-
135
136
def fetch_results (self , task_id : str ):
136
137
response = request (
137
138
"GET" ,
@@ -143,8 +144,7 @@ def fetch_results(self, task_id: str):
143
144
},
144
145
)
145
146
if response .status_code != 200 :
146
- print (
147
- f"HTTP request failed with status code: { response .status_code } " )
147
+ print (f"HTTP request failed with status code: { response .status_code } " )
148
148
print ("HTTP responce: " , response .text )
149
149
return None
150
150
@@ -164,8 +164,7 @@ def fetch_results(self, task_id: str):
164
164
googledoc = record .get ("resultsFileUrl" )
165
165
166
166
# convert the preview URL to download URL
167
- googledoc = convert_preview_to_download (
168
- googledoc )
167
+ googledoc = convert_preview_to_download (googledoc )
169
168
res = get (googledoc )
170
169
res .raise_for_status ()
171
170
data = res .json ()
@@ -177,6 +176,7 @@ def fetch_results(self, task_id: str):
177
176
class TestHTTPHandler (HTTPHandlerABC ):
178
177
pass
179
178
179
+
180
180
@dataclass
181
181
@Serializer .register
182
182
class ExclusiveRemoteTask (CustomRemoteTaskABC ):
@@ -186,7 +186,7 @@ class ExclusiveRemoteTask(CustomRemoteTaskABC):
186
186
_http_handler : HTTPHandlerABC = field (default_factory = HTTPHandler )
187
187
_task_id : str | None = None
188
188
_task_result_ir : QuEraTaskResults | None = None
189
-
189
+
190
190
def __post_init__ (self ):
191
191
float_sites = list (
192
192
map (lambda x : (float (x [0 ]), float (x [1 ])), self ._task_ir .lattice .sites )
@@ -195,7 +195,6 @@ def __post_init__(self):
195
195
float_sites , self ._task_ir .lattice .filling , self ._parallel_decoder
196
196
)
197
197
198
-
199
198
@classmethod
200
199
def from_compile_results (cls , task_ir , metadata , parallel_decoder ):
201
200
return cls (
@@ -212,12 +211,19 @@ def _submit(self, force: bool = False) -> "ExclusiveRemoteTask":
212
211
)
213
212
self ._task_id = str (uuid .uuid4 ())
214
213
215
- if self ._http_handler .submit_task_via_zapier (self ._task_ir , self ._task_id , None ) == "success" :
214
+ if (
215
+ self ._http_handler .submit_task_via_zapier (
216
+ self ._task_ir , self ._task_id , None
217
+ )
218
+ == "success"
219
+ ):
216
220
self ._task_result_ir = QuEraTaskResults (
217
- task_status = QuEraTaskStatusCode .Accepted )
221
+ task_status = QuEraTaskStatusCode .Accepted
222
+ )
218
223
else :
219
224
self ._task_result_ir = QuEraTaskResults (
220
- task_status = QuEraTaskStatusCode .Failed )
225
+ task_status = QuEraTaskStatusCode .Failed
226
+ )
221
227
return self
222
228
223
229
def fetch (self ):
@@ -235,8 +241,7 @@ def fetch(self):
235
241
236
242
status = self .status ()
237
243
if status in [QuEraTaskStatusCode .Completed , QuEraTaskStatusCode .Partial ]:
238
- self ._task_result_ir = self ._http_handler .fetch_results (
239
- self ._task_id )
244
+ self ._task_result_ir = self ._http_handler .fetch_results (self ._task_id )
240
245
else :
241
246
self ._task_result_ir = QuEraTaskResults (task_status = status )
242
247
@@ -248,18 +253,15 @@ def pull(self):
248
253
raise NotImplementedError (
249
254
"Pulling is not supported. Please use fetch() instead."
250
255
)
251
-
256
+
252
257
def cancel (self ):
253
258
# This is not supported
254
- raise NotImplementedError (
255
- "Cancelling is not supported."
256
- )
259
+ raise NotImplementedError ("Cancelling is not supported." )
257
260
258
261
def status (self ) -> QuEraTaskStatusCode :
259
262
if self ._task_id is None :
260
263
return QuEraTaskStatusCode .Unsubmitted
261
264
res = self ._http_handler .query_task_status (self ._task_id )
262
- #print("Query task status: ", res)
263
265
if res == "Failed" :
264
266
raise ValueError ("Query task status failed." )
265
267
elif res == "Submitted" :
@@ -335,15 +337,12 @@ def _deserializer(d: Dict[str, any]) -> ExclusiveRemoteTask:
335
337
d1 = dict ()
336
338
d1 ["_task_ir" ] = QuEraTaskSpecification (** d ["task_ir" ])
337
339
d1 ["_parallel_decoder" ] = (
338
- ParallelDecoder (** d ["parallel_decoder" ]
339
- ) if d [ "parallel_decoder" ] else None )
340
+ ParallelDecoder (** d ["parallel_decoder" ]) if d [ "parallel_decoder" ] else None
341
+ )
340
342
d1 ["_metadata" ] = d ["metadata" ]
341
343
d1 ["_task_result_ir" ] = (
342
- QuEraTaskResults (** d ["task_result_ir" ])
343
- if d ["task_result_ir" ]
344
- else None
344
+ QuEraTaskResults (** d ["task_result_ir" ]) if d ["task_result_ir" ] else None
345
345
)
346
346
d1 ["_task_id" ] = d ["task_id" ]
347
347
348
348
return ExclusiveRemoteTask (** d1 )
349
-
0 commit comments