|
cppqed-multidiagonal 0.4.0
Multidiagonal sparse operator algebra for C++QED
|
A header-only C++ library implementing sparse multidiagonal operator algebra for quantum systems of arbitrary arity, extracted from the C++QED framework.
A multidiagonal operator over a Hilbert space of dimension \(N\) is a sparse matrix storing an arbitrary set of diagonals at arbitrary offsets. In the context of quantum optics and cavity QED, Hamiltonians and jump operators naturally take this form. For example, the harmonic oscillator ladder operators \(a\), \(a^\dagger\) are single off-diagonals; number operators \(a^\dagger a\) are main diagonals, and so is the Kerr term \((a^\dagger)^2 a^2 = a^\dagger a\,(a^\dagger a - 1)\); two-photon terms like \(a^2\) sit two diagonals off the main one.
For a composite system of \(R\) subsystems, MultiDiagonal<RANK> stores an operator of the form
\[A = \sum_{\mathbf{o} \in \mathbb{K}} \sum_{\mathbf{n}} \alpha^{\mathbf{o}}_{\mathbf{n}} \left|\mathbf{n}\right\rangle\left\langle \mathbf{n}+\mathbf{o}\right|\]
with \(\mathbb{K} \subset \mathbb{Z}^R\) an arbitrary set of signed offset vectors, one entry per subsystem axis, and \(\mathbf{n}\) running over the multi-indices for which both \(\mathbf{n}\) and \(\mathbf{n}+\mathbf{o}\) lie in the truncated Hilbert space. A positive entry thus means an upper diagonal on that axis. The coefficients need not factorize over the subsystems; direct products, built with operator*, are the special case where they do.
Each diagonal is keyed by a signed offset per axis — positive = upper, negative = lower, zero = main diagonal. The offset counts the quanta a diagonal moves along each axis, and the algebra reduces to arithmetic on these counts:
The diagonal at signed offset \(o\) (per axis, dimension \(D\)) has extent \(D-|o|\), and entry \(k\) holds the matrix element \(A_{k+o^-,\,k+o^+}\) with \(o^\pm=\max(\pm o,0)\). This min(row,col)-anchored storage is symmetric under transposition, which is what makes conjugation a pure key negation. Application to a state vector costs \(O(N \cdot d)\) for \(d\) diagonals; composition runs over the \(d_A \cdot d_B\) diagonal pairs, each in one pass over the window where the two diagonals overlap.
The predecessor class Tridiagonal<RANK> in C++QED v2 stored exactly three diagonals per axis — at positions \(-K\), \(0\), \(+K\) for a single shared offset \(K\) — and was not closed under composition: the product of two tridiagonal operators is in general pentadiagonal, and with different offsets is not tridiagonal at all. MultiDiagonal lifts all these restrictions — any number of diagonals at independent arbitrary offsets, and the operator algebra is closed under composition (operator|), direct product (operator*), addition, and Hermitian conjugation.
The truncated algebra is represented exactly, including its physical deviations from the untruncated one: e.g. \([a,a^\dagger]\) at cutoff \(D\) is \(\mathrm{diag}(1,\ldots,1,1-D)\), not \(\mathbb{1}\) — as it must be for any finite-dimensional representation.
MultiDiagonal<RANK> for any compile-time arity \(M\), including composition at arbitrary rankcopy)tag_invoke, useful for testing and debuggingOn the reference machine (Intel Core i7-7700, GCC 16, Eigen 5.0.1), with the operator in cache, applying a MultiDiagonal is:
These figures are for the default build of both. The speed-up depends on the processor: on a 2022 laptop core it was 1.5–2.4× and 1.0–1.6×. -fcx-limited-range makes Eigen 10–17% faster and leaves the library unchanged, since its kernel multiplies in real arithmetic. Before the application loop was rewritten (#1, #23), the Jaynes–Cummings coupling was 3.4–4.5× slower than Eigen.
For operators as large as the state, the library is between 1.5× faster and 1.45× slower than Eigen. Once both sides are limited by memory bandwidth, no format gains much over a sparse matrix. What still holds the library back is its loop shape, one diagonal at a time with a store to the output for each:
That is a matter of the loop, not of the representation.
Measurements, causes and outlook, including what is not yet measured (the interaction picture): Performance of MultiDiagonal.
include/ to the include path and writing #include <multidiagonal/MultiDiagonal.h>)The library itself is header-only — nothing to build. Configuring and building compiles the test suite:
(Debug enables the library's bounds and dimension checks, which the test suite exercises; CI runs the Debug×Release matrix on GCC and Clang.)
A copy of the source tree in your project works too, via add_subdirectory.
Either way, the headers are included with their directory prefix, e.g. #include <multidiagonal/MultiDiagonal.h>.
Distributed under the Boost Software License, Version 1.0.
András Vukics — extracted from C++QED, originally developed 2006–2026.