---
title: 'Matrix Hafnians: Theory, Complexity, and Applications'
url: https://www.emergentmind.com/topics/matrix-hafnians
type: topic
---

# Matrix Hafnians: Theory, Complexity, and Applications

A matrix hafnian is a combinatorial matrix function that, for an even-dimensional symmetric matrix, encodes the sum over all perfect matchings of the complete graph with edge weights given by the matrix entries. Hafnians generalize the permanent (with applications in bipartite graphs) to nonbipartite, undirected settings and are fundamental in a variety of fields, including combinatorics, quantum optics (notably Gaussian Boson Sampling), and statistical physics. Efficient computation and sharp bounds for hafnians remain central subjects in computational complexity and algorithm design, with connections to #P-completeness and quantum advantage proposals.

## 1. Formal Definition and Key Properties

Given a symmetric matrix $A\in\mathbb{C}^{2n\times 2n}$, the hafnian is defined by
\[
\mathrm{haf}(A) = \sum_{\text{pair partitions } \{i_1,j_1\},\dots,\{i_n,j_n\}=\{1,\dots,2n\}} \prod_{k=1}^n a_{i_kj_k}
\]
where the sum runs over the $(2n)!/(2^n n!)$ unordered partitions of $\{1,\dots,2n\}$ into $n$ disjoint unordered pairs. The function is zero unless $A$ is of even order, and is fully symmetric in the matrix entries.

The hafnian generalizes the notion of the permanent, with the precise algebraic relationship:
\[
\mathrm{per}(B) = \mathrm{haf}\left(\begin{array}{cc}
0 & B \\
B^T & 0
\end{array}\right)
\]
for any $n\times n$ matrix $B$ [1405.1303], [1601.07518].

For general $N\times N$ (not necessarily even) matrices, the **loop hafnian** or $\lhaf(A)$ is defined as the sum over all "single-pair matchings" (partitions into pairs and singletons), allowing for loops (diagonal terms) [2108.01622], [2507.16100]. For zero-diagonal $A$, $\lhaf(A)$ reduces to $\mathrm{haf}(A)$.

## 2. Connections to Graph Theory and Combinatorics

For an unweighted, undirected graph $\Gamma$ on $2n$ vertices with adjacency matrix $A$, the hafnian $\mathrm{haf}(A)$ counts the number of perfect matchings in $\Gamma$, i.e., sets of $n$ edges covering each vertex exactly once and with no shared vertices [1409.3905], [2101.09722]. For graphs with edge weights, the hafnian becomes the sum over all perfect matchings of the product of edge weights in the matching.

In combinatorics, hafnians naturally enumerate certain chord diagrams, dimer coverings, and appear in the enumeration of matchings in regular and random graphs. Exact formulas and combinatorial interpretations exist for special classes of matrices, such as certain Toeplitz types and two-parameter models, yielding closed expressions in terms of matchings of subtemplate graphs [2101.09722], [1904.08651].

## 3. Computational Complexity and Algorithms

Computing the hafnian of a general symmetric matrix is #P-hard, mirroring the complexity of the permanent [1601.07518], [2212.06067]. For an $n\times n$ matrix, classical algorithms require exponential time. The fastest known exact algorithms, e.g., the Björklund–Gupt–Quesada method, achieve $O(n^3 2^{n/2})$ time by exploiting inclusion–exclusion and power trace techniques, and are strongly parallelizable [1805.12498], [2108.01622]. The algorithm's cubic prefactor per $2^{n/2}$ subset is asymptotically optimal, unless one assumes major breakthroughs in permanent computation.

For quasi-polynomial-time (in $n$) **approximate** computation, Barvinok established that if $A$ meets entrywise analytic conditions (e.g., $|a_{ij}-1|<0.19$ for all $i < j$), then $\mathrm{haf}(A)$ can be deterministically approximated within any prescribed relative error $\epsilon>0$ in $n^{O(\ln n-\ln\epsilon)}$ time by Taylor expansion of a suitable analytic generating function and complex-zeros analysis [1405.1303], [1601.07518]. This framework can also be extended to the computation of the logarithm of the hafnian for matrices with entries bounded in $[\delta,1]$ for arbitrary fixed $\delta>0$ [1601.07518].

For random nonnegative matrices with strong expansion (highly connected underlying graphs), a randomized estimator via determinants of random skew-symmetric Gaussian matrices provides a polynomial-time scheme with subexponential error guarantees, under expansion assumptions analogous to those required for probabilistic permanent estimators [1409.3905].

Table: Complexity of Key Hafnian Algorithms

| Algorithm/Estimator                  | Time Complexity         | Error/Regime                        |
|--------------------------------------|------------------------|-------------------------------------|
| Björklund–Gupt–Quesada exact method  | $O(n^3 2^{n/2})$       | Exact, general matrices             |
| Barvinok quasi-poly approx (analytic)| $n^{O(\ln n-\ln\epsilon)$ | Relative error $\epsilon$; entrywise analytic conditions |
| Randomized (Barvinok estimator)      | Poly($n$)              | Subexp error, strong expansion      |

For loop hafnians, similarly structured algorithms exist, and additional optimized schemes exploit collision structure and repeated row/column patterns to reduce prefactors in Gaussian Boson Sampling simulations [2108.01622], [1905.07011].

## 4. Analytic, Combinatorial, and Generating Function Techniques

Hafnians admit expansion and reduction identities, including a row-expansion (analogous to Laplace expansion for determinants) and a sum-of-matrices identity:
\[
\mathrm{haf}(A+B) = \sum_{k=0}^{n/2} \sum_{a\in Q_{2k,n}} \mathrm{haf}(A[a])\,\mathrm{haf}(B\{a\})
\]
where $A[a]$ is the principal submatrix indexed by $a$, and $B\{a\}$ is its complement [2101.09722], [1904.08651].

Generating function methods, notably via Gaussian integration, link (loop) hafnians directly to exponential moments of quadratic forms [2507.16100]. For a symmetric matrix $A$, the generating function
\[
G(\{z_j\})= \frac{ \exp\left(\frac 12 \mathbf{v}^T (I-\mathbb{Z}A)^{-1} \mathbb{Z} \mathbf{v} \right) }{\sqrt{ \det(I-\mathbb{Z}A) } }
\]
generates all loop hafnians of block expansions of $A$; the coefficient of $\prod_j z_j^{n_j}$ yields $\lhaf$ for repeated row/column blocks as appearing in Gaussian state output probability amplitudes [2507.16100].

For certain structured families, such as special Toeplitz matrices, closed-form analytic expressions for hafnians are available, with complexity reduced to $O(n)$ or $O(m^3)$ for matrix dimension $n=2m$ [1904.08651], [2101.09722].

## 5. Inequalities, Extremal Problems, and Asymptotics

Matrix hafnians satisfy nontrivial upper and lower bounds, with significant interest in analogues of van der Waerden’s and Tverberg’s theorems for permanents.

- **Upper bounds:** A generalized Laplace expansion combined with subset-convolution yields multiplicative upper bounds for hafnians, including, e.g.,
\[
|\mathrm{haf}(A)| \leq \frac{n!}{m!2^m} \left( \frac{1}{n(n-1)}\sum_{i\ne j} |a_{ij}|^2 \right)^{m/2}
\]
which can outperform Fischer’s permanent-based bound, especially for matrices with sign cancellations or complex structure [1906.06176].

- **Lower bounds:** Extension of hyperbolic polynomial techniques enables explicit lower bounds on the minimal hafnian over convex hulls of symmetric permutation matrices with zero diagonal. For the uniform matrix $C = (2n-1)^{-1}A(K_{2n})$,
\[
\mathrm{haf}_n(C) = \frac{(2n)!}{(2n-1)^n 2^n n!}
\]
which achieves asymptotic value $e^{-n}\sqrt{e}$ as $n\to\infty$ [1102.2542].

While local minimality for this bound is established for all $k$-matchings, global minimality fails for large $n$ due to explicit expander graph constructions, making the precise identification of global extremizers open for many regimes.

## 6. Applications in Quantum Optics and Gaussian Boson Sampling

Matrix hafnians and, crucially, loop hafnians are central in the calculation of output amplitudes and probabilities for quantum-optical experiments based on Gaussian states and photon-number measurements. In Gaussian Boson Sampling (GBS), the probability amplitude for a given detection pattern is proportional to the loop hafnian of a matrix derived from the state’s covariance matrix and displacement (for general detection patterns with collisions or loss) [2108.01622], [2212.06067], [1905.07011]. For the ideal zero-displacement case and no collisions, the hafnian suffices.

Recent developments include:
- Explicit demonstration of nonnegativity and monotonicity properties for hafnians of physical GBS-derivable matrices with block structure $A(Y,B)$, where $B$ is Hermitian and positive semidefinite [1811.10342].
- Development of fast classical algorithms and eigenvalue-sieve acceleration schemes enabling GBS simulation with much larger photon/mode numbers than previously possible, closing the quantum-classical computational divide for several experimental regimes [2108.01622].
- Tailored loop hafnian algorithms, exploiting repeated structure, enable orders-of-magnitude acceleration for heralded state preparation, underpinning simulation studies of non-Gaussian state generation under loss and practical device imperfections [1905.07011].

## 7. Extensions and Open Problems

Significant extensions include multidimensional hafnians (hyperhafnians), direct connections to moment-generating functions in Gaussian analysis, and structural links to other combinatorial matrix functions such as the Montrealer (encoding cumulants and Hamiltonian cycles) [2212.06067].

Open problems are abundant: optimal global lower bounds for hafnians on convex polytopes of fixed degree, further reduction of exponential prefactors in practical algorithms, and closed combinatorial characterizations for new classes of structured matrices. The boundary between classical and quantum computation for hafnian-based problems remains a dynamic research front [2108.01622].

---

In summary, matrix hafnians provide the fundamental, computationally hard, and algorithmically rich bridge between graph matchings and modern quantum-combinatorial phenomena. Their study integrates analytic, algebraic, computational, and physical methods and continues to inform the frontiers of both mathematical theory and quantum technology.

Source: https://www.emergentmind.com/topics/matrix-hafnians