Skip to content

Commit 2510872

Browse files
committed
feat(client): wip models
1 parent 4e8a099 commit 2510872

File tree

4 files changed

+106
-140
lines changed

4 files changed

+106
-140
lines changed

README.md

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,39 @@ pip install scaleway-qaas-client
1212

1313
## Getting started
1414

15-
To instantiate the ScalewayProvider, you need to have an access token and a project_id
15+
To use QaaS client, you need to have an access secret_key and a Scaleway's project_id
16+
1617

1718
```python
18-
from qiskit import QuantumCircuit
19-
from qiskit_scaleway import ScalewayProvider
19+
from scaleway_qaas_client import QaaSClient
2020

21-
provider = ScalewayProvider(
22-
project_id="<your-scaleway-project-id>",
23-
secret_key="<your-scaleway-secret-key>",
21+
client = QaaSClient(
22+
project_id=os.environ["SCALEWAY_PROJECT_ID"],
23+
secret_key=os.environ["SCALEWAY_API_TOKEN"],
2424
)
25-
```
2625

27-
Alternatively, the Scaleway Provider can discover your access token from environment variables or from your .env file
26+
platforms = client.list_platforms(name="aer_simulation_pop_c16m128")
2827

29-
```
30-
export QISKIT_SCALEWAY_PROJECT_ID="project_id"
31-
export QISKIT_SCALEWAY_API_TOKEN="token"
32-
```
28+
assert platforms is not None
29+
assert len(platforms) == 1
3330

34-
Then you can instantiate the provider without any arguments:
31+
target_platform = platforms[0]
3532

36-
```python
37-
from qiskit import QuantumCircuit
38-
from qiskit_scaleway import ScalewayProvider
33+
assert target_platform.id is not None
3934

40-
provider = ScalewayProvider()
41-
```
35+
session = client.create_session(platform_id=target_platform.id, max_duration="2min", max_idle_duration="2min")
4236

43-
Now you can have acces to the supported backends:
37+
assert session is not None
38+
assert session.id is not None
39+
assert session.platform_id == target_platform.id
4440

41+
while session.status == "starting":
42+
session = client.get_session(session.id)
43+
time.sleep(3)
4544

46-
```python
47-
# List all operational backends
48-
backends = provider.backends(operational=True)
49-
print(backends)
50-
51-
# List all backends with a minimum number of qbits
52-
backends = provider.backends(min_num_qubits=35)
53-
print(backends)
54-
55-
# Retrieve a backend by providing search criteria. The search must have a single match
56-
backend = provider.get_backend("aer_simulation_h100")
57-
```
58-
59-
Define a quantum circuit and run it
60-
61-
```python
62-
# Define a quantum circuit that produces a 4-qubit GHZ state.
63-
qc = QuantumCircuit(4)
64-
qc.h(0)
65-
qc.cx(0, 1)
66-
qc.cx(0, 2)
67-
qc.cx(0, 3)
68-
qc.measure_all()
69-
70-
# Create and send a job to a new QPU's session (or on an existing one)
71-
result = backend.run(qc, method="statevector", shots=1000).result()
72-
73-
if result.success:
74-
print(result.get_counts())
75-
else:
76-
print(result.to_dict()["error"])
45+
assert session.status == "running"
7746

47+
client.delete_session(session.id)
7848
```
7949

8050
## Development

scaleway_qaas_client/client.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
2+
import randomname
23

3-
from typing import List, Optional, Dict
4+
from typing import List, Optional, Dict, Union
45

56
from quantum_as_a_service_api_client.models import (
67
CreateJobBody,
@@ -14,25 +15,28 @@
1415
)
1516

1617
from quantum_as_a_service_api_client.api.sessions.create_session import (
17-
sync as create_session_sync,
18+
sync as _create_session_sync,
19+
)
20+
from quantum_as_a_service_api_client.api.sessions.get_session import (
21+
sync as _get_session_sync,
1822
)
1923
from quantum_as_a_service_api_client.api.sessions.terminate_session import (
20-
sync as terminate_session_sync,
24+
sync as _terminate_session_sync,
2125
)
2226
from quantum_as_a_service_api_client.api.sessions.delete_session import (
23-
sync_detailed as delete_session_sync,
27+
sync_detailed as _delete_session_sync,
2428
)
2529
from quantum_as_a_service_api_client.api.platforms.list_platforms import (
26-
sync as list_platforms_sync,
30+
sync as _list_platforms_sync,
2731
)
2832
from quantum_as_a_service_api_client.api.jobs.create_job import (
29-
sync as create_job_sync,
33+
sync as _create_job_sync,
3034
)
3135
from quantum_as_a_service_api_client.api.jobs.get_job import (
32-
sync as get_job_sync,
36+
sync as _get_job_sync,
3337
)
3438
from quantum_as_a_service_api_client.api.jobs.list_job_results import (
35-
sync as list_job_result_sync,
39+
sync as _list_job_result_sync,
3640
)
3741

3842
from quantum_as_a_service_api_client.client import Client
@@ -42,33 +46,34 @@
4246

4347

4448
class QaaSClient:
45-
def __init__(self, project_id: str, token: str, url: str = __DEFAULT_URL):
46-
self.__token = token
49+
def __init__(self, project_id: str, secret_key: str, url: str = __DEFAULT_URL):
4750
self.__project_id = project_id
4851

4952
self.__client = Client(
50-
headers={"X-Auth-Token": self.__token},
51-
base_url=self.__url,
53+
headers={"X-Auth-Token": secret_key},
54+
base_url=url,
5255
timeout=10.0,
5356
verify_ssl="https" in url,
5457
)
5558

5659
def list_platforms(self, name: Optional[str]) -> List[ScalewayQaasV1Alpha1Platform]:
57-
response = list_platforms_sync(client=self.__client, name=name)
60+
response = _list_platforms_sync(client=self.__client, name=name)
5861

5962
assert response
6063

6164
return response.platforms
6265

6366
def create_session(
6467
self,
65-
name: str,
6668
platform_id: str,
67-
deduplication_id: str,
6869
max_duration: str,
6970
max_idle_duration: str,
71+
deduplication_id: Optional[str] = None,
72+
name: Optional[str] = None,
7073
) -> ScalewayQaasV1Alpha1Session:
71-
session = create_session_sync(
74+
name = name if name else f"qs-{randomname.get_name()}"
75+
76+
session = _create_session_sync(
7277
client=self.__client,
7378
body=CreateSessionBody(
7479
project_id=self.__project_id,
@@ -82,8 +87,13 @@ def create_session(
8287

8388
return session
8489

90+
def get_session(self, session_id: str) -> ScalewayQaasV1Alpha1Session:
91+
session = _get_session_sync(client=self.__client, session_id=session_id)
92+
93+
return session
94+
8595
def terminate_session(self, session_id: str) -> ScalewayQaasV1Alpha1Session:
86-
session = terminate_session_sync(
96+
session = _terminate_session_sync(
8797
client=self.__client,
8898
body=TerminateSessionBody(
8999
session_id=session_id,
@@ -93,30 +103,34 @@ def terminate_session(self, session_id: str) -> ScalewayQaasV1Alpha1Session:
93103
return session
94104

95105
def delete_session(self, session_id: str):
96-
delete_session_sync(client=self.__client, session_id=session_id)
106+
_delete_session_sync(client=self.__client, session_id=session_id)
97107

98108
def create_job(
99-
self, name: str, session_id: str, circuits: Dict
109+
self,
110+
session_id: str,
111+
payload: Union[Dict, List, str],
112+
name: Optional[str] = None,
100113
) -> ScalewayQaasV1Alpha1Job:
101-
circuits = circuits if isinstance(circuits, str) else json.dumps(circuits)
114+
payload = payload if isinstance(payload, str) else json.dumps(payload)
115+
name = name if name else f"qj-{randomname.get_name()}"
102116

103-
job = create_job_sync(
117+
job = _create_job_sync(
104118
client=self.__client,
105119
body=CreateJobBody(
106120
name=name,
107121
session_id=session_id,
108-
circuit=CreateJobBodyCircuit(qiskit_circuit=circuits),
122+
circuit=CreateJobBodyCircuit(qiskit_circuit=payload),
109123
),
110124
)
111125

112126
return job
113127

114128
def get_job(self, job_id: str) -> ScalewayQaasV1Alpha1Job:
115-
job = get_job_sync(client=self.__client, job_id=job_id)
129+
job = _get_job_sync(client=self.__client, job_id=job_id)
116130

117131
return job
118132

119133
def list_job_results(self, job_id: str) -> List[ScalewayQaasV1Alpha1JobResult]:
120-
response = list_job_result_sync(client=self.__client, job_id=job_id)
134+
response = _list_job_result_sync(client=self.__client, job_id=job_id)
121135

122136
return response.job_results

scaleway_qaas_client/models.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,50 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
from enum import Enum
15-
from typing import List
15+
from typing import List, Dict
1616

1717
from dataclasses import dataclass
1818
from dataclasses_json import dataclass_json
1919

2020

2121
class SerializationType(Enum):
22-
UNKOWN = 0
22+
UNKOWN_SERIALIZATION = 0
2323
QASM_V1 = 1
2424
QASM_V2 = 2
2525
QASM_V3 = 3
26-
JSON = 4
2726

2827

2928
@dataclass_json
3029
@dataclass
31-
class CircuitPayload:
30+
class CircuitData:
3231
serialization_type: SerializationType
3332
circuit_serialization: str
3433

3534

3635
@dataclass_json
3736
@dataclass
38-
class RunPayload:
39-
circuits: List[CircuitPayload]
40-
options: dict
37+
class RunData:
38+
circuits: List[CircuitData]
39+
options: Dict
4140

4241

4342
@dataclass_json
4443
@dataclass
45-
class BackendPayload:
44+
class BackendData:
4645
name: str
4746
version: str
48-
options: dict
47+
options: Dict
4948

5049

5150
@dataclass_json
5251
@dataclass
53-
class ClientPayload:
52+
class ClientData:
5453
user_agent: str
5554

5655

5756
@dataclass_json
5857
@dataclass
59-
class JobPayload:
60-
client: ClientPayload
61-
backend: BackendPayload
62-
run: RunPayload
58+
class JobData:
59+
client: ClientData
60+
backend: BackendData
61+
run: RunData

0 commit comments

Comments
 (0)