Skip to content

Commit 0d97b1a

Browse files
committed
add SWAP test theory
1 parent 0cc1060 commit 0d97b1a

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

bloch_angles_estimation.ipynb

+78-7
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
"\n",
9494
"[1]: https://github.com/DavitKhach/quantum-algorithms-tutorials\n",
9595
"[2]: https://www.cambridge.org/am/academic/subjects/physics/quantum-physics-quantum-information-and-quantum-computation/quantum-computation-and-quantum-information-10th-anniversary-edition?format=HB\n",
96-
"[3]: https://en.wikipedia.org/wiki/Purity_(quantum_mechanics)"
96+
"[3]: https://en.wikipedia.org/wiki/Purity_(quantum_mechanics)\n",
97+
"[4]: https://en.wikipedia.org/wiki/Swap_test"
9798
]
9899
},
99100
{
@@ -190,7 +191,12 @@
190191
"\n",
191192
"where $\\left| \\psi'' \\right\\rangle = H_y \\left| \\psi \\right\\rangle$ and $\\left\\langle \\psi' \\right| = \\left( H_y \\left| \\psi \\right\\rangle \\right)^\\dagger = \\left\\langle \\psi \\right| H_y$, because $H_y^{\\dagger} = H_y$. So, after aplying $H_y$ to the qubit, we should calculate the $\\left\\langle \\psi'' \\right| Z \\left| \\psi'' \\right\\rangle$ that will give as the desired $\\left\\langle \\psi \\right| Y \\left| \\psi \\right\\rangle$.\n",
192193
"\n",
193-
"Now let's write a function that will calculate the expectation value for the given Pauli operator ($I$ not included):"
194+
"Now let's write a function that will calculate the expectation value for the given Pauli operator ($I$ not included):\n",
195+
"\n",
196+
"[1]: https://github.com/DavitKhach/quantum-algorithms-tutorials\n",
197+
"[2]: https://www.cambridge.org/am/academic/subjects/physics/quantum-physics-quantum-information-and-quantum-computation/quantum-computation-and-quantum-information-10th-anniversary-edition?format=HB\n",
198+
"[3]: https://en.wikipedia.org/wiki/Purity_(quantum_mechanics)\n",
199+
"[4]: https://en.wikipedia.org/wiki/Swap_test"
194200
]
195201
},
196202
{
@@ -249,7 +255,8 @@
249255
"\n",
250256
"[1]: https://github.com/DavitKhach/quantum-algorithms-tutorials\n",
251257
"[2]: https://www.cambridge.org/am/academic/subjects/physics/quantum-physics-quantum-information-and-quantum-computation/quantum-computation-and-quantum-information-10th-anniversary-edition?format=HB\n",
252-
"[3]: https://en.wikipedia.org/wiki/Purity_(quantum_mechanics)"
258+
"[3]: https://en.wikipedia.org/wiki/Purity_(quantum_mechanics)\n",
259+
"[4]: https://en.wikipedia.org/wiki/Swap_test"
253260
]
254261
},
255262
{
@@ -388,10 +395,7 @@
388395
"\n",
389396
"estimated_theta, estimated_phi = estimate_bloch_angles(quantum_register[0], circuit_bell, backend, shots=8192)\n",
390397
"\n",
391-
"print(\"theta = \", bloch_theta)\n",
392398
"print(\"Estimated theta = \", estimated_theta)\n",
393-
"\n",
394-
"print(\"phi = \", bloch_phi)\n",
395399
"print(\"Estimated phi = \", estimated_phi)"
396400
]
397401
},
@@ -401,8 +405,75 @@
401405
"source": [
402406
"If you see the warning massage than the code does what we wanted from it! \n",
403407
"\n",
404-
"Now let's introduce one more interesting procidure that will help us to double check the main procidure. "
408+
"Now let's introduce one more interesting procidure that will help us to double check the main procidure. In the first example we chosed Bloch angles randomly and estimated them with the `estimate_bloch_angles` function. At the end we had estimated Bloch angles and true angles and it was easy to test the estimation procidure just by comparing them. What if we don't know the true Bloch angles and the comparition cannot be made. In this cas we can implement the [SWAP test][4] method to determine the absolute value of the inner product between the input quantum state $\\left|\\psi_{in} \\right\\rangle$ and created quantum state with the estimated Bloch angles $\\left|\\psi_{est} \\right\\rangle$. In other words, the output of the SWAP test is equal to $\\left|\\left\\langle \\psi_{in} \\right|\\left| \\psi_{est} \\right\\rangle \\right|^2$, that is equal to $1$ if the quantum states are the same (or different only by a global phase factor $\\left| \\psi_{est} \\right\\rangle = e^{i\\phi}\\left|\\psi_{in} \\right\\rangle$). So, after runing the SWAP test and checking if $\\left|\\left\\langle \\psi_{in} \\right|\\left| \\psi_{est} \\right\\rangle \\right|^2 \\approx 1$, then we have currect estimation procidure.\n",
409+
"\n",
410+
"In the SWAP test for one qubit state we have $3$ qubits (for $n$ qubit states we will need $2 n+ 1$ qubits). First qubit is an auxillary qubit on which we will apply measurement that eventually will estimate $\\left|\\left\\langle \\psi_{1} \\right|\\left| \\psi_{2} \\right\\rangle \\right|^2$. The other two qubits are in the given $\\left| \\psi_{1} \\right\\rangle$ and $\\left| \\psi_{2} \\right\\rangle$ quantum state for which we need to calculate $\\left|\\left\\langle \\psi_{1} \\right|\\left| \\psi_{2} \\right\\rangle \\right|^2$. The circuit looks like this:\n",
411+
"\n",
412+
"<img src=\"images/SWAP_test.png\" alt=\"drawing\" width=\"400\"/>\n",
413+
"\n",
414+
"\n",
415+
"where $\\left| \\Psi_{1} \\right\\rangle$ is the initial quantum state and $\\left| \\Psi_{2} \\right\\rangle$ is the final quantum state befor measurement. Here we are going to prove that the expectation value of the $IIZ$ operator will give us the desired $\\left|\\left\\langle \\psi_{1} \\right|\\left| \\psi_{2} \\right\\rangle \\right|^2$. In other words we should prove that:\n",
416+
"\n",
417+
"$$\\left\\langle \\Psi_2 \\right| IIZ \\left| \\Psi_2 \\right\\rangle = P(0) - P(1) = \\left|\\left\\langle \\psi_{1} \\right|\\left| \\psi_{2} \\right\\rangle \\right|^2$$\n",
418+
"\n",
419+
"where the $P(0)$ ($P(1)$) is the probability of measuring the auxillary qubit in the $\\left| 0 \\right\\rangle$ ($\\left| 1 \\right\\rangle$) state.\n",
420+
"\n",
421+
"First of all let's writh down the initial quantum state:\n",
422+
"\n",
423+
"$$\\left| \\Psi_{1} \\right\\rangle = \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle \\left| 0 \\right\\rangle$$\n",
424+
"\n",
425+
"After the fist Hadamard gate:\n",
426+
"\n",
427+
"$$ IIH \\cdot \\left| \\Psi_{1} \\right\\rangle = \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle \\left| + \\right\\rangle = \\frac{1}{\\sqrt{2}} \\left(\\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle \\left| 0 \\right\\rangle + \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle \\left| 1 \\right\\rangle \\right)$$\n",
428+
"\n",
429+
"Now let's apply CSWAP gate:\n",
430+
"\n",
431+
"$$\\text{CSWAP} \\frac{1}{\\sqrt{2}} \\left(\\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle \\left| 0 \\right\\rangle + \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle \\left| 1 \\right\\rangle \\right) = \\frac{1}{\\sqrt{2}} \\left(\\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle \\left| 0 \\right\\rangle + \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\left| 1 \\right\\rangle \\right)$$\n",
432+
"\n",
433+
"The CSWAP gate has changed (swaped) the quantum states when the control qubit was in the $\\left| 1 \\right\\rangle$ state. Finnaly Let's apply the last Hadamard gate:\n",
434+
"\n",
435+
"$$IIH \\cdot \\frac{1}{\\sqrt{2}} \\left(\\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle \\left| 0 \\right\\rangle + \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\left| 1 \\right\\rangle \\right) = \\frac{1}{2} \\left( \\left( \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle + \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right) \\left| 0 \\right\\rangle + \\left( \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle - \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right) \\left| 1 \\right\\rangle \\right)$$\n",
436+
"\n",
437+
"So, the final quantum state befor the measurement:\n",
438+
"\n",
439+
"$$\\left| \\Psi_{2} \\right\\rangle = \\frac{1}{2} \\left( \\left( \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle + \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right) \\left| 0 \\right\\rangle + \\left( \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle - \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right) \\left| 1 \\right\\rangle \\right)$$\n",
440+
"\n",
441+
"Now we can calculate the expectation value of the $IIZ$ operator for $\\left| \\Psi_{2} \\right\\rangle$ state:\n",
442+
"\n",
443+
"$$\\left\\langle \\Psi_2 \\right| IIZ \\left| \\Psi_2 \\right\\rangle = \\frac{1}{4} \\left(\\left| \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle + \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right|^2 - \\left| \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle - \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right|^2\\right) = \\left|\\left\\langle \\psi_{1} \\right|\\left| \\psi_{2} \\right\\rangle \\right|^2$$\n",
444+
"\n",
445+
"Note that associated with the $IIZ$ operator we have two projector operators: $Pr_0 = II\\left|0 \\right\\rangle \\left\\langle 0 \\right|$ and $Pr_1 = II\\left|1 \\right\\rangle \\left\\langle 1 \\right|$. According to the definition of the projective measurement (page 87 of the [[2]]) the probabilities of measuring $\\left|0 \\right\\rangle$ or $\\left|1\\right\\rangle$ can be found the following way:\n",
446+
"\n",
447+
"$$\n",
448+
"P(0) = \\left\\langle \\Psi_2 \\right| Pr_0 \\left| \\Psi_2 \\right\\rangle = \\frac{1}{4} \\left| \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle + \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right|^2\\\\\n",
449+
"P(1) = \\left\\langle \\Psi_2 \\right| Pr_1 \\left| \\Psi_2 \\right\\rangle = \\frac{1}{4} \\left| \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle - \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right|^2\n",
450+
"$$\n",
451+
"\n",
452+
"With this we prove that:\n",
453+
"\n",
454+
"$$\\frac{1}{4} \\left(\\left| \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle + \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right|^2 - \\left| \\left| \\psi_{2} \\right\\rangle \\left| \\psi_{1} \\right\\rangle - \\left| \\psi_{1} \\right\\rangle \\left| \\psi_{2} \\right\\rangle \\right|^2\\right) = P(0) - P(1)$$\n",
455+
"\n",
456+
"But this was equal also to the $\\left|\\left\\langle \\psi_{1} \\right|\\left| \\psi_{2} \\right\\rangle \\right|^2$. So:\n",
457+
"\n",
458+
"$$\\left|\\left\\langle \\psi_{1} \\right|\\left| \\psi_{2} \\right\\rangle \\right|^2 = P(0) - P(1) = 2 P(0) - 1$$\n",
459+
"\n",
460+
"Hence, we can use the same `z_expectation_from_counts` in order to calculate the expectation value for $IIZ$ operator and consequatly the desired square of the absolute value of the inner product between the quantum states $\\left|\\left\\langle \\psi_{1} \\right|\\left| \\psi_{2} \\right\\rangle \\right|^2$.\n",
461+
"\n",
462+
"Here is the function that implements the SWAP test procidure:\n",
463+
"\n",
464+
"\n",
465+
"[1]: https://github.com/DavitKhach/quantum-algorithms-tutorials\n",
466+
"[2]: https://www.cambridge.org/am/academic/subjects/physics/quantum-physics-quantum-information-and-quantum-computation/quantum-computation-and-quantum-information-10th-anniversary-edition?format=HB\n",
467+
"[3]: https://en.wikipedia.org/wiki/Purity_(quantum_mechanics)\n",
468+
"[4]: https://en.wikipedia.org/wiki/Swap_test"
405469
]
470+
},
471+
{
472+
"cell_type": "code",
473+
"execution_count": null,
474+
"metadata": {},
475+
"outputs": [],
476+
"source": []
406477
}
407478
],
408479
"metadata": {

images/SWAP_test.png

75.7 KB
Loading

0 commit comments

Comments
 (0)