Skip to content

Commit c07dc3a

Browse files
Adam GitterAdam Gitter
authored andcommitted
math renders correctly
1 parent 3017dba commit c07dc3a

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

tutorials/documentation_materials/classiq_101/hadamard_test/hadamard_test.ipynb

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,54 @@
2222
]
2323
},
2424
{
25-
"metadata": {},
2625
"cell_type": "markdown",
26+
"id": "147da155aa39d68e",
27+
"metadata": {},
2728
"source": [
2829
"The overall implementation consists of three functional building blocks followed by a measurement step, as illustrated in the scheme below:\n",
2930
"<div style=\"text-align:center;\">\n",
3031
" <img src=\"https://docs.classiq.io/resources/hadamard_new_blocks.png\" alt=\"hadamard_test_blocks\" border=\"0\">\n",
3132
"</div>\n"
32-
],
33-
"id": "147da155aa39d68e"
33+
]
3434
},
3535
{
3636
"cell_type": "markdown",
3737
"id": "4bd3bffc-f6f4-4820-8d5b-78fe0485b252",
3838
"metadata": {},
3939
"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",
4141
"\n",
4242
"You can refer to the [Mathemtaical Description](#mathematical-description) section describing the full mathematical derivation of a general Hadamard test implementation.\n",
4343
"\n",
4444
"\n"
4545
]
4646
},
4747
{
48-
"metadata": {},
4948
"cell_type": "markdown",
50-
"source": "",
51-
"id": "1e2f444765f86a7"
52-
},
53-
{
49+
"id": "856eee8d023169ff",
5450
"metadata": {},
55-
"cell_type": "markdown",
5651
"source": [
5752
"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",
5853
"\n",
59-
"<details>\n",
54+
"<details markdown>\n",
6055
"\n",
61-
"<summary>NOTE: Quantum Fourier Transform </summary>\n",
56+
"<summary markdown>NOTE: Quantum Fourier Transform </summary>\n",
6257
"\n",
6358
"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",
6560
"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",
6763
"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",
6864
"</details>\n"
69-
],
70-
"id": "856eee8d023169ff"
65+
]
7166
},
7267
{
7368
"cell_type": "markdown",
7469
"id": "4210f979-b262-4b2e-90f7-bf4f85aa266b",
7570
"metadata": {},
7671
"source": [
77-
"## Guided Implementation:"
72+
"## Guided Implementation"
7873
]
7974
},
8075
{
@@ -85,10 +80,14 @@
8580
"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",
8681
"\n",
8782
"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",
8884
"1) Applying the Hadamard gate $H$ to the `expectation_value` qubit as a preparation step, creating a uniform superposition.\n",
85+
"\n",
8986
"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",
9088
"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",
9291
"\n"
9392
]
9493
},
@@ -219,27 +218,28 @@
219218
"metadata": {},
220219
"source": [
221220
"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",
223222
"\\text{Re}\\big(\\langle 0000|U_{qft}|0000\\rangle\\big)=\\langle 0000|++++\\rangle= 0.25\n",
224-
"\\end{equation}\n",
223+
"$$\n",
225224
"\n",
226225
"You can refer to the note below for the complete derivation.\n",
227226
"\n",
228-
"<details>\n",
229227
"\n",
230-
"<summary>Complete derivation </summary>\n",
228+
"<details markdown>\n",
229+
"<summary markdown>Complete derivation </summary>\n",
231230
"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",
233233
"\\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",
235235
"Since applying QFT on $|{0000}\\rangle$ is equivalent to applying a 4-qubit Hadamard transform, transforming it to the $|{++++}\\rangle$ state.\n",
236236
"\n",
237237
"\n",
238238
"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",
239239
"\n",
240-
"\\begin{equation}\n",
240+
"$$\n",
241241
"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",
243243
"where the result of the inner product $\\langle 0000|++++\\rangle=\\frac{1}{4}\\langle 0000|0000\\rangle=\\frac{1}{4}$ is used. \n",
244244
"\n",
245245
"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,9 +351,9 @@
351351
"id": "13ac87dd38425c30",
352352
"metadata": {},
353353
"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",
357357
"\n",
358358
"</details>"
359359
]
@@ -363,15 +363,17 @@
363363
"id": "b6936948c9016610",
364364
"metadata": {},
365365
"source": [
366-
"<details>\n",
366+
"<details markdown>\n",
367367
"\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",
369369
"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",
372373
"\\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",
375377
"</details>"
376378
]
377379
},
@@ -380,7 +382,7 @@
380382
"id": "77c23a51-38b4-4787-9b79-798e9f7e1e4d",
381383
"metadata": {},
382384
"source": [
383-
"## Mathematical Description:"
385+
"## Mathematical Description"
384386
]
385387
},
386388
{
@@ -391,32 +393,32 @@
391393
"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",
392394
"\n",
393395
"The control qubit is first prepared in a uniform superposition by applying a Hadamard transform $H$:\n",
394-
"\\begin{equation}\n",
396+
"$$\n",
395397
"|{\\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",
396398
" 1 & 1 \\\\\n",
397399
" 1 & -1 \\\\\n",
398400
" \\end{array} } \\right)\n",
399-
"\\end{equation}\n",
401+
"$$\n",
400402
"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",
402404
"|{\\phi_2}\\rangle=V|{\\phi_1}\\rangle= |{0}\\rangle U|{\\psi}\\rangle+|{1}\\rangle|{\\psi}\\rangle\n",
403-
"\\end{equation}\n",
405+
"$$\n",
404406
"Another Hadamard transform is then applied to the control qubit:\n",
405-
"\\begin{equation}\n",
407+
"$$\n",
406408
"|{\\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",
408410
"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",
410412
"|{\\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",
412414
"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",
414416
"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",
416418
"And algebraically manipulating it to receive the real part of the expectation value of $U$:\n",
417-
"\\begin{equation}\n",
419+
"$$\n",
418420
"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+
"$$"
420422
]
421423
},
422424
{

0 commit comments

Comments
 (0)