Skip to content

Commit 4ac3236

Browse files
committed
Made things working with service
1 parent e47e5ff commit 4ac3236

File tree

6 files changed

+207
-66
lines changed

6 files changed

+207
-66
lines changed

docs/getting_started/basic/01_running_program.ipynb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"source": [
9393
"client = ServerlessClient(\n",
9494
" token=os.environ.get(\"GATEWAY_TOKEN\", \"awesome_token\"),\n",
95+
" instance=os.environ.get(\"GATEWAY_INSTANCE\", \"an_awesome_crn\"),\n",
9596
" host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost:8000\"),\n",
9697
" # If you are using the kubernetes approach the URL must be http://localhost\n",
9798
")\n",
@@ -193,7 +194,7 @@
193194
{
194195
"data": {
195196
"text/plain": [
196-
"<Job | 860c98e2-ec3c-4197-8042-115ec3c6f5e8>"
197+
"<Job | 0deba5e4-a39e-4af4-a029-0dcdc7635e74>"
197198
]
198199
},
199200
"execution_count": 5,
@@ -216,17 +217,17 @@
216217
},
217218
{
218219
"cell_type": "code",
219-
"execution_count": 15,
220+
"execution_count": 6,
220221
"id": "cc7ccea6-bbae-4184-ba7f-67b6c20a0b0b",
221222
"metadata": {},
222223
"outputs": [
223224
{
224225
"data": {
225226
"text/plain": [
226-
"'DONE'"
227+
"'RUNNING'"
227228
]
228229
},
229-
"execution_count": 15,
230+
"execution_count": 6,
230231
"metadata": {},
231232
"output_type": "execute_result"
232233
}
@@ -245,17 +246,17 @@
245246
},
246247
{
247248
"cell_type": "code",
248-
"execution_count": 16,
249+
"execution_count": 7,
249250
"id": "ca05d063",
250251
"metadata": {},
251252
"outputs": [
252253
{
253254
"data": {
254255
"text/plain": [
255-
"{'00': 539, '11': 485}"
256+
"{'11': 489, '00': 535}"
256257
]
257258
},
258-
"execution_count": 16,
259+
"execution_count": 7,
259260
"metadata": {},
260261
"output_type": "execute_result"
261262
}
@@ -274,15 +275,15 @@
274275
},
275276
{
276277
"cell_type": "code",
277-
"execution_count": 17,
278+
"execution_count": 8,
278279
"id": "eb5ec85f",
279280
"metadata": {},
280281
"outputs": [
281282
{
282283
"name": "stdout",
283284
"output_type": "stream",
284285
"text": [
285-
"2024-12-05 17:26:40,785\tINFO job_manager.py:531 -- Runtime env is setting up.\n",
286+
"2025-10-23 17:12:05,481\tINFO job_manager.py:531 -- Runtime env is setting up.\n",
286287
"Running function...\n",
287288
"Completed running function.\n",
288289
"\n"
@@ -314,9 +315,9 @@
314315
],
315316
"metadata": {
316317
"kernelspec": {
317-
"display_name": "Python 3 (ipykernel)",
318+
"display_name": "Python (qiskit_serverless test venv)",
318319
"language": "python",
319-
"name": "python3"
320+
"name": "qiskit_serverless_test_venv"
320321
},
321322
"language_info": {
322323
"codemirror_mode": {

docs/getting_started/basic/02_arguments_and_results.ipynb

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
"Here is the function:\n",
1616
"\n",
1717
"```python\n",
18+
"\"\"\"function with arguments for jupyter notebook.\"\"\"\n",
1819
"from qiskit import QuantumCircuit\n",
19-
"from qiskit.providers import BackendV2\n",
20+
"from qiskit.providers.exceptions import QiskitBackendNotFoundError\n",
2021
"from qiskit.transpiler import generate_preset_pass_manager\n",
2122
"from qiskit_serverless import get_arguments, save_result\n",
23+
"from qiskit_ibm_runtime import QiskitRuntimeService\n",
24+
"from qiskit_ibm_runtime.fake_provider import FakeProviderForBackendV2\n",
2225
"from qiskit_ibm_runtime import SamplerV2 as Sampler\n",
2326
"\n",
2427
"\n",
@@ -27,11 +30,24 @@
2730
"\n",
2831
"# get specific argument that we are interested in\n",
2932
"circuit = arguments.get(\"circuit\")\n",
30-
"backend = arguments.get(\"backend\")\n",
33+
"backend_name = arguments.get(\"backend_name\")\n",
34+
"service = arguments.get(\"service\")\n",
3135
"\n",
3236
"# verifying arguments types\n",
33-
"assert isinstance(circuit, QuantumCircuit)\n",
34-
"assert isinstance(backend, BackendV2)\n",
37+
"if not isinstance(circuit, QuantumCircuit):\n",
38+
" raise ValueError(\"circuit must be QuantumCircuit.\")\n",
39+
"if not isinstance(backend_name, str):\n",
40+
" raise ValueError(\"backend_name must be str.\")\n",
41+
"\n",
42+
"if \"fake\" in backend_name.lower():\n",
43+
" service = FakeProviderForBackendV2()\n",
44+
"if isinstance(service, (FakeProviderForBackendV2, QiskitRuntimeService)):\n",
45+
" try:\n",
46+
" backend = service.backend(backend_name)\n",
47+
" except QiskitBackendNotFoundError as e:\n",
48+
" raise ValueError(f\"Error retrieving backend {backend_name}: {e}\") from e\n",
49+
"else:\n",
50+
" raise ValueError(f\"A service of type `QiskitRuntimeService` is required for using the backend named {backend_name}.\")\n",
3551
"\n",
3652
"# matching our run to the backend argument\n",
3753
"sampler = Sampler(backend)\n",
@@ -41,12 +57,11 @@
4157
"# running the circuit\n",
4258
"quasi_dists = sampler.run([isa_circuit]).result()[0].data.meas.get_counts()\n",
4359
"\n",
44-
"print(f\"Quasi distribution: {quasi_dists}\")\n",
45-
"\n",
4660
"# saving results of the execution\n",
4761
"save_result({\n",
4862
" \"quasi_dists\": quasi_dists\n",
4963
"})\n",
64+
"\n",
5065
"```\n",
5166
"\n",
5267
"As you can see, the circuit construction is not inside the function anymore. Instead, we parse the arguments by calling the [get_arguments](https://qiskit.github.io/qiskit-serverless/stubs/qiskit_serverless.serializers.get_arguments.html#qiskit_serverless.serializers.get_arguments) function."
@@ -62,7 +77,9 @@
6277
{
6378
"cell_type": "code",
6479
"execution_count": 1,
65-
"metadata": {},
80+
"metadata": {
81+
"scrolled": true
82+
},
6683
"outputs": [
6784
{
6885
"data": {
@@ -103,17 +120,50 @@
103120
{
104121
"cell_type": "markdown",
105122
"metadata": {},
106-
"source": "Then, we will instance our `BackendV2` object. We chose `FakeVigoV2` to be our backend for this example."
123+
"source": [
124+
"Our Function can run on either simulator or a real hardware. For real hardware usage, a service object is needed to connect and send the data to the IQP. Here is how we can do it"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 2,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"name": "stderr",
134+
"output_type": "stream",
135+
"text": [
136+
"management.get:WARNING:2025-10-30 12:34:31,507: Loading default saved account\n"
137+
]
138+
}
139+
],
140+
"source": [
141+
"from qiskit_ibm_runtime import QiskitRuntimeService\n",
142+
" \n",
143+
"service = QiskitRuntimeService(\n",
144+
" # channel=\"ibm_quantum_platform\",\n",
145+
" # token=\"API_KEY\",\n",
146+
" # instance=\"CRN\"\n",
147+
")\n",
148+
"backend_name = service.least_busy(simulator=False, operational=True)"
149+
]
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"metadata": {},
154+
"source": [
155+
"We decide to use a simulator as this is an example. Because of that we will write oer our backend name and service so our function will run over a simulator.In this ecample, we chose `FakeVigoV2` to be our backend:"
156+
]
107157
},
108158
{
109159
"cell_type": "code",
110-
"execution_count": null,
160+
"execution_count": 3,
111161
"metadata": {},
112162
"outputs": [],
113163
"source": [
114164
"from qiskit_ibm_runtime.fake_provider import FakeVigoV2\n",
115165
"\n",
116-
"backend = FakeVigoV2()"
166+
"backend_name = FakeVigoV2().name"
117167
]
118168
},
119169
{
@@ -132,7 +182,7 @@
132182
},
133183
{
134184
"cell_type": "code",
135-
"execution_count": null,
185+
"execution_count": 4,
136186
"metadata": {},
137187
"outputs": [
138188
{
@@ -141,7 +191,7 @@
141191
"<gateway-client>"
142192
]
143193
},
144-
"execution_count": 2,
194+
"execution_count": 4,
145195
"metadata": {},
146196
"output_type": "execute_result"
147197
}
@@ -152,6 +202,7 @@
152202
"\n",
153203
"client = ServerlessClient(\n",
154204
" token=os.environ.get(\"GATEWAY_TOKEN\", \"awesome_token\"),\n",
205+
" instance=os.environ.get(\"GATEWAY_INSTANCE\", \"an_awesome_crn\"),\n",
155206
" host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost:8000\"),\n",
156207
" # If you are using the kubernetes approach the URL must be http://localhost\n",
157208
")\n",
@@ -160,7 +211,7 @@
160211
},
161212
{
162213
"cell_type": "code",
163-
"execution_count": 3,
214+
"execution_count": 5,
164215
"metadata": {},
165216
"outputs": [
166217
{
@@ -169,7 +220,7 @@
169220
"QiskitFunction(function-with-arguments)"
170221
]
171222
},
172-
"execution_count": 3,
223+
"execution_count": 5,
173224
"metadata": {},
174225
"output_type": "execute_result"
175226
}
@@ -195,7 +246,7 @@
195246
},
196247
{
197248
"cell_type": "code",
198-
"execution_count": 4,
249+
"execution_count": 6,
199250
"metadata": {},
200251
"outputs": [
201252
{
@@ -204,7 +255,7 @@
204255
"QiskitFunction(function-with-arguments)"
205256
]
206257
},
207-
"execution_count": 4,
258+
"execution_count": 6,
208259
"metadata": {},
209260
"output_type": "execute_result"
210261
}
@@ -216,22 +267,22 @@
216267
},
217268
{
218269
"cell_type": "code",
219-
"execution_count": 5,
270+
"execution_count": 7,
220271
"metadata": {},
221272
"outputs": [
222273
{
223274
"data": {
224275
"text/plain": [
225-
"<Job | a2cfbcdc-f503-4be3-9bcf-8914ee110bb2>"
276+
"<Job | 8abf9e9f-42ff-4b5b-a395-71aadb009bd8>"
226277
]
227278
},
228-
"execution_count": 5,
279+
"execution_count": 7,
229280
"metadata": {},
230281
"output_type": "execute_result"
231282
}
232283
],
233284
"source": [
234-
"job = my_function.run(circuit=circuit, backend=backend)\n",
285+
"job = my_function.run(circuit=circuit, backend_name=backend_name, service=service)\n",
235286
"job"
236287
]
237288
},
@@ -244,30 +295,48 @@
244295
},
245296
{
246297
"cell_type": "code",
247-
"execution_count": 6,
298+
"execution_count": 8,
248299
"metadata": {},
249300
"outputs": [
250301
{
251302
"data": {
252303
"text/plain": [
253-
"{'quasi_dists': {'11': 524, '00': 500}}"
304+
"{'quasi_dists': {'11': 504, '00': 520}}"
254305
]
255306
},
256-
"execution_count": 6,
307+
"execution_count": 8,
257308
"metadata": {},
258309
"output_type": "execute_result"
259310
}
260311
],
261312
"source": [
262313
"job.result()"
263314
]
315+
},
316+
{
317+
"cell_type": "code",
318+
"execution_count": 9,
319+
"metadata": {},
320+
"outputs": [
321+
{
322+
"name": "stdout",
323+
"output_type": "stream",
324+
"text": [
325+
"2025-10-30 09:42:40,698\tINFO job_manager.py:531 -- Runtime env is setting up.\n",
326+
"\n"
327+
]
328+
}
329+
],
330+
"source": [
331+
"print(job.logs())"
332+
]
264333
}
265334
],
266335
"metadata": {
267336
"kernelspec": {
268-
"display_name": "Python 3 (ipykernel)",
337+
"display_name": "Python (qiskit_serverless test venv)",
269338
"language": "python",
270-
"name": "python3"
339+
"name": "qiskit_serverless_test_venv"
271340
},
272341
"language_info": {
273342
"codemirror_mode": {

0 commit comments

Comments
 (0)