|
15 | 15 | "Here is the function:\n", |
16 | 16 | "\n", |
17 | 17 | "```python\n", |
| 18 | + "\"\"\"function with arguments for jupyter notebook.\"\"\"\n", |
18 | 19 | "from qiskit import QuantumCircuit\n", |
19 | | - "from qiskit.providers import BackendV2\n", |
| 20 | + "from qiskit.providers.exceptions import QiskitBackendNotFoundError\n", |
20 | 21 | "from qiskit.transpiler import generate_preset_pass_manager\n", |
21 | 22 | "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", |
22 | 25 | "from qiskit_ibm_runtime import SamplerV2 as Sampler\n", |
23 | 26 | "\n", |
24 | 27 | "\n", |
|
27 | 30 | "\n", |
28 | 31 | "# get specific argument that we are interested in\n", |
29 | 32 | "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", |
31 | 35 | "\n", |
32 | 36 | "# 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", |
35 | 51 | "\n", |
36 | 52 | "# matching our run to the backend argument\n", |
37 | 53 | "sampler = Sampler(backend)\n", |
|
41 | 57 | "# running the circuit\n", |
42 | 58 | "quasi_dists = sampler.run([isa_circuit]).result()[0].data.meas.get_counts()\n", |
43 | 59 | "\n", |
44 | | - "print(f\"Quasi distribution: {quasi_dists}\")\n", |
45 | | - "\n", |
46 | 60 | "# saving results of the execution\n", |
47 | 61 | "save_result({\n", |
48 | 62 | " \"quasi_dists\": quasi_dists\n", |
49 | 63 | "})\n", |
| 64 | + "\n", |
50 | 65 | "```\n", |
51 | 66 | "\n", |
52 | 67 | "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 | 77 | { |
63 | 78 | "cell_type": "code", |
64 | 79 | "execution_count": 1, |
65 | | - "metadata": {}, |
| 80 | + "metadata": { |
| 81 | + "scrolled": true |
| 82 | + }, |
66 | 83 | "outputs": [ |
67 | 84 | { |
68 | 85 | "data": { |
|
103 | 120 | { |
104 | 121 | "cell_type": "markdown", |
105 | 122 | "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 | + ] |
107 | 157 | }, |
108 | 158 | { |
109 | 159 | "cell_type": "code", |
110 | | - "execution_count": null, |
| 160 | + "execution_count": 3, |
111 | 161 | "metadata": {}, |
112 | 162 | "outputs": [], |
113 | 163 | "source": [ |
114 | 164 | "from qiskit_ibm_runtime.fake_provider import FakeVigoV2\n", |
115 | 165 | "\n", |
116 | | - "backend = FakeVigoV2()" |
| 166 | + "backend_name = FakeVigoV2().name" |
117 | 167 | ] |
118 | 168 | }, |
119 | 169 | { |
|
132 | 182 | }, |
133 | 183 | { |
134 | 184 | "cell_type": "code", |
135 | | - "execution_count": null, |
| 185 | + "execution_count": 4, |
136 | 186 | "metadata": {}, |
137 | 187 | "outputs": [ |
138 | 188 | { |
|
141 | 191 | "<gateway-client>" |
142 | 192 | ] |
143 | 193 | }, |
144 | | - "execution_count": 2, |
| 194 | + "execution_count": 4, |
145 | 195 | "metadata": {}, |
146 | 196 | "output_type": "execute_result" |
147 | 197 | } |
|
152 | 202 | "\n", |
153 | 203 | "client = ServerlessClient(\n", |
154 | 204 | " token=os.environ.get(\"GATEWAY_TOKEN\", \"awesome_token\"),\n", |
| 205 | + " instance=os.environ.get(\"GATEWAY_INSTANCE\", \"an_awesome_crn\"),\n", |
155 | 206 | " host=os.environ.get(\"GATEWAY_HOST\", \"http://localhost:8000\"),\n", |
156 | 207 | " # If you are using the kubernetes approach the URL must be http://localhost\n", |
157 | 208 | ")\n", |
|
160 | 211 | }, |
161 | 212 | { |
162 | 213 | "cell_type": "code", |
163 | | - "execution_count": 3, |
| 214 | + "execution_count": 5, |
164 | 215 | "metadata": {}, |
165 | 216 | "outputs": [ |
166 | 217 | { |
|
169 | 220 | "QiskitFunction(function-with-arguments)" |
170 | 221 | ] |
171 | 222 | }, |
172 | | - "execution_count": 3, |
| 223 | + "execution_count": 5, |
173 | 224 | "metadata": {}, |
174 | 225 | "output_type": "execute_result" |
175 | 226 | } |
|
195 | 246 | }, |
196 | 247 | { |
197 | 248 | "cell_type": "code", |
198 | | - "execution_count": 4, |
| 249 | + "execution_count": 6, |
199 | 250 | "metadata": {}, |
200 | 251 | "outputs": [ |
201 | 252 | { |
|
204 | 255 | "QiskitFunction(function-with-arguments)" |
205 | 256 | ] |
206 | 257 | }, |
207 | | - "execution_count": 4, |
| 258 | + "execution_count": 6, |
208 | 259 | "metadata": {}, |
209 | 260 | "output_type": "execute_result" |
210 | 261 | } |
|
216 | 267 | }, |
217 | 268 | { |
218 | 269 | "cell_type": "code", |
219 | | - "execution_count": 5, |
| 270 | + "execution_count": 7, |
220 | 271 | "metadata": {}, |
221 | 272 | "outputs": [ |
222 | 273 | { |
223 | 274 | "data": { |
224 | 275 | "text/plain": [ |
225 | | - "<Job | a2cfbcdc-f503-4be3-9bcf-8914ee110bb2>" |
| 276 | + "<Job | 8abf9e9f-42ff-4b5b-a395-71aadb009bd8>" |
226 | 277 | ] |
227 | 278 | }, |
228 | | - "execution_count": 5, |
| 279 | + "execution_count": 7, |
229 | 280 | "metadata": {}, |
230 | 281 | "output_type": "execute_result" |
231 | 282 | } |
232 | 283 | ], |
233 | 284 | "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", |
235 | 286 | "job" |
236 | 287 | ] |
237 | 288 | }, |
|
244 | 295 | }, |
245 | 296 | { |
246 | 297 | "cell_type": "code", |
247 | | - "execution_count": 6, |
| 298 | + "execution_count": 8, |
248 | 299 | "metadata": {}, |
249 | 300 | "outputs": [ |
250 | 301 | { |
251 | 302 | "data": { |
252 | 303 | "text/plain": [ |
253 | | - "{'quasi_dists': {'11': 524, '00': 500}}" |
| 304 | + "{'quasi_dists': {'11': 504, '00': 520}}" |
254 | 305 | ] |
255 | 306 | }, |
256 | | - "execution_count": 6, |
| 307 | + "execution_count": 8, |
257 | 308 | "metadata": {}, |
258 | 309 | "output_type": "execute_result" |
259 | 310 | } |
260 | 311 | ], |
261 | 312 | "source": [ |
262 | 313 | "job.result()" |
263 | 314 | ] |
| 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 | + ] |
264 | 333 | } |
265 | 334 | ], |
266 | 335 | "metadata": { |
267 | 336 | "kernelspec": { |
268 | | - "display_name": "Python 3 (ipykernel)", |
| 337 | + "display_name": "Python (qiskit_serverless test venv)", |
269 | 338 | "language": "python", |
270 | | - "name": "python3" |
| 339 | + "name": "qiskit_serverless_test_venv" |
271 | 340 | }, |
272 | 341 | "language_info": { |
273 | 342 | "codemirror_mode": { |
|
0 commit comments