# The hafnian¶

*Section author: Nicolás Quesada <nicolas@xanadu.ai>*

## The hafnian¶

The hafnian of an \(n \times n\), symmetric matrix \(\mathbf{A} =\mathbf{A}^T\) is defined as [27]

where \(\text{PMP}(n)\) stands for the set of perfect matching permutations of \(n\) (even) objects, i.e., permutations \(\sigma:[n]\rightarrow [n]\) such that \(\forall i:\sigma(2i-1)<\sigma(2i)\) and \(\forall i:\sigma(2i-1)<\sigma(2i+1)\).

It was so named by Eduardo R. Caianiello [28] “to mark the fruitful period of stay in Copenhagen (*Hafnia* in Latin).” [29]

The set PMP(\(n\)) used to define the hafnian contains

elements and thus as defined it takes \((n-1)!!\) additions of products of \(n/2\) numbers to calculate the hafnian of an \(n \times n\) matrix. Note that the diagonal elements of the matrix \(\mathbf{A}\) do not appear in the calculation of the hafnian and are (conventionally) taken to be zero.

For \(n=4\) the set of perfect matchings is

and the hafnian of a \(4 \times 4\) matrix \(\mathbf{B}\) is

The hafnian of an odd sized matrix is defined to be zero; if \(\mathbf{A}=\mathbf{A}^T\) is \(M\) dimensional and \(M\) is odd then \(\haf(\mathbf{A}) = 0\). Note that, for convenience, we define the hafnian of an empty matrix, i.e., a matrix of dimension zero by zero, to be 1.

The hafnian is a homogeneous function of degree \(n/2\) in the matrix entries of an \(n \times n\) matrix \(\mathbf{A}\). This implies that

where \(\mu\) is a scalar. More generally, if \(\mathbf{W} = \text{diag}(w_0,\ldots,w_{n-1})\), then it holds that ( see proposition 4.2.3 of [27])

The definition used to introduce the hafnian is rather algebraic and brings little intuition. To gain more insight in the next section we introduce some graph theory language and use it to present a more intuitive vision of what the hafnian “counts”.

## Basics of graphs¶

A graph is an ordered pair \(G=(V,E)\) where \(V\) is the set of vertices, and \(E\) is the set of edges linking the vertices of the graph, i.e., if \(e \in E\) then \(e=(i,j)\) where \(i,j \in V\). In this section we consider graphs without loops (we relax this in the next section), thus we do not allow for edges \(e = (i,i)\) connecting a given vertex to itself. A 6 vertex graph is shown here

the vertices are labelled \(V = \{1,2,3,4,5,6 \}\) and the edges are \(E=\{(1,4),(2,4),(2,5),(3,4),(3,5),(3,6),(5,5) \}\).

A matching \(M\) is a subset of the edges in which no two edges share a vertex. An example of matching is \(M=(1,4)(3,6)\) represented by the blue lines in the following figure

In the figure above we know we have a matching because none of the highlighted edges shares a vertex.

A perfect matching is a matching which matches all the vertices of the graph, such as for example \(M=(1,4)(2,5)(3,6)\), which is represented again by the blue lines in the following figure

The blue lines represent a *perfect* matching because, they are a matching, i.e., the edges do no overlap on any vertex *and* all the vertices are covered by one and only edge.

A complete graph is a graph where every vertex is connected to every other vertex. For loopless graphs having \(n\) vertices, the number of perfect matchings is precisely [27]

where we use \(\text{PMP}(n)\) to indicate the set of perfect matchings of introduced in the previous section, and the notation \(|V|\) to indicate the number of elements in the set \(V\). Note that this number is nonzero only for even \(n\), since for odd \(n\) there will always be one unmatched vertex.

In the following figure we illustrate the 3 perfect matchings of a complete graph with 4 vertices

## Perfect matchings and hafnians¶

An important question concerning a given graph \(G=(V,E)\) is the number of perfect matchings it has. One possible way to answer this question is to iterate over the perfect matchings of a complete graph and at each step check if the given perfect matching of the complete graph is also a perfect matching of the given graph. A simple way to automatize this process is by constructing the adjacency matrix of the graph. The adjacency matrix \(\mathbf{A}\) of a graph \(G=(V,E)\) is a 0-1 matrix that has \(\mathbf{A}_{i,j} = \mathbf{A}_{j,i}=1\) if, and only if, \((i,j) \in E\) and 0 otherwise. For the example graph in the previous section, the adjacency matrix is

The number of perfect matchings of a (loopless) graph is simply given by the hafnian of its adjacency matrix

For the graph in the previous section we can easily confirm that the perfect matching we found is the only perfect matching since

The definition of the hafnian immediately generalizes to weighted graphs, where we assign a real or complex number to the entries of the symmetric matrix \(\mathbf{A}\).

## Special values of the hafnian¶

Here we list some special values of the hafnian for certain special matrices.

If the matrix \(\mathbf{A}\) has the following block form

then it holds that \(\text{haf}\left( \mathbf{A}_{\text{block}} \right) = \text{per}(\mathbf{C})\) where \(\text{per}\) is the permanent matrix function defined as [27]

The sum here extends over all elements \(\sigma\) of the symmetric group \(S_n\).

If \(\mathbf{A}_{\text{rank-one}} = \mathbf{e} \mathbf{e}^T\) is a rank one matrix of size \(n\) then

In particular, the hafnian of the all ones matrix is precisely \((n-1)!!\).

If \(\mathbf{A}_{\text{direct sum}} = \mathbf{A}_1 \oplus \mathbf{A}_2\) is a block diagonal matrix then

This identity simply expresses the fact that the number of perfect matchings of a graph that is made of two disjoint subgraphs is simply the product of the number of perfect matchings of the two disjoint subgraphs.

## Contents

## Downloads