{ "cells": [ { "cell_type": "markdown", "id": "681e955a", "metadata": {}, "source": [ "# 量子线路高阶操作\n", "\n", "[](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.2/mindquantum/zh_cn/beginner/mindspore_advanced_operations_of_quantum_circuit.ipynb) \n", "[](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/r2.2/mindquantum/zh_cn/beginner/mindspore_advanced_operations_of_quantum_circuit.py) \n", "[](https://gitee.com/mindspore/docs/blob/r2.2/docs/mindquantum/docs/source_zh_cn/beginner/advanced_operations_of_quantum_circuit.ipynb)\n", "[](https://authoring-modelarts-cnnorth4.huaweicloud.com/console/lab?share-url-b64=aHR0cHM6Ly9taW5kc3BvcmUtd2Vic2l0ZS5vYnMuY24tbm9ydGgtNC5teWh1YXdlaWNsb3VkLmNvbS9ub3RlYm9vay9yMi4yL21pbmRxdWFudHVtL3poX2NuL2JlZ2lubmVyL21pbmRzcG9yZV9hZHZhbmNlZF9vcGVyYXRpb25zX29mX3F1YW50dW1fY2lyY3VpdC5pcHluYg%3D%3D&imageid=73522c7f-6d98-43d8-b306-8ff374c7d3a7)\n", "\n", "在前面变分量子线路操作指导中介绍了[量子线路](https://mindspore.cn/mindquantum/docs/zh-CN/r0.9/beginner/parameterized_quantum_circuit.html#%E9%87%8F%E5%AD%90%E7%BA%BF%E8%B7%AF)的基本用法。接下来,我们将进一步探索MindSpore Quantum为量子线路定义的一些高阶操作的用法。\n", "\n", "## [mindquantum.core.circuit.controlled](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.controlled.html)\n", "\n", "[controlled](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.controlled.html) 方法用于对任意量子线路或量子算符添加控制量子比特(可以是多个)。\n", "\n", "例如,我们构建一个仅包含两个量子比特的量子线路,并通过 [controlled](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.controlled.html) 方法对其添加一个控制量子比特——$q_2$:" ] }, { "cell_type": "code", "execution_count": 1, "id": "c44755ab", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/2 </text><rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd62d9e2fa0>" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.algorithm.library import qft\n", "from mindquantum.core.circuit import controlled\n", "\n", "u1 = qft(range(2)) # 构建量子线路\n", "u1.svg()" ] }, { "cell_type": "code", "execution_count": 2, "id": "9330f35e", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"188.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"188.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><circle cx=\"92.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" /><circle cx=\"152.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/2 </text><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" /><circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190d5f70>" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u2 = controlled(u1)(2) # 对线路添加控制量子位q2,返回一个新的线路\n", "u2.svg()" ] }, { "cell_type": "markdown", "id": "4d63b2ef", "metadata": {}, "source": [ "实际上,[controlled()](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.controlled.html) 返回的是一个函数。例如:" ] }, { "cell_type": "code", "execution_count": 3, "id": "6144080f", "metadata": {}, "outputs": [], "source": [ "u3 = controlled(u1)" ] }, { "cell_type": "markdown", "id": "3c2a5acf", "metadata": {}, "source": [ "我们可通过调用u3来对初始线路u1添加控制量子位,和上面的效果是一样的:" ] }, { "cell_type": "code", "execution_count": 4, "id": "39a3af2d", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"188.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"188.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><circle cx=\"92.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" /><circle cx=\"152.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/2 </text><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" /><circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190f3220>" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u4 = u3(2)\n", "u4.svg()" ] }, { "cell_type": "markdown", "id": "5381a350", "metadata": {}, "source": [ "注意:添加的控制量子比特不能是线路中已存在的某个门的目标量子比特,否则会报错!\n", "\n", "另外,我们还可以对量子线路批量添加控制位。例如,在下面的示例中,我们对$q_0$和$q_1$分别添加控制位——$q_2$和$q_3$:" ] }, { "cell_type": "code", "execution_count": 5, "id": "bb5e74ff", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"248.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"248.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><circle cx=\"92.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" /><circle cx=\"92.8\" cy=\"220.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"220.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" /><circle cx=\"152.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"220.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/2 </text><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" /><circle cx=\"212.8\" cy=\"220.0\" r=\"4\" fill=\"#5e7ce0\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"220.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" /><circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><circle cx=\"272.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190e3160>" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u = controlled(qft)\n", "u = u([2, 3], [0, 1]) # 批量添加控制位\n", "u.svg()" ] }, { "cell_type": "markdown", "id": "f991b869", "metadata": {}, "source": [ "## [mindquantum.core.circuit.dagger](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.dagger.html)\n", "\n", "[dagger](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.dagger.html) 方法用于求量子线路或量子算符的厄米共轭。\n", "\n", "以下示例提供了两种进行 [dagger](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.dagger.html) 操作的方式:" ] }, { "cell_type": "code", "execution_count": 6, "id": "7d203008", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"516.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"516.8\" height=\"200.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"496.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"496.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"496.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/2 </text><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"212.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/4 </text><rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"332.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"332.8\" x2=\"332.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"312.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"332.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/2 </text><rect x=\"372.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"392.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><line x1=\"452.8\" x2=\"452.8\" y1=\"20.0\" y2=\"180.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"432.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 443.2 36.31384387633061 L 448.0 28.0 L 452.8 36.31384387633061 L 449.44 36.31384387633061 L 449.44 52.0 L 446.56 52.0 L 446.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 462.40000000000003 43.68615612366939 L 457.6 52.0 L 452.8 43.68615612366939 L 456.16 43.68615612366939 L 456.16 28.0 L 459.04 28.0 L 459.04 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"432.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 443.2 156.31384387633062 L 448.0 148.0 L 452.8 156.31384387633062 L 449.44 156.31384387633062 L 449.44 172.0 L 446.56 172.0 L 446.56 156.31384387633062 Z\" fill=\"#ffffff\" /><path d=\"M 462.40000000000003 163.68615612366938 L 457.6 172.0 L 452.8 163.68615612366938 L 456.16 163.68615612366938 L 456.16 148.0 L 459.04 148.0 L 459.04 163.68615612366938 Z\" fill=\"#ffffff\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190ff280>" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.core.circuit import dagger\n", "\n", "u1 = qft(range(3))\n", "u1.svg()" ] }, { "cell_type": "code", "execution_count": 7, "id": "1229c41f", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"516.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"516.8\" height=\"200.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"496.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"496.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"496.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"20.0\" y2=\"180.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"72.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 83.19999999999999 36.31384387633061 L 88.0 28.0 L 92.8 36.31384387633061 L 89.44 36.31384387633061 L 89.44 52.0 L 86.56 52.0 L 86.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 102.4 43.68615612366939 L 97.6 52.0 L 92.8 43.68615612366939 L 96.16 43.68615612366939 L 96.16 28.0 L 99.03999999999999 28.0 L 99.03999999999999 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"72.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 83.19999999999999 156.31384387633062 L 88.0 148.0 L 92.8 156.31384387633062 L 89.44 156.31384387633062 L 89.44 172.0 L 86.56 172.0 L 86.56 156.31384387633062 Z\" fill=\"#ffffff\" /><path d=\"M 102.4 163.68615612366938 L 97.6 172.0 L 92.8 163.68615612366938 L 96.16 163.68615612366938 L 96.16 148.0 L 99.03999999999999 148.0 L 99.03999999999999 163.68615612366938 Z\" fill=\"#ffffff\" /><rect x=\"132.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"212.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/2 </text><rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"332.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"332.8\" x2=\"332.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"312.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"332.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/4 </text><circle cx=\"392.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"372.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"392.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"392.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/2 </text><rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd619123ac0>" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u2 = dagger(u1)\n", "u2.svg()" ] }, { "cell_type": "code", "execution_count": 8, "id": "26239556", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"516.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"516.8\" height=\"200.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"496.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"496.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"496.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"20.0\" y2=\"180.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"72.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 83.19999999999999 36.31384387633061 L 88.0 28.0 L 92.8 36.31384387633061 L 89.44 36.31384387633061 L 89.44 52.0 L 86.56 52.0 L 86.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 102.4 43.68615612366939 L 97.6 52.0 L 92.8 43.68615612366939 L 96.16 43.68615612366939 L 96.16 28.0 L 99.03999999999999 28.0 L 99.03999999999999 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"72.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 83.19999999999999 156.31384387633062 L 88.0 148.0 L 92.8 156.31384387633062 L 89.44 156.31384387633062 L 89.44 172.0 L 86.56 172.0 L 86.56 156.31384387633062 Z\" fill=\"#ffffff\" /><path d=\"M 102.4 163.68615612366938 L 97.6 172.0 L 92.8 163.68615612366938 L 96.16 163.68615612366938 L 96.16 148.0 L 99.03999999999999 148.0 L 99.03999999999999 163.68615612366938 Z\" fill=\"#ffffff\" /><rect x=\"132.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"212.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/2 </text><rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"332.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"332.8\" x2=\"332.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"312.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"332.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/4 </text><circle cx=\"392.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"372.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"392.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"392.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >-π/2 </text><rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6a06bda00>" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u3 = dagger(qft)\n", "u4 = u3(range(3))\n", "u4.svg()" ] }, { "cell_type": "markdown", "id": "69117023", "metadata": {}, "source": [ "可看到,两种方式得到的线路是相同的。\n", "\n", "## [mindquantum.core.circuit.apply](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.apply.html)\n", "\n", "[apply](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.apply.html) 方法用于将量子线路或量子算符作用在指定的量子比特(可以是多个)上。" ] }, { "cell_type": "code", "execution_count": 9, "id": "865da650", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"152.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/2 </text><rect x=\"192.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd619097850>" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.core.circuit import apply\n", "\n", "u1 = qft([0, 1])\n", "circuit1 = apply(u1, [1, 0]) # 将量子线路u1作用在比特q1, q0上\n", "circuit1.svg()" ] }, { "cell_type": "code", "execution_count": 10, "id": "67ed697a", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"336.8\" height=\"140.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#fac209\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >PS </text><text x=\"152.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >π/2 </text><rect x=\"192.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"212.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190914c0>" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u2 = apply(qft, [1, 0]) # 将qft作用在比特q0, q1上\n", "circuit2 = u2([0, 1])\n", "circuit2.svg()" ] }, { "cell_type": "markdown", "id": "553beec8", "metadata": {}, "source": [ "## [mindquantum.core.circuit.add_prefix](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.add_prefix.html)\n", "\n", "[add_prefix](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.add_prefix.html) 方法用于在参数化量子电路或参数化量子算符的参数名上加上前缀。这在神经网络分层中十分有用,例如:" ] }, { "cell_type": "code", "execution_count": 11, "id": "5c70b2f8", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"196.8\" height=\"80.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"196.8\" height=\"80.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><line x1=\"48.8\" x2=\"176.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >theta </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190b9fa0>" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.core.circuit import add_prefix, Circuit\n", "from mindquantum.core.gates import RX, H\n", "\n", "circ = Circuit().rx(\"theta\", 0)\n", "circ.svg()" ] }, { "cell_type": "code", "execution_count": 12, "id": "3ecfc597", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"196.8\" height=\"80.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"196.8\" height=\"80.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><line x1=\"48.8\" x2=\"176.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >l0_theta </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd619123ee0>" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circ = add_prefix(circ, 'l0') # 添加后,参数\"theta\"就变成了\"l0_theta\"\n", "circ.svg()" ] }, { "cell_type": "markdown", "id": "e3c09fbc", "metadata": {}, "source": [ "我们可以在含参量子门作用于具体的量子比特后对其参数名称添加前缀,也可以在其还未应用于量子比特的时候添加。例如:" ] }, { "cell_type": "code", "execution_count": 13, "id": "71f56c75", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"256.8\" height=\"80.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"256.8\" height=\"80.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><line x1=\"48.8\" x2=\"236.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"132.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"172.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >ansatz_a </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd619123f40>" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u = lambda qubit: Circuit([H.on(0), RX('a').on(qubit)])\n", "\n", "u1 = u(0)\n", "u1 = add_prefix(u1, 'ansatz')\n", "u1.svg()" ] }, { "cell_type": "code", "execution_count": 14, "id": "edbf7b03", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"256.8\" height=\"80.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"256.8\" height=\"80.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><line x1=\"48.8\" x2=\"236.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"132.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"172.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >ansatz_a </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190b5cd0>" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "u2 = add_prefix(u, 'ansatz')\n", "u2 = u2(0)\n", "u2.svg()" ] }, { "cell_type": "markdown", "id": "c05a8195", "metadata": {}, "source": [ "## [mindquantum.core.circuit.change_param_name](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.change_param_name.html)\n", "\n", "[change_param_name](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.change_param_name.html) 方法用于修改参数化量子电路或参数化量子算符的参数名。" ] }, { "cell_type": "code", "execution_count": 15, "id": "d844ba22", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"80.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"80.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >b </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190a8400>" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.core.circuit import change_param_name, Circuit\n", "from mindquantum.core.gates import RX, H\n", "\n", "u = lambda qubit: Circuit([H.on(0), RX('a').on(qubit)])\n", "\n", "u1 = u(0)\n", "u1 = change_param_name(u1, {'a': 'b'})\n", "u1.svg()" ] }, { "cell_type": "code", "execution_count": 16, "id": "3a1d5d5f", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"80.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"80.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RX </text><text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >b </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190e3100>" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u2 = change_param_name(u, {'a': 'b'})\n", "u2 = u2(0)\n", "u2.svg()" ] }, { "cell_type": "markdown", "id": "522ec9fc", "metadata": {}, "source": [ "## [mindquantum.core.circuit.UN](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.UN.html)\n", "\n", "[UN](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.UN.html) 模块用于将量子门作用于不同的目标量子比特和控制量子比特。\n", "\n", "一般格式如下:`mindquantum.circuit.UN(gate, maps_obj, maps_ctrl=None)`,参数`gate`是要执行的量子门,`maps_obj`是需要执行该量子门的目标量子比特,`maps_ctrl`是控制量子比特(无控制量子位时为`None`)。若每个量子比特位都执行同一不含参的量子门,则可以直接使用`UN(gate, N)`,`N`表示量子比特个数。\n", "\n", "以下是一个简单的示例,我们在构建的线路中对每一位量子比特都应用一个 [H](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/gates/mindquantum.core.gates.HGate.html) 门:" ] }, { "cell_type": "code", "execution_count": 17, "id": "6ba3b3ff", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"260.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"156.8\" height=\"260.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><line x1=\"48.8\" x2=\"136.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190971c0>" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.core.circuit import change_param_name, Circuit, UN\n", "from mindquantum.core.gates import X, H, SWAP\n", "\n", "circuit1 = Circuit()\n", "circuit1 += UN(H, 4) # 将H门作用在每一位量子比特上\n", "circuit1.svg()" ] }, { "cell_type": "markdown", "id": "52a24be7", "metadata": {}, "source": [ "以下示例中,我们对量子比特$q_2$和$q_0$、$q_3$和$q_1$分别添加一个[CNOT](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/gates/mindquantum.core.gates.XGate.html)门,其中$q_2$为控制位,$q_0$为目标位;$q_3$为控制位,$q_1$为目标位:" ] }, { "cell_type": "code", "execution_count": 18, "id": "3fbb0af7", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"248.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"248.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><circle cx=\"92.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"72.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"78.8\" x2=\"106.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6190af430>" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circuit2 = UN(X, maps_obj=[0, 1], maps_ctrl=[2, 3])\n", "circuit2.svg()" ] }, { "cell_type": "markdown", "id": "e44d378e", "metadata": {}, "source": [ "针对类似 [SWAP](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/gates/mindquantum.core.gates.SWAPGate.html) 门的用法如下:" ] }, { "cell_type": "code", "execution_count": 19, "id": "2ccd1cf8", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"260.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"260.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"196.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"72.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 83.19999999999999 36.31384387633061 L 88.0 28.0 L 92.8 36.31384387633061 L 89.44 36.31384387633061 L 89.44 52.0 L 86.56 52.0 L 86.56 36.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 102.4 43.68615612366939 L 97.6 52.0 L 92.8 43.68615612366939 L 96.16 43.68615612366939 L 96.16 28.0 L 99.03999999999999 28.0 L 99.03999999999999 43.68615612366939 Z\" fill=\"#ffffff\" /><rect x=\"72.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 83.19999999999999 96.31384387633061 L 88.0 88.0 L 92.8 96.31384387633061 L 89.44 96.31384387633061 L 89.44 112.0 L 86.56 112.0 L 86.56 96.31384387633061 Z\" fill=\"#ffffff\" /><path d=\"M 102.4 103.68615612366939 L 97.6 112.0 L 92.8 103.68615612366939 L 96.16 103.68615612366939 L 96.16 88.0 L 99.03999999999999 88.0 L 99.03999999999999 103.68615612366939 Z\" fill=\"#ffffff\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"140.0\" y2=\"240.0\" stroke-width=\"3\" stroke=\"#16acff\" /><rect x=\"72.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 83.19999999999999 156.31384387633062 L 88.0 148.0 L 92.8 156.31384387633062 L 89.44 156.31384387633062 L 89.44 172.0 L 86.56 172.0 L 86.56 156.31384387633062 Z\" fill=\"#ffffff\" /><path d=\"M 102.4 163.68615612366938 L 97.6 172.0 L 92.8 163.68615612366938 L 96.16 163.68615612366938 L 96.16 148.0 L 99.03999999999999 148.0 L 99.03999999999999 163.68615612366938 Z\" fill=\"#ffffff\" /><rect x=\"72.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" /><path d=\"M 83.19999999999999 216.31384387633062 L 88.0 208.0 L 92.8 216.31384387633062 L 89.44 216.31384387633062 L 89.44 232.0 L 86.56 232.0 L 86.56 216.31384387633062 Z\" fill=\"#ffffff\" /><path d=\"M 102.4 223.68615612366938 L 97.6 232.0 L 92.8 223.68615612366938 L 96.16 223.68615612366938 L 96.16 208.0 L 99.03999999999999 208.0 L 99.03999999999999 223.68615612366938 Z\" fill=\"#ffffff\" /><circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd61904d0a0>" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circuit3 = UN(SWAP, maps_obj=[[0, 1], [2, 3]]).x(2, 1)\n", "circuit3.svg()" ] }, { "cell_type": "markdown", "id": "9b0d1910", "metadata": {}, "source": [ "## [mindquantum.core.circuit.shift](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.shift.html)\n", "\n", "[shift](https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/core/circuit/mindquantum.core.circuit.shift.html) 方法用于修改给定线路的量子位范围,参数为要挪动的步长。注意,步长不能为负数。并且,只能挪到最后一个量子比特,不能循环到$q_0$(第一个量子位)。" ] }, { "cell_type": "code", "execution_count": 20, "id": "71d000ca", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"128.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"12.0\" width=\"156.8\" height=\"128.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><line x1=\"48.8\" x2=\"136.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><circle cx=\"92.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"72.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"78.8\" x2=\"106.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6a058b880>" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.core.circuit import shift, Circuit\n", "from mindquantum.core.gates import X\n", "\n", "circ = Circuit().x(1, 0)\n", "circ.svg()" ] }, { "cell_type": "code", "execution_count": 21, "id": "180fde8c", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"128.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"12.0\" width=\"156.8\" height=\"128.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><line x1=\"48.8\" x2=\"136.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"136.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><circle cx=\"92.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"72.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"78.8\" x2=\"106.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"92.8\" x2=\"92.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd6a06b0910>" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circ = shift(circ, 1)\n", "circ.svg() # 线路作用的量子比特从q0,q1变为q1,q2" ] }, { "cell_type": "markdown", "id": "b5050bd9", "metadata": {}, "source": [ "### 案例——鸢尾花分类:搭建Encoder\n", "\n", "以上高阶操作的示例显示了量子线路高阶操作的适用性及便捷性,如果我们进一步将它们组合起来用于构建量子线路,又会产生什么神奇魔力呢?\n", "\n", "接下来我们将以<a href=\"https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.9/case_library/classification_of_iris_by_qnn.html#%E9%80%9A%E8%BF%87%E9%87%8F%E5%AD%90%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%AF%B9%E9%B8%A2%E5%B0%BE%E8%8A%B1%E8%BF%9B%E8%A1%8C%E5%88%86%E7%B1%BB\" target=\"_blank\"> 通过量子神经网络对鸢尾花进行分类\n", " </a>的案例中<a href=\"https://mindspore.cn/mindquantum/docs/zh-CN/r0.9/case_library/classification_of_iris_by_qnn.html#%E6%90%AD%E5%BB%BAencoder\" target=\"_blank\"> 搭建Encoder </a>部分为例,搭建下图所示的量子线路:\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "89bbaf78", "metadata": {}, "outputs": [ { "data": { "text/html": [ "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #ff0000; text-decoration-color: #ff0000; font-weight: bold\"> Circuit Summary </span>\n", "╭──────────────────────┬───────────────────────────────────────────────────────────────╮\n", "│<span style=\"font-weight: bold\"> </span><span style=\"color: #3b3b95; text-decoration-color: #3b3b95; font-weight: bold\">Info</span><span style=\"font-weight: bold\"> </span>│<span style=\"font-weight: bold\"> </span><span style=\"color: #3b3b95; text-decoration-color: #3b3b95; font-weight: bold\">value</span><span style=\"font-weight: bold\"> </span>│\n", "├──────────────────────┼───────────────────────────────────────────────────────────────┤\n", "│ <span style=\"font-weight: bold\">Number of qubit</span> │ 4 │\n", "├──────────────────────┼───────────────────────────────────────────────────────────────┤\n", "│ <span style=\"font-weight: bold\">Total number of gate</span> │ 17 │\n", "│ Barrier │ 0 │\n", "│ Noise Channel │ 0 │\n", "│ Measurement │ 0 │\n", "├──────────────────────┼───────────────────────────────────────────────────────────────┤\n", "│ <span style=\"font-weight: bold\">Parameter gate</span> │ 7 │\n", "│ 7 ansatz parameters │ <span style=\"color: #48c9b0; text-decoration-color: #48c9b0\">0_alpha, 1_alpha, 2_alpha, 3_alpha, 4_alpha, 5_alpha, 6_alpha</span> │\n", "╰──────────────────────┴───────────────────────────────────────────────────────────────╯\n", "</pre>\n" ], "text/plain": [ "\u001b[1;38;2;255;0;0m Circuit Summary \u001b[0m\n", "╭──────────────────────┬───────────────────────────────────────────────────────────────╮\n", "│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mInfo\u001b[0m\u001b[1m \u001b[0m\u001b[1m \u001b[0m│\u001b[1m \u001b[0m\u001b[1;38;2;59;59;149mvalue\u001b[0m\u001b[1m \u001b[0m\u001b[1m \u001b[0m│\n", "├──────────────────────┼───────────────────────────────────────────────────────────────┤\n", "│ \u001b[1mNumber of qubit\u001b[0m │ 4 │\n", "├──────────────────────┼───────────────────────────────────────────────────────────────┤\n", "│ \u001b[1mTotal number of gate\u001b[0m │ 17 │\n", "│ Barrier │ 0 │\n", "│ Noise Channel │ 0 │\n", "│ Measurement │ 0 │\n", "├──────────────────────┼───────────────────────────────────────────────────────────────┤\n", "│ \u001b[1mParameter gate\u001b[0m │ 7 │\n", "│ 7 ansatz parameters │ \u001b[38;2;72;201;176m0_alpha, 1_alpha, 2_alpha, 3_alpha, 4_alpha, 5_alpha, 6_alpha\u001b[0m │\n", "╰──────────────────────┴───────────────────────────────────────────────────────────────╯\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from mindquantum.core.circuit import shift, add_prefix, Circuit, UN\n", "from mindquantum.core.gates import RZ, X, H\n", "\n", "template = Circuit([X.on(1, 0), RZ('alpha').on(1), X.on(1, 0)])\n", "encoder = UN(H, 4) + (RZ(f'{i}_alpha').on(i) for i in range(4)) + sum(add_prefix(shift(template, i), f'{i+4}') for i in range(3))\n", "encoder.summary()" ] }, { "cell_type": "code", "execution_count": 23, "id": "1686faa1", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"916.8\" height=\"260.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><rect x=\"0\" y=\"0.0\" width=\"916.8\" height=\"260.0\" fill=\"#ffffff\" /><text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q0: </text><text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q1: </text><text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q2: </text><text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >q3: </text><line x1=\"48.8\" x2=\"896.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"896.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"896.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><line x1=\"48.8\" x2=\"896.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" /><rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" /><text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >H </text><rect x=\"132.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"172.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >0_alpha </text><rect x=\"132.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"172.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >1_alpha </text><rect x=\"132.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"172.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >2_alpha </text><rect x=\"132.8\" y=\"200.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"172.8\" y=\"216.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"172.8\" y=\"232.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >3_alpha </text><circle cx=\"252.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"252.8\" x2=\"252.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"232.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"238.8\" x2=\"266.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"252.8\" x2=\"252.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"292.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"332.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"332.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >4_alpha </text><circle cx=\"412.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"412.8\" x2=\"412.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"392.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"398.8\" x2=\"426.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"412.8\" x2=\"412.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"472.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"472.8\" x2=\"472.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"452.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"458.8\" x2=\"486.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"472.8\" x2=\"472.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"512.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"552.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"552.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >5_alpha </text><circle cx=\"632.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"632.8\" x2=\"632.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"612.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"618.8\" x2=\"646.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"632.8\" x2=\"632.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><circle cx=\"692.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"692.8\" x2=\"692.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"672.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"678.8\" x2=\"706.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"692.8\" x2=\"692.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><rect x=\"732.8\" y=\"200.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" /><text x=\"772.8\" y=\"216.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >RZ </text><text x=\"772.8\" y=\"232.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >6_alpha </text><circle cx=\"852.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" /><line x1=\"852.8\" x2=\"852.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" /><rect x=\"832.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" /><line x1=\"838.8\" x2=\"866.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" /><line x1=\"852.8\" x2=\"852.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" /></svg>" ], "text/plain": [ "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7fd619072370>" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "encoder.svg()" ] }, { "cell_type": "markdown", "id": "8dcb676b", "metadata": {}, "source": [ "在上面的代码中,我们使用UN模块将不含参数的H门作用于目标量子比特$q_0$~$q_3$,然后将RZ(i_alpha)门作用于第i位量子比特。\n", "接着,观察到线路中有三个一样的模块,它们都由一个受控X门、一个RZ门和一个受控X门构成,因此,我们构造对应的模板template,它由一个受$q_0$控制、作用在$q_1$的X门,一个作用在$q_1$的RZ门和一个受$q_0$控制、作用在$q_1$的X门构成。然后我们通过shift(template, i)来改变模板作用的量子比特范围,构建三个结构相同但作用的量子比特范围相差1的模块,其涉及的量子比特范围分别为$q_0$和$q_1$、$q_1$和$q_2$、$q_2$和$q_3$。\n", "最后,我们使用add_prefix方法为通过shift构造的所有含参量子门的参数名加上一个数字前缀。\n", "\n", "至此,通过MindSpore Quantum提供的这些量子线路高阶操作,我们仅用两行代码就构建了所需的Encoder!" ] }, { "cell_type": "code", "execution_count": 24, "id": "abfafaad", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "<table border=\"1\">\n", " <tr>\n", " <th>Software</th>\n", " <th>Version</th>\n", " </tr>\n", "<tr><td>mindquantum</td><td>0.9.11</td></tr>\n", "<tr><td>scipy</td><td>1.10.1</td></tr>\n", "<tr><td>numpy</td><td>1.24.4</td></tr>\n", "<tr>\n", " <th>System</th>\n", " <th>Info</th>\n", "</tr>\n", "<tr><td>Python</td><td>3.8.17</td></tr><tr><td>OS</td><td>Linux x86_64</td></tr><tr><td>Memory</td><td>16.62 GB</td></tr><tr><td>CPU Max Thread</td><td>16</td></tr><tr><td>Date</td><td>Tue Jan 2 14:38:55 2024</td></tr>\n", "</table>\n" ], "text/plain": [ "<mindquantum.utils.show_info.InfoTable at 0x7fd619072e20>" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.utils.show_info import InfoTable\n", "\n", "InfoTable('mindquantum', 'scipy', 'numpy')\n" ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 5 }