Skip to content

Commit 647e942

Browse files
Merge remote-tracking branch 'origin/add-emulators' into add-emulators
2 parents 3451f5d + 5d1bee1 commit 647e942

File tree

8 files changed

+47
-42
lines changed

8 files changed

+47
-42
lines changed

src/oqd_cloud/client.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515

16-
from typing import Literal, Optional, Sequence
16+
from typing import Optional, Sequence
17+
import urllib.request
1718

1819
import requests
19-
import json
2020
from oqd_core.backend.task import Task
2121
from pydantic import BaseModel, ConfigDict
2222

@@ -135,12 +135,7 @@ def connect(self, provider: Provider, username: str, password: str):
135135
# self.connect(self, self.provider)
136136
# pass
137137

138-
def submit_job(
139-
self,
140-
task: Task,
141-
backend: str,
142-
tags: str
143-
):
138+
def submit_job(self, task: Task, backend: str, tags: str):
144139
"""Submit a Task as an AnalogCircuit, DigitalCircuit, or AtomicCircuit to a backend."""
145140
response = requests.post(
146141
self.provider.job_submission_url(backend=backend),
@@ -167,6 +162,10 @@ def retrieve_job(self, job_id):
167162
job = Job.model_validate(response.json())
168163

169164
if response.status_code == 200:
165+
# download result file from temporary link
166+
with urllib.request.urlopen(job.result) as f:
167+
job.result = f.read().decode("utf-8")
168+
170169
self._jobs[job_id] = job
171170
return self.jobs[job_id]
172171

src/oqd_cloud/provider.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import requests
15+
import requests
1616
from oqd_cloud.server.model import Backends
1717

1818

@@ -28,17 +28,15 @@ def __init__(self, host: str = "http://localhost", port: int = 8000):
2828

2929
# get available backends
3030
self.backends = Backends(available=[])
31-
response = requests.get(
32-
self.url + "/available_backends"
33-
)
31+
response = requests.get(self.url + "/available_backends")
3432
backends = Backends.model_validate(response.json())
3533
if response.status_code == 200:
3634
self.backends = backends
3735

3836
@property
3937
def available_backends(self):
4038
return self.backends.available
41-
39+
4240
@property
4341
def registration_url(self):
4442
return self.url + "/auth/register"

src/oqd_cloud/server/jobqueue.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@
3939

4040
async def _report_success(job, connection, result, *args, **kwargs):
4141
async with asynccontextmanager(get_db)() as db:
42-
4342
save_obj(job, result)
4443
url = get_temp_link(job)
45-
status_update = dict(status="finished", result=url)
46-
# status_update = dict(status="finished", result=result.model_dump_json())
44+
status_update = dict(status="finished", result=url)
45+
# status_update = dict(status="finished", result=result.model_dump_json())
4746
query = await db.execute(select(JobInDB).filter(JobInDB.job_id == job.id))
4847
job_in_db = query.scalars().first()
4948
for k, v in status_update.items():

src/oqd_cloud/server/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
if __name__ == "__main__":
1616
import uvicorn
1717

18-
uvicorn.run("app:app", host="0.0.0.0", port=8007, log_level='debug')
18+
uvicorn.run("app:app", host="0.0.0.0", port=8007, log_level="debug")

src/oqd_cloud/server/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Job(BaseModel):
4545
result: Optional[str] = None
4646
user_id: str
4747
tags: Optional[str] = None
48-
48+
4949

5050
class Backends(BaseModel):
51-
available: Sequence[str]
51+
available: Sequence[str]

src/oqd_cloud/server/route/job.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Literal, Sequence, Optional, Annotated
15+
from typing import Literal, Annotated
1616

1717
from fastapi import APIRouter, HTTPException, Body
1818
from fastapi import status as http_status
1919

2020
########################################################################################
21-
import oqd_analog_emulator #.qutip_backend import QutipBackend
21+
import oqd_analog_emulator # .qutip_backend import QutipBackend
2222
import oqd_trical
2323
from oqd_core.backend.task import Task
2424
from rq.job import Callback
@@ -51,8 +51,8 @@
5151
@job_router.get("/available_backends")
5252
async def available_backends():
5353
return backends
54-
55-
54+
55+
5656
@job_router.post("/submit/{backend}", tags=["Job"])
5757
async def submit_job(
5858
backend: Literal[tuple(backends.available)],
@@ -61,7 +61,8 @@ async def submit_job(
6161
user: user_dependency,
6262
db: db_dependency,
6363
):
64-
print(f"Queueing {task} on server {backend} backend. {len(queue)} jobs in queue.")
64+
print(f"Queueing task on server {backend} backend. {len(queue)} jobs in queue.")
65+
# print(f"Queueing {task} on server {backend} backend. {len(queue)} jobs in queue.")
6566

6667
job = queue.enqueue(
6768
_backends[backend].run,

src/oqd_cloud/server/storage.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,48 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Literal
1615
from minio import Minio
1716
import os
1817
import io
1918
from datetime import timedelta
2019

21-
from oqd_cloud.server.database import JobInDB, get_db
20+
from oqd_cloud.server.database import JobInDB
2221

2322
########################################################################################
2423

2524
minio_client = Minio(
26-
f"{os.getenv("MINIO_ENDPOINT")}:9000",
25+
"localhost:9000",
2726
access_key=os.getenv("MINIO_ROOT_USER"),
2827
secret_key=os.getenv("MINIO_ROOT_PASSWORD"),
29-
secure=False
28+
secure=False,
3029
)
3130

3231
DEFAULT_MINIO_BUCKET = os.getenv("MINIO_DEFAULT_BUCKETS")
3332
RESULT_FILENAME = "result.json"
3433

34+
if not minio_client.bucket_exists(DEFAULT_MINIO_BUCKET):
35+
minio_client.make_bucket(DEFAULT_MINIO_BUCKET)
36+
print("Created bucket", DEFAULT_MINIO_BUCKET)
37+
else:
38+
print("Bucket", DEFAULT_MINIO_BUCKET, "already exists")
39+
40+
3541
def save_obj(job: JobInDB, result):
3642
# if the file is already saved, fput can be used
3743
# minio_client.fput_object(
3844
# BUCKET, destination_file, source_file,
3945
# )
40-
41-
# here we dump to json, todo: future version should dump to HDF5
42-
json_bytes = result.model_dump_json().encode('utf-8')
46+
47+
# here we dump to json, todo: future version should dump to HDF5
48+
json_bytes = result.model_dump_json().encode("utf-8")
4349
buffer = io.BytesIO(json_bytes)
4450

4551
minio_client.put_object(
46-
DEFAULT_MINIO_BUCKET,
47-
f"{job.id}/{RESULT_FILENAME}",
48-
data=buffer,
49-
length=len(json_bytes),
50-
content_type='application/json'
52+
DEFAULT_MINIO_BUCKET,
53+
f"{job.id}/{RESULT_FILENAME}",
54+
data=buffer,
55+
length=len(json_bytes),
56+
content_type="application/json",
5157
)
5258

5359
return

tests/test_client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
from oqd_core.interface.analog.operation import AnalogCircuit, AnalogGate
2424
from oqd_core.interface.analog.operator import PauliX, PauliZ
2525

26-
from oqd_core.interface.atomic.circuit import AtomicCircuit
2726

2827
from oqd_cloud.client import Client
2928
from oqd_cloud.provider import Provider
30-
from rich.pretty import pprint
29+
from rich.pretty import pprint
3130

3231
X = PauliX()
3332
Z = PauliZ()
@@ -67,20 +66,23 @@
6766
task.model_dump_json()
6867

6968

70-
#%%
69+
# %%
7170
client = Client()
7271
provider = Provider(port=8007)
7372
client.connect(provider=provider, username="ben", password="pwd")
7473
client.status_report
7574

76-
#%%
75+
# %%
7776
backends = provider.available_backends
7877
print(backends)
7978

8079
# %%
8180
print(client.jobs)
82-
job = client.submit_job(task=task, backend="oqd-analog-emulator-qutip", tags='a')
81+
job = client.submit_job(task=task, backend="oqd-analog-emulator-qutip", tags="a")
82+
pprint(job)
83+
84+
# %%
85+
job = client.retrieve_job(job_id=job.job_id)
8386
pprint(job)
8487

8588
# %%
86-
client.retrieve_job(job_id=job.job_id)

0 commit comments

Comments
 (0)