Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
In the present notebook we simulate the 2D Maxwell equations for a homogeneous medium, employing a quantum Hamiltonian simulation. We work in CGS units, where the Maxwell equations take a symmetric form with a single wave speed parameter cc. Earlier quantum approaches to Maxwell-type wave equations include explicit time-marching schemes [1] and variational/quantum-classical solvers [2]; here we instead block-encode the spatial operator and evolve it via Hamiltonian simulation. The transverse-magnetic (TM) mode on a 2-D domain involves three field components, the out-of-plane electric field EzE_z and the in-plane magnetic field components HxH_x, HyH_y, governed by Ezt=c ⁣(HyxHxy),Hxt=cEzy,Hyt=cEzx.\frac{\partial E_z}{\partial t} = c\!\left(\frac{\partial H_y}{\partial x} - \frac{\partial H_x}{\partial y}\right), \qquad \frac{\partial H_x}{\partial t} = -c\,\frac{\partial E_z}{\partial y}, \qquad \frac{\partial H_y}{\partial t} = c\,\frac{\partial E_z}{\partial x}. Discretizing on a Yee lattice [3] with spacing ΔL\Delta L and using backward (b\nabla^{b}) and forward (f\nabla^{f}) finite differences, the system can be written in matrix form as ddt(EzHxHy)=cΔL(0ybxbyf00xf00)A(EzHxHy).(1)\frac{d}{dt}\begin{pmatrix} \vec{E}_z \\[4pt] \vec{H}_x \\[4pt] \vec{H}_y \end{pmatrix} = \frac{c}{\Delta L}\, \underbrace{\begin{pmatrix} 0 & -\nabla_y^{b} & \nabla_x^{b} \\ -\nabla_y^{f} & 0 & 0 \\ \nabla_x^{f} & 0 & 0 \end{pmatrix}}_{A} \begin{pmatrix} \vec{E}_z \\[4pt] \vec{H}_x \\[4pt] \vec{H}_y \end{pmatrix}. \tag{1} The matrix AA is real and anti-symmetric (A=ATA = -A^T), which makes it anti-Hermitian. Therefore the time-evolution operator eAct/ΔLe^{A\,c\,t / \Delta L} is unitary. This makes the system a natural fit for quantum Hamiltonian simulation, with H=iAH=iA. The anti-Hermiticity of the dynamical generator stems from the fact that Maxwell equation are time-translation invariant, therefore conserve the total energy (Noether’s theorem). Consequently, this anti-Hermiticity of the generator is a characteristic of the homogeneous Maxwell equation, not only in the TM mode. We enforce Perfect Electric Conductor (PEC) boundary conditions on the exterior of the domain and on a rectangular obstacle inside it.

Implementation with Classiq

We begin by importing the required Python packages.
We introduce two helper functions allowing to compare quantum and classical solutions: phase normalization and a fidelity measure. The remaining utilities - field plotting, state-vector reconstruction, a synthesis/execution wrapper, and the classical reference simulation itself, are introduced later, each where it is first used.

Problem Definition

We discretize the domain on an L×LL \times L Yee lattice. In this staggered grid the electric field EzE_z lives at the vertices (integer grid points), while the magnetic components HxH_x and HyH_y are located at the edge midpoints:
  • Ez(i,j)E_z(i,\,j) - grid vertices.
  • Hx(i,j+12)H_x(i,\,j+\tfrac{1}{2}) - midpoints of vertical edges (between consecutive yy-nodes at fixed xx).
  • Hy(i+12,j)H_y(i+\tfrac{1}{2},\,j) - midpoints of horizontal edges (between consecutive xx-nodes at fixed yy).
Yee lattice We next set the problem parameters, the grid, the timescale, and the geometry of the system, along with the width and center of the Gaussian pulse we will later use as the initial state.
Problem geometry

Quantum Encoding of the Electromagnetic System

We introduce a QStruct to represent the EM field, utilizing two QNums to encode the position (one for the xx-coordinate and one for yy), with two further qubits encoding the vector [Ez, unused, Hx, Hy] at each coordinate.

Construction of the Quantum Functions

Gradients

We start by defining the 1D periodic backward and forward gradient operators on a grid of size NN. These come from the first-order finite-difference approximation of a derivative. For a function sampled at the grid points fi=f(iΔL)f_i = f(i\,\Delta L), the spatial derivative can be estimated by comparing a point to its neighbor, either one step backward or one step forward: fxi    fifi1ΔL(backward),fxi    fi+1fiΔL(forward),\left.\frac{\partial f}{\partial x}\right|_i \;\approx\; \frac{f_i - f_{i-1}}{\Delta L} \quad\text{(backward)}, \qquad \left.\frac{\partial f}{\partial x}\right|_i \;\approx\; \frac{f_{i+1} - f_i}{\Delta L} \quad\text{(forward)}, each accurate to O(ΔL)\mathcal{O}(\Delta L). The common 1/ΔL1/\Delta L factor is pulled out into the c/ΔLc/\Delta L prefactor of Eq. (1), so the gradient operators themselves carry only the dimensionless differences fifi1f_i - f_{i-1} and fi+1fif_{i+1} - f_i. Introducing the cyclic shift S+1S^{+1} with (S+1f)i=fi1(S^{+1} f)_i = f_{i-1} and its inverse (S1f)i=fi+1(S^{-1} f)_i = f_{i+1}, these read b=IS+1\nabla^{b} = I - S^{+1} and f=S1I\nabla^{f} = S^{-1} - I. The periodic (wrap-around) boundary turns the lone off-diagonal corner entry on, making them N×NN \times N circulant matrices: b=IS+1=(1001110001100011),f=S1I=(1100011000111001),\nabla^{b} = I - S^{+1} = \begin{pmatrix} 1 & 0 & \cdots & 0 & -1 \\ -1 & 1 & 0 & \cdots & 0 \\ 0 & -1 & 1 & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & 0 \\ 0 & \cdots & 0 & -1 & 1 \end{pmatrix}, \qquad \nabla^{f} = S^{-1} - I = \begin{pmatrix} -1 & 1 & 0 & \cdots & 0 \\ 0 & -1 & 1 & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & 0 \\ 0 & \cdots & 0 & -1 & 1 \\ 1 & 0 & \cdots & 0 & -1 \end{pmatrix}, Note that the backward and forward operators are transposes up to a sign, f=(b)T\nabla^{f} = -(\nabla^{b})^{\mathsf T}, making the assembled Maxwell matrix AA anti-symmetric. We build the corresponding quantum functions and plot the non-zero components of the backward gradient.
Backward gradient \nabla^b We next assemble the full gradient from the backward and forward gradients. We build the full matrix as a decomposition into block operators: AEz,Hx=(00yb00000yf0000000)Ez(unused)HxHyA_{E_z,H_x} = \begin{pmatrix} 0 & 0 & -\nabla_y^{b} & 0 \\ 0 & 0 & 0 & 0 \\ -\nabla_y^{f} & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix} \quad \begin{matrix} \leftarrow E_z \\ \leftarrow \text{(unused)} \\ \leftarrow H_x \\ \leftarrow H_y \end{matrix} with AEz,Hy=(000xb00000000xf000)Ez(unused)HxHy(2)A_{E_z,H_y} = \begin{pmatrix} 0 & 0 & 0 & \nabla_x^{b} \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ \nabla_x^{f} & 0 & 0 & 0 \end{pmatrix} \quad \begin{matrix} \leftarrow E_z \\ \leftarrow \text{(unused)} \\ \leftarrow H_x \\ \leftarrow H_y \end{matrix} \tag{2} and the periodic Maxwell operator is assembled as an LCU of the two interactions: A=AEz,Hx+AEz,Hy=(00ybxb0000yf000xf000)  .A = A_{E_z,H_x} + A_{E_z,H_y} = \begin{pmatrix} 0 & 0 & -\nabla_y^{b} & \nabla_x^{b} \\ 0 & 0 & 0 & 0 \\ -\nabla_y^{f} & 0 & 0 & 0 \\ \nabla_x^{f} & 0 & 0 & 0 \end{pmatrix}~~. \quad To construct the gradient we first introduce a utility quantum function grad_backwards_forwards_periodic, which builds a block operator including the backward and forward gradients on the diagonal: (b00f)  .\begin{pmatrix} \nabla^{b} & 0 \\ 0 & \nabla^{f}\\ \end{pmatrix}~~.
We now construct the quantum functions corresponding to AEz,HxA_{E_z, H_x} and AEz,HyA_{E_z, H_y} of Eq. (2), operating on the quantum state {Ez,unused,Hx,Hy}T\{E_z, \text{unused}, H_x, H_y \}^T.
We impose Perfect Electric Conductor (PEC) boundary conditions: on the surface of a perfect conductor the tangential electric field must vanish, so we force Ez=0E_z = 0 on every conducting site - both the outer edge of the domain and the interior rectangular obstacle. Physically this makes the conductor act as a perfect mirror for the wave. We implement this by deleting the corresponding entries of the Maxwell matrix AA with a flag (block) qubit: whenever the state sits on a site where EzE_z must vanish, we flip the flag, pushing that amplitude out of the all-zeros block subspace so it no longer contributes to the encoded operator. The flag is applied only to the EzE_z component (field == 0 and direction == 0); because Hx,HyH_x, H_y are coupled to EzE_z through the dynamics, their boundary behavior follows implicitly. Two regions are flagged (see the @qperm functions below):
  • Exterior boundary - since the gradient operators are periodic, fixing the origin of each axis (x=0x = 0 or y=0y = 0) is enough; the opposite edge is pinned automatically by the wrap-around.
Because the corner (0,0)(0,0) is decoupled, the two axis conditions can be combined with an XOR instead of an AND, avoiding an extra ancilla.
  • Interior obstacle - every site inside the rectangle is flagged directly.
Finally, the flag is applied via within_apply (UVUU^\dagger \, V \, U), so the same condition removes both the rows and the columns of AA, not just the rows. Deleting columns as well preserves the anti-symmetry A=ATA = -A^{\mathsf T} - and hence the unitarity of the evolution. Since the diagonal of the periodic operator is zero, a single flag qubit can be reused for both, saving one block qubit. In the run algorithm section, we summarize the number of block qubits (55) and analyze the source of each of them.
Full Maxwell operator - sparsity pattern

Field Dynamics Employing Hamiltonian Simulation with GQSP

Our goal is to implement the time-evolution operator e(c/ΔL)Ate^{(c/\Delta L)\,A\,t}, where AA is the anti-symmetric Maxwell matrix. We achieve this through Generalized Quantum Signal Processing (GQSP) [4] in three steps. For a comprehensive description of the method see the Hamiltonian simulation with GQSP notebook.

  1. From block encoding to walk operator
Suppose UAU_A is a block encoding of A/αA/\alpha (with α\alpha the encoding scale). Since AA is anti-symmetric, H=iAH = iA is Hermitian, so multiplying the block encoding by a global phase of ii gives a block encoding of the Hermitian matrix H/αH/\alpha. The walk operator is then W=R(iUA),R=20 ⁣0blockI,W = R \cdot (i\,U_A), \qquad R = 2\,|0\rangle\!\langle 0|_{\text{block}}-I, where RR is the reflection about the block-encoding subspace. If λk\lambda_k are the eigenvalues of H/αH/\alpha (real, with λk1|\lambda_k| \le 1), then the walk operator has eigenvalues e±iθke^{\pm i\,\theta_k} with θk=arccos(λk)\theta_k = \arccos(\lambda_k).

  1. Jacobi-Anger polynomial approximation
The desired evolution in the eigenbasis is eiHt=eAte^{-i H t} = e^{A t}. In terms of the walk operator eigenphases: eiαteffcosθwithteff=ctΔL,e^{-i\,\alpha\,t_{\text{eff}}\,\cos\theta} \quad\text{with}\quad t_{\text{eff}} = \frac{c\,t}{\Delta L}, where αteff\alpha\,t_{\text{eff}} is the effective evolution parameter. This function is approximated by a truncated Jacobi-Anger expansion - a Laurent polynomial in eiθe^{i\theta}: eiαteffcosθ    d=DDcdeidθ,e^{-i\,\alpha\,t_{\text{eff}}\,\cos\theta} \;\approx\; \sum_{d=-D}^{D} c_d \, e^{i\,d\,\theta}, where DD (the GQSP degree) is chosen to achieve a target precision ε\varepsilon. Specifically we approximate a scaled version of the function, to guarantee GQSP phase finding stability.
Next we introduce a utility function that will allow plotting the initial and final fields

Initial State Preparation

We initialize the field as a 2D Gaussian pulse in the electric field EzE_z only - the magnetic components start at rest (Hx=Hy=0H_x = H_y = 0). On the EzE_z sites the amplitude is Ez(x,y)    exp ⁣((xμ)2+(yμ)22σ2),E_z(x, y) \;\propto\; \exp\!\left(-\frac{(x-\mu)^2 + (y-\mu)^2}{2\sigma^2}\right), a bump centered at (μ,μ)(\mu, \mu) with width σ\sigma. To stay consistent with the PEC boundary conditions, the amplitude is forced to zero on the conducting sites - the x=0x=0 and y=0y=0 edges and every point inside the rectangular obstacle. The function below evaluates this profile on the lattice; the resulting (real, normalized) amplitudes are then loaded into the EMState register by amplitude encoding (prepare_amplitudes), which sets up ψ(0)|\psi(0)\rangle for the subsequent time evolution.

Run Full Algorithm

We begin by defining the number of block qubits. There are in total 55 block qubits, they result from the following algorithmic steps (each step corresponds to a single qubit):
  1. Linear combination of unitaries in the addition of II and S+1S^{+1} in the one dimensional backward gradient (grad_backwards_periodic).
  2. sub-block selection in the implementation of ez_hx_interaction or ez_hy_interaction.
  3. LCU combining AEz,HxA_{E_z,H_x} and AEz,HyA_{E_z,H_y} (periodic_maxwell_operator).
  4. PEC boundary-condition flag (reused for exterior + obstacle, saving one qubit since the periodic diagonal is zero) (maxwell_operator).
  5. GQSP auxiliary qubit for the walk operator polynomical (in hamiltonian_simulation).
The physical system is described by an additional 1212 qubits, leading to a full circuit width of 1717 quibits.
We wrap synthesis and execution in a single helper, run_simulation: it builds the model, synthesizes it, and samples the resulting program on a state-vector simulator, filtering for the all-zeros block subspace (the subspace in which the block encoding realizes the desired operator).
Output:
output
Output:
Output:
Output:
Output:
In order to plot the results we first introduce additional utility functions, which extract the dataframe to a numpy array and plot the array.
Finally, the plots of the final fields show the field configuration around the conducting object.
output

Classical Validation

We next benchmark the quantum calculation against a classical solution, employing standard matrix exponentiation. The initial field state is propagated, and the fields at final times are compared. The classical simulation is performed by the function classical_maxwell_simulation, which employs build_initial_state_vector to build the initial state field and build_maxwell_evolution_matrix to construct the dynamical generator associated with the Maxwell equation.
Output:
As the fidelity comparison shows, the quantum and classical calculations agree perfectly. To visualize this result we plot both fields next to each other, as expected the plots appear identical.
output output

References

[1]: Costa, P. C. S., Jordan, S., and Ostrander, A. Quantum algorithm for simulating the wave equation. Physical Review A 99, 012323 (2019). [2]: Suau, A., Staffelbach, G., and Calandra, H. Practical quantum computing: solving the wave equation using a quantum approach. ACM Transactions on Quantum Computing 2, 1–35 (2021). [3]: Yee, K. Numerical solution of initial boundary value problems involving Maxwell’s equations in isotropic media. IEEE Transactions on Antennas and Propagation 14, 302–307 (1966). [4]: Motlagh, D., and Wiebe, N. Generalized quantum signal processing. PRX Quantum 5, 020368 (2024).