Skip to content

Commit e8431f0

Browse files
authored
Merge pull request #3 from scaleway/qio-upgrade
Qio upgrade
2 parents d7ec094 + a6a79fd commit e8431f0

File tree

5 files changed

+61
-56
lines changed

5 files changed

+61
-56
lines changed

cirq_scaleway/scaleway_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from scaleway_qaas_client.v1alpha1 import QaaSClient, QaaSPlatform
1919

20-
from .scaleway_session import ScalewaySession
20+
from cirq_scaleway.scaleway_session import ScalewaySession
2121

2222

2323
class ScalewayDevice(cirq.devices.Device):

cirq_scaleway/scaleway_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from scaleway_qaas_client.v1alpha1 import QaaSClient
1919

20-
from .scaleway_device import ScalewayDevice
20+
from cirq_scaleway.scaleway_device import ScalewayDevice
2121

2222

2323
class ScalewayQuantumService:

cirq_scaleway/scaleway_session.py

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import time
15-
import json
1615
import cirq
1716
import httpx
1817

1918
from typing import Union, Optional, List
2019

21-
from cirq.study import ResultDict
20+
from qio.core import (
21+
QuantumProgram,
22+
QuantumProgramSerializationFormat,
23+
QuantumProgramResult,
24+
QuantumComputationModel,
25+
QuantumComputationParameters,
26+
BackendData,
27+
ClientData,
28+
)
2229

2330
from scaleway_qaas_client.v1alpha1 import (
2431
QaaSClient,
2532
QaaSJobResult,
26-
QaaSJobData,
27-
QaaSJobClientData,
28-
QaaSCircuitData,
29-
QaaSJobRunData,
30-
QaaSJobBackendData,
31-
QaaSCircuitSerializationFormat,
3233
)
3334

34-
from .versions import USER_AGENT
35+
from cirq_scaleway.versions import USER_AGENT
3536

3637

3738
class ScalewaySession(cirq.work.Sampler):
@@ -173,24 +174,19 @@ def run_sweep(
173174

174175
for param_resolver in cirq.study.to_resolvers(params):
175176
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-
],
177+
178+
program = QuantumProgram.from_cirq_circuit(
179+
circuit, QuantumProgramSerializationFormat.CIRQ_CIRCUIT_JSON_V1
186180
)
187181

188-
results = self._submit(run_opts, self.__id)
182+
results = self._submit(program, repetitions, self.__id)
189183
trial_results.append(results)
190184

191185
return trial_results
192186

193-
def _extract_payload_from_response(self, job_result: QaaSJobResult) -> str:
187+
def _extract_payload_from_response(
188+
self, job_result: QaaSJobResult
189+
) -> QuantumProgramResult:
194190
result = job_result.result
195191

196192
if result is None or result == "":
@@ -199,12 +195,11 @@ def _extract_payload_from_response(self, job_result: QaaSJobResult) -> str:
199195
if url is not None:
200196
resp = httpx.get(url)
201197
resp.raise_for_status()
202-
203-
return resp.text
198+
result = resp.text
204199
else:
205-
raise Exception("Got result with both empty data and url fields")
206-
else:
207-
return result
200+
raise RuntimeError("Got result with empty data and url fields")
201+
202+
return QuantumProgramResult.from_json_str(result)
208203

209204
def _wait_for_result(
210205
self, job_id: str, timeout: Optional[int] = None, fetch_interval: int = 2
@@ -217,47 +212,56 @@ def _wait_for_result(
217212
elapsed = time.time() - start_time
218213

219214
if timeout is not None and elapsed >= timeout:
220-
raise Exception("Timed out waiting for result")
215+
raise RuntimeError("Timed out waiting for result")
221216

222217
job = self.__client.get_job(job_id)
223218

224219
if job.status == "completed":
225220
return self.__client.list_job_results(job_id)
226221

227222
if job.status in ["error", "unknown_status"]:
228-
raise Exception("Job error")
229-
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-
)
223+
raise RuntimeError(f"Job failed: {job.progress_message}")
224+
225+
def _submit(
226+
self, program: QuantumProgram, shots: int, session_id: str
227+
) -> cirq.study.Result:
228+
backend_data = BackendData(
229+
name=self.__device.name,
230+
version=self.__device.version,
231+
)
232+
233+
client_data = ClientData(
234+
user_agent=USER_AGENT,
249235
)
250236

237+
computation_model_json = QuantumComputationModel(
238+
programs=[program],
239+
backend=backend_data,
240+
client=client_data,
241+
).to_json_str()
242+
243+
computation_parameters_json = QuantumComputationParameters(
244+
shots=shots,
245+
).to_json_str()
246+
251247
model = self.__client.create_model(
252-
payload=data,
248+
payload=computation_model_json,
253249
)
254250

255251
if not model:
256252
raise RuntimeError("Failed to push circuit data")
257253

258-
job_id = self.__client.create_job(session_id=session_id, model_id=model.id).id
254+
job_id = self.__client.create_job(
255+
session_id=session_id,
256+
model_id=model.id,
257+
parameters=computation_parameters_json,
258+
).id
259+
260+
job_results = self._wait_for_result(job_id, 60 * 100, 2)
261+
262+
if len(job_results) != 1:
263+
raise RuntimeError("Expected a single result for Cirq job")
259264

260-
job_results = self._wait_for_result(job_id, 60 * 10, 2)
261-
result = self._to_cirq_result(job_results)
265+
result = self._extract_payload_from_response(job_results[0]).to_cirq_result()
262266

263267
return result

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pandas==2.3.1
22
cirq-core>=1.3.0
3-
scaleway-qaas-client>=0.1.16
3+
scaleway-qaas-client>=0.1.16
4+
qio>=0.1.12

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
setup(
2424
name="cirq_scaleway",
25-
version="0.1.9",
25+
version="0.1.10",
2626
project_urls={
2727
"Documentation": "https://www.scaleway.com/en/quantum-as-a-service/",
2828
"Source": "https://github.com/scaleway/cirq-scaleway",

0 commit comments

Comments
 (0)