Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing small errors in 101 #741

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 39 additions & 58 deletions algorithms/swap_test/swap_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
"source": [
"# The Swap Test Algorithm\n",
"\n",
"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",
"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",
"$$\n",
"\\alpha^2 = \\frac{1}{2}\\left(1+|\\langle \\psi_1 |\\psi_2 \\rangle |^2\\right).\n",
"$$\n",
"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",
"\n",
"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",
"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",
"\n",
"A general scheme of the Swap test algorithm:\n",
"\n",
"<center>\n",
"<table><tr>\n",
"<td> <img src=\"https://docs.classiq.io/resources/swap_test_closed.png\" style=\"width:100%\">\n",
"<td> <img src=\"https://docs.classiq.io/resources/swap_test_opened.png\" style=\"width:100%\">\n",
"</tr></table>\n",
"<figcaption align = \"middle\"> Closed (left panel) and opened (right panel) visualization of the swap test algorithm </figcaption>\n",
"<div style=\"text-align:center;\">\n",
" <img src=\"https://docs.classiq.io/resources/Swap_Test_Circuit.png\" alt=\"Swap_Test_blocks\" border=\"0\">\n",
"</div>\n",
"</center>"
]
},
Expand All @@ -34,16 +34,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"id": "3172465a-c12d-4636-8092-430c8506622f",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-07T14:30:58.002513Z",
"iopub.status.busy": "2024-05-07T14:30:58.002260Z",
"iopub.status.idle": "2024-05-07T14:30:58.132861Z",
"shell.execute_reply": "2024-05-07T14:30:58.132021Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
Expand All @@ -69,17 +62,18 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"id": "eb3efd6a-ed62-4607-a297-f903f2415c93",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-07T14:30:58.138930Z",
"iopub.status.busy": "2024-05-07T14:30:58.137654Z",
"iopub.status.idle": "2024-05-07T14:31:03.338893Z",
"shell.execute_reply": "2024-05-07T14:31:03.337995Z"
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opening: https://platform.classiq.io/circuit/2sDZTfkLYOewJWOLqSzQB7pgqMW?version=0.66.0\n"
]
}
},
"outputs": [],
],
"source": [
"from classiq import *\n",
"\n",
Expand All @@ -105,9 +99,9 @@
"id": "eeaa11bb-4a3a-4172-bc29-1bb8860f232a",
"metadata": {},
"source": [
"## swap test - qmod implementations\n",
"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",
"Users can easily add their own functions"
"## Swap Test - Qmod Implementations\n",
"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",
"Users can easily add their own functions."
]
},
{
Expand All @@ -120,16 +114,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"id": "deea6297-699c-44c2-9d39-52cf44010a15",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-07T14:31:03.534246Z",
"iopub.status.busy": "2024-05-07T14:31:03.533227Z",
"iopub.status.idle": "2024-05-07T14:31:04.983428Z",
"shell.execute_reply": "2024-05-07T14:31:04.982620Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"result = execute(qprog).result_value()"
Expand All @@ -140,28 +127,21 @@
"id": "959995ad-aa6b-4b31-a25b-95396f8ffa7f",
"metadata": {},
"source": [
"## Comparing the measured overlap with the exact overlap\n",
"## Comparing Measured and Exact Overlap\n",
"Using the expected probability of measuring the state $|0\\rangle$ as defined above:\n",
"$$\n",
"\\alpha^2 = \\frac{1}{2}\\left(1+|\\langle \\psi_1 |\\psi_2 \\rangle |^2\\right).\n",
"$$\n",
"we extract the overlap $|\\langle \\psi_1 |\\psi_2 \\rangle |^2=\\sqrt{2 P\\left(q_{\\text{test}}=|0\\rangle\\right)-1}$ \n",
"we extract the overlap $|\\langle \\psi_1 |\\psi_2 \\rangle |=\\sqrt{2 P\\left(q_{\\text{test}}=|0\\rangle\\right)-1}$ \n",
"The exact overlap is computed with the dot product of the two state-vectors.\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"id": "c95fc7a8-c609-4734-95cb-55054d593dbb",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-07T14:31:04.988036Z",
"iopub.status.busy": "2024-05-07T14:31:04.986986Z",
"iopub.status.idle": "2024-05-07T14:31:04.992140Z",
"shell.execute_reply": "2024-05-07T14:31:04.991431Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"overlap_from_swap_test = np.sqrt(\n",
Expand Down Expand Up @@ -199,16 +179,9 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "27a9308b-5840-44c5-b7b7-7299d8d6175a",
"metadata": {
"execution": {
"iopub.execute_input": "2024-05-07T14:31:05.006570Z",
"iopub.status.busy": "2024-05-07T14:31:05.005418Z",
"iopub.status.idle": "2024-05-07T14:31:05.011253Z",
"shell.execute_reply": "2024-05-07T14:31:05.010541Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"RTOL = 0.05\n",
Expand All @@ -217,6 +190,14 @@
"), f\"\"\"\n",
"The quantum result is too far than classical one, by a relative tolerance of {RTOL}. Please verify your parameters\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d39ccb95-5989-460f-8a49-2b2392cb4c9f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -235,7 +216,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
47 changes: 46 additions & 1 deletion algorithms/swap_test/swap_test.synthesis_options.json
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
{}
{
"constraints": {
"max_gate_count": {},
"optimization_parameter": "no_opt"
},
"preferences": {
"machine_precision": 8,
"custom_hardware_settings": {
"basis_gates": [
"cx",
"r",
"u2",
"rx",
"x",
"z",
"sx",
"id",
"h",
"ry",
"t",
"s",
"rz",
"p",
"u1",
"sdg",
"y",
"cy",
"tdg",
"cz",
"sxdg",
"u"
],
"is_symmetric_connectivity": true
},
"debug_mode": true,
"synthesize_all_separately": false,
"optimization_level": 3,
"output_format": [
"qasm"
],
"pretty_qasm": true,
"transpilation_option": "auto optimize",
"timeout_seconds": 300,
"random_seed": 1860637157
}
}
Loading
Loading