View on GitHub
Open this notebook in GitHub to run it yourself
Implementation with Classiq
We begin by importing the required Python packages.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 Yee lattice. In this staggered grid the electric field lives at the vertices (integer grid points), while the magnetic components and are located at the edge midpoints:- — grid vertices.
- — midpoints of vertical edges (between consecutive -nodes at fixed ).
- — midpoints of horizontal edges (between consecutive -nodes at fixed ).


Quantum Encoding of the Electromagnetic System
We introduce a QStruct to represent the EM field, utilizing twoQNums to encode the position (one for the -coordinate and one for ), 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 . These come from the first-order finite-difference approximation of a derivative. For a function sampled at the grid points , the spatial derivative can be estimated by comparing a point to its neighbor, either one step backward or one step forward: each accurate to . The common factor is pulled out into the prefactor of Eq. (1), so the gradient operators themselves carry only the dimensionless differences and . Introducing the cyclic shift with and its inverse , these read and . The periodic (wrap-around) boundary turns the lone off-diagonal corner entry on, making them circulant matrices: Note that the backward and forward operators are transposes up to a sign, , making the assembled Maxwell matrix anti-symmetric. We build the corresponding quantum functions and plot the non-zero components of the backward gradient.
grad_backwards_forwards_periodic, which builds a block operator including the backward and forward gradients on the diagonal:
field == 0 and direction == 0); because are coupled to 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 ( or ) is enough; the opposite edge is pinned automatically by the wrap-around.
XOR instead of an AND, avoiding an extra ancilla.
- Interior obstacle — every site inside the rectangle is flagged directly.
within_apply (), so the same condition removes both the rows and the columns of , not just the rows.
Deleting columns as well preserves the anti-symmetry — 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 () and analyze the source of each of them.

Field Dynamics Employing Hamiltonian Simulation with GQSP
Our goal is to implement the time-evolution operator , where 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.- From block encoding to walk operator
- Jacobi–Anger polynomial approximation
Initial State Preparation
We initialize the field as a 2D Gaussian pulse in the electric field only — the magnetic components start at rest (). On the sites the amplitude is a bump centered at with width . To stay consistent with the PEC boundary conditions, the amplitude is forced to zero on the conducting sites — the and 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 theEMState register by amplitude encoding (prepare_amplitudes), which sets up for the subsequent time evolution.
Run Full Algorithm
We begin by defining the number of block qubits. There are in total block qubits, they result from the following algorithmic steps (each step corresponds to a single qubit):- Linear combination of unitaries in the addition of and in the one dimensional backward gradient (
grad_backwards_periodic). - sub-block selection in the implementation of
ez_hx_interactionorez_hy_interaction. - LCU combining and (
periodic_maxwell_operator). - PEC boundary-condition flag (reused for exterior + obstacle, saving one qubit since the periodic diagonal is zero) (
maxwell_operator). - GQSP auxiliary qubit for the walk operator polynomical (in
hamiltonian_simulation).
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:
| em_state.x | em_state.y | em_state.direction | em_state.field | block | amplitude | magnitude | phase | probability | bitstring | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 25 | 25 | 0 | 0 | 0 | 0.000000-0.037809j | 0.04 | -0.50π | 0.001430 | 00000110011100100000 |
| 1 | 25 | 26 | 0 | 0 | 0 | 0.000001-0.034855j | 0.03 | -0.50π | 0.001215 | 00000110101100100000 |
| 2 | 26 | 25 | 0 | 0 | 0 | 0.000001-0.034852j | 0.03 | -0.50π | 0.001215 | 00000110011101000000 |
| 3 | 24 | 26 | 0 | 0 | 0 | 0.000002-0.031485j | 0.03 | -0.50π | 0.000991 | 00000110101100000000 |
| 4 | 26 | 24 | 0 | 0 | 0 | 0.000002-0.031483j | 0.03 | -0.50π | 0.000991 | 00000110001101000000 |

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 functionclassical_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:

