1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414import time
15- import json
1615import cirq
1716import httpx
1817
1918from typing import Union , Optional , List
2019
21- from cirq .study import ResultDict
20+ from qio .core import (
21+ QuantumProgram ,
22+ QuantumProgramResult ,
23+ QuantumComputationModel ,
24+ QuantumComputationParameters ,
25+ BackendData ,
26+ ClientData ,
27+ )
2228
2329from scaleway_qaas_client .v1alpha1 import (
2430 QaaSClient ,
2531 QaaSJobResult ,
26- QaaSJobData ,
27- QaaSJobClientData ,
28- QaaSCircuitData ,
29- QaaSJobRunData ,
30- QaaSJobBackendData ,
31- QaaSCircuitSerializationFormat ,
3232)
3333
34- from .versions import USER_AGENT
34+ from qiskit_scaleway .versions import USER_AGENT
3535
3636
3737class ScalewaySession (cirq .work .Sampler ):
@@ -173,24 +173,17 @@ def run_sweep(
173173
174174 for param_resolver in cirq .study .to_resolvers (params ):
175175 circuit = cirq .protocols .resolve_parameters (program , param_resolver )
176- serialized_circuit = cirq .to_json (circuit )
177-
178- run_opts = QaaSJobRunData (
179- options = {"shots" : repetitions },
180- circuits = [
181- QaaSCircuitData (
182- serialization_format = QaaSCircuitSerializationFormat .JSON ,
183- circuit_serialization = serialized_circuit ,
184- )
185- ],
186- )
187-
188- results = self ._submit (run_opts , self .__id )
176+
177+ program = QuantumProgram .from_cirq_circuit (circuit )
178+
179+ results = self ._submit (program , repetitions , self .__id )
189180 trial_results .append (results )
190181
191182 return trial_results
192183
193- def _extract_payload_from_response (self , job_result : QaaSJobResult ) -> str :
184+ def _extract_payload_from_response (
185+ self , job_result : QaaSJobResult
186+ ) -> QuantumProgramResult :
194187 result = job_result .result
195188
196189 if result is None or result == "" :
@@ -199,12 +192,11 @@ def _extract_payload_from_response(self, job_result: QaaSJobResult) -> str:
199192 if url is not None :
200193 resp = httpx .get (url )
201194 resp .raise_for_status ()
202-
203- return resp .text
195+ result = resp .text
204196 else :
205- raise Exception ("Got result with both empty data and url fields" )
206- else :
207- return result
197+ raise RuntimeError ("Got result with empty data and url fields" )
198+
199+ return QuantumProgramResult . from_json_str ( result )
208200
209201 def _wait_for_result (
210202 self , job_id : str , timeout : Optional [int ] = None , fetch_interval : int = 2
@@ -227,37 +219,46 @@ def _wait_for_result(
227219 if job .status in ["error" , "unknown_status" ]:
228220 raise Exception ("Job error" )
229221
230- def _to_cirq_result (self , job_results : List [QaaSJobResult ]) -> cirq .Result :
231- if len (job_results ) == 0 :
232- raise Exception ("Empty result list" )
233-
234- payload = self ._extract_payload_from_response (job_results [0 ])
235- payload_dict = json .loads (payload )
236- cirq_result = ResultDict ._from_json_dict_ (** payload_dict )
237-
238- return cirq_result
239-
240- def _submit (self , run_opts : QaaSJobRunData , session_id : str ) -> cirq .study .Result :
241- data = QaaSJobData .schema ().dumps (
242- QaaSJobData (
243- backend = QaaSJobBackendData (
244- name = self .__device .name , version = self .__device .version , options = {}
245- ),
246- client = QaaSJobClientData (user_agent = USER_AGENT ),
247- run = run_opts ,
248- )
222+ def _submit (
223+ self , program : QuantumProgram , shots : int , session_id : str
224+ ) -> cirq .study .Result :
225+ backend_data = BackendData (
226+ name = self .__device .name ,
227+ version = self .__device .version ,
228+ )
229+
230+ client_data = ClientData (
231+ user_agent = USER_AGENT ,
249232 )
250233
234+ computation_model_dict = QuantumComputationModel (
235+ programs = [program ],
236+ backend = backend_data ,
237+ client = client_data ,
238+ ).to_dict ()
239+
240+ computation_parameters_dict = QuantumComputationParameters (
241+ shots = shots ,
242+ ).to_dict ()
243+
251244 model = self .__client .create_model (
252- payload = data ,
245+ payload = computation_model_dict ,
253246 )
254247
255248 if not model :
256249 raise RuntimeError ("Failed to push circuit data" )
257250
258- job_id = self .__client .create_job (session_id = session_id , model_id = model .id ).id
251+ job_id = self .__client .create_job (
252+ session_id = session_id ,
253+ model_id = model .id ,
254+ parameters = computation_parameters_dict ,
255+ ).id
256+
257+ job_results = self ._wait_for_result (job_id , 60 * 100 , 2 )
258+
259+ if len (job_results ) != 1 :
260+ raise Exception ("Expected a single result for Cirq job" )
259261
260- job_results = self ._wait_for_result (job_id , 60 * 10 , 2 )
261- result = self ._to_cirq_result (job_results )
262+ result = self ._extract_payload_from_response (job_results [0 ]).to_cirq_result ()
262263
263264 return result
0 commit comments