{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "681e955a", "metadata": {}, "source": [ "# Advanced Operations of Quantum Circuit\n", "\n", "[](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/en/beginner/mindspore_advanced_operations_of_quantum_circuit.ipynb) \n", "[](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/en/beginner/mindspore_advanced_operations_of_quantum_circuit.py) \n", "[](https://gitee.com/mindspore/docs/blob/master/docs/mindquantum/docs/source_en/beginner/advanced_operations_of_quantum_circuit.ipynb)\n", "\n", "In previous tutorial we introduced the basic usage of [quantum circuit](https://mindspore.cn/mindquantum/docs/en/master/beginner/parameterized_quantum_circuit.html#quantum-circuit). In this tutorial, we will introduce how to operator the circuit in high level.\n", "\n", "## [mindquantum.core.circuit.controlled](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.controlled.html)\n", "\n", "The [controlled](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.controlled.html) method is used to add control qubits (which can be multiple) to any quantum circuit or quantum operator.\n", "\n", "For example, let's build a quantum circuit containing only two qubits and add a control qubit - $q_2$ to it by a [controlled](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.controlled.html) method:" ] }, { "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 0x7f88981ad670>" ] }, "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)) # Build the quantum circuit\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 0x7f889808cac0>" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u2 = controlled(u1)(2) # Add control qubit q2 to the circuit, returning a new circuit\n", "u2.svg()" ] }, { "cell_type": "markdown", "id": "4d63b2ef", "metadata": {}, "source": [ "Actually, [controlled()](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.controlled.html) returns a function. E.g:" ] }, { "cell_type": "code", "execution_count": 3, "id": "6144080f", "metadata": {}, "outputs": [], "source": [ "u3 = controlled(u1)" ] }, { "cell_type": "markdown", "id": "3c2a5acf", "metadata": {}, "source": [ "We can add control qubits to the initial circuit u1 by calling u3, which has the same effect as above:" ] }, { "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 0x7f87edb743d0>" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u4 = u3(2)\n", "u4.svg()" ] }, { "cell_type": "markdown", "id": "5381a350", "metadata": {}, "source": [ "Note: The added control qubit cannot be the target qubit of a gate that already exists in the circuit, otherwise an error will be reported!\n", "\n", "In addition, we can batch add control qubits to the quantum circuit. For example, in the following example we add control qubits - $q_2$ and $q_3$ to $q_0$ and $q_1$ respectively:" ] }, { "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 0x7f88981ada90>" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u = controlled(qft)\n", "u = u([2, 3], [0, 1]) # Bulk add control qubits\n", "u.svg()" ] }, { "cell_type": "markdown", "id": "f991b869", "metadata": {}, "source": [ "## [mindquantum.core.circuit.dagger](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.dagger.html)\n", "\n", "The [dagger](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.dagger.html) method is used to find the Hermitian conjugates of quantum circuits or quantum operators.\n", "\n", "The following examples provide two ways of doing dagger operations:" ] }, { "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 0x7f889808ca90>" ] }, "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 0x7f87edba4d90>" ] }, "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 0x7f889b23dc40>" ] }, "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": [ "It can be seen that the circuits obtained by the two methods are the same.\n", "\n", "## [mindquantum.core.circuit.apply](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.apply.html)\n", "\n", "The [apply](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.apply.html) method is used to apply a quantum circuit or quantum operator to the specified qubits (which can be multiple)." ] }, { "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 0x7f87ebb0c910>" ] }, "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]) # Apply quantum circuit u1 to qubits 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 0x7f889808cc40>" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u2 = apply(qft, [1, 0]) # Apply qft to qubits 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/en/master/core/circuit/mindquantum.core.circuit.add_prefix.html)\n", "\n", "The [add_prefix](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.add_prefix.html) method is used to prefix the parameter names of parameterized quantum circuits or parameterized quantum operators. This is useful in neural network layering, for example:" ] }, { "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 0x7f87ebb2a9a0>" ] }, "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 0x7f87ebb11e80>" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# After adding, the parameter \"theta\" becomes \"l0_theta\"\n", "circ = add_prefix(circ, 'l0')\n", "circ.svg()" ] }, { "cell_type": "markdown", "id": "e3c09fbc", "metadata": {}, "source": [ "We can prefix the parameter name of a parameterized qubit after it acts on a specific qubit, or we can add it when it has not yet been applied to the qubit. E.g:" ] }, { "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 0x7f889808c910>" ] }, "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 0x7f87ebb05d90>" ] }, "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/en/master/core/circuit/mindquantum.core.circuit.change_param_name.html)\n", "\n", "The [change_param_name](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.change_param_name.html) method is used to modify the parameter name of a parameterized quantum circuit or a parameterized quantum operator." ] }, { "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 0x7f87edb78340>" ] }, "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 0x7f88981b2430>" ] }, "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/en/master/core/circuit/mindquantum.core.circuit.UN.html)\n", "\n", "The [UN](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.UN.html) module is used to actuate quantum gates on different target qubits and control qubits.\n", "\n", "The general format is as follows: `mindquantum.circuit.UN(gate, maps_obj, maps_ctrl=None)`, the parameter `gate` is the gate to be executed, `maps_obj` is the target qubit to be executed, and `maps_Ctrl` is the control qubit (None if there are no control qubits). If each qubit implements the same quantum gate without parameters, `UN(gate, N)` can be used directly, where `N` represents the number of qubits.\n", "\n", "Here is a simple example where we apply an [H-gate](https://www.mindspore.cn/mindquantum/docs/en/master/core/gates/mindquantum.core.gates.HGate.html) to each qubit in the [circuit](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.Circuit.html) we build:" ] }, { "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 0x7f87ebb0cb80>" ] }, "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) # Apply the H gate to each qubit\n", "circuit1.svg()" ] }, { "cell_type": "markdown", "id": "52a24be7", "metadata": {}, "source": [ "In the following example, we add a [CNOT](https://www.mindspore.cn/mindquantum/docs/en/master/core/gates/mindquantum.core.gates.XGate.html) gate to the qubits $q_2$ and $q_0$, $q_3$ and $q_1$ respectively, where $q_2$ is the control bit, $q_0$ is the target bit; $q_3$ is the control bit , $q_1$ is the target bit:" ] }, { "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 0x7f87ebb36250>" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circuit2 = UN(X, maps_obj=[0, 1], maps_ctrl=[2, 3])\n", "circuit2.svg()" ] }, { "attachments": {}, "cell_type": "markdown", "id": "e44d378e", "metadata": {}, "source": [ "For [SWAP](https://www.mindspore.cn/mindquantum/docs/en/master/core/gates/mindquantum.core.gates.SWAPGate.html) gate, the usage is as follows:" ] }, { "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 0x7f87ebadfe80>" ] }, "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/en/master/core/circuit/mindquantum.core.circuit.shift.html)\n", "\n", "The [shift](https://www.mindspore.cn/mindquantum/docs/en/master/core/circuit/mindquantum.core.circuit.shift.html) method is used to modify the qubit range of a given circuit, and the parameter is the step size to be shifted. Note that the step size cannot be negative. Moreover, it can only move to the last qubit, and cannot cycle to $q_0$ (the first qubit)." ] }, { "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 0x7f87ebaccb50>" ] }, "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 0x7f87ebadfd00>" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circ = shift(circ, 1)\n", "circ.svg() # The qubit acting on the circuit changes from q0, q1 to q1, q2" ] }, { "attachments": {}, "cell_type": "markdown", "id": "b5050bd9", "metadata": {}, "source": [ "### Case - Iris Classification: Building Encoder\n", "\n", "The above examples of higher-order operations show the applicability and convenience of higher-order operations in quantum circuits. What magic will happen if we further combine them to build quantum circuits?\n", "\n", "Next we will start with [Classification of irises by quantum neural network](https://mindspore.cn/mindquantum/docs/en/master/case_library/classification_of_iris_by_qnn.html). Build the Encoder part as an example, build the quantum circuit shown in the figure below:\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 0x7f87ebb36160>" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "encoder.svg()" ] }, { "cell_type": "markdown", "id": "8dcb676b", "metadata": {}, "source": [ "In the above code, we use the UN module to apply the parameter-free H gate to the target qubits $q_0$~$q_3$, and then apply the RZ(i_alpha) gate to the i-th qubit.\n", "Next, it is observed that there are three identical modules in the circuit, and they are all composed of a controlled X gate, a RZ gate and a controlled X gate. Therefore, we construct the corresponding template template, which is controlled by $q_0$, an X gate acting on $q_1$, an RZ gate acting on $q_1$, and an X gate controlled by $q_0$ acting on $q_1$ are formed. Then we use shift(template, i) to change the qubit range that the template acts on, and build three modules with the same structure but the qubit range differs by 1. The qubit ranges involved are $q_0$ and $q_1$, $q_1$ and $q_2$, $q_2$ and $q_3$.\n", "Finally, we use the add_prefix method to prefix the parameter names of all parameter quantum gates constructed by shift with a number.\n", "\n", "So far, through these advanced operations of quantum circuit provided by MindSpore Quantum, we have built the required Encoder with only two lines of code!" ] }, { "cell_type": "code", "execution_count": 24, "id": "038c6317", "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:22 2024</td></tr>\n", "</table>\n" ], "text/plain": [ "<mindquantum.utils.show_info.InfoTable at 0x7f87edb64a90>" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindquantum.utils.show_info import InfoTable\n", "\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" } }, "nbformat": 4, "nbformat_minor": 5 }