Skip to content

Commit f01a183

Browse files
Adam GitterAdam Gitter
authored andcommitted
changed to new visualization in Swap Test and fixed error
1 parent 51e94c0 commit f01a183

File tree

2 files changed

+85
-59
lines changed

2 files changed

+85
-59
lines changed

algorithms/swap_test/swap_test.ipynb

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
"source": [
88
"# The Swap Test Algorithm\n",
99
"\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",
1111
"$$\n",
1212
"\\alpha^2 = \\frac{1}{2}\\left(1+|\\langle \\psi_1 |\\psi_2 \\rangle |^2\\right).\n",
1313
"$$\n",
1414
"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",
1515
"\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",
1719
"\n",
1820
"<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",
2424
"</center>"
2525
]
2626
},
@@ -34,16 +34,9 @@
3434
},
3535
{
3636
"cell_type": "code",
37-
"execution_count": 1,
37+
"execution_count": 3,
3838
"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": {},
4740
"outputs": [],
4841
"source": [
4942
"import numpy as np\n",
@@ -69,17 +62,18 @@
6962
},
7063
{
7164
"cell_type": "code",
72-
"execution_count": 2,
65+
"execution_count": 4,
7366
"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+
]
8075
}
81-
},
82-
"outputs": [],
76+
],
8377
"source": [
8478
"from classiq import *\n",
8579
"\n",
@@ -105,9 +99,9 @@
10599
"id": "eeaa11bb-4a3a-4172-bc29-1bb8860f232a",
106100
"metadata": {},
107101
"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."
111105
]
112106
},
113107
{
@@ -120,16 +114,9 @@
120114
},
121115
{
122116
"cell_type": "code",
123-
"execution_count": 5,
117+
"execution_count": 7,
124118
"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": {},
133120
"outputs": [],
134121
"source": [
135122
"result = execute(qprog).result_value()"
@@ -140,28 +127,21 @@
140127
"id": "959995ad-aa6b-4b31-a25b-95396f8ffa7f",
141128
"metadata": {},
142129
"source": [
143-
"## Comparing the measured overlap with the exact overlap\n",
130+
"## Comparing Measured and Exact Overlap\n",
144131
"Using the expected probability of measuring the state $|0\\rangle$ as defined above:\n",
145132
"$$\n",
146133
"\\alpha^2 = \\frac{1}{2}\\left(1+|\\langle \\psi_1 |\\psi_2 \\rangle |^2\\right).\n",
147134
"$$\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",
149136
"The exact overlap is computed with the dot product of the two state-vectors.\n",
150137
"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."
151138
]
152139
},
153140
{
154141
"cell_type": "code",
155-
"execution_count": 6,
142+
"execution_count": 8,
156143
"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": {},
165145
"outputs": [],
166146
"source": [
167147
"overlap_from_swap_test = np.sqrt(\n",
@@ -199,16 +179,9 @@
199179
},
200180
{
201181
"cell_type": "code",
202-
"execution_count": 8,
182+
"execution_count": 9,
203183
"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": {},
212185
"outputs": [],
213186
"source": [
214187
"RTOL = 0.05\n",
@@ -217,6 +190,14 @@
217190
"), f\"\"\"\n",
218191
"The quantum result is too far than classical one, by a relative tolerance of {RTOL}. Please verify your parameters\"\"\""
219192
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": null,
197+
"id": "d39ccb95-5989-460f-8a49-2b2392cb4c9f",
198+
"metadata": {},
199+
"outputs": [],
200+
"source": []
220201
}
221202
],
222203
"metadata": {
@@ -235,7 +216,7 @@
235216
"name": "python",
236217
"nbconvert_exporter": "python",
237218
"pygments_lexer": "ipython3",
238-
"version": "3.11.9"
219+
"version": "3.11.7"
239220
}
240221
},
241222
"nbformat": 4,
Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
{}
1+
{
2+
"constraints": {
3+
"max_gate_count": {},
4+
"optimization_parameter": "no_opt"
5+
},
6+
"preferences": {
7+
"machine_precision": 8,
8+
"custom_hardware_settings": {
9+
"basis_gates": [
10+
"cx",
11+
"r",
12+
"u2",
13+
"rx",
14+
"x",
15+
"z",
16+
"sx",
17+
"id",
18+
"h",
19+
"ry",
20+
"t",
21+
"s",
22+
"rz",
23+
"p",
24+
"u1",
25+
"sdg",
26+
"y",
27+
"cy",
28+
"tdg",
29+
"cz",
30+
"sxdg",
31+
"u"
32+
],
33+
"is_symmetric_connectivity": true
34+
},
35+
"debug_mode": true,
36+
"synthesize_all_separately": false,
37+
"optimization_level": 3,
38+
"output_format": [
39+
"qasm"
40+
],
41+
"pretty_qasm": true,
42+
"transpilation_option": "auto optimize",
43+
"timeout_seconds": 300,
44+
"random_seed": 1860637157
45+
}
46+
}

0 commit comments

Comments
 (0)