Skip to content

Commit

Permalink
math renders correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Gitter authored and Adam Gitter committed Dec 8, 2024
1 parent 3017dba commit c07dc3a
Showing 1 changed file with 50 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,54 @@
]
},
{
"metadata": {},
"cell_type": "markdown",
"id": "147da155aa39d68e",
"metadata": {},
"source": [
"The overall implementation consists of three functional building blocks followed by a measurement step, as illustrated in the scheme below:\n",
"<div style=\"text-align:center;\">\n",
" <img src=\"https://docs.classiq.io/resources/hadamard_new_blocks.png\" alt=\"hadamard_test_blocks\" border=\"0\">\n",
"</div>\n"
],
"id": "147da155aa39d68e"
]
},
{
"cell_type": "markdown",
"id": "4bd3bffc-f6f4-4820-8d5b-78fe0485b252",
"metadata": {},
"source": [
"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",
"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",
"\n",
"You can refer to the [Mathemtaical Description](#mathematical-description) section describing the full mathematical derivation of a general Hadamard test implementation.\n",
"\n",
"\n"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "",
"id": "1e2f444765f86a7"
},
{
"id": "856eee8d023169ff",
"metadata": {},
"cell_type": "markdown",
"source": [
"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",
"\n",
"<details>\n",
"<details markdown>\n",
"\n",
"<summary>NOTE: Quantum Fourier Transform </summary>\n",
"<summary markdown>NOTE: Quantum Fourier Transform </summary>\n",
"\n",
"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",
"\\begin{equation}\n",
"$$\n",
"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",
"\\end{equation}\n",
"$$\n",
"\n",
"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",
"</details>\n"
],
"id": "856eee8d023169ff"
]
},
{
"cell_type": "markdown",
"id": "4210f979-b262-4b2e-90f7-bf4f85aa266b",
"metadata": {},
"source": [
"## Guided Implementation:"
"## Guided Implementation"
]
},
{
Expand All @@ -85,10 +80,14 @@
"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",
"\n",
"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",
"\n",
"1) Applying the Hadamard gate $H$ to the `expectation_value` qubit as a preparation step, creating a uniform superposition.\n",
"\n",
"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",
"\n",
"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",
"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",
"\n",
"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",
"\n"
]
},
Expand Down Expand Up @@ -219,27 +218,28 @@
"metadata": {},
"source": [
"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",
"\\begin{equation}\n",
"$$\n",
"\\text{Re}\\big(\\langle 0000|U_{qft}|0000\\rangle\\big)=\\langle 0000|++++\\rangle= 0.25\n",
"\\end{equation}\n",
"$$\n",
"\n",
"You can refer to the note below for the complete derivation.\n",
"\n",
"<details>\n",
"\n",
"<summary>Complete derivation </summary>\n",
"<details markdown>\n",
"<summary markdown>Complete derivation </summary>\n",
"After the execution of the `main` function, the system is evolved into its final quantum state:\n",
"\\begin{equation}\n",
"\n",
"$$\n",
"\\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",
"\\end{equation}\n",
"$$\n",
"Since applying QFT on $|{0000}\\rangle$ is equivalent to applying a 4-qubit Hadamard transform, transforming it to the $|{++++}\\rangle$ state.\n",
"\n",
"\n",
"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",
"\n",
"\\begin{equation}\n",
"$$\n",
"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",
"\\end{equation}\n",
"$$\n",
"where the result of the inner product $\\langle 0000|++++\\rangle=\\frac{1}{4}\\langle 0000|0000\\rangle=\\frac{1}{4}$ is used. \n",
"\n",
"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",
Expand Down Expand Up @@ -351,9 +351,9 @@
"id": "13ac87dd38425c30",
"metadata": {},
"source": [
"<details>\n",
"<summary> Optional Exercise </summary>\n",
"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",
"<details markdown>\n",
"<summary markdown> Optional Exercise </summary>\n",
"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",
"\n",
"</details>"
]
Expand All @@ -363,15 +363,17 @@
"id": "b6936948c9016610",
"metadata": {},
"source": [
"<details>\n",
"<details markdown>\n",
"\n",
"<summary>NOTE: Precision of the Results (read only after completing the exercise) </summary>\n",
"<summary markdown> NOTE: Precision of the Results (read only after completing the exercise) </summary>\n",
"Increasing the `tot_num_shots` parameter in the execution preferences will enhance the precision of the expectation value estimation. \n",
"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",
"\\begin{equation}\n",
"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",
"\n",
"$$\n",
"\\sigma_{\\langle U\\rangle}=\\frac{\\sigma}{\\sqrt{n}}\n",
"\\end{equation}\n",
"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",
"$$\n",
"\n",
"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",
"</details>"
]
},
Expand All @@ -380,7 +382,7 @@
"id": "77c23a51-38b4-4787-9b79-798e9f7e1e4d",
"metadata": {},
"source": [
"## Mathematical Description:"
"## Mathematical Description"
]
},
{
Expand All @@ -391,32 +393,32 @@
"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",
"\n",
"The control qubit is first prepared in a uniform superposition by applying a Hadamard transform $H$:\n",
"\\begin{equation}\n",
"$$\n",
"|{\\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",
" 1 & 1 \\\\\n",
" 1 & -1 \\\\\n",
" \\end{array} } \\right)\n",
"\\end{equation}\n",
"$$\n",
"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",
"\\begin{equation}\n",
"$$\n",
"|{\\phi_2}\\rangle=V|{\\phi_1}\\rangle= |{0}\\rangle U|{\\psi}\\rangle+|{1}\\rangle|{\\psi}\\rangle\n",
"\\end{equation}\n",
"$$\n",
"Another Hadamard transform is then applied to the control qubit:\n",
"\\begin{equation}\n",
"$$\n",
"|{\\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",
"\\end{equation}\n",
"$$\n",
"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",
"\\begin{equation}\n",
"$$\n",
"|{\\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",
"\\end{equation}\n",
"$$\n",
"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",
"\\begin{equation}\n",
"$$\n",
"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",
"\\end{equation}\n",
"$$\n",
"And algebraically manipulating it to receive the real part of the expectation value of $U$:\n",
"\\begin{equation}\n",
"$$\n",
"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",
"\\end{equation}"
"$$"
]
},
{
Expand Down

0 comments on commit c07dc3a

Please sign in to comment.