|
22 | 22 | ] |
23 | 23 | }, |
24 | 24 | { |
25 | | - "metadata": {}, |
26 | 25 | "cell_type": "markdown", |
| 26 | + "id": "147da155aa39d68e", |
| 27 | + "metadata": {}, |
27 | 28 | "source": [ |
28 | 29 | "The overall implementation consists of three functional building blocks followed by a measurement step, as illustrated in the scheme below:\n", |
29 | 30 | "<div style=\"text-align:center;\">\n", |
30 | 31 | " <img src=\"https://docs.classiq.io/resources/hadamard_new_blocks.png\" alt=\"hadamard_test_blocks\" border=\"0\">\n", |
31 | 32 | "</div>\n" |
32 | | - ], |
33 | | - "id": "147da155aa39d68e" |
| 33 | + ] |
34 | 34 | }, |
35 | 35 | { |
36 | 36 | "cell_type": "markdown", |
37 | 37 | "id": "4bd3bffc-f6f4-4820-8d5b-78fe0485b252", |
38 | 38 | "metadata": {}, |
39 | 39 | "source": [ |
40 | | - "To implement the Hadamard test algorithm with a unitary $U$ and target state $|{\\psi}\\rangle$ as inputs and control-qubit probabilities as the outputs, a Hadamard gate $H$ is first applied to place the control qubit in a uniform superposition $\\frac{1}{\\sqrt{2}}\\big(|{0}\\rangle|{\\psi}\\rangle+|{1}\\rangle|{\\psi}\\rangle\\big)$. Next, a controlled unitary gate is applied to the target qubit array, resulting in the state $\\frac{1}{\\sqrt{2}}\\big(|{0}\\rangle|{\\psi}\\rangle+|{1}\\rangle U|{\\psi}\\rangle\\big)$. Another Hadamard gate $H$ is then applied to the control qubit, yielding $ \\frac{1}{2}\\big[|{0}\\rangle\\big(\\mathbb{I}+U\\big)|{\\psi}\\rangle+|{1}\\rangle\\big(\\mathbb{I}-U\\big)|{\\psi}\\rangle\\big]$, after which a final measurement is performed on the control qubit. The probability of measuring the control qubit in state $|{0}\\rangle$, given by $P(0)=||\\frac{1}{2}\\big(\\mathbb{I}+U\\big)|{\\psi}\\rangle||^2$, enables the calculation the real part of the expectation value $\\langle\\psi|U|\\psi\\rangle$ in a post-processing step, through the simple algebraic operation $\\text{Re}\\langle\\psi|U|\\psi\\rangle=2P(0)-1$. A different algebraic operation can be used to retrieve the imaginary part of the expectation value.\n", |
| 40 | + "To implement the Hadamard test algorithm with a unitary $U$ and target state $|{\\psi}\\rangle$ as inputs and control-qubit probabilities as the outputs, a Hadamard gate $H$ is first applied to place the control qubit in a uniform superposition $\\frac{1}{\\sqrt{2}}\\big(|{0}\\rangle|{\\psi}\\rangle+|{1}\\rangle|{\\psi}\\rangle\\big)$. Next, a controlled unitary gate is applied to the target qubit array, resulting in the state $\\frac{1}{\\sqrt{2}}\\big(|{0}\\rangle|{\\psi}\\rangle+|{1}\\rangle U|{\\psi}\\rangle\\big)$. Another Hadamard gate $H$ is then applied to the control qubit, yielding $\\frac{1}{2}\\big(|{0}\\rangle\\big(\\mathbb{I}+U\\big)|{\\psi}\\rangle+|{1}\\rangle\\big(\\mathbb{I}-U\\big)|{\\psi}\\rangle\\big)$, after which a final measurement is performed on the control qubit. The probability of measuring the control qubit in state $|{0}\\rangle$, given by $P(0)=||\\frac{1}{2}\\big(\\mathbb{I}+U\\big)|{\\psi}\\rangle||^2$, enables the calculation the real part of the expectation value $\\langle\\psi|U|\\psi\\rangle$ in a post-processing step, through the simple algebraic operation $\\text{Re}\\langle\\psi|U|\\psi\\rangle=2P(0)-1$. A different algebraic operation can be used to retrieve the imaginary part of the expectation value.\n", |
41 | 41 | "\n", |
42 | 42 | "You can refer to the [Mathemtaical Description](#mathematical-description) section describing the full mathematical derivation of a general Hadamard test implementation.\n", |
43 | 43 | "\n", |
44 | 44 | "\n" |
45 | 45 | ] |
46 | 46 | }, |
47 | 47 | { |
48 | | - "metadata": {}, |
49 | 48 | "cell_type": "markdown", |
50 | | - "source": "", |
51 | | - "id": "1e2f444765f86a7" |
52 | | - }, |
53 | | - { |
| 49 | + "id": "856eee8d023169ff", |
54 | 50 | "metadata": {}, |
55 | | - "cell_type": "markdown", |
56 | 51 | "source": [ |
57 | 52 | "In this tutorial, we will implement a Hadamard test for the Quantum Fourier Transform (QFT) unitary $U_{QFT}$ ([read more](https://docs.classiq.io/latest/explore/functions/qmod_library_reference/classiq_open_library/qft/qft/ )), acting on a 4-qubit target state $|0000\\rangle$ using the Classiq IDE/ SDK. This implementation leverages Classiq's high-level functional design utilities, following the functional block structure outlined above.\n", |
58 | 53 | "\n", |
59 | | - "<details>\n", |
| 54 | + "<details markdown>\n", |
60 | 55 | "\n", |
61 | | - "<summary>NOTE: Quantum Fourier Transform </summary>\n", |
| 56 | + "<summary markdown>NOTE: Quantum Fourier Transform </summary>\n", |
62 | 57 | "\n", |
63 | 58 | "The Quantum Fourier Transform (QFT) function is the quantum analog for the discrete Fourier transform. It is applied on the quantum register state vector in the following manner:\n", |
64 | | - "\\begin{equation}\n", |
| 59 | + "$$\n", |
65 | 60 | "U_{QFT}|{j}\\rangle=\\frac{1}{\\sqrt{2^n}}\\sum_{k=0}^{2^n-1}e^{\\frac{2\\pi i}{2^n}jk}|{k}\\rangle=\\otimes_{t=1}^n\\frac{1}{\\sqrt{2}}\\big(|{0}\\rangle+e^{\\frac{2\\pi i}{2^{t}}j}|{1}\\rangle)\n", |
66 | | - "\\end{equation}\n", |
| 61 | + "$$\n", |
| 62 | + "\n", |
67 | 63 | "Where $j$ and $k$ are the binary numbers the $n$ qubits represent. [more information](https://docs.classiq.io/latest/explore/functions/qmod_library_reference/classiq_open_library/qft/qft/ )\n", |
68 | 64 | "</details>\n" |
69 | | - ], |
70 | | - "id": "856eee8d023169ff" |
| 65 | + ] |
71 | 66 | }, |
72 | 67 | { |
73 | 68 | "cell_type": "markdown", |
74 | 69 | "id": "4210f979-b262-4b2e-90f7-bf4f85aa266b", |
75 | 70 | "metadata": {}, |
76 | 71 | "source": [ |
77 | | - "## Guided Implementation:" |
| 72 | + "## Guided Implementation" |
78 | 73 | ] |
79 | 74 | }, |
80 | 75 | { |
|
85 | 80 | "To implement the Hadamard test for the QFT unitary $U_{QFT}$ and the state $|0000\\rangle$, one control qubit and an array of four target qubits are initialized. The control qubit variable will be named `expectation_value` and is of `QBit` type, and the target qubit array will be captured by the `QArray` type variable `psi`. All qubits are initialized in $|0\\rangle$ states, and the state of the `psi` qubit array will remain unchanged throughout the implementation ($|0000\\rangle$). \n", |
86 | 81 | "\n", |
87 | 82 | "Our implementation of the Hadamard test involves three main steps, followed by a measurement of the `expectation_value` qubit and a post-processing step to obtain the real part of the expectation value:\n", |
| 83 | + "\n", |
88 | 84 | "1) Applying the Hadamard gate $H$ to the `expectation_value` qubit as a preparation step, creating a uniform superposition.\n", |
| 85 | + "\n", |
89 | 86 | "2) Applying the unitary gate $U_{QFT}$ on the `psi` qubit array in a controlled manner, conditioned on the control qubit being in the $|{1}\\rangle$ state.\n", |
| 87 | + "\n", |
90 | 88 | "3) Re-application of a Hadamard gate $H$ to the control qubit that can be seen as an inverse preparation step, with $H$ acting as its own inverse.\n", |
91 | | - "4) a projective measurement of the `expectation_value`, yielding the probabilities of measuring it in the $|0\\rangle$ and $|1\\rangle$ states. The probability $P(0)$ of being in the $|0\\rangle$ state is then algebraically manipulated in a post-processing step to yield the real part of the expectation value by using the expression $2P(0)-1$.\n", |
| 89 | + "\n", |
| 90 | + "4) A projective measurement of the `expectation_value` qubit, yielding the probabilities of measuring it in the $|0\\rangle$ and $|1\\rangle$ states. The probability $P(0)$ of being in the $|0\\rangle$ state is then algebraically manipulated in a post-processing step to yield the real part of the expectation value by using the expression $2P(0)-1$.\n", |
92 | 91 | "\n" |
93 | 92 | ] |
94 | 93 | }, |
|
219 | 218 | "metadata": {}, |
220 | 219 | "source": [ |
221 | 220 | "By declaring `psi` as a local variable in the `main` function (in contrast to `expectation_value`, which is declared globally), the execution of the quantum program on the Classiq simulator will yield the measurement outcomes of the `expectation_value` qubit in states $|{0}\\rangle$ and $|{1}\\rangle$, along with the corresponding measurement probabilities. The probability $P(0)$ can then be algebraically manipulated to calculate the real part of the expectation value, which should align with the analytical result:\n", |
222 | | - "\\begin{equation}\n", |
| 221 | + "$$\n", |
223 | 222 | "\\text{Re}\\big(\\langle 0000|U_{qft}|0000\\rangle\\big)=\\langle 0000|++++\\rangle= 0.25\n", |
224 | | - "\\end{equation}\n", |
| 223 | + "$$\n", |
225 | 224 | "\n", |
226 | 225 | "You can refer to the note below for the complete derivation.\n", |
227 | 226 | "\n", |
228 | | - "<details>\n", |
229 | 227 | "\n", |
230 | | - "<summary>Complete derivation </summary>\n", |
| 228 | + "<details markdown>\n", |
| 229 | + "<summary markdown>Complete derivation </summary>\n", |
231 | 230 | "After the execution of the `main` function, the system is evolved into its final quantum state:\n", |
232 | | - "\\begin{equation}\n", |
| 231 | + "\n", |
| 232 | + "$$\n", |
233 | 233 | "\\frac{1}{2}\\Big(|{0}\\rangle\\big(\\mathbb{I}+U_{QFT}\\big)|{0000}\\rangle+|{1}\\rangle\\big(\\mathbb{I}-U_{QFT}\\big)|{0000}\\rangle\\Big)=\\frac{1}{2}\\Big(|{0}\\rangle\\big(|{0000}\\rangle+|{++++}\\rangle\\big)+|{1}\\rangle\\big(|{0000}\\rangle-|{++++}\\rangle\\big)\\Big)\n", |
234 | | - "\\end{equation}\n", |
| 234 | + "$$\n", |
235 | 235 | "Since applying QFT on $|{0000}\\rangle$ is equivalent to applying a 4-qubit Hadamard transform, transforming it to the $|{++++}\\rangle$ state.\n", |
236 | 236 | "\n", |
237 | 237 | "\n", |
238 | 238 | "Running the program on the Classiq simulator outputs the measurement results for both states of the control qubits, which can be analytically calculated and compared:\n", |
239 | 239 | "\n", |
240 | | - "\\begin{equation}\n", |
| 240 | + "$$\n", |
241 | 241 | "P(0)=\\frac{1}{4}|||{0000}\\rangle+|{++++}\\rangle||^2=\\frac{1}{2}\\big(1+\\frac{1}{4}\\big)=0.625,\\;\\;\\;\\;\\;\\;\\;\\; P(1)=\\frac{1}{4}|||{0000}\\rangle-|{++++}\\rangle||^2=\\frac{1}{2}\\big(1-\\frac{1}{4}\\big)=0.375\n", |
242 | | - "\\end{equation}\n", |
| 242 | + "$$\n", |
243 | 243 | "where the result of the inner product $\\langle 0000|++++\\rangle=\\frac{1}{4}\\langle 0000|0000\\rangle=\\frac{1}{4}$ is used. \n", |
244 | 244 | "\n", |
245 | 245 | "The probabilities can then be manipulated to calculate the expectation value as $\\text{Re}\\big(\\langle 0000|U_{qft}|0000\\rangle\\big)=2P(0)-1=0.25$, yielding the same result as the direct calculation provided above in the main text.\n", |
|
351 | 351 | "id": "13ac87dd38425c30", |
352 | 352 | "metadata": {}, |
353 | 353 | "source": [ |
354 | | - "<details>\n", |
355 | | - "<summary> Optional Exercise </summary>\n", |
356 | | - "Run the above example of the Hadamard Test for the QFT and the $|0000\\rangle$ state from the SDK using 1,000, 2,000, 4,000, 8,000 and 16,000 shots. For each job, calculate the real part of the expectation value using the formula $\\text{Re}\\big(\\langle{0000}|U_{QFT}|\\rangle{0000}\\big) = 2P_0-1$. Plot a graph of the expectation value as a function of the number of shots. Add to the graph the theoretical value. Explain the results, what is the mean and the variance for each execution? Why is that?\n", |
| 354 | + "<details markdown>\n", |
| 355 | + "<summary markdown> Optional Exercise </summary>\n", |
| 356 | + "Run the above example of the Hadamard Test for the QFT and the $|0000\\rangle$ state from the SDK using 1,000, 2,000, 4,000, 8,000 and 16,000 shots. For each job, calculate the real part of the expectation value using the formula $\\text{Re}\\big(\\langle{0000}|U_{QFT}|\\rangle{0000}\\big) = 2P_0-1$. Plot a graph of the expectation value as a function of the number of shots. Add to the graph the theoretical value. Explain the results. What is the mean and the variance for each execution? Why is that?\n", |
357 | 357 | "\n", |
358 | 358 | "</details>" |
359 | 359 | ] |
|
363 | 363 | "id": "b6936948c9016610", |
364 | 364 | "metadata": {}, |
365 | 365 | "source": [ |
366 | | - "<details>\n", |
| 366 | + "<details markdown>\n", |
367 | 367 | "\n", |
368 | | - "<summary>NOTE: Precision of the Results (read only after completing the exercise) </summary>\n", |
| 368 | + "<summary markdown> NOTE: Precision of the Results (read only after completing the exercise) </summary>\n", |
369 | 369 | "Increasing the `tot_num_shots` parameter in the execution preferences will enhance the precision of the expectation value estimation. \n", |
370 | | - "This improvement arises from the statistical nature of the measurements: each measurement represents a sample from a distribution modeled by a classical random variable, with the expectation value corresponding to the mean of this distribution. As a result, the law of large numbers applies, and the standard error of the sample mean $ \\langle U \\rangle$ is inversely proportional to the square root of the sample size: \n", |
371 | | - "\\begin{equation}\n", |
| 370 | + "This improvement arises from the statistical nature of the measurements; each measurement represents a sample from a distribution modeled by a classical random variable, with the expectation value corresponding to the mean of this distribution. As a result, the law of large numbers applies, and the standard error of the sample mean $ \\langle U \\rangle$ is inversely proportional to the square root of the sample size: \n", |
| 371 | + "\n", |
| 372 | + "$$\n", |
372 | 373 | "\\sigma_{\\langle U\\rangle}=\\frac{\\sigma}{\\sqrt{n}}\n", |
373 | | - "\\end{equation}\n", |
374 | | - "where $\\sigma $ is the standard deviation of the random variable modeling the measurements, and $ n $ represents the total number of measurements (`tot_num_shots`). This formula demonstrates that increasing the number of measurements reduces the statistical error of the estimated mean, resulting in a more reliable estimation of $ \\langle U \\rangle$ \n", |
| 374 | + "$$\n", |
| 375 | + "\n", |
| 376 | + "Where $\\sigma $ is the standard deviation of the random variable modeling the measurements, and $ n $ represents the total number of measurements (`tot_num_shots`). This formula demonstrates that increasing the number of measurements reduces the statistical error of the estimated mean, resulting in a more reliable estimation of $ \\langle U \\rangle$ \n", |
375 | 377 | "</details>" |
376 | 378 | ] |
377 | 379 | }, |
|
380 | 382 | "id": "77c23a51-38b4-4787-9b79-798e9f7e1e4d", |
381 | 383 | "metadata": {}, |
382 | 384 | "source": [ |
383 | | - "## Mathematical Description:" |
| 385 | + "## Mathematical Description" |
384 | 386 | ] |
385 | 387 | }, |
386 | 388 | { |
|
391 | 393 | "The following mathematical description is for an implementation of a Hadamard test on a system of one control qubit and a target qubit-array of $N$ qubits, initiated in $|{0}\\rangle|{\\psi}\\rangle$, where $|{\\psi}\\rangle$ may essentially be in any prepared state. \n", |
392 | 394 | "\n", |
393 | 395 | "The control qubit is first prepared in a uniform superposition by applying a Hadamard transform $H$:\n", |
394 | | - "\\begin{equation}\n", |
| 396 | + "$$\n", |
395 | 397 | "|{\\phi_1}\\rangle=\\big(H\\otimes\\mathbb{I}\\big)|{0}\\rangle|{\\psi}\\rangle= \\frac{1}{\\sqrt{2}}\\Big(|{0}\\rangle|{\\psi}\\rangle+|{1}\\rangle|{\\psi}\\rangle\\Big) \\qquad\\qquad;\\qquad\\qquad H=\\frac{1}{\\sqrt{2}}\\left( {\\begin{array}{cc}\n", |
396 | 398 | " 1 & 1 \\\\\n", |
397 | 399 | " 1 & -1 \\\\\n", |
398 | 400 | " \\end{array} } \\right)\n", |
399 | | - "\\end{equation}\n", |
| 401 | + "$$\n", |
400 | 402 | "This step is sequentially followed by a selection step, in which a controlled unitary operation of the form $V=|{0}\\rangle\\langle{0}|\\otimes \\mathbb{I}+|{1}\\rangle\\langle{1}|\\otimes U$ is successively applied to the target qubit(s), where $U$ is some general unitary matrix:\n", |
401 | | - "\\begin{equation}\n", |
| 403 | + "$$\n", |
402 | 404 | "|{\\phi_2}\\rangle=V|{\\phi_1}\\rangle= |{0}\\rangle U|{\\psi}\\rangle+|{1}\\rangle|{\\psi}\\rangle\n", |
403 | | - "\\end{equation}\n", |
| 405 | + "$$\n", |
404 | 406 | "Another Hadamard transform is then applied to the control qubit:\n", |
405 | | - "\\begin{equation}\n", |
| 407 | + "$$\n", |
406 | 408 | "|{\\phi_3}\\rangle=\\big(H\\otimes\\mathbb{I}\\big)|{\\phi_2}\\rangle= \\frac{1}{2}\\Big(|{0}\\rangle|{\\psi}\\rangle+|{1}\\rangle|{\\psi}\\rangle+|{0}\\rangle U|{\\psi}\\rangle-|{1}\\rangle U|{\\psi}\\rangle\\Big)=\\frac{1}{2}\\Big(|{0}\\rangle\\big(\\mathbb{I}+U\\big)|{\\psi}\\rangle+|{1}\\rangle\\big(\\mathbb{I}-U\\big)|{\\psi}\\rangle\\Big)\n", |
407 | | - "\\end{equation}\n", |
| 409 | + "$$\n", |
408 | 410 | "The final step, prior to the post-processing algebraic manipulation, is a projective measurement of the control (ancilla) qubit onto the $|{0}\\rangle$ subspace $\\mathcal{P}=|{0}\\rangle\\langle{0}|\\otimes\\mathbb{I}$:\n", |
409 | | - "\\begin{equation}\n", |
| 411 | + "$$\n", |
410 | 412 | "|{\\phi_4}\\rangle=\\mathcal{P}|{\\phi_3}\\rangle=\\big(|{0}\\rangle\\langle{0}|\\otimes\\mathbb{I}\\big)|{\\phi_3}\\rangle=\\frac{1}{2}|{0}\\rangle\\big(\\mathbb{I}+U\\big)|{\\psi}\\rangle\n", |
411 | | - "\\end{equation}\n", |
| 413 | + "$$\n", |
412 | 414 | "This measurement is effectively translated into a measurement of the expectation value $\\text{Re}\\big(\\langle{\\psi}|U|{\\psi}\\rangle\\big)$ by first obtaining the probability that the control qubit is in the $|{0}\\rangle$ state:\n", |
413 | | - "\\begin{equation}\n", |
| 415 | + "$$\n", |
414 | 416 | "P(0)=||\\frac{1}{2}\\big(\\mathbb{I}+U\\big)|{\\psi}\\rangle||^2=\\frac{1}{4}\\langle{\\psi}|\\big(\\mathbb{I}+U^\\dagger\\big)\\big(\\mathbb{I}+U\\big)|{\\psi}\\rangle=\\frac{1}{4}\\Big(\\langle{\\psi}|{\\psi}\\rangle+\\langle{\\psi}|U^\\dagger|{\\psi}\\rangle+\\langle{\\psi}|U|{\\psi}\\rangle+\\langle{\\psi}|UU^\\dagger|{\\psi}\\rangle\\Big)=\\frac{1}{4}\\Big(2+\\langle{\\psi}|U^\\dagger|{\\psi}\\rangle+\\langle{\\psi}|U|{\\psi}\\rangle\\Big)\n", |
415 | | - "\\end{equation}\n", |
| 417 | + "$$\n", |
416 | 418 | "And algebraically manipulating it to receive the real part of the expectation value of $U$:\n", |
417 | | - "\\begin{equation}\n", |
| 419 | + "$$\n", |
418 | 420 | "2P(0)-1=\\frac{1}{2}\\Big(2+\\langle{\\psi}|U^\\dagger|{\\psi}\\rangle+\\langle{\\psi}|U|{\\psi}\\rangle\\Big)-1=\\frac{1}{2}\\Big(\\langle{\\psi}|U^\\dagger|{\\psi}\\rangle+\\langle{\\psi}|U|{\\psi}\\rangle\\Big)=\\frac{1}{2}\\Big(\\big(\\langle{\\psi}|U|{\\psi}\\rangle\\big)^\\dagger+\\langle{\\psi}|U|{\\psi}\\rangle\\Big)=\\text{Re}\\big(\\langle{\\psi}|U|{\\psi}\\rangle\\big)\n", |
419 | | - "\\end{equation}" |
| 421 | + "$$" |
420 | 422 | ] |
421 | 423 | }, |
422 | 424 | { |
|
0 commit comments