{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 比特映射\n", "\n", "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.7.0rc1/resource/_static/logo_notebook.svg)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.7.0rc1/mindquantum/zh_cn/middle_level/mindspore_qubit_mapping.ipynb) \n", "[![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.7.0rc1/resource/_static/logo_download_code.svg)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.7.0rc1/mindquantum/zh_cn/middle_level/mindspore_qubit_mapping.py) \n", "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.7.0rc1/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/r2.7.0rc1/docs/mindquantum/docs/source_zh_cn/middle_level/qubit_mapping.ipynb)\n", "\n", "用户在设计量子线路时往往是根据自己的算法需求来进行设计,但是现阶段的量子芯片难以实现所有比特之间都有耦合。因此,在量子计算硬件上执行量子线路时,我们需要通过一定的算法来将量子算法中所用到的比特进行重新排列,或者引入一些 [SWAP](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.11/core/gates/mindquantum.core.gates.SWAPGate.html) 门,来将本来不能耦合的比特耦合起来。这也就是比特映射算法。\n", "\n", "在这篇教程中,我们将首先介绍构成量子芯片中的量子节点对象 [QubitNode](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/device/mindquantum.device.QubitNode.html#mindquantum.device.QubitNode) 和量子拓扑图对象 [QubitsTopology](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/device/mindquantum.device.QubitsTopology.html#mindquantum.device.QubitsTopology),然后将介绍如何利用比特映射算法来实现量子线路的编译。\n", "\n", "## Qubit Node\n", "\n", "[QubitNode](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/device/mindquantum.device.QubitNode.html#mindquantum.device.QubitNode)表示量子芯片中的比特,当前,主要包含四个属性:\n", "\n", "- qubit_id:量子比特的唯一识别码\n", "- color:在绘制拓扑结构图时,量子比特的颜色, 默认为 #000000\n", "- poi_x:在绘制拓扑结构图时,量子比特的 x 位置,默认为 0.0\n", "- poi_y:在绘制拓扑结构图时,量子比特的 y 位置,默认为 0.0\n", "\n", "下面,我们申明一个量子比特:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "qubit id: 0, with color: #121212, and position (0, 0)\n" ] } ], "source": [ "from mindquantum.device import QubitNode\n", "\n", "q0 = QubitNode(0, '#121212', 0, 0)\n", "\n", "print(f\"qubit id: {q0.qubit_id}, with color: {q0.color}, and position ({q0.poi_x}, {q0.poi_y})\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "量子比特之间可以通过运算符 `>>` 和 `<<` 来将两个比特连接起来,也可以同过 `>` 和 `<` 取消连接。不同方向的连接或者取消连接算符会有不同的返回值。\n", "\n", "|lhs|op|rhs|效果|返回值|\n", "| --|--|-- |-- |--|\n", "|q0 |`>>`|q1 |连接两个比特|q1|\n", "|q0 |`<<`|q1 |连接两个比特|q0|\n", "|q0 |`>`|q1 |取消连接两个比特|q1|\n", "|q0 |`<`|q1 |取消连接两个比特|q0|\n", "\n", "通过如上定义的操作,我们可以快速连接不同的比特,如:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q0, q1, q2 = QubitNode(0), QubitNode(1), QubitNode(2)\n", "\n", "q0 >> q1 >> q2" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "在上述代码中 `q0 >> q1` 会将 `q0` 和 `q1` 比特连接起来,并返回 `q1` 比特,而后再通过 `>> q2` 就可以将 `q1` 和 `q2` 连接起来。" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### QubitsTopology\n", "\n", "[QubitsTopology](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/device/mindquantum.device.QubitsTopology.html#mindquantum.device.QubitsTopology) 类是量子比特的容器,可以有效地组织和操控量子比特,例如当我们想要构建一个一维链的量子比特时,我们可以如下操作:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "from mindquantum.device import QubitsTopology, QubitNode\n", "\n", "n = 5\n", "\n", "topology = QubitsTopology([QubitNode(i, poi_x=i) for i in range(n)])\n", "print(topology)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "MindQuantum 中自带了绘制量子比特拓扑结构图的接口,接口如下:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 4 " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.io.display import draw_topology\n", "\n", "draw_topology(topology)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "由上我们可以发现,我们成功产生了 5 个量子比特,但是还没有将其连接起来,下面我们就来将其进行耦合。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 4 " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "left_node = topology[0]\n", "for i in range(1, n):\n", " left_node = left_node << topology[i]\n", "\n", "draw_topology(topology)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "在上述代码中,我们通过取值运算符 `[]`,获取对应 qubit_id 的量子比特,然后我们利用 `<<` 运算符进行比特之间的连接。\n", "\n", "在 MindQuantum 中,我们已经集成了多种结构,如这里提到的[一维链结构](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/device/mindquantum.device.LinearQubits.html#mindquantum.device.LinearQubits),和[方格子结构](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/device/mindquantum.device.GridQubits.html):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from mindquantum.device import LinearQubits, GridQubits\n", "from IPython.display import display_svg\n", "\n", "t1 = LinearQubits(3)\n", "t2 = GridQubits(4, 5)\n", "\n", "display_svg(draw_topology(t1))\n", "display_svg(draw_topology(t2))" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "我们还可以对拓扑结构图进行更多的操作,如删除某个节点:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2.remove_qubit_node(6)\n", "\n", "draw_topology(t2)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "孤立某个节点:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2.isolate_with_near(13)\n", "\n", "draw_topology(t2)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "修改节点颜色:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2.set_color(7, '#ff0000')\n", "\n", "draw_topology(t2)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "修改节点位置:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2.set_position(4, 5.0, 0.5)\n", "\n", "draw_topology(t2)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "重新耦合比特:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2[4] << t2[14]\n", "\n", "draw_topology(t2)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "我们还可以获取拓扑结构中所有的耦合边:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{(0, 1),\n", " (0, 5),\n", " (1, 2),\n", " (2, 3),\n", " (2, 7),\n", " (3, 4),\n", " (3, 8),\n", " (4, 9),\n", " (4, 14),\n", " (5, 10),\n", " (7, 8),\n", " (7, 12),\n", " (8, 9),\n", " (9, 14),\n", " (10, 11),\n", " (10, 15),\n", " (11, 12),\n", " (11, 16),\n", " (12, 17),\n", " (14, 19),\n", " (15, 16),\n", " (16, 17),\n", " (17, 18),\n", " (18, 19)}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t2.edges_with_id()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Qubit Mapping\n", "\n", "当我们在真实量子硬件上运行量子线路时,我们往往不能直接运行,因为真实硬件中的比特拓扑关系和用户给定的量子线路结构并不一定匹配,这时比特映射技术就应运而生。我们可以通过对量子比特进行重新映射,或插入一下 [SWAP](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/core/mindquantum.core.gates.html#%E9%A2%84%E5%AE%9E%E4%BE%8B%E5%8C%96%E9%97%A8) 门,来使得线路能够成功运行。\n", "\n", "我们给定一个量子线路:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "q0: q1: q2: RX a RX b RX c " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.core.circuit import Circuit\n", "\n", "circ = Circuit().rx('a', 0).rx('b', 1).rx('c', 2).x(1, 0).x(2, 1).x(0, 2)\n", "circ.svg()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "再给定一个 2x2 的方格子量子拓扑结构:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = GridQubits(2, 2)\n", "\n", "draw_topology(t)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "由于上述量子线路中的量子比特两两都有相互作用,因此无法直接在上述的量子硬件中运行。我们引入 [SABER](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/algorithm/mapping/mindquantum.algorithm.mapping.SABRE.html#mindquantum.algorithm.mapping.SABRE) 算法解决该问题。更多算法细节,请参考论文:[Tackling the Qubit Mapping Problem for NISQ-Era Quantum Devices](https://arxiv.org/abs/1809.02573)。" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "from mindquantum.algorithm.mapping import SABRE\n", "\n", "solver = SABRE(circ, t)\n", "new_circ, init_mapping, final_mapping = solver.solve(5, 0.5, 0.3, 0.2)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "我们可以看到新给出的量子线路为:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "q0: q1: q2: q3: RX a RX b RX c " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_circ.svg()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "通过对比旧的量子线路,我们得知,算法重新分配的比特编号,并在合适的位置加入了 [SWAP](https://mindspore.cn/mindquantum/docs/zh-CN/r0.11/core/mindquantum.core.gates.html#%E9%A2%84%E5%AE%9E%E4%BE%8B%E5%8C%96%E9%97%A8) 门,使得线路得以正常运行。" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "q0: q1: q2: RX a RX b RX c " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circ.svg()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "这里 `init_mapping` 和 `final_mapping` 分别告诉我们,最开始量子线路应该如何作用在量子硬件上,以及运行完线路后,各量子比特与原始线路中量子比特的对应关系。" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "initial mapping: [3, 2, 0, 1]\n", " final mapping: [1, 2, 0, 3]\n" ] } ], "source": [ "print(f\"initial mapping: {init_mapping}\")\n", "print(f\" final mapping: {final_mapping}\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "我们还可以通过 `draw_topology`,在量子拓扑图中绘制出新的量子线路用到了哪些量子比特以及其耦合的边。" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "0 1 2 3 " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "draw_topology(t, new_circ)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "我们发现,其中的4个量子比特和4条边全部都用上了。" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "
SoftwareVersion
mindquantum0.9.11
scipy1.10.1
numpy1.24.4
SystemInfo
Python3.8.17
OSLinux x86_64
Memory16.62 GB
CPU Max Thread16
DateTue Jan 2 16:54:34 2024
\n" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.utils.show_info import InfoTable\n", "InfoTable('mindquantum', 'scipy', 'numpy')" ] } ], "metadata": { "kernelspec": { "display_name": "MindSpore", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.17" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }