Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
The Poisson equation is a partial differential equation that appears in various research fields, such as physics and engineering. It describes the distribution of a potential field, uu, under some source term bb: ∇2u=b,\nabla^2 u = b, where the ∇2\nabla^2 is the Laplacian (second derivatives) operator. One approach for numerically solving the Poisson equation is to move from the continuous description to a discrete one, using the finite difference method that casts the problem into a set of linear equations. Then, the solution can be obtained by a linear solver. In this notebook we treat the Poisson equation on a rectangular geometry, Lx×LyL_x\times L_y, with a Dirichlet boundary condition on the xx axis and a Neumann boundary condition on the yy axis: u(0)=u(Lx)=f0,    ∂yu∣y=0=∂yu∣y=Ly=g0.u(0) = u(L_x)=f_0,\,\,\,\, \partial_y u|_{y=0} = \partial_y u|_{y=L_y} = g_0. Furthermore, we assume that f0=g0=0f_0=g_0=0. The discretization of space, including the treatment of the above boundary conditions, is given in Figure
  1. The resulting linear equation reads:
L⋅u⃗=b⃗,       L=Lxx⊗Iy+Ix⊗Lyy,\mathcal{L}\cdot \vec{u} = \vec{b}, \,\,\,\,\,\,\, \mathcal{L} = \mathcal{L}_{xx} \otimes I_y + I_x \otimes \mathcal{L}_{yy}, where Lxx\mathcal{L}_{xx} and Lyy\mathcal{L}_{yy} are the Laplacian operators [1]: Lxx=1Δx2(3−10⋯0−12−1⋯00−12⋯0⋮⋮⋮⋱−100⋯−13),   Lyy=1Δy2(1−10⋯0−12−1⋯00−12⋯0⋮⋮⋮⋱−100⋯−11)\mathcal{L}_{xx} = \frac{1}{\Delta x^2} \begin{pmatrix} 3 & -1 & 0 & \cdots & 0 \\ -1 & 2 & -1 & \cdots & 0 \\ 0 & -1 & 2 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & -1 \\ 0 & 0 & \cdots & -1 & 3 \\ \end{pmatrix},\,\,\, \mathcal{L}_{yy} = \frac{1}{\Delta y^2} \begin{pmatrix} 1 & -1 & 0 & \cdots & 0 \\ -1 & 2 & -1 & \cdots & 0 \\ 0 & -1 & 2 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & -1 \\ 0 & 0 & \cdots & -1 & 1 \\ \end{pmatrix} and Δx\Delta x and Δy\Delta y are the discretization of the xx and yy axes, respectively. The square matrices above, which are of dimensions NxN_x and NyN_y, respectively, represent the solution at the inner part of our geometry.

Figure 1. A schematic description of discretizing the Poisson equation. The blue line designates the area for solving the problem. The linear equations are written only for the inner filled grid points. The unfilled ghost points are used to impose the boundary conditions. In the example above, a Dirichlet boundary condition is given by setting $u_{0,k}=-u_{-1,k}$ and $u_{3,k}=-u_{4,k}$, whereas a Neumann boundary condition reads $u_{j,0}=u_{j,-1}$ and $u_{j,3}=u_{j,4}$.


In this notebook we solve the Poisson problem with the HHL quantum linear solver. We utilize similar ideas appearing in Ref.[2], where a quantum cosine and a quantum sine transforms [3] are performed towards achieving scalable implementation.

Building the Algorithm with Classiq

The HHL algorithm essentially applies a matrix inversion. Here we treat the Laplacian matrix, which can be diagonalized by quantum sine and cosine transforms. Thus, the matrix to invert is diagonal. The four main quantum blocks of the algorithm are thus (see Figure 2):
  1. Prepare the amplitudes of the source term on a quantum variable.
  2. Perform QST and QCT at the beginning of the computation.
This is done by applying the QST to the x qubits and the QCT to the y qubits.
  1. Perform matrix inversion for a diagonal matrix.
  2. Uncompute the QST and QCT at the end of the computation.
Screenshot 2025-07-01 at 0.10.30.png

Figure

  1. The quantum circuit for solving the Poisson equation.
Below we define several classical and quantum functions for constructing our quantum linear solver.

Diagonal Hamiltonian Calculation

The eigenvalues of the Poisson equation with Dirichlet boundary conditions in the x direction and Neumann boundary conditions in the y direction are given by λk,j≡λx,k+λy,j\lambda_{k,j} \equiv \lambda_{x,k} +\lambda_{y,j} where λx,k=4Δx2sin⁡2(π2Nx(k+1)),        λy,j=4Δy2sin⁡2(π2Nyj),\lambda_{x,k} = \frac{4}{\Delta x^2} \sin^2\left(\frac{\pi}{2N_x} (k+1)\right),\,\,\,\,\,\,\,\, \lambda_{y,j} = \frac{4}{\Delta y^2} \sin^2\left(\frac{\pi}{2N_y} j\right), and k=0,1,…,Ny−1k = 0, 1, \ldots, N_y-1 and j=0,1,…,Nx−1j = 0, 1, \ldots, N_x-1. The HHL algorithm requires the application of an Hamiltonian simulation eiHe^{iH}. In this notebook this quantum block is implemented using the Suzuki-Trotter built-in function. We start with defining a function that gets NxN_x and NyN_y and returns the corresponding Hamiltonian. The decomposition of the diagonal matrix to the Pauli basis is done using the Walsh Hadamard transform.

Hamiltonian Evolution for QPE

The HHL is based on a QPE applied on eiHte^{iHt}. For this, we need to define a function implementing (eiHt)p\left(e^{iHt}\right)^p for an integer power pp. Since in our case the Hamiltonian is diagonal, an exact implementation is given by the first order Suzuki-Trotter formula, where in addition (eiHt)p=eipHt\left(e^{iHt}\right)^p = e^{ipHt}.

Sine and Cosine Transforms

For the system treated in this notebook, diagonalization of the 2D2D Laplacian is given by appying a quantum sine (cosine) transform on the xx (yy) dimension. We define a quantum function for implementing this, using the open library functions:

Matrix Inversion

Finally, we define a matrix inversion using a QPE routine, similar to the example given in the basic HHL notebook.

Example: Non-Separable Source Term

We solve an example with a square grid of Nx, Ny=23N_x,\, N_y=2^3. For the source term we take a non-separable 2Nx+Ny2^{N_x+N_y} vector that represents the function b=F[xy(x−Lx)(y−Ly)].b = F\left[xy(x-L_x)(y-L_y)\right]. We choose F(z)=tanh⁡(z)2F(z)=\tanh(z)^2, which satisfies the boundary conditions.
We can plot the source term:
output Next, we calculate the Hamiltonian for the specific discretization:
Now, we need to define the resolution (QPE size) for the quantum solver. The required number of QPE phase qubits depends on the condition number κ\kappa of the inverted matrix. In the case of the Laplacian matrix, this parameter is of the order of the matrix dimension O(2Nx+Ny)O(2^{N_x+N_y}), which eliminates the exponential advantage. However, in our example we have a smooth source term, and high modes are expected to have minor effects on the solution. The highest mode of L\mathcal{L} is λmax⁡=8\lambda_{\max}=8, whereas the smallest one is λx,0∼4π2/(2Nx)2\lambda_{x,0}\sim 4\pi^2/(2N_x)^2. Let us assume that the highest mode participating in the solution is ∼26λx,0\sim 2^6 \lambda_{x,0}, and take a QPE size of six qubits.
We build the model and synthesize it: Comment: the model is designed for the case of symmetric grid Nx=NyN_x=N_y, working with QArray[QNum,2]. In the more general case one can define a QStruct with two QNum variable of different sizes
Output:

Executing and Plotting the Result

We run the quantum program on a statevector simulator to retrieve the full solution.
Output:
The resulting statevector is given up to a sign. The sign in the center of the solution is expected to be positive. We can correct accordingly:
Finally, we print the result and compare it to the classical solution:
output

References

[1]: Strang, G., 1999 SIAM Review 41 135. The discrete cosine transform. [2]: Yudong Cao et al., 2013 New J. Phys. 15 013021. Quantum algorithm and circuit design solving the Poisson equation. [3]: Klappenecker, A., & Rotteler M., Discrete cosine transforms on quantum computers.