diff --git a/algorithms/aqc/solving_qlsp/solving_qlsp_with_aqc.ipynb b/algorithms/aqc/solving_qlsp/solving_qlsp_with_aqc.ipynb index bab11aaa2..2bf68f8f4 100644 --- a/algorithms/aqc/solving_qlsp/solving_qlsp_with_aqc.ipynb +++ b/algorithms/aqc/solving_qlsp/solving_qlsp_with_aqc.ipynb @@ -13,25 +13,22 @@ "id": "60a44213-afd1-429b-ab6d-7bab93de1734", "metadata": {}, "source": [ - "### **Overview**\n", - "\n", - "\n", - "**Adiabatic Quantum Computing (AQC)** leverages the adiabatic theorem to solve computational problems by gradually evolving a quantum system from an initial ground state to the ground state of a problem-specific Hamiltonian (see our AQC tutorial). \n", + "**Adiabatic Quantum Computing (AQC)** leverages the adiabatic theorem to solve computational problems by gradually evolving a quantum system from an initial ground state to the ground state of a problem-specific Hamiltonian (see the AQC tutorial). \n", "\n", "This tutorial focuses on applying the AQC approach to solve the **Quantum Linear Systems Problem (QLSP)**, a cornerstone problem in quantum computing with significant applications in fields like machine learning, physics, and optimization.\n", "\n", - "Specifically, we aim to demonstrate how AQC can be utilized to approximate the solution to the QLSP [[1](#QLSP)]. This problem involves finding a quantum state that corresponds to the solution of a linear system of equations. The tutorial provides a structured overview of the QLSP, its mathematical formulation, and the steps needed to transform it into an eigenvalue problem, laying the foundation for solving it within the AQC framework.\n", + "Specifically, we aim to demonstrate how to utilize AQC to approximate the solution to the QLSP [[1](#QLSP)]. This problem involves finding a quantum state that corresponds to the solution of a linear system of equations. The tutorial provides a structured overview of the QLSP, its mathematical formulation, and the steps needed to transform it into an eigenvalue problem, laying the foundation for solving it within the AQC framework.\n", "\n", - "*The following demonstration we will follow the paper [[1](#QLSP)]. The notebook was written in collaboration with Prof. Lin Lin and Dr. Dong An, the authors of the paper.*\n", + "*This demonstration follows the [[1](#QLSP)] paper. This notebook was written in collaboration with Prof. Lin Lin and Dr. Dong An, the authors of the paper.*\n", "\n", "\n", "---\n", "\n", "\n", - "### **1. Problem Statement**\n", + "## Problem Statement\n", "\n", "Given a Hermitian positive-definite matrix $A$ and a vector $|b\\rangle$, the goal is to approximate \n", - "$|x\\rangle$, the solution to the linear system $A|x\\rangle=|b\\rangle$, as a quantum state.\n", + "$|x\\rangle$—the solution to the linear system $A|x\\rangle=|b\\rangle$—as a quantum state.\n", "\n", "\n", "We are given:\n", @@ -39,19 +36,18 @@ "- **Vector** $|b\\rangle \\in \\mathbb{C}^N$, a normalized vector.\n", "- **Target Error** $\\epsilon$, specifying the desired accuracy.\n", "\n", - "The goal is to prepare a quantum state $|x_a\\rangle$, which is an $\\epsilon$-approximation of the normalized solution $|x\\rangle = A^{-1}|b\\rangle / \\|A^{-1}|b\\rangle\\|_2$. The approximation satisfies:\n", + "The goal is to prepare a quantum state $|x_a\\rangle$, which is an $\\epsilon$-approximation of the normalized solution $|x\\rangle = A^{-1}|b\\rangle / \\|A^{-1}|b\\rangle\\|_2$. The approximation satisfies\n", "\n", "$$\n", "\\| |x_a\\rangle \\langle x_a| - |x\\rangle \\langle x| \\|_2 \\leq \\epsilon.\n", "$$\n", "\n", - "---\n", "\n", - "### **2. Transformation into AQC**\n", + "## Transformation into AQC\n", "\n", - "The QLSP is converted into an equivalent eigenvalue problem to leverage quantum computation. This involves the following steps:\n", + "The QLSP is converted into an equivalent eigenvalue problem to leverage quantum computation. This involves the following steps.\n", "\n", - "#### **2.1 Constructing $H_0$**\n", + "### 1. Constructing $H_0$\n", "Define:\n", "\n", "$$\n", @@ -74,7 +70,7 @@ "$$\n", "\n", "\n", - ">*The **null space** of a matrix $A$ is the set of all vectors $ \\mathbf{x} $ such that:*\n", + ">*The **null space** of a matrix $A$ is the set of all vectors $ \\mathbf{x} $ such that*\n", ">\n", "> $A\\mathbf{x} = 0.$\n", ">\n", @@ -82,7 +78,7 @@ ">- *The null space corresponds to the eigenspace of $A$ associated with the eigenvalue $0$.*\n", ">- *Any vector in the null space is an eigenvector of $A$ with eigenvalue $0$.*\n", "\n", - "#### **2.2 Constructing $H_1$**\n", + "### 2. Constructing $H_1$\n", "Define:\n", "\n", "$$\n", @@ -97,16 +93,12 @@ "\n", "Key properties:\n", "- If $A|x\\rangle \\propto |b\\rangle$, then $Q_bA|x\\rangle = Q_b|b\\rangle = 0$.\n", - "- Null space of $H_1$: $\\text{Null}(H_1) = \\text{span}(|\\tilde{x}\\rangle, |\\bar{b}\\rangle)$ , where\n", + "- Null space of $H_1$: $\\text{Null}(H_1) = \\text{span}(|\\tilde{x}\\rangle, |\\bar{b}\\rangle)$, where\n", "$$\n", "|\\tilde{x}\\rangle = |0, x\\rangle = \\begin{bmatrix} x \\\\ 0 \\end{bmatrix}, \\quad \n", "$$ \n", "\n", - "\n", - "\n", - "\n", - "\n", - "#### **2.3 Adiabatic Interpolation**\n", + "### 3. Adiabatic Interpolation\n", "Construct an interpolation Hamiltonian:\n", "\n", "$$\n", @@ -115,27 +107,23 @@ "\n", "where $f(s)$ is a monotonic function mapping $[0, 1] \\to [0, 1]$.\n", "\n", - "\n", - "\n", - "#### **2.4 Spectral Gap**\n", + "### 4. Spectral Gap\n", "- $Q_b$ is a projection operator, and the spectral gap between $0$ and the rest of the eigenvalues of $H_0$ is $1$. \n", "- For $H_1$, the gap between $0$ and the rest of the eigenvalues is bounded from below by $1/\\kappa$. [[1](#QLSP)]\n", "\n", "\n", + "### 5. Adiabatic Evolution and Null Space\n", "\n", + "Note that there is a degeneracy in the number of null states (unlike the regular adiabatic algorithm usage where we typically look at a single ground state):\n", + "$$\\text{Null}(H_1) = \\text{span}(|\\tilde{x}\\rangle, |\\bar{b}\\rangle)$.$\n", "\n", - "#### **2.5 Adiabatic Evolution and Null Space**\n", - "\n", - "Notice that there is a degeneracy in the number of Null states (unlike the regular adiabatic algorithm usage where we are typically looking at a single grounde state)\n", - "$$\\text{Null}(H_1) = \\text{span}(|\\tilde{x}\\rangle, |\\bar{b}\\rangle)$$\n", - "\n", - "We also note that for any $s$, $|\\bar{b}\\rangle$ is always in the null space of $H(f(s))$, i.e.,\n", + "We also note that for any $s$, $|\\bar{b}\\rangle$ is always in the null space of $H(f(s))$; i.e.,\n", "\n", "$$\n", "|\\bar{b}\\rangle \\in \\text{Null}(H(f(s))).\n", "$$\n", "\n", - "Therefore, there exist an additional statestate $|\\tilde{x(s)}\\rangle = |0\\rangle \\otimes |x(s)\\rangle$, such that \n", + "Therefore, there exists an additional statestate $|\\tilde{x(s)}\\rangle = |0\\rangle \\otimes |x(s)\\rangle$, such that \n", "\n", "$$\n", "\\text{Null}(H(f(s))) = \\{|\\tilde{x(s)}\\rangle, |\\bar{b}\\rangle\\}.\n", @@ -147,26 +135,21 @@ "\n", "Thus, $|\\tilde{x(s)}\\rangle$ state represents the desired **adiabatic path** for the evolution [[1](#QLSP)].\n", "\n", - "\n", - "---\n", - "\n", - "### **3. AQC Approach to solve QLSP**\n", + "## AQC Approach to Solving QLSP\n", "The adiabatic quantum algorithm prepares the zero-energy state $|\\tilde{x}\\rangle$ of $H_1$ as follows:\n", - "1. Initialize in the ground state of $H_0$, i.e., $|\\tilde{b}\\rangle$.\n", + "1. Initialize in the ground state of $H_0$; i.e., $|\\tilde{b}\\rangle$.\n", "2. Slowly evolve the system by varying $f(s)$ from $f(0) = 0$ to $f(1) = 1$.\n", - "3. At the end of the evolution, the system will approximate $|\\tilde{x}\\rangle$, embedding $|x\\rangle$ - the solution of the QLSP.\n", - "\n", - "---\n", + "3. At the end of the evolution, the system approximates $|\\tilde{x}\\rangle$, embedding $|x\\rangle$, which is the solution of the QLSP.\n", "\n", - "### **4. Goals**\n", - "- **Set up a QLSP example:** Derive $H_0$, $H_1$ and define the interpolation Hamiltonian\n", - "- **Quantum Circuit Design:** Implement Hamiltonian simulation for $H(f(s))$.\n", + "Goals:\n", + "- **Set up a QLSP example:** Derive $H_0$, $H_1$ and define the interpolation Hamiltonian.\n", + "- **Quantum circuit design:** Implement Hamiltonian simulation for $H(f(s))$.\n", "- **Evaluate results:** Compare quantum simulation results with the numeric calculation.\n", "\n", "---\n", "\n", "\n", - "Let’s begin with the mathematical setup and proceed to implementation!\n" + "Let’s begin with the mathematical setup and continue on to implementation.\n" ] }, { @@ -174,11 +157,9 @@ "id": "3372f8e1-f1a5-4ca5-a50a-212cb5a3db0b", "metadata": {}, "source": [ - "## **Setting up a QLSP example where A is a 4x4 matrix:**\n", - "\n", - "### **Setting an example**\n", + "## Setting up a QLSP Example Where A Is a 4x4 Matrix\n", "\n", - "For simplicity, we first assume A is Hermitian and positive definite\n" + "For simplicity, we first assume A is Hermitian and positive definite.\n" ] }, { @@ -200,7 +181,7 @@ "id": "e1d22ca6-9bce-4c59-9554-524a79b83ddd", "metadata": {}, "source": [ - "As a purely mathematical pre-processing step, we will calculate the condition number $k$ for $A$. \n", + "As a purely mathematical preprocessing step, we calculate the condition number $k$ for $A$. \n", "> In practical scenarios, the condition number is often approximated or known beforehand based on external factors or prior knowledge." ] }, @@ -259,11 +240,11 @@ "id": "3168a534-2ce2-4150-95f9-c8e88b28a3b4", "metadata": {}, "source": [ - "### **Construct $H_0$ and $H_1$:**\n", + "### Constructing $H_0$ and $H_1$\n", "\n", "The `setup_QLSP` function prepares the necessary Hamiltonians and normalized components to solve the Quantum Linear Systems Problem (QLSP). \n", "\n", - "The built-in function `matrix_to_hamiltonian`, used within `setup_QLSP` function, encodes the hamitonian matrix into a sum of Pauli strings that will be used to exponentiate the hamiltonians with a product formula (Suzuki-Trotter) in the next step." + "The built-in `matrix_to_hamiltonian` function, used in the `setup_QLSP` function, encodes the Hamitonian matrix into a sum of Pauli strings that is used to exponentiate the Hamiltonians with a product formula (Suzuki-Trotter) in the next step." ] }, { @@ -333,9 +314,9 @@ "id": "28c47603-387c-4ac3-8b7c-7a21ed45390d", "metadata": {}, "source": [ - "### **Define the interpolation Hamiltonian:**\n", + "### Defining the Interpolation Hamiltonian\n", "\n", - "For the sake of simplicity, **we will first use the \"vanilla AQC\" linear scheduling function $f(s(t)) = s(t) = t / T$**.\n", + "For the sake of simplicity, **we first use the \"vanilla AQC\" linear scheduling function $f(s(t)) = s(t) = t / T$**\n", "to define the time-dependent interpolated Hamiltonian, where T is the total evolution time.\n", "\n", "As $t$ progresses from 0 to $T$, $f(s)$ satisfies the $[0, 1] \\to [0, 1]$ mapping." @@ -348,7 +329,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Define the time-dependent Interpolated Hamiltonian, where T is the total evolution time\n", + "# Define the time-dependent interpolated Hamiltonian, where T is the total evolution time\n", "\n", "\n", "def hamiltonian_t(H0, H1, t, T):\n", @@ -361,9 +342,9 @@ "id": "83e110a2-aa85-4fd0-af48-5750e8667577", "metadata": {}, "source": [ - "#### **Spectral gap analysis:**\n", + "### Analyzing the Spectral Gap\n", "\n", - "From the quantum adiabatic theorem [[3](#ETA), Theorem 3] the formula for the adiabatic error bound $\\eta$ at any point $s$ is:\n", + "From the quantum adiabatic theorem [[3](#ETA), Theorem 3] the formula for the adiabatic error bound $\\eta$ at any point $s$ is\n", "\n", "$$\n", "\\eta(s) = C \\left\\{\n", @@ -377,11 +358,10 @@ "\\right\\}.\n", "$$\n", "\n", - "*See [Appendix A](#Appendix-A-Explanation-of-the-adiabatic-error-bound-components) for a detailed explanation of the components.*\n", - "\n", + "See [Appendix A](#Appendix-A-Explanation-of-the-adiabatic-error-bound-components) for a detailed explanation of the components.\n", "\n", "\n", - "***The formula shows that the adiabatic error is minimized when:***\n", + "The formula shows that the adiabatic error is minimized when\n", "1. The total runtime $T$ is large (slow evolution).\n", "2. The spectral gap $\\Delta$ is large (well-separated ground and excited states).\n", "3. The derivatives $\\|H^{(1)}\\|$ and $\\|H^{(2)}\\|$ are small (smooth Hamiltonian changes).\n" @@ -478,7 +458,7 @@ "id": "e850297f-4ff7-4da4-83cd-e3b0310e561b", "metadata": {}, "source": [ - "Should we want to focus on the spectral gap from the Null states, we can zoom-in on our plot:" + "To focus on the spectral gap from the null states, we can zoom in on our plot:" ] }, { @@ -507,7 +487,7 @@ "id": "8e89b285-f0d1-4652-aebf-f0370bc4a338", "metadata": {}, "source": [ - "We can visually observe from the above that although the spectral gap does change throughout $s$ it still stays quite large in our example, so we can choose $T$ accordingly. Without going into details we will choose a simple value for $T$ (represented as `TOTAL_EVOLUTION_TIME`). However, if we simply assume $ \\|H^{(1)}\\|^2 $, $ \\|H^{(2)}\\|^2 $ are bounded by constants, and use the worst-case bound that $ \\Delta \\geq \\kappa^{-1} $, it can be shown that in order to have $ \\eta(1) \\leq \\epsilon $, the runtime of vanilla AQC is $ T \\propto \\kappa^3 / \\epsilon $.\n" + "We can visually observe from the above that although the spectral gap does change throughout $s$ it still stays quite large in our example, so we can choose $T$ accordingly. Without going into detail we choose a simple value for $T$ (represented as `TOTAL_EVOLUTION_TIME`). However, if we simply assume $ \\|H^{(1)}\\|^2 $, $ \\|H^{(2)}\\|^2 $ are bounded by constants, and use the worst-case bound that $ \\Delta \\geq \\kappa^{-1} $, it can be shown that to have $ \\eta(1) \\leq \\epsilon $, the runtime of vanilla AQC is $ T \\propto \\kappa^3 / \\epsilon $.\n" ] }, { @@ -526,18 +506,18 @@ "metadata": {}, "source": [ "------\n", - "### **AQC Implementation:**\n", + "### Implementing AQC\n", "\n", "\n", "Since $$ |\\psi_T(s) \\rangle = \\mathcal{T}\\exp \\left( -iT \\int_0^s H(f(s')) ds' \\right) |\\psi_T(0) \\rangle \\ , $$ where $\\mathcal{T}$ is the time-ordering operator, it is sufficient to implement an efficient time-dependent Hamiltonian simulation of $H(f(s))$.\n", "\n", - "One straightforward approach to achieve this is by using the Trotter splitting method. The lowest order approximation takes the form:\n", + "One straightforward approach to achieve this is using the Trotter splitting method. The lowest order approximation takes the form\n", "\n", "$$\n", "\\mathcal{T}\\exp \\left( -iT \\int_0^s H(f(s')) ds' \\right) \\approx \\prod_{m=1}^M \\exp \\left( -iTh H(f(s_m)) \\right)\n", "$$\n", "\n", - "which can further be approximated as:\n", + "which can further be approximated as\n", "\n", "$$\n", "\\prod_{m=1}^M \\exp \\left( -iTh(1 - f(s_m)) H_0 \\right) \\exp \\left( -iTh f(s_m) H_1 \\right)\n", @@ -557,20 +537,17 @@ "M = \\mathcal{O}(\\text{poly}(\\log(N)) \\cdot T(\\epsilon)^2 / \\epsilon).\n", "$$\n", "\n", - "*(Note that in our case $T$ also dependents on $\\epsilon$).*\n", + "(Note that in our case $T$ also depends on $\\epsilon$.)\n", "\n", "\n", - "\n", - "\n", - "#### **Building the Quantum model:**\n", - "\n", + "#### Building the Quantum Model\n", "\n", "Using the Classiq platform, we implement the adiabatic path with Suzuki-Trotter decomposition for Hamiltonian exponentiation.\n", "\n", "In our model $T$ is represented by the `TOTAL_EVOLUTION_TIME` and $M$ is represented by `NUM_STEPS`.\n", "\n", "\n", - "In the Trotter implementation $M$ scales as $O(T^2)$ with respect to the runtime $T$, therefore our rough choice of $M$:" + "In the Trotter implementation, $M$ scales as $O(T^2)$ with respect to the runtime $T$, therefore this is our rough choice of $M$:" ] }, { @@ -591,7 +568,7 @@ "\n", "We are now ready to build our quantum model.\n", "\n", - "**The function `adiabatic_evolution_qfunc` implements the adiabatic path with Suzuki-Trotter decomposition for Hamiltonian exponentiation:** " + "**The `adiabatic_evolution_qfunc` function implements the adiabatic path with Suzuki-Trotter decomposition for Hamiltonian exponentiation:** " ] }, { @@ -634,7 +611,7 @@ "id": "c4b062d6-8ee9-4f18-b865-db1646b98c79", "metadata": {}, "source": [ - "To solve the QLSP we first prepare $H_0$ zero state $|\\tilde{b}\\rangle$ and than initiate the evolution:\n" + "To solve the QLSP we first prepare the $H_0$ zero state $|\\tilde{b}\\rangle$ and then initiate the evolution:\n" ] }, { @@ -674,9 +651,9 @@ "id": "944b1337-008a-4085-9255-a9de2efc7a43", "metadata": {}, "source": [ - "#### **Synthesize, verify and execute** \n", + "#### Synthesizing, Verifying, and Executing\n", "\n", - "Synthesize the model into a Quantum Program, verify it and execute it on a state vector simulator:** " + "Synthesize the model into a quantum program, verify it, and execute it on a state vector simulator:" ] }, { @@ -706,7 +683,7 @@ "id": "624e3a1d-42c6-4330-9c9e-83955a0375ea", "metadata": {}, "source": [ - "#### **Evaluate results:**" + "#### Evaluating the Results" ] }, { @@ -820,7 +797,7 @@ "id": "51ec6fd4-5fa3-442e-a921-cc5b7b28e05c", "metadata": {}, "source": [ - "***By comparing the quantum-computed results with the mathematically expected solution, we observe a good alignment - showcasing the potential of the AQC approach for solving linear problems.***\n", + "**By comparing the quantum-computed results with the mathematically expected solution, we observe a good alignment, showcasing the potential of the AQC approach for solving linear problems.**\n", "\n" ] }, @@ -829,7 +806,7 @@ "id": "750ad310-4e50-4641-bae2-a688a64af629", "metadata": {}, "source": [ - "We can observe the runtime of the above implementation by analyzing the depth parameter from the transpiled circuit data of our quantum program" + "We can observe the runtime of the above implementation by analyzing the depth parameter from the transpiled circuit data of our quantum program:" ] }, { @@ -855,7 +832,7 @@ "id": "6c858710-204d-4caa-a550-07ec4ac63f46", "metadata": {}, "source": [ - "**For alternative QLSP configurations, selecting different values for $T$ and $M$ will be necessary, and these choices will naturally impact the circuit depth.** For instance::" + "For alternative QLSP configurations, it may be necessary to select different values for $T$ and $M$, and these choices will naturally impact the circuit depth. For instance:" ] }, { @@ -916,7 +893,7 @@ "id": "3e29e2fa-fe17-43fc-b201-fc9f099ca4f1", "metadata": {}, "source": [ - "Although the condition number is higher, the spectral gap remains relatively large. However, using the same values of $T$ and $M$ as chosen above will result in increased error:" + "Although the condition number is higher, the spectral gap remains relatively large. However, using the same values of $T$ and $M$ as chosen above result in an increased error:" ] }, { @@ -1031,7 +1008,7 @@ "id": "88bd95b3-2d36-4def-b3e0-7bedb36a45c9", "metadata": {}, "source": [ - "Choosing $T$ ans $M$ accordingly will improve results but affect the overall runtime:" + "Choosing $T$ and $M$ accordingly improves results but affects the overall runtime:" ] }, { @@ -1157,7 +1134,7 @@ "id": "09984d53-2eb8-4d62-9b84-3060e496efa3", "metadata": {}, "source": [ - "As expected, the overall circuit depth increased to achieve a high degree of similarity in the results." + "As expected, the overall circuit depth increases to achieve a high degree of similarity in the results." ] }, { @@ -1165,10 +1142,7 @@ "id": "dd54d3cf-76d8-48c4-92a3-acd75859319a", "metadata": {}, "source": [ - "