Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
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:
  • Input: A matrix A\textbf{A} and a known vector b|\textbf{b}\rangle.
  • Output: An approximation of a normalized solution x|x\rangle proportional to x|\textbf{x}\rangle, satisfying the equation Ax=b\textbf{A} |\textbf{x}\rangle = |\textbf{b}\rangle.

While the output of VQLS mirrors that of the HHL Quantum Linear-Solving Algorithm, VQLS distinguishes itself by its ability to operate on Noisy Intermediate-Scale Quantum (NISQ) computers. In contrast, HHL necessitates more robust quantum hardware and a larger qubit count, despite offering a faster computation speedup. This tutorial covers an implementation example of a Variational Quantum Linear Solver [1] using block encoding. In particular, we use linear combinations of unitaries (LCUs) for the block encoding. 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.

Building the Algorithm with Classiq

Quantum Part: Variational Circuit

Given a block encoding of the matrix A: U=[A](1)\begin{aligned} U = \begin{bmatrix} A & \cdot \\ \cdot & \cdot \end{bmatrix} \end{aligned} \tag{1} we can prepare the state Ψ:=Ax/xAAx.|\Psi\rangle := A |x\rangle/\sqrt{\langle x |A^\dagger A |x\rangle}. We can approximate the solution x|x\rangle with a variational quantum circuit, i.e., a unitary circuit VV, depending on a finite number of classical real parameters w=(w0,w1,)w = (w_0, w_1, \dots): x=V(w)0.|x \rangle = V(w) |0\rangle. Our objective is to address the task of preparing a quantum state x|x\rangle such that AxA |x\rangle is proportional to b|b\rangle; or, equivalently, ensuring that Ψ:=AxxAAxb.|\Psi\rangle := \frac{A |x\rangle}{\sqrt{\langle x |A^\dagger A |x\rangle}} \approx |b\rangle. The state b|b\rangle arises from a unitary operation UbU_b applied to the ground state of nn qubits; i.e., b=Ub0.|b\rangle = U_b |0\rangle. To maximize the overlap between the quantum states Ψ|\Psi\rangle and b|b\rangle, we optimize the parameters, defining a cost function: C=1bΨ2.C = 1- |\langle b | \Psi \rangle|^2. At a high level, the above could be implemented as follows: We construct a quantum model as depicted in the figure below. When measuring the circuit in the computational basis, the probability of finding the system qubits in the ground state (given the ancillary qubits measured in their ground state) is 0UbΨ2=bΨ2.|\langle 0 | U_b^\dagger |\Psi \rangle|^2 = |\langle b | \Psi \rangle|^2. Screenshot 2024-09-22 at 18.11.21.png To block encode a Variational Quantum Linear Solver as explained above, we can define a high-level block_encoding_vqls function as follows:
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.

Classical Part: Finding Optimal Parameters

To estimate the overlap of the ground state with the post-selected state, we could directly make use of the measurement samples. However, since we want to optimize the cost function, it is useful to express everything in terms of expectation values through Bayes’ theorem: bΨ2=P(sys=groundanc=ground)=P(all=ground)/P(anc=ground)|\langle b | \Psi \rangle|^2= P( \mathrm{sys}=\mathrm{ground}\,|\, \mathrm{anc} = \mathrm{ground}) = P( \mathrm{all}=\mathrm{ground})/P( \mathrm{anc}=\mathrm{ground}) To evaluate the conditional probability from the above equation, we construct the following utility function to operate on the measurement results: To variationally solve our linear problem, we define the cost function C=1bΨ2C = 1- |\langle b | \Psi \rangle|^2 that we are going to minimize. As explained above, we express it in terms of expectation values through Bayes’ theorem. We define a classical function that gets the quantum program, minimizes the cost function using the COBYLA optimizer, and returns the optimal parameters.

Once the optimal variational weights w are found, we can generate the quantum state x|x\rangle. By measuring x|x\rangle in the computational basis we can estimate the probability of each basis state.

Example Using LCU Block Encoding

We treat a specific example based on a system of three qubits: A=c0A0+c1A1+c2A2= 0.55I + 0.225Z1 + 0.225Z2b=Ub0=H0H1H20,\begin{aligned} \begin{align} A &= c_0 A_0 + c_1 A_1 + c_2 A_2 = \ 0.55 \mathbb{I} \ + \ 0.225 Z_1 \ + \ 0.225 Z_2 \\ \\ |b\rangle &= U_b |0 \rangle = H_0 H_1 H_2 |0\rangle, \end{align} \end{aligned} where Zj,Xj,HjZ_j, X_j, H_j represent the Pauli ZZ, Pauli XX, and Hadamard gates applied to the qubit with index jj. To block encode the matrix A we use the LCU method. This can be done with the lcu_paulis library function. Note that this function can get a unnormalized Pauli operator, thus we calculate the normalization factor for the post-process analysis. The LCU quantum circuit looks as follows: Screenshot 2024-05-19 at 18.56.22.png

Fixed Hardware Ansatz

Let’s consider our ansatz V(w)V(w), such that x=V(w)0.|x\rangle = V(w) |0\rangle. This allows us to “search” the state space by varying a set of parameters, ww. 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:
To view our ansatz implementation we create a model and view the synthesis result:
Output:
Screenshot 2025-07-31 at 16.03.50.png 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 RyRy gates allow us to search the state space, while the CZCZ gates create “interference” between the different qubit states.

Running the VQLS

Now, we can define the main function: we call block_encoding_vqls with the arguments of our specific example.
Constructing the model, synthesizing, and executing on the Classiq simulator:
Output:
Screenshot 2025-07-31 at 16.07.09.png We run the classical optimizer to get the optimal parameters:
Output:
output

Measuring the Quantum Solution

Finally, we can apply the optimal parameters to measure the quantum results for x\vec{x}:
Output:

Comparing to the Classical Solution

Since the specific problem considered in this tutorial has a small size, we can also solve it in a classical way and then compare the results with our quantum solution. We use the explicit matrix representation in terms of numerical NumPy arrays. Classical calculation:
Calculating the classical x\vec{x} that solves the equation:
Output:
To compare the classical to the quantum results we compute the post-processing by applying AA to our optimal vector ψo|\psi\rangle_o, normalizing it, then calculating the inner product squared of this vector and the solution vector, b|b\rangle! We can put this all into code as follows:
Output:
output The classical cost function basically agrees with the algorithm result.

References

[1]: Bravo-Prieto et al.,Variational Quantum Linear Solver, 2020. [2]: Robin Kothari. “Efficient algorithms in quantum query complexity.” PhD thesis, University of Waterloo, 2014.