Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

English suggestions for VQLS #767

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 55 additions & 63 deletions algorithms/vqls/lcu_vqls/vqls_with_lcu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"The Variational Quantum Linear Solver (VQLS) is a quantum algorithm that harnesses the power of Variational Quantum Eigensolvers (VQE) to efficiently solve systems of linear equations:\n",
"The Variational Quantum Linear Solver (VQLS) is a quantum algorithm that harnesses the power of Variational Quantum Eigensolvers (VQE) to solve systems of linear equations efficiently:\n",
"\n",
"* **Input:** A matrix $\\textbf{A}$ and a known vector $|\\textbf{b}\\rangle$.\n",
"\n",
Expand All @@ -29,16 +29,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In this tutorial we will cover an implementation example of a **Variational Quantum Linear Solver** [[1](#VQLS)] using block encoding , in particular, we will use linear combinations of unitaries, or LCUs for short, for the block encoding\n",
"This tutorial covers an implementation example of a **Variational Quantum Linear Solver** [[1](#VQLS)] using block encoding. In particular, we use linear combinations of unitaries (LCUs) for the block encoding.\n",
"\n",
"As all variational algorithms, the VQLS is a hybrid algorithm in which we apply a classical optimization on the results of a parametrized (ansatz) quantum program"
"As with all variational algorithms, the VQLS is a hybrid algorithm in which we apply a classical optimization on the results of a parametrized (ansatz) quantum program."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to Build the Algorithm with Classiq"
"## Building the Algorithm with Classiq"
]
},
{
Expand All @@ -61,37 +61,35 @@
"$$\\begin{aligned}\n",
"U = \\begin{bmatrix} A & \\cdot \\\\ \\cdot & \\cdot \\end{bmatrix}\n",
"\\end{aligned} \\tag{1}$$ \n",
"we can preapre the state \n",
"$$|\\Psi\\rangle := A |x\\rangle/\\sqrt{\\langle x |A^\\dagger A |x\\rangle}$$\n",
"we can prepare the state \n",
"$$|\\Psi\\rangle := A |x\\rangle/\\sqrt{\\langle x |A^\\dagger A |x\\rangle}.$$\n",
"\n",
"\n",
"We can approximate the solution $|x\\rangle$ with a variational quantum\n",
"circuit, i.e., a unitary circuit $V$ depending on a finite number of classical real parameters $w = (w_0, w_1, \\dots)$\n",
"circuit, i.e., a unitary circuit $V$, depending on a finite number of classical real parameters $w = (w_0, w_1, \\dots)$:\n",
"\n",
"$$|x \\rangle = V(w) |0\\rangle.$$\n",
"\n",
"\n",
"Our objective is to address the task of preparing a quantum state $|x\\rangle$ such that $A |x\\rangle$ is proportional to $|b\\rangle$, or equivalently, ensuring that:\n",
"Our objective is to address the task of preparing a quantum state $|x\\rangle$ such that $A |x\\rangle$ is proportional to $|b\\rangle$; or, equivalently, ensuring that\n",
"\n",
"$$|\\Psi\\rangle := \\frac{A |x\\rangle}{\\sqrt{\\langle x |A^\\dagger A |x\\rangle}} \\approx |b\\rangle.$$\n",
"\n",
"The state $|b\\rangle$ arises from a unitary operation $U_b$ applied to the ground state of $n$ qubits: i.e.,\n",
"The state $|b\\rangle$ arises from a unitary operation $U_b$ applied to the ground state of $n$ qubits; i.e.,\n",
"\n",
"$$|b\\rangle = U_b |0\\rangle,$$\n",
"$$|b\\rangle = U_b |0\\rangle.$$\n",
"\n",
"The parameters should be optimized in order to maximize the overlap\n",
"between the quantum states $|\\Psi\\rangle$ and $|b\\rangle$. We define the\n",
"following cost function,\n",
"To maximize the overlap between the quantum states $|\\Psi\\rangle$ and $|b\\rangle$, we optimize the parameters, defining a cost function:\n",
"\n",
"$$C = 1- |\\langle b | \\Psi \\rangle|^2$$\n",
"$$C = 1- |\\langle b | \\Psi \\rangle|^2.$$\n",
"\n",
"\n",
"At high level, the above could be implemented as follows:\n",
"At a high level, the above could be implemented as follows:\n",
"\n",
"We construct a quantum model as depicted in the figure below. When measuring the above circuit in the computational basis, the probability of\n",
"finding the system qubits it in the ground state (given the ancillary qubits measured\n",
"in their ground state), is\n",
" $|\\langle 0 | U_b^\\dagger |\\Psi \\rangle|^2 = |\\langle b | \\Psi \\rangle|^2$\n",
"We construct a quantum model as depicted in the figure below. When measuring the circuit in the computational basis, the probability of\n",
"finding the system qubits in the ground state (given the ancillary qubits measured\n",
"in their ground state) is\n",
" $|\\langle 0 | U_b^\\dagger |\\Psi \\rangle|^2 = |\\langle b | \\Psi \\rangle|^2.$\n",
"\n",
"![Screenshot 2024-09-22 at 18.11.21.png](attachment:0f2460c6-518f-47ca-9d30-a085268720fb.png)\n",
"\n"
Expand All @@ -101,7 +99,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"To block encode a Variational Quantum Linear Solver as explained above we can define a high-level `block_encoding_vqls` function as follows:"
"To block encode a Variational Quantum Linear Solver as explained above, we can define a high-level `block_encoding_vqls` function as follows:"
]
},
{
Expand Down Expand Up @@ -144,7 +142,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"From here, all we need to do is to define our `ansatz`, `block_encoding` and `prepare_b_state` to fit the above specific example and we are ready to build our model, synthesize it, execute it and analyze the results."
"From here, we only need to define `ansatz`, `block_encoding`, and `prepare_b_state` to fit the specific example above. Now we are ready to build our model, synthesize it, execute it, and analyze the results."
]
},
{
Expand All @@ -159,24 +157,23 @@
"metadata": {},
"source": [
"To estimate the overlap of the ground state with the post-selected\n",
"state, one could directly make use of the measurement samples. However,\n",
"state, we could directly make use of the measurement samples. However,\n",
"since we want to optimize the cost function, it is useful to express\n",
"everything in terms of expectation values through Bayes\\' theorem:\n",
"\n",
"$$|\\langle b | \\Psi \\rangle|^2=\n",
"P( \\mathrm{sys}=\\mathrm{ground}\\,|\\, \\mathrm{anc} = \\mathrm{ground}) =\n",
"P( \\mathrm{all}=\\mathrm{ground})/P( \\mathrm{anc}=\\mathrm{ground})$$\n",
"\n",
"To evaluate the conditional probability from\n",
"the above equation we construct the following utility function to operate on the measurements results:\n"
"To evaluate the conditional probability from the above equation, we construct the following utility function to operate on the measurement results:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"In order to variationally solve our linear problem, we define the\n",
"To variationally solve our linear problem, we define the\n",
"cost function $C = 1- |\\langle b | \\Psi \\rangle|^2$ that we are going to\n",
"minimize. As explained above, we express it in terms of expectation\n",
"values through Bayes\\' theorem.\n",
Expand Down Expand Up @@ -272,9 +269,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example using LCU Block Encoding\n",
"## Example Using LCU Block Encoding\n",
"\n",
"We treat a specific example based on a system of 3 qubits:\n",
"We treat a specific example based on a system of three qubits:\n",
"\n",
"$$\\begin{aligned}\n",
"\\begin{align}\n",
Expand All @@ -285,24 +282,20 @@
"\\end{align}\n",
"\\end{aligned}$$\n",
"\n",
"where $Z_j, X_j, H_j$ represent the Pauli $Z$, Pauli $X$ and Hadamard\n",
"where $Z_j, X_j, H_j$ represent the Pauli $Z$, Pauli $X$, and Hadamard\n",
"gates applied to the qubit with index $j$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Block encoding the matrix A with LCU\n",
"### Block Encoding the Matrix A with LCU\n",
"\n",
"\n",
"Our first goal is to encode the Matrix A on a quantum circuit. \n",
"\n",
"Using *linear combinations of unitaries* (LCU) we can encode the operator A inside U as in Eq.([1](#mjx-eqn-1))\n",
"\n",
"\n",
"This block is defined by the subspace of all states where the auxiliary\n",
"qubits are in state $|0\\rangle$.\n",
"Using *linear combinations of unitaries* (LCU) we can encode the operator A inside U as in Eq.([1](#mjx-eqn-1)). This block is defined by the subspace of all states where the auxiliary qubits are in state $|0\\rangle$.\n",
"\n",
"\n",
"Given a $2^n \\times 2^n$ matrix $A$, representable as a linear combination of $L$ unitary matrices $A_0, A_1, \\dots, A_{L-1}$ we can implement a quantum circuit that applies the associated operator.\n",
Expand All @@ -325,7 +318,7 @@
"source": [
"\n",
"\n",
"The concept of applying a linear combination of unitary operations has been explored in Ref[[2](#coherent)] and we adopt a similar approach here.\n",
"The concept of applying a linear combination of unitary operations has been explored in Ref[[2](#coherent)], and we adopt a similar approach here.\n",
"\n",
"We can assume, without loss of generality, that the coefficients $c=(c_1, c_2, \\dots c_L)$ in the definition of $A$ form a positive and normalized probability distribution:\n",
"\n",
Expand All @@ -335,7 +328,7 @@
"The complex phase of each coefficient $c_l$ can be absorbed into its associated unitary $A_l$, resulting in a vector comprised of positive values. Additionally, as the linear problem remains unchanged under constant scaling, we can normalize the coefficients to form a probability distribution.\n",
"\n",
"\n",
"To simplify matters, we assume that $L$ is a power of 2, specifically $L=2^m$ for a positive integer $m$ (we can always pad $c$ with additional zeros if needed).\n",
"To simplify matters, we assume that $L$ is a power of 2, specifically $L=2^m$ for a positive integer $m$. (We can always pad $c$ with additional zeros if needed.)\n",
"\n",
"Let us consider a unitary circuit $U_c$, embedding the square root of\n",
"$c$ into the quantum state $|\\sqrt{c}\\rangle$ of $m$ ancillary qubits:\n",
Expand All @@ -361,7 +354,7 @@
"i.e., the unitary $A_l$ is applied only when the ancillary is in\n",
"the corresponding basis state $|l\\rangle$.\n",
"\n",
"So our LCU quantum circuit will look as follows:\n",
"So our LCU quantum circuit looks as follows:\n",
"![Screenshot 2024-05-19 at 18.56.22.png](attachment:9b0897af-b689-4b83-b119-f7de76db95fd.png)\n",
"\n",
"\n",
Expand All @@ -372,14 +365,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's apply the previous theory on our simple example based on a system of 3 qubits:"
"Let's apply the previous theory on our simple example based on a system of three qubits:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we will need to define some unitary operations associated to the simple\n",
"First we need to define unitary operations associated to the simple\n",
"example presented above.\n",
"\n",
"The coefficients of the linear combination are three positive numbers\n",
Expand Down Expand Up @@ -410,9 +403,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"To block encode the A we need only 3 thing:\n",
"1. Representing it as a linear combination of $L$ unitary matrices (in our case we will use Pauli matrices)]\n",
"2. A a unitary circuit $U_c$ that embeds the square root of $c$ (linear combination coefficients) into the quantum state\n",
"To block encode the A we only need these things:\n",
"1. Representation of it as a linear combination of $L$ unitary matrices (in our case we use Pauli matrices)\n",
"2. A unitary circuit $U_c$ that embeds the square root of $c$ (linear combination coefficients) into the quantum state\n",
"3. A circuit that encodes $CA_l$"
]
},
Expand All @@ -421,7 +414,7 @@
"metadata": {},
"source": [
"#### Preparing $U_c$ \n",
"To construct $\\vec{c}$ from the above example we will we want to apply our Pauli's strings decomposition. For this we need additional functions:"
"To construct $\\vec{c}$ from the above example we want to apply our Pauli's strings decomposition. For this, we need additional functions:"
]
},
{
Expand Down Expand Up @@ -493,12 +486,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### LCU sequence of all controlled-unitaries $CA_l$\n",
"#### LCU Sequence of All Controlled Unitaries $CA_l$\n",
"\n",
"Next, we are left to define the LCU sequence of all controlled-unitaries $CA_l$,\n",
"Next, we are left to define the LCU sequence of all controlled unitaries $CA_l$,\n",
"acting as $A_l$ on the system whenever the ancillary state is\n",
"$|l\\rangle$. \n",
"The `prepare_ca` function iterates over a list of Pauli terms $A_l$ and will apply $CA_l$ on the 3 qubit state `phi` controlled by the ancillary state $\\vec{c}$ "
"The `prepare_ca` function iterates over a list of Pauli terms $A_l$ and applies $CA_l$ on the three-qubit state `phi` controlled by the ancillary state $\\vec{c}$: "
]
},
{
Expand Down Expand Up @@ -552,14 +545,14 @@
"source": [
"### Fixed Hardware Ansatz\n",
"\n",
"Let's consider our ansatz $V(w)$, such that : \n",
"Let's consider our ansatz $V(w)$, such that \n",
"\n",
"$$|x\\rangle = V(w) |0\\rangle $$\n",
"$$|x\\rangle = V(w) |0\\rangle.$$\n",
"\n",
"\n",
"This allows us to \"search\" the state space by varying some set of parameters, $w$. \n",
"This allows us to \"search\" the state space by varying a set of parameters, $w$. \n",
"\n",
"The ansatz that we will use for this 3 qubits system implementation takes in 9 parameters as defined in `apply_fixed_3_qubit_system_ansatz` function:"
"The ansatz that we use for this three-qubit system implementation takes in nine parameters as defined in the `apply_fixed_3_qubit_system_ansatz` function:"
]
},
{
Expand Down Expand Up @@ -598,7 +591,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"To view our ansatz implementation we will create a model and view the synthesis result: "
"To view our ansatz implementation we create a model and view the synthesis result: "
]
},
{
Expand Down Expand Up @@ -636,7 +629,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This is called a **fixed hardware ansatz**: the configuration of quantum gates remains the same for each run of the circuit, all that changes are the parameters. Unlike the QAOA ansatz, it is not composed solely of Trotterized Hamiltonians. The applications of $Ry$ gates allow us to search the state space, while the $CZ$ gates create \"interference\" between the different qubit states."
"This is called a **fixed hardware ansatz** in that the configuration of quantum gates remains the same for each run of the circuit, and all that changes are the parameters. Unlike the QAOA ansatz, it is not composed solely of Trotterized Hamiltonians. The applications of $Ry$ gates allow us to search the state space, while the $CZ$ gates create \"interference\" between the different qubit states."
]
},
{
Expand Down Expand Up @@ -692,7 +685,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Constructing the model, synthesizing and executing on Classiq's simulator:"
"Constructing the model, synthesizing, and executing on the Classiq simulator:"
]
},
{
Expand Down Expand Up @@ -744,7 +737,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We run the classical optimizer to get the optimal parameters"
"We run the classical optimizer to get the optimal parameters:"
]
},
{
Expand Down Expand Up @@ -792,7 +785,7 @@
"source": [
"### Measuring the Quantum Solution\n",
"\n",
"Finally, we can apply the optimal params to measure the quantum results for $\\vec{x}$:"
"Finally, we can apply the optimal parameters to measure the quantum results for $\\vec{x}$:"
]
},
{
Expand Down Expand Up @@ -923,7 +916,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Comparison to the classical solution\n",
"### Comparing to the Classical Solution\n",
"\n",
"Since the specific problem considered in this tutorial has a small size,\n",
"we can also solve it in a classical way and then compare the results\n",
Expand All @@ -934,8 +927,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"To solve the problem in a classical way, we use the explicit matrix\n",
"representation in terms of numerical NumPy arrays."
"We use the explicit matrix representation in terms of numerical NumPy arrays."
]
},
{
Expand Down Expand Up @@ -969,7 +961,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Calculating the classical $\\vec{x}$ that solves the equation :"
"Calculating the classical $\\vec{x}$ that solves the equation:"
]
},
{
Expand Down Expand Up @@ -1000,7 +992,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"To compare the classical to the quantum results we will compute some post-processing. In order to do this, we will apply $A$ to our optimal vector $|\\psi\\rangle_o$, normalize it, then calculate the inner product squared of this vector and the solution vector, $|b\\rangle$! We can put this all into code as:"
"To compare the classical to the quantum results we compute the post-processing by applying $A$ to our optimal vector $|\\psi\\rangle_o$, normalizing it, then calculating the inner product squared of this vector and the solution vector, $|b\\rangle$! We can put this all into code as follows:"
]
},
{
Expand Down Expand Up @@ -1076,7 +1068,7 @@
"source": [
"## References\n",
"\n",
"<a name='VQLS'>[1]</a>: [Bravo-Prieto et al.,Variational Quantum Linear Solver. (2020)](https://arxiv.org/pdf/1909.05820.pdf)\n",
"<a name='VQLS'>[1]</a>: [Bravo-Prieto et al.,Variational Quantum Linear Solver, 2020.](https://arxiv.org/pdf/1909.05820.pdf)\n",
"\n",
"\n",
"<a name='coherent'>[2]</a>: Robin Kothari. \\\"Efficient algorithms in quantum query complexity.\\\"\n",
Expand All @@ -1103,9 +1095,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "prod_py3.11",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "prod_py3.11"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down
Loading