---
title: 'SQWLib: Efficient Szegedy Walk Simulator'
url: https://www.emergentmind.com/topics/sqwlib
type: topic
---

# SQWLib: Efficient Szegedy Walk Simulator

Searching arXiv for the cited SQWLib/SQUWALS papers to ground the article.
SQWLib is a Python library that implements the full simulation framework developed in “Efficient Simulation of Szegedy Quantum Walk Formulations and Algorithms.” It is a classical, NumPy/SciPy–based simulator specialized for Szegedy-type discrete-time quantum walks and for algorithms that couple these walks to quantum phase estimation (QPE). Its defining design goal is to avoid constructing the full unitary evolution operator of size \(N^2\times N^2\) for a graph with \(N\) nodes, while supporting several different but spectrally equivalent walk formulations. For dense graphs it achieves \(O(N^2)\) time and memory, and for sparse graphs its cost scales as \(O(|E|)\), where \(|E|\) is the number of undirected edges [2606.14226].

## 1. Formal setting and supported walk formulations

SQWLib is built around Szegedy’s quantization of a classical Markov chain. Given a graph with \(N\) nodes and a column-stochastic transition matrix \(G\), the walk acts on the doubled Hilbert space
\[
\mathcal{H}=\mathbb{C}^N\otimes\mathbb{C}^N
=\mathrm{span}\{\;|i\rangle_1|j\rangle_2:i,j=0,\ldots,N-1\;\}.
\]
For each node \(i\), one defines
\[
|\psi_i\rangle=|i\rangle_1\otimes|\omega_i\rangle_2,
\qquad
|\omega_i\rangle_2=\sum_{k=0}^{N-1}\sqrt{G_{ki}}\;|k\rangle_2.
\]
The projector onto the span of the \(|\psi_i\rangle\) is
\[
\Pi=\sum_{i=0}^{N-1}|\psi_i\rangle\langle\psi_i|,
\]
and the basic reflection and swap operators are
\[
R=2\Pi-\mathbbm{1},
\qquad
S=\sum_{i,j=0}^{N-1}|i,j\rangle\langle j,i|.
\]

SQWLib supports the principal Szegedy walk formulations used in the literature.

| Formulation | Expression | Role |
|---|---|---|
| Single-step Szegedy operator | \(U=SR\) | Common one-step evolution |
| Double-step Szegedy operator | \(W=R_B R_A=SRSR=U^2\) | Szegedy’s original two-reflection form |
| Similarity-transformed walk | \(\widetilde{W}=V^\dagger S V R_0 V^\dagger S V R_0\) | Used in QSA and related algorithms |

For reversible Markov chains with stationary distribution \(\pi\), the walk admits the stationary eigenstate
\[
|\pi\rangle=\sum_{i=0}^{N-1}\sqrt{\pi_i}\,|\psi_i\rangle
=\sum_{i=0}^{N-1}\sqrt{\pi_i}\,|i\rangle_1|\omega_i\rangle_2.
\]
Within the dynamical subspace
\[
\mathcal{H}_D=\mathrm{span}\{\;|\psi_i\rangle,S|\psi_i\rangle:i=0,\ldots,N-1\;\},
\]
this is the unique eigenstate with eigenvalue \(1\) for \(W\), and measuring the first register of \(|\pi\rangle\) yields a sample from the classical stationary distribution. SQWLib also supports the update-operator formulation based on \(V\) and
\[
R_0=2\sum_{i=0}^{N-1}|i,0\rangle\langle i,0|-\mathbbm{1},
\qquad
R=V R_0 V^\dagger,
\]
for which the stationary state takes the form
\[
|\widetilde{\pi}\rangle=\sum_{i=0}^{N-1}\sqrt{\pi_i}\,|i\rangle_1|0\rangle_2.
\]
This state is independent of the Markov chain outside the amplitudes \(\sqrt{\pi_i}\), a property used directly in quantum simulated annealing [2606.14226].

## 2. State representation and primitive operators

The library’s efficiency derives from representing walk states as structured arrays rather than as flat vectors. Any state
\[
|\phi\rangle=\sum_{i,j=0}^{N-1} a_{ij}\,|i\rangle_1|j\rangle_2
\]
is stored as an \(N\times N\) matrix \(\Phi\) with entries
\[
\Phi_{ij}=a_{ji}.
\]
Under this convention, the column index corresponds to the first register and the row index to the second register. This layout makes several basic operations immediate: the swap \(S\) becomes matrix transposition, \(\Phi\mapsto\Phi^T\), and the simple reflection \(R_0\) is implemented by multiplying all rows except the first by \(-1\) [2606.14226].

The nontrivial primitive is the reflection \(R\) about \(\mathrm{span}\{|\psi_i\rangle\}\). SQWLib implements it through an expansion–recomposition scheme. For operators of the form
\[
\Pi_{XY}=\sum_{i=0}^{N-1}|y_i\rangle\langle x_i|,
\]
with
\[
|x_i\rangle=|i\rangle_1\otimes\sum_k X_{ki}|k\rangle_2,
\qquad
|y_i\rangle=|i\rangle_1\otimes\sum_k Y_{ki}|k\rangle_2,
\]
the expansion step computes coefficients
\[
C_i=\langle x_i|\phi\rangle,
\]
and the recomposition step forms \(\Pi_{XY}|\phi\rangle=\sum_i C_i|y_i\rangle\). Numerically, expansion is an element-wise multiplication followed by summation along an axis, while recomposition is a broadcasting operation. For \(R\), one uses \(X=Y=\Psi\), with
\[
\Psi_{ki}=\sqrt{G_{ki}},
\qquad
R=2\,\Pi_{\Psi\Psi}-\mathbbm{1}.
\]

SQWLib further introduces two concrete unitary implementations of the update operator \(V\). The first, \(V_1\), is defined on the update subspace
\[
\mathcal{H}_V=\mathrm{span}\{\;|i,0\rangle,|\psi_i\rangle:i=0,\ldots,N-1\;\}
\]
and acts as the identity on \(\mathcal{H}_V^\perp\). The second, \(V_2\), is a block-diagonal Householder construction,
\[
V_2=\mathbbm{1}-2\sum_{i=0}^{N-1}|i,v_i\rangle\langle i,v_i|,
\]
with \(V_2|i,0\rangle=|\psi_i\rangle\) and \(V_2^\dagger=V_2\). The paper states that \(V_2\) is more efficient than \(V_1\), since it requires fewer expansion/recomposition steps and has the self-inverse property [2606.14226].

## 3. Dense and sparse simulation framework

The central algorithmic principle is to simulate the action of primitive operators rather than assemble full \(N^2\times N^2\) unitary matrices. In dense settings, all major objects—\(G\), \(\Psi\), and the state matrices \(\Phi\)—are \(N\times N\) dense arrays. Each basic operator touches each entry at most a constant number of times, so time and memory per walk step are \(O(N^2)\). The paper characterizes this as optimal, because the minimal description of a dense Markov chain already requires \(N^2\) numbers [2606.14226].

For sparse graphs, SQWLib exploits the fact that the walk is confined to a reduced subspace
\[
\mathcal{H}_R=\mathrm{span}\{\;|i,j\rangle:A_{ji}=1\;\},
\]
where \(A\) is the adjacency matrix of the undirected backbone. Instead of storing an \(N\times N\) matrix, the state is stored as a reduced vector of length \(N_R=\dim\mathcal{H}_R\). The implementation uses three auxiliary structures: **STARTS**, a list of offsets marking the start of each block; **SIZES**, the block sizes; and **PERMUTATION**, a permutation array that maps each reduced index to the index corresponding to the swapped edge.

Under this representation, swap \(S\) is implemented by applying **PERMUTATION**, while \(R_0\) multiplies all entries by \(-1\) and then flips the sign back on the indices corresponding to \(|i,0\rangle\). Expansion–recomposition becomes a blockwise computation using `numpy.add.reduceat` and blockwise broadcasting. Memory consumption is therefore \(O(N_R)=O(|E|)\), and time per operator is also \(O(|E|)\). For bounded-degree graph families, where \(|E|=O(N)\), the simulation becomes linear in the number of nodes. The paper also notes that even when the update operator \(V\) requires adding edges from node \(0\) to all nodes so that \(|i,0\rangle\in\mathcal{H}_R\), this adds only \(O(N)\) entries and preserves overall \(O(N)\) scaling [2606.14226].

## 4. Quantum phase estimation layer

A defining extension of SQWLib beyond earlier simulators is its integrated QPE framework. For a unitary \(U\), the library represents a joint QPE state as
\[
|\Psi\rangle=\sum_{x=0}^{2^p-1}|x\rangle\otimes|\phi_x\rangle,
\]
where the phase index \(x\) occupies one tensor axis and each column contains an entire Szegedy walk state. This tensorized representation separates the two computational roles: QPE operations act along the phase axis, while walk operators act within each column or batch [2606.14226].

Controlled powers of the walk are simulated without constructing controlled-unitary matrices. In the general implementation, one evolves a batch of walk states in parallel: after each step, the leftmost column is removed, so that after \(2^p-1\) iterations the column with index \(x\) has received exactly \(x\) applications of \(U\). For the common direct-QPE case with input \(|0\rangle^{\otimes p}\otimes|\phi\rangle\), SQWLib uses a simpler method: it stores the successive states \(|\phi\rangle,U|\phi\rangle,U^2|\phi\rangle,\ldots\) across the phase axis and then applies inverse QFT numerically.

The Hadamard layer \(H^{\otimes p}\) and QFT/QFT\(^\dagger\) are implemented via Walsh–Hadamard transforms and FFTs along the phase axis. The framework also supports multiple phase registers. With \(k\) phase registers of \(p\) qubits each, the total number of controlled-\(U\) applications is \((2^p-1)k\), rather than \(2^{kp}-1\). This construction is used in the approximate reflection operator that underlies the walk-based quantum search algorithm simulated in the package [2606.14226].

## 5. Implemented algorithms and representative results

The paper presents SQWLib as a simulator not only for plain walk dynamics but for full algorithmic protocols built from Szegedy walks and QPE.

| Algorithm | Demonstration setup | Reported outcome |
|---|---|---|
| Marked-node detection | 2D lattice, \(N=1024\), \(M=0\) or \(M=10\), \(p=6\) | For \(M=0\), phase outcome \(0\) has probability \(1\); for \(M=10\), outcomes concentrate on non-zero phases |
| Quantum simulated annealing | 1D Ising spin glass, \(n=10\), \(N=1024\), \(p=3\), 5 temperature steps from \(\beta=0\) to \(\beta=1\) | Marginals match the classical stationary distribution; phase-0 probability at each step is \(>0.9\); joint probability over all 5 steps is \(\approx 0.7\) |
| Quantum search on graphs | Same Ising system, \(M=29\), \(p=3\), varying number of phase registers \(k\) | Larger \(k\) makes the success-probability curve approach the theoretical amplitude-amplification behavior |

For marked-node detection, the underlying transition matrix is modified so that marked vertices become absorbing sinks,
\[
G'_{ji}=
\begin{cases}
G_{ji}, & i\notin\mathcal{M},\\
\delta_{ji}, & i\in\mathcal{M}.
\end{cases}
\]
The algorithm prepares a uniform superposition over unmarked \(|\psi_i\rangle\) states. If there are no marked nodes, that state is an eigenstate with eigenvalue \(1\), so QPE returns phase \(0\). If marked nodes are present, the state has no support on the eigenspace with eigenvalue \(1\), and QPE yields non-zero phase with high probability given sufficient precision.

For quantum simulated annealing, SQWLib uses the similarity-transformed walk \(\widetilde{W}\) built from a Metropolis–Hastings chain. The acceptance probabilities are
\[
A_{ji}=\min\left(1,e^{\beta(E_i-E_j)}\right),
\]
and the transition matrix is
\[
G_{ji}=
\begin{cases}
\dfrac{A_{ji}}{|B_i|}, & j\in B_i,\\[4pt]
1-\sum_{k\in B_i}\dfrac{A_{ki}}{|B_i|}, & j=i,\\
0, & \text{otherwise.}
\end{cases}
\]
The library simulates the standard QPE-with-postselection protocol across a temperature schedule. In the demonstrated 1D Ising spin-glass instance,
\[
E=J\sum_{i=1}^{n-1}s_i s_{i+1},
\qquad
s_i\in\{-1,1\},
\qquad
J=1,
\]
the measured marginals agree closely with the classical Boltzmann distributions at each intermediate temperature.

For quantum search on graphs, SQWLib implements approximate amplitude amplification using an oracle
\[
Q=\mathbbm{1}_N-2\sum_{k\in\mathcal{M}}|k\rangle\langle k|,
\qquad
Q_1=Q\otimes\mathbbm{1}_N,
\qquad
Q_2=\mathbbm{1}_N\otimes Q,
\]
together with an approximate reflection around the stationary state \(|\pi\rangle\) obtained through QPE. The paper reports that as the number of phase registers increases, the success-probability curve approaches the theoretical one for perfect amplitude amplification; at the first maximum, the measured distribution over marked nodes matches the stationary distribution restricted to the marked subset [2606.14226].

## 6. Relation to SQUWALS, implementation dependence, and limitations

SQWLib is the direct successor to the earlier simulator “SQUWALS: A Szegedy QUantum WALks Simulator,” which already provided a memory-saving classical simulator for the original Szegedy walk and showed \(O(N^2)\) scaling in both time and memory for dense graphs [2307.14314]. SQUWALS supported the reflection \(R\), swap \(S\), simple oracles, mixed-state evolution, the semiclassical Szegedy walk, and high-level applications such as quantum PageRank. Its central contribution was the matrix-state representation that replaced \(N^2\times N^2\) operator storage with \(N\times N\) array operations [2307.14314].

SQWLib extends that earlier framework in four explicit directions. First, it adds operator-level generality by introducing efficient simulation for the primitive operators \(V\), \(V^\dagger\), and \(R_0\), which allows simulation of a broader class of Szegedy formulations. Second, it adds a NumPy-based sparse-state representation in the reduced subspace \(\mathcal{H}_R\), bringing time and memory down to \(O(|E|)\). Third, it integrates a QPE framework, including controlled-\(U\) simulation, multiple phase registers, and approximate reflections. Fourth, it analyzes implementation dependence by providing both \(V_1\) and \(V_2\): the reported QSA results are independent of whether \(V_1\) or \(V_2\) is used when measurement is included at each step, whereas some heuristic multi-chain behaviors without measurement are implementation-dependent [2606.14226].

The package is therefore not a framework for controlling quantum hardware; it is a classical simulator for numerical exploration of Szegedy-walk-based algorithms. The paper also states several limitations. Practical system size remains bounded by memory: \(N^2\) scaling for dense graphs limits \(N\) to a few thousands depending on the machine, and sparse simulations are limited by \(|E|\) and by the overhead of reduced-subspace tensors. The implementation assumes column-stochastic transition matrices and is tailored to Markov-chain-derived Szegedy walks. Alternative formulations that strongly exploit the second register may behave differently depending on how \(V\) is defined. Within those constraints, SQWLib is presented as a practical tool for studying Szegedy-walk-based algorithms numerically beyond purely analytical treatments; the code and scripts are available at `https://github.com/qDNA-yonsei/SQWLib` [2606.14226].

Source: https://www.emergentmind.com/topics/sqwlib