|
7 | 7 | "source": [ |
8 | 8 | "# The Swap Test Algorithm\n", |
9 | 9 | "\n", |
10 | | - "The swap test is a quantum function that checks the overlap between two quantum states: the inputs of the function are two quantum registers of the same size, $|\\psi_1\\rangle, \\,|\\psi_2\\rangle$, and it returns as an output a single \"test\" qubit whose state encodes the overlap between the two inputs: $|q\\rangle_{\\rm test} = \\alpha|0\\rangle + \\sqrt{1-\\alpha^2}|1\\rangle$, with\n", |
| 10 | + "The Swap test is a quantum function that checks the overlap between two quantum states: the inputs of the function are two quantum registers of the same size, $|\\psi_1\\rangle, \\,|\\psi_2\\rangle$, and it returns as an output a single test qubit whose state encodes the overlap between the two inputs: $|q\\rangle_{\\rm test} = \\alpha|0\\rangle + \\sqrt{1-\\alpha^2}|1\\rangle$, with\n", |
11 | 11 | "$$\n", |
12 | 12 | "\\alpha^2 = \\frac{1}{2}\\left(1+|\\langle \\psi_1 |\\psi_2 \\rangle |^2\\right).\n", |
13 | 13 | "$$\n", |
14 | 14 | "Thus, the probability of measuring the test qubit at state $|0\\rangle$ is $1$ if the states are identical (up to a global phase) and 0.5 if the states are orthogonal to each other.\n", |
15 | 15 | "\n", |
16 | | - "The quantum model starts with an H gate on the test qubit, followed by swapping between the two states controlled on the test qubit (a controlled-SWAP gate for each of the qubits in the two states), and a final H gate on the test qubit:\n", |
| 16 | + "The quantum model starts with an $H$ gate on the test qubit, followed by swapping between the two states controlled on the test qubit (a controlled-SWAP gate for each of the qubits in the two states), and a final $H$ gate on the test qubit\n", |
| 17 | + "\n", |
| 18 | + "A general scheme of the Swap test algorithm:\n", |
17 | 19 | "\n", |
18 | 20 | "<center>\n", |
19 | | - "<table><tr>\n", |
20 | | - "<td> <img src=\"https://docs.classiq.io/resources/swap_test_closed.png\" style=\"width:100%\">\n", |
21 | | - "<td> <img src=\"https://docs.classiq.io/resources/swap_test_opened.png\" style=\"width:100%\">\n", |
22 | | - "</tr></table>\n", |
23 | | - "<figcaption align = \"middle\"> Closed (left panel) and opened (right panel) visualization of the swap test algorithm </figcaption>\n", |
| 21 | + "<div style=\"text-align:center;\">\n", |
| 22 | + " <img src=\"https://docs.classiq.io/resources/Swap_Test_Circuit.png\" alt=\"Swap_Test_blocks\" border=\"0\">\n", |
| 23 | + "</div>\n", |
24 | 24 | "</center>" |
25 | 25 | ] |
26 | 26 | }, |
|
34 | 34 | }, |
35 | 35 | { |
36 | 36 | "cell_type": "code", |
37 | | - "execution_count": 1, |
| 37 | + "execution_count": 3, |
38 | 38 | "id": "3172465a-c12d-4636-8092-430c8506622f", |
39 | | - "metadata": { |
40 | | - "execution": { |
41 | | - "iopub.execute_input": "2024-05-07T14:30:58.002513Z", |
42 | | - "iopub.status.busy": "2024-05-07T14:30:58.002260Z", |
43 | | - "iopub.status.idle": "2024-05-07T14:30:58.132861Z", |
44 | | - "shell.execute_reply": "2024-05-07T14:30:58.132021Z" |
45 | | - } |
46 | | - }, |
| 39 | + "metadata": {}, |
47 | 40 | "outputs": [], |
48 | 41 | "source": [ |
49 | 42 | "import numpy as np\n", |
|
69 | 62 | }, |
70 | 63 | { |
71 | 64 | "cell_type": "code", |
72 | | - "execution_count": 2, |
| 65 | + "execution_count": 4, |
73 | 66 | "id": "eb3efd6a-ed62-4607-a297-f903f2415c93", |
74 | | - "metadata": { |
75 | | - "execution": { |
76 | | - "iopub.execute_input": "2024-05-07T14:30:58.138930Z", |
77 | | - "iopub.status.busy": "2024-05-07T14:30:58.137654Z", |
78 | | - "iopub.status.idle": "2024-05-07T14:31:03.338893Z", |
79 | | - "shell.execute_reply": "2024-05-07T14:31:03.337995Z" |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "name": "stdout", |
| 71 | + "output_type": "stream", |
| 72 | + "text": [ |
| 73 | + "Opening: https://platform.classiq.io/circuit/2sDZTfkLYOewJWOLqSzQB7pgqMW?version=0.66.0\n" |
| 74 | + ] |
80 | 75 | } |
81 | | - }, |
82 | | - "outputs": [], |
| 76 | + ], |
83 | 77 | "source": [ |
84 | 78 | "from classiq import *\n", |
85 | 79 | "\n", |
|
105 | 99 | "id": "eeaa11bb-4a3a-4172-bc29-1bb8860f232a", |
106 | 100 | "metadata": {}, |
107 | 101 | "source": [ |
108 | | - "## swap test - qmod implementations\n", |
109 | | - "The swap test is defined as a library function in the qmod language (definition can be found on our [public github](https://github.com/Classiq/classiq-library/blob/main/functions/open_library_definitions/swap_test.qmod ) ).\n", |
110 | | - "Users can easily add their own functions" |
| 102 | + "## Swap Test - Qmod Implementations\n", |
| 103 | + "The Swap test is defined as a library function in the qmod language (definition can be found on our [public github](https://github.com/Classiq/classiq-library/blob/main/functions/open_library_definitions/swap_test.qmod)).\n", |
| 104 | + "Users can easily add their own functions." |
111 | 105 | ] |
112 | 106 | }, |
113 | 107 | { |
|
120 | 114 | }, |
121 | 115 | { |
122 | 116 | "cell_type": "code", |
123 | | - "execution_count": 5, |
| 117 | + "execution_count": 7, |
124 | 118 | "id": "deea6297-699c-44c2-9d39-52cf44010a15", |
125 | | - "metadata": { |
126 | | - "execution": { |
127 | | - "iopub.execute_input": "2024-05-07T14:31:03.534246Z", |
128 | | - "iopub.status.busy": "2024-05-07T14:31:03.533227Z", |
129 | | - "iopub.status.idle": "2024-05-07T14:31:04.983428Z", |
130 | | - "shell.execute_reply": "2024-05-07T14:31:04.982620Z" |
131 | | - } |
132 | | - }, |
| 119 | + "metadata": {}, |
133 | 120 | "outputs": [], |
134 | 121 | "source": [ |
135 | 122 | "result = execute(qprog).result_value()" |
|
140 | 127 | "id": "959995ad-aa6b-4b31-a25b-95396f8ffa7f", |
141 | 128 | "metadata": {}, |
142 | 129 | "source": [ |
143 | | - "## Comparing the measured overlap with the exact overlap\n", |
| 130 | + "## Comparing Measured and Exact Overlap\n", |
144 | 131 | "Using the expected probability of measuring the state $|0\\rangle$ as defined above:\n", |
145 | 132 | "$$\n", |
146 | 133 | "\\alpha^2 = \\frac{1}{2}\\left(1+|\\langle \\psi_1 |\\psi_2 \\rangle |^2\\right).\n", |
147 | 134 | "$$\n", |
148 | | - "we extract the overlap $|\\langle \\psi_1 |\\psi_2 \\rangle |^2=\\sqrt{2 P\\left(q_{\\text{test}}=|0\\rangle\\right)-1}$ \n", |
| 135 | + "we extract the overlap $|\\langle \\psi_1 |\\psi_2 \\rangle |=\\sqrt{2 P\\left(q_{\\text{test}}=|0\\rangle\\right)-1}$ \n", |
149 | 136 | "The exact overlap is computed with the dot product of the two state-vectors.\n", |
150 | 137 | "Note that for the sake of this demonstration we execute this circuit $100,000$ times to improve the precision of the probability estimate. This is usually not required in actual programs." |
151 | 138 | ] |
152 | 139 | }, |
153 | 140 | { |
154 | 141 | "cell_type": "code", |
155 | | - "execution_count": 6, |
| 142 | + "execution_count": 8, |
156 | 143 | "id": "c95fc7a8-c609-4734-95cb-55054d593dbb", |
157 | | - "metadata": { |
158 | | - "execution": { |
159 | | - "iopub.execute_input": "2024-05-07T14:31:04.988036Z", |
160 | | - "iopub.status.busy": "2024-05-07T14:31:04.986986Z", |
161 | | - "iopub.status.idle": "2024-05-07T14:31:04.992140Z", |
162 | | - "shell.execute_reply": "2024-05-07T14:31:04.991431Z" |
163 | | - } |
164 | | - }, |
| 144 | + "metadata": {}, |
165 | 145 | "outputs": [], |
166 | 146 | "source": [ |
167 | 147 | "overlap_from_swap_test = np.sqrt(\n", |
|
199 | 179 | }, |
200 | 180 | { |
201 | 181 | "cell_type": "code", |
202 | | - "execution_count": 8, |
| 182 | + "execution_count": 9, |
203 | 183 | "id": "27a9308b-5840-44c5-b7b7-7299d8d6175a", |
204 | | - "metadata": { |
205 | | - "execution": { |
206 | | - "iopub.execute_input": "2024-05-07T14:31:05.006570Z", |
207 | | - "iopub.status.busy": "2024-05-07T14:31:05.005418Z", |
208 | | - "iopub.status.idle": "2024-05-07T14:31:05.011253Z", |
209 | | - "shell.execute_reply": "2024-05-07T14:31:05.010541Z" |
210 | | - } |
211 | | - }, |
| 184 | + "metadata": {}, |
212 | 185 | "outputs": [], |
213 | 186 | "source": [ |
214 | 187 | "RTOL = 0.05\n", |
|
217 | 190 | "), f\"\"\"\n", |
218 | 191 | "The quantum result is too far than classical one, by a relative tolerance of {RTOL}. Please verify your parameters\"\"\"" |
219 | 192 | ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": null, |
| 197 | + "id": "d39ccb95-5989-460f-8a49-2b2392cb4c9f", |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [], |
| 200 | + "source": [] |
220 | 201 | } |
221 | 202 | ], |
222 | 203 | "metadata": { |
|
235 | 216 | "name": "python", |
236 | 217 | "nbconvert_exporter": "python", |
237 | 218 | "pygments_lexer": "ipython3", |
238 | | - "version": "3.11.9" |
| 219 | + "version": "3.11.7" |
239 | 220 | } |
240 | 221 | }, |
241 | 222 | "nbformat": 4, |
|
0 commit comments