|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "cdf46a7a", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "!pip install quri-parts-oqtopus" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "id": "9c0a699a", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "# OQTOPUSの量子クラウドでジョブを実行する\n", |
| 19 | + "\n", |
| 20 | + "Welcome to QURI Parts OQTOPUS!\n", |
| 21 | + "\n", |
| 22 | + "This page explains how to install QURI Parts OQTOPUS and use it with basic operations." |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "id": "0396c2a7", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "# Sampling on OQTOPUS Cloud\n", |
| 31 | + "\n", |
| 32 | + "This section requires topics described in QURI Parts document ([Sampling simulation](https://quri-parts.qunasys.com/docs/tutorials/basics/sampler/)), so you need to read it before this section.\n", |
| 33 | + "\n", |
| 34 | + "In QURI Parts document ([Sampling simulation](https://quri-parts.qunasys.com/docs/tutorials/basics/sampler/)), it is described how to estimate expectation value of operators using sampling measurements on a quantum circuit simulator. Since QURI Parts is designed to be platform independent, you can execute almost the same code on a real quantum computer." |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "attachments": {}, |
| 39 | + "cell_type": "markdown", |
| 40 | + "id": "f0ff7af6", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "## Prerequisite\n", |
| 44 | + "\n", |
| 45 | + "**QURI Parts OQTOPUS** is a library that allows QURI Parts users to run quantum computers using the User API of OQTOPUS Cloud.\n", |
| 46 | + "\n", |
| 47 | + "- **Provides a Backend for QURI Parts**: QURI Parts users can execute quantum programs on quantum computers in OQTOPUS Cloud using the backend provided by QURI Parts OQTOPUS.\n", |
| 48 | + "- **Utilizes the User API of OQTOPUS Cloud**: QURI Parts OQTOPUS executes quantum programs via the User API of OQTOPUS Cloud and handles communication with the cloud. This allows users to run quantum computers without having to be aware of the communication protocols of OQTOPUS Cloud.\n", |
| 49 | + "\n", |
| 50 | + "The User API of OQTOPUS Cloud is a REST interface, and post requests of jobs include OpenQASM 3 format quantum circuits.\n", |
| 51 | + "However, this API currently supports only a subset of OpenQASM 3, such as basic gates and measurements.\n", |
| 52 | + "\n", |
| 53 | + "In order to use OQTOPUS Cloud, you need to sign up for OQTOPUS Cloud. Please contact OQTOPUS Cloud operator for information on how to sign up. In this section, instead, we perform quantum circuits on the OQTOPUS Cloud.\n", |
| 54 | + "\n", |
| 55 | + "You can install `quri-parts-oqtopus` as follows:" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "attachments": {}, |
| 60 | + "cell_type": "markdown", |
| 61 | + "id": "b6f5ddab", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "## Prepare ``~/.oqtopus`` configuration file\n", |
| 65 | + "\n", |
| 66 | + "Create a configuration file in path ``~/.oqtopus``.\n", |
| 67 | + "Replace ``<base URL>`` and ``<API token>`` with your settings for OQTOPUS Cloud server and execute the following code:" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": null, |
| 73 | + "id": "f1d44aaa", |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [], |
| 76 | + "source": [ |
| 77 | + "from pathlib import Path\n", |
| 78 | + "\n", |
| 79 | + "config = \"\"\"[default]\n", |
| 80 | + "url=<base URL>\n", |
| 81 | + "api_token=<API token>\n", |
| 82 | + "\"\"\"\n", |
| 83 | + "Path(\"~/.oqtopus\").expanduser().write_text(config)\n", |
| 84 | + "print(\"Configuration saved.\")" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "id": "8aaa543b", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "## Prepare a circuit\n", |
| 93 | + "\n", |
| 94 | + "As a preparation, we create a circuit to be sampled:" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "id": "89fd8970", |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "from quri_parts.circuit import QuantumCircuit\n", |
| 105 | + "\n", |
| 106 | + "circuit = QuantumCircuit(2)\n", |
| 107 | + "circuit.add_H_gate(0)\n", |
| 108 | + "circuit.add_CNOT_gate(0, 1)" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "attachments": {}, |
| 113 | + "cell_type": "markdown", |
| 114 | + "id": "ddbcded0", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "## OqtopusSamplingBackend\n", |
| 118 | + "\n", |
| 119 | + "In order to use a real device, you need to create a `OqtopusSamplingBackend` object. " |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": null, |
| 125 | + "id": "c6e85888", |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [], |
| 128 | + "source": [ |
| 129 | + "from quri_parts_oqtopus.backend import OqtopusSamplingBackend\n", |
| 130 | + "\n", |
| 131 | + "# Create a SamplingBackend with the device\n", |
| 132 | + "backend = OqtopusSamplingBackend()" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "id": "d552513b", |
| 138 | + "metadata": {}, |
| 139 | + "source": [ |
| 140 | + "The `OqtopusSamplingBackend` has `sample()` function, which returns a `OqtopusSamplingJob` object, and you can extract a result of the sampling job.\n", |
| 141 | + "Please replace `device_id` with the one used in your system." |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": null, |
| 147 | + "id": "e0e34af0", |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [], |
| 150 | + "source": [ |
| 151 | + "job = backend.sample(\n", |
| 152 | + " circuit,\n", |
| 153 | + " device_id=\"qulacs\",\n", |
| 154 | + " shots=1000,\n", |
| 155 | + ")\n", |
| 156 | + "print(job.job_id)\n", |
| 157 | + "\n", |
| 158 | + "result = job.result()\n", |
| 159 | + "print(result.counts)" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "markdown", |
| 164 | + "id": "c92513e7", |
| 165 | + "metadata": {}, |
| 166 | + "source": [ |
| 167 | + "## Sampling with transpiler\n", |
| 168 | + "\n", |
| 169 | + "Specify information about the transpiler in the `transpiler_info` parameter of the sample function as a `dict`.\n", |
| 170 | + "Set the name of transpiler library in `transpiler_lib` and the options to pass to the transpiler in `transpiler_options`.\n", |
| 171 | + "\n", |
| 172 | + "For example, to apply Qiskit's transpiler with `optimization_level=2`, specify it as follows:" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "id": "a51055a3", |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [], |
| 181 | + "source": [ |
| 182 | + "job = backend.sample(\n", |
| 183 | + " circuit,\n", |
| 184 | + " device_id=\"qulacs\",\n", |
| 185 | + " shots=1000,\n", |
| 186 | + " transpiler_info={\n", |
| 187 | + " \"transpiler_lib\": \"qiskit\",\n", |
| 188 | + " \"transpiler_options\": {\"optimization_level\": 2},\n", |
| 189 | + " },\n", |
| 190 | + ")\n", |
| 191 | + "\n", |
| 192 | + "result = job.result()\n", |
| 193 | + "print(result.counts)" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "markdown", |
| 198 | + "id": "6a75871c", |
| 199 | + "metadata": {}, |
| 200 | + "source": [ |
| 201 | + "To disable the transpiler, set `transpiler_lib` to `None`." |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "code", |
| 206 | + "execution_count": null, |
| 207 | + "id": "083521e5", |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [], |
| 210 | + "source": [ |
| 211 | + "import math\n", |
| 212 | + "\n", |
| 213 | + "circuit = QuantumCircuit(2)\n", |
| 214 | + "circuit.add_RZ_gate(0, math.pi / 2)\n", |
| 215 | + "circuit.add_SqrtX_gate(0)\n", |
| 216 | + "circuit.add_RZ_gate(0, math.pi / 2)\n", |
| 217 | + "circuit.add_CNOT_gate(0, 1)\n", |
| 218 | + "\n", |
| 219 | + "job = backend.sample(\n", |
| 220 | + " circuit,\n", |
| 221 | + " device_id=\"qulacs\",\n", |
| 222 | + " shots=1000,\n", |
| 223 | + " transpiler_info={\n", |
| 224 | + " \"transpiler_lib\": None,\n", |
| 225 | + " },\n", |
| 226 | + ")\n", |
| 227 | + "print(job.job_id)\n", |
| 228 | + "\n", |
| 229 | + "result = job.result()\n", |
| 230 | + "print(result.counts)" |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "attachments": {}, |
| 235 | + "cell_type": "markdown", |
| 236 | + "id": "99617015", |
| 237 | + "metadata": {}, |
| 238 | + "source": [ |
| 239 | + "## Sampler\n", |
| 240 | + "\n", |
| 241 | + "Instead of using the backend directly, you can create a `Sampler` from it.\n", |
| 242 | + "See the [document](https://quri-parts.qunasys.com/docs/tutorials/basics/sampler/) in QURI Parts document for more details." |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "markdown", |
| 247 | + "id": "adea03a2", |
| 248 | + "metadata": {}, |
| 249 | + "source": [ |
| 250 | + "# Estimation on OQTOPUS Cloud\n", |
| 251 | + "\n", |
| 252 | + "## OqtopusEstimationBackend\n", |
| 253 | + "\n", |
| 254 | + "In order to use a real device, you need to create a `OqtopusEstimationBackend` object. " |
| 255 | + ] |
| 256 | + }, |
| 257 | + { |
| 258 | + "cell_type": "code", |
| 259 | + "execution_count": null, |
| 260 | + "id": "8b386018", |
| 261 | + "metadata": {}, |
| 262 | + "outputs": [], |
| 263 | + "source": [ |
| 264 | + "from quri_parts_oqtopus.backend import OqtopusEstimationBackend\n", |
| 265 | + "\n", |
| 266 | + "# Create a OqtopusEstimationBackend with the device\n", |
| 267 | + "backend = OqtopusEstimationBackend()" |
| 268 | + ] |
| 269 | + }, |
| 270 | + { |
| 271 | + "cell_type": "code", |
| 272 | + "execution_count": null, |
| 273 | + "id": "482e65ac", |
| 274 | + "metadata": {}, |
| 275 | + "outputs": [], |
| 276 | + "source": [ |
| 277 | + "from quri_parts.circuit import QuantumCircuit\n", |
| 278 | + "from quri_parts.core.operator import Operator, pauli_label\n", |
| 279 | + "\n", |
| 280 | + "circuit = QuantumCircuit(2)\n", |
| 281 | + "circuit.add_H_gate(0)\n", |
| 282 | + "circuit.add_CNOT_gate(0, 1)\n", |
| 283 | + "\n", |
| 284 | + "operator = Operator({\n", |
| 285 | + " pauli_label(\"X0 X1\"): 1.0,\n", |
| 286 | + " pauli_label(\"Z0 Z1\"): 1.0,\n", |
| 287 | + "})\n", |
| 288 | + "\n", |
| 289 | + "job = backend.estimate(\n", |
| 290 | + " circuit,\n", |
| 291 | + " operator=operator,\n", |
| 292 | + " device_id=\"qulacs\",\n", |
| 293 | + " shots=10000,\n", |
| 294 | + ")\n", |
| 295 | + "print(job.job_id)\n", |
| 296 | + "\n", |
| 297 | + "result = job.result()\n", |
| 298 | + "print(result.exp_value)" |
| 299 | + ] |
| 300 | + }, |
| 301 | + { |
| 302 | + "cell_type": "markdown", |
| 303 | + "id": "be42f717", |
| 304 | + "metadata": {}, |
| 305 | + "source": [ |
| 306 | + "# QURI Parts OQTOPUS Examples\n", |
| 307 | + "\n", |
| 308 | + "If you would like to view more examples, please refer to the following URL.\n", |
| 309 | + "[https://github.com/oqtopus-team/quri-parts-oqtopus/tree/main/examples](https://github.com/oqtopus-team/quri-parts-oqtopus/tree/main/examples) \n", |
| 310 | + "\n", |
| 311 | + "`submit_job_sse.py`は量子コンピュータを長時間占有するため、今回のハンズオンでは実行しないでください。" |
| 312 | + ] |
| 313 | + }, |
| 314 | + { |
| 315 | + "cell_type": "markdown", |
| 316 | + "id": "397e5dbc-4aa5-40af-b084-2c50beb20ae7", |
| 317 | + "metadata": {}, |
| 318 | + "source": [ |
| 319 | + "# Qulacs Simulator\n", |
| 320 | + "\n", |
| 321 | + "Qulacs Simulatorを利用すると、ブラウザ上のGUIで量子回路を作成・実行できます。ただし、実機を実行するにはアカウントが必要です。 \n", |
| 322 | + "[https://qulacs-gui.github.io/qulacs-simulator/](https://qulacs-gui.github.io/qulacs-simulator/)" |
| 323 | + ] |
| 324 | + }, |
| 325 | + { |
| 326 | + "cell_type": "markdown", |
| 327 | + "id": "1b779be0", |
| 328 | + "metadata": {}, |
| 329 | + "source": [] |
| 330 | + } |
| 331 | + ], |
| 332 | + "metadata": { |
| 333 | + "kernelspec": { |
| 334 | + "display_name": "Python 3 (ipykernel)", |
| 335 | + "language": "python", |
| 336 | + "name": "python3" |
| 337 | + }, |
| 338 | + "language_info": { |
| 339 | + "codemirror_mode": { |
| 340 | + "name": "ipython", |
| 341 | + "version": 3 |
| 342 | + }, |
| 343 | + "file_extension": ".py", |
| 344 | + "mimetype": "text/x-python", |
| 345 | + "name": "python", |
| 346 | + "nbconvert_exporter": "python", |
| 347 | + "pygments_lexer": "ipython3", |
| 348 | + "version": "3.12.9" |
| 349 | + } |
| 350 | + }, |
| 351 | + "nbformat": 4, |
| 352 | + "nbformat_minor": 5 |
| 353 | +} |
0 commit comments