|
5 | 5 | "id": "e865ba60-b8b6-4dbb-b1d9-fd6883d15d86", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | | - "# The Swap Test Algorithm\n", |
| 8 | + "# 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 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 | 17 | "\n", |
18 | | - "A general scheme of the Swap test algorithm:\n", |
| 18 | + "A general scheme of the swap test algorithm:\n", |
19 | 19 | "\n", |
20 | 20 | "<center>\n", |
21 | 21 | "<div style=\"text-align:center;\">\n", |
|
99 | 99 | "id": "eeaa11bb-4a3a-4172-bc29-1bb8860f232a", |
100 | 100 | "metadata": {}, |
101 | 101 | "source": [ |
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." |
| 102 | + "## Swap Test Qmod Implementations\n", |
| 103 | + "The swap test is defined as a library function in the Qmod language. The definition is in the [public GitHub](https://github.com/Classiq/classiq-library/blob/main/functions/open_library_definitions/swap_test.qmod).\n", |
| 104 | + "You can add your own functions." |
105 | 105 | ] |
106 | 106 | }, |
107 | 107 | { |
|
127 | 127 | "id": "959995ad-aa6b-4b31-a25b-95396f8ffa7f", |
128 | 128 | "metadata": {}, |
129 | 129 | "source": [ |
130 | | - "## Comparing Measured and Exact Overlap\n", |
131 | | - "Using the expected probability of measuring the state $|0\\rangle$ as defined above:\n", |
| 130 | + "## Comparing Measured with Exact Overlap\n", |
| 131 | + "Using the expected probability of measuring the state $|0\\rangle$ as defined above,\n", |
132 | 132 | "$$\n", |
133 | | - "\\alpha^2 = \\frac{1}{2}\\left(1+|\\langle \\psi_1 |\\psi_2 \\rangle |^2\\right).\n", |
| 133 | + "\\alpha^2 = \\frac{1}{2}\\left(1+|\\langle \\psi_1 |\\psi_2 \\rangle |^2\\right),\n", |
134 | 134 | "$$\n", |
135 | | - "we extract the overlap $|\\langle \\psi_1 |\\psi_2 \\rangle |=\\sqrt{2 P\\left(q_{\\text{test}}=|0\\rangle\\right)-1}$ \n", |
136 | | - "The exact overlap is computed with the dot product of the two state-vectors.\n", |
| 135 | + "we extract the overlap $|\\langle \\psi_1 |\\psi_2 \\rangle |=\\sqrt{2 P\\left(q_{\\text{test}}=|0\\rangle\\right)-1}$. \n", |
| 136 | + "The exact overlap is computed with the dot product of the two state vectors.\n", |
137 | 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." |
138 | 138 | ] |
139 | 139 | }, |
|
188 | 188 | "assert np.isclose(\n", |
189 | 189 | " overlap_from_swap_test, exact_overlap, RTOL\n", |
190 | 190 | "), f\"\"\"\n", |
191 | | - "The quantum result is too far than classical one, by a relative tolerance of {RTOL}. Please verify your parameters\"\"\"" |
| 191 | + "The quantum result is too far from the classical one by a relative tolerance of {RTOL}. Please verify your parameters\"\"\"" |
192 | 192 | ] |
193 | 193 | }, |
194 | 194 | { |
|
0 commit comments