C++ API

The library is header-only under include/pauliengine/ and needs nothing but a C++20 compiler; OpenMP is optional (compile with -DPAULIENGINE_HAS_OPENMP and your compiler’s OpenMP flag). PauliString, QubitHamiltonian (in Python PauliStringSum), the sparse classes, PauliCycle, PauliCycleSum and PauliOrbitSum are templates over the coefficient type: std::complex<double> or the polynomial pauliengine::symbolic::Expression from Polynomial.h. Operations exposed in Python are available in C++ under the same name, with these exceptions:

  • PauliStringSum.from_matrix is the free function pauli_decomposition (PauliDecomposition.h).

  • to_pauli_string_sum (cycles, orbits, unweighted sets) is to_qubit_hamiltonian.

  • inner_product and mixed-type commutator are free functions inner_product(a, b) and commutator(a, b) (Arithmetic.h).

  • subs is substitute and PauliString.get_pauli_at_index is get_pauli_from_index.

  • The factories (pe.PauliString, pe.PauliStringSum, …) and set_sparse_threshold exist in Python only: in C++ you choose the class, for example SparsePauliStringSum<C> for low-weight operators on many qubits, and convert with to_dense() or the constructor from a QubitHamiltonian.

Pauli strings

template<typename Coeff>
class PauliString

Public Types

using Pauli_structure = std::pair<Coeff, std::unordered_map<int, std::string>>
using Hamiltonian_structure = std::vector<Pauli_structure>

Public Functions

PauliString() = default
inline PauliString(const std::vector<uint64_t> &x, const std::vector<uint64_t> &y, Coeff coeff)
inline PauliString(const WordVec &x, const WordVec &y, Coeff coeff)
inline PauliString(WordVec &&x_in, WordVec &&y_in, Coeff coeff)
inline PauliString(Coeff coeff, const std::unordered_map<int, std::string> &data)
inline PauliString(const std::pair<Coeff, std::vector<std::pair<char, int>>> &input)
inline PauliString(Coeff &coeff, const std::string &pauli_string)

Construct a new Pauli String object.

Parameters:
  • pauli_string – : Dense such as “XIZ” or sparse such as “X0 Z2”.

  • coeff – : The coefficient associated with the Pauli string.

inline uint64_t operator==(const PauliString &other) const
inline size_t hash() const

Value hash over the operator part (x, y) only.

This is consistent with operator== (equal strings share x and y, hence the same hash) while deliberately ignoring the coefficient: hashing a complex or symbolic coefficient consistently with the numeric/symbolic equality used above is error-prone (e.g. -0.0 == 0.0, or (x+1)^2 == x^2+2x+1), so strings differing only by coefficient are allowed to collide. Both x and y are trimmed of trailing zero words on construction, so equal strings always hash identically.

inline PauliString operator*(const PauliString &other) const
inline PauliString &operator*=(const PauliString &other)
inline PauliString operator*(const std::complex<double> scalar) const
inline PauliString operator*(const int scalar) const
inline PauliString operator*(const double scalar) const
inline PauliString &operator*=(const std::complex<double> scalar)
inline uint64_t is_all_z() const
inline std::vector<uint64_t> get_hash() const

Coefficient-independent, invertible key of the operator part: the base-4 number sum_q p_q * 4^q with p_q = x_q + 2*y_q (I=0, X=1, Y=2, Z=3), as little-endian 64-bit limbs (limb k holds qubits 32k..32k+31).

Trailing zero limbs are dropped, so the identity maps to {}.

inline size_t size() const

Number of qubits with non-identity operator.

inline int count_y() const

Number of Y operators (x bit = 0, y bit = 1).

inline PauliString naked() const

Return a copy with coefficient set to 1.

inline Pauli_structure to_dictionary() const
inline PauliString map_qubits(std::unordered_map<int, int> qubit_map) const
inline std::string to_string() const
inline PauliString commutator(const PauliString &other) const
inline Coeff targeted_commutator(const PauliString &other, const PauliString &target) const

Coefficient of a single target operator in [this, other].

The commutator of two Pauli strings is at most one term, 2*PQ or 0, so this has the same O(n) cost as the full commutator; it only skips forming the product words: the XOR rule decides whether PQ can be target, and a single phase pass gives both the commutation parity and i^k.

inline Coeff get_coeff() const
inline void set_coeff(Coeff new_coeff)
inline PauliString trace_out_qubits(const std::vector<int> &qubits, const std::vector<int> &state) const

Project this Pauli string onto a computational-basis state for the listed qubits.

state[i] = 0 means qubit qubits[i] is |0>, state[i] = 1 means |1>. Returns a NEW PauliString with the traced qubits removed;

Rules per traced qubit q:

  • I: <s|I|s> = 1 → keep as-is

  • X: <0|X|0> = <1|X|1> = 0 → result is 0

  • Y: <0|Y|0> = <1|Y|1> = 0 → result is 0

  • Z: <0|Z|0> = +1, <1|Z|1> = -1 → drop the Z, sign-flip if state=1

inline PauliString trace_out_qubits(const std::vector<int> &qubits, const std::vector<std::pair<std::complex<double>, std::complex<double>>> &states) const

Project this Pauli string onto an arbitrary single-qubit state |psi_i> = a_i |0> + b_i |1> for each traced qubit.

inline std::vector<int> qubits() const

Sorted indices of the qubits with a non-identity operator.

inline std::vector<std::pair<char, int>> key_openfermion() const

OpenFermion-style key: (Pauli, qubit) pairs for the non-identity sites, sorted by qubit.

inline std::string get_pauli_from_index(int index)
inline PauliString diff(const std::string &symbol_name) const
template<typename Values>
inline PauliString substitute(const Values &substitution_map) const

Values ({name: complex}) or polynomials ({name: Expression}) substituted for symbols of the coefficient.

inline PauliString copy() const
inline bool equals(const PauliString &other)

Public Members

WordVec x
WordVec y
Coeff coeff = {0.0}

Public Static Functions

static inline PauliString from_hash(const std::vector<uint64_t> &limbs, Coeff coeff)

Inverse of get_hash(): the Pauli string with that operator part and the given coefficient.

static inline Coeff commutator_coeff(const Coeff &a, const Coeff &b, int power_of_i)

Coefficient 2 * i^k * a * b of the commutator term of two anti-commuting strings with coefficients a, b and product phase i^k.

static inline std::complex<double> to_complex(const std::complex<double> &expr)
static inline std::complex<double> to_complex(const Expression &expr)

Value of a numeric expression; throws std::runtime_error if symbols are left.

class unweighted_pauliString

A Pauli string without a coefficient: i^phase * P with P a tensor product of I, X, Y and Z and phase in {0, 1, 2, 3}.

Use it only as a basis element of a Lie algebra; linear combinations need PauliString and QubitHamiltonian.

The product of two Pauli operators is again a Pauli operator up to a power of i, so these strings are closed under multiplication: the phase is an exact integer modulo 4 and no floating-point number is involved. P itself is Hermitian, so phase 0 means +P and not a phase relative to an XZ ordering.

Public Functions

unweighted_pauliString() = default

The identity with phase 0.

inline explicit unweighted_pauliString(const std::unordered_map<int, std::string> &paulis, int phase = 0)

From a map {qubit: “X”/”Y”/”Z”/”I”}.

inline explicit unweighted_pauliString(const std::vector<std::pair<char, int>> &paulis, int phase = 0)

From a list of (Pauli, qubit) pairs.

inline explicit unweighted_pauliString(const std::string &paulis, int phase = 0)

From a dense text such as “XIZ” or a sparse one such as “X0 Z2”; empty is the identity.

template<typename Coeff>
inline explicit unweighted_pauliString(const PauliString<Coeff> &string, int phase = 0)

The operator part of a Pauli string; its coefficient is dropped.

inline unweighted_pauliString(const uint64_t *x_words, const uint64_t *y_words, size_t n_words, int phase)

From symplectic words; trailing zero words are dropped.

inline int phase() const noexcept

Exponent k of the phase i^k, in {0, 1, 2, 3}.

inline std::complex<double> phase_factor() const noexcept

The phase i^k as a complex number, with exact real and imaginary parts.

inline unweighted_pauliString operator*(const unweighted_pauliString &other) const

Product including the phase of the Pauli algebra.

inline unweighted_pauliString &operator*=(const unweighted_pauliString &other)
inline unweighted_pauliString operator-() const

The string multiplied by -1.

inline bool operator==(const unweighted_pauliString &other) const

Equal operator and equal phase.

inline bool equals(const unweighted_pauliString &other) const

Equal operator, ignoring the phase.

inline bool commutes(const unweighted_pauliString &other) const

True if the two strings commute, false if they anti-commute.

inline std::optional<unweighted_pauliString> commutator(const unweighted_pauliString &other) const

The commutator without its factor 2, as used for a Lie algebra basis: [P, Q] = 2 * P * Q if the strings anti-commute, so this returns P * Q then and nothing if they commute.

inline unweighted_pauliString dagger() const

Hermitian conjugate: the operator stays and the phase is conjugated.

inline unweighted_pauliString naked() const

Copy with phase 0.

template<typename Coeff>
inline PauliString<Coeff> to_pauli_string(Coeff coeff) const

The Pauli string with coefficient coeff * i^phase.

inline size_t hash() const

Value hash over the operator part, consistent with operator==; strings differing only by the phase are allowed to collide.

inline size_t size() const

See PauliString::size().

inline int count_y() const

See PauliString::count_y().

inline std::vector<int> qubits() const

See PauliString::qubits().

inline std::vector<std::pair<char, int>> key_openfermion() const

See PauliString::key_openfermion().

inline std::vector<uint64_t> get_hash() const

See PauliString::get_hash(); the phase is not part of the key.

inline std::string get_pauli_from_index(int index) const

Pauli operator (“I”, “X”, “Y” or “Z”) on the given qubit.

inline std::pair<std::complex<double>, std::unordered_map<int, std::string>> to_dictionary() const

The string as a (phase factor, {qubit: “X”/”Y”/”Z”}) pair.

inline std::string to_string() const

Human-readable form, e.g. “+i : X(0)Z(2)”.

Public Members

WordVec x
WordVec y

Public Static Functions

static inline unweighted_pauliString from_hash(const std::vector<uint64_t> &limbs, int phase = 0)

Inverse of get_hash().

class WordVec

Small-buffer word storage for the symplectic bit representation: up to kInlineWords 64-bit words (= 128 qubits) live inline without touching the heap; longer strings fall back to a single heap allocation.

Pauli string multiplication creates one short-lived string per pair, so avoiding malloc/free on that path matters. Size and capacity are 32-bit, which keeps the object at 32 bytes; a sum of millions of strings is written faster.

Public Functions

inline WordVec() noexcept
inline explicit WordVec(size_t n)
inline WordVec(const WordVec &o)
inline WordVec(WordVec &&o) noexcept
inline WordVec(const std::vector<uint64_t> &v)
inline WordVec &operator=(const WordVec &o)
inline WordVec &operator=(WordVec &&o) noexcept
inline ~WordVec()
inline size_t size() const noexcept
inline bool empty() const noexcept
inline uint64_t *data() noexcept
inline const uint64_t *data() const noexcept
inline uint64_t &operator[](size_t i) noexcept
inline const uint64_t &operator[](size_t i) const noexcept
inline uint64_t *begin() noexcept
inline uint64_t *end() noexcept
inline const uint64_t *begin() const noexcept
inline const uint64_t *end() const noexcept
inline void resize(size_t n)

Grows zero-filled; shrinking never reallocates.

inline void assign(const uint64_t *first, const uint64_t *last)
inline std::vector<uint64_t> to_vector() const

Public Static Attributes

static constexpr size_t kInlineWords = 2

Friends

inline friend bool operator==(const WordVec &a, const WordVec &b)
inline friend bool operator!=(const WordVec &a, const WordVec &b)

Qubit Hamiltonians

template<typename Coeff>
class QubitHamiltonian

A sum of Pauli strings.

Kept compacted: no two terms share an operator, and zero terms are dropped (only scaling by zero leaves them in place).

The terms are stored flat for both coefficient types: the operator words of all terms in one block, complex coefficients as an array of numbers (pauliengine_detail::FlatTerms), polynomial ones as a vector of values (pauliengine_detail::SymbolicTerms). Products, commutators, sums and compaction work on that storage directly and in parallel; terms() builds Pauli strings from it on request.

Public Types

using TermStorage = Storage

The flat storage of the terms: pauliengine_detail::FlatTerms for complex, pauliengine_detail::SymbolicTerms for polynomial coefficients.

Public Functions

inline QubitHamiltonian(const std::vector<PauliString<Coeff>> &terms)
inline QubitHamiltonian(std::vector<PauliString<Coeff>> &&terms)
inline QubitHamiltonian(std::vector<PauliString<Coeff>> &&terms, already_compacted_t)

Fast-path constructor: skips compact().

Only use when you can prove the vector has no duplicates and no zero-coefficient terms.

inline QubitHamiltonian(TermStorage &&store, already_compacted_t)

From flat storage whose terms are already compacted: no two share an operator, and no coefficient is zero.

Takes it over without sorting.

inline const TermStorage &term_storage() const

The flat storage of the terms (read-only), for conversions without building one Pauli string per term.

inline QubitHamiltonian(const Hamiltonian_structure<Coeff> &data)
inline size_t size() const

Number of terms.

inline PauliString<Coeff> term(size_t i) const

Term i as a Pauli string.

inline std::vector<PauliString<Coeff>> terms() const

All terms as Pauli strings, built from the flat storage.

inline QubitHamiltonian trace_out_qubits(const std::vector<int> &qubits, const std::vector<int> &state) const
inline QubitHamiltonian trace_out_qubits(const std::vector<int> &qubits, const std::vector<std::pair<std::complex<double>, std::complex<double>>> &states) const

Overload accepting arbitrary single-qubit amplitudes (a, b) for each traced qubit (general superposition states).

inline std::string to_string() const
inline QubitHamiltonian operator*(std::complex<double> scale) const
inline QubitHamiltonian operator*(int scale) const
inline QubitHamiltonian operator*(double scale) const
inline QubitHamiltonian operator*(const QubitHamiltonian &other) const

Product this * other.

For h * h (the same object on both sides) the square formula below needs only about a quarter of the pairs.

inline QubitHamiltonian operator+(const QubitHamiltonian &other) const
inline QubitHamiltonian operator-(const QubitHamiltonian &other) const
inline QubitHamiltonian operator-() const
inline bool operator==(const QubitHamiltonian &other) const
inline Coeff targeted_commutator(const QubitHamiltonian &other, const PauliString<Coeff> &target) const

Coefficient of a single target Pauli operator in the commutator [this, other], computed as a hash-join instead of the full O(|A||B|) pair scan (PauliEngine Features, “targeted commutators”).

For a fixed term P and target R there is exactly one operator Q that can produce R, namely v(Q) = v(P) XOR v(R) (equivalently Q ∝ P·R). We hash one side by its operator signature and, for each term of the other side, look up the unique matching partner, accumulating the commutator coefficient of the target. Cost O(min(|A|,|B|) L) expected word operations for the lookups plus O(max(|A|,|B|) L) for building the index, where L is the number of words per string. Works on the stored terms directly: the candidate operator is a plain XOR of words, and one phase pass per found partner gives both the commutation parity and i^k, without forming the product.

inline QubitHamiltonian set_all_coeff(std::complex<double> value) const
inline QubitHamiltonian diff(std::string symbol) const
template<typename Values>
inline QubitHamiltonian substitute(const Values &substitution_map) const

Values ({name: complex}) or polynomials ({name: Expression}) substituted for symbols of every coefficient.

template<typename C>
inline Coeff contains(const PauliString<C> &ps) const
inline Coeff contains(const std::unordered_map<int, std::string> &ops) const
inline Coeff contains(const std::vector<std::pair<char, int>> &ops) const
inline Coeff contains(const std::string &ops) const
inline QubitHamiltonian compact()
inline Hamiltonian_structure<Coeff> to_dictionary() const
inline std::vector<PauliString<Coeff>> to_list() const
inline QubitHamiltonian commutator(const QubitHamiltonian &other) const
inline std::vector<int> qubits() const
inline int n_qubits() const
inline bool is_all_z() const
inline int count_measurements() const
inline bool is_hermitian() const
inline bool is_antihermitian() const
inline std::vector<PauliString<Coeff>> paulistrings() const
inline QubitHamiltonian dagger() const
inline QubitHamiltonian conjugate() const

Complex conjugate: conjugated coefficients, and a sign for an odd number of Y.

inline QubitHamiltonian transpose() const

Transpose: a sign for an odd number of Y.

inline QubitHamiltonian simplify(double threshold = 0.0) const
inline std::pair<QubitHamiltonian, QubitHamiltonian> split() const
inline QubitHamiltonian map_qubits(std::unordered_map<int, int> qubit_map) const
inline QubitHamiltonian power(int n) const
inline QubitHamiltonian square() const

this * this, from the pairs i <= j only.

P_i P_j and P_j P_i are equal if the strings commute and cancel if they anti-commute, and P_i P_i = I, so

H^2 = (sum_i a_i^2) I + 2 sum_{i < j, P_i P_j = P_j P_i} a_i a_j P_i P_j .

About a quarter of the pairs of the general product remain, and the anti-commuting pairs leave no rounding residue.

inline Matrix2D to_matrix(bool ignore_unused_qubits = true) const

Public Static Functions

static inline QubitHamiltonian zero()
static inline QubitHamiltonian unit()
class unweighted_QubitHamiltonian

An ordered set of Pauli strings without coefficients, each with its own phase i^k, to be used only as the basis of a Lie algebra.

It is not a linear combination: every operator occurs once and the first phase seen for an operator wins, so A + A = A.

An element keeps its index for good, and an open-addressing table finds an operator without touching most other elements.

Public Functions

unweighted_QubitHamiltonian() = default

The empty set.

inline explicit unweighted_QubitHamiltonian(const std::vector<unweighted_pauliString> &terms)

From a list of strings; later duplicates of an operator are ignored.

template<typename Coeff>
inline explicit unweighted_QubitHamiltonian(const QubitHamiltonian<Coeff> &hamiltonian)

The operators of a Hamiltonian with phase 0; its coefficients are dropped.

inline size_t size() const noexcept

Number of elements.

inline bool insert(const unweighted_pauliString &term)

Adds the string unless its operator is present; returns whether it was added.

inline bool contains(const unweighted_pauliString &term) const

True if the operator of the string is present, whatever the phases are.

inline std::optional<size_t> find(const unweighted_pauliString &term) const

Index of the operator of the string, if it is present.

inline const std::vector<unweighted_pauliString> &terms() const noexcept

All elements in insertion order.

inline unweighted_QubitHamiltonian operator+(const unweighted_QubitHamiltonian &other) const

Union of two sets: the elements of this one, then the new ones of other.

inline unweighted_QubitHamiltonian &operator+=(const unweighted_QubitHamiltonian &other)
inline unweighted_QubitHamiltonian operator-() const

Every element multiplied by -1 (its phase i^k becomes i^(k+2)).

inline unweighted_QubitHamiltonian operator*(const unweighted_QubitHamiltonian &other) const

The set of all products a * b in the order of a, then b.

inline unweighted_QubitHamiltonian commutator(const unweighted_QubitHamiltonian &other) const

The set of commutators without their factor 2: a * b for every anti-commuting pair, in the order of a, then b.

See unweighted_pauliString::commutator().

inline bool operator==(const unweighted_QubitHamiltonian &other) const

Equal sets with equal phases, whatever the order of the elements is.

template<typename Coeff>
inline QubitHamiltonian<Coeff> to_qubit_hamiltonian(Coeff coeff) const

The ordinary Hamiltonian with coefficient coeff * i^k on every element.

Sparse Pauli strings and sums

For low-weight operators on many qubits: only the non-identity sites are stored, so products and commutators cost O(weight) per pair instead of O(n / 64).

template<typename Coeff>
class SparsePauliString

Public Types

using Site = pauliengine_detail::Site

Public Functions

inline explicit SparsePauliString(Coeff c = pauliengine_detail::coeff_one<Coeff>())

coeff times the identity.

inline SparsePauliString(Coeff c, const std::unordered_map<int, std::string> &paulis)

From a coefficient and a map {qubit: “X”/”Y”/”Z”/”I”}.

inline SparsePauliString(Coeff c, std::vector<Site> s)

From a coefficient and sites (ascending qubits, no identities; checked).

inline explicit SparsePauliString(const PauliString<Coeff> &dense)

The same string as a PauliString.

inline PauliString<Coeff> to_dense() const

The string as a PauliString (bit vectors up to its highest qubit).

inline bool fits_sparse() const

Whether the string still fits the sparse representation (see pauliengine_detail::sparse_stays); otherwise to_dense() is the better form.

inline size_t size() const

Number of non-identity sites.

inline std::vector<int> qubits() const

Sorted qubits with a non-identity operator.

inline std::string get_pauli_at_index(int q) const

Pauli operator (“I”, “X”, “Y” or “Z”) on qubit q.

inline int count_y() const
inline bool is_all_z() const
inline std::pair<Coeff, std::unordered_map<int, std::string>> to_dictionary() const

(coefficient, {qubit: “X”/”Y”/”Z”}).

inline std::vector<std::pair<char, int>> key_openfermion() const

OpenFermion-style key: [(Pauli, qubit), …] by ascending qubit.

inline std::string to_string() const
inline size_t hash() const

Hash of the operator part only, consistent with equals() and operator==.

inline bool equals(const SparsePauliString &other) const

Whether the operators are equal, ignoring the coefficients.

inline bool operator==(const SparsePauliString &other) const
inline SparsePauliString operator*(const SparsePauliString &other) const

Product with the phase of the Pauli algebra.

inline SparsePauliString operator*(std::complex<double> scale) const
inline SparsePauliString operator*(double scale) const
inline SparsePauliString operator*(int scale) const
inline SparsePauliString operator-() const
inline bool anticommutes(const SparsePauliString &other) const

Whether the strings anti-commute.

inline SparsePauliString commutator(const SparsePauliString &other) const

[this, other]: 2 * this * other if they anti-commute, else the zero string (identity with coefficient 0).

inline Coeff targeted_commutator(const SparsePauliString &other, const SparsePauliString &target) const

Coefficient of target’s operator in [this, other].

inline SparsePauliString naked() const

Copy with coefficient 1.

inline SparsePauliString map_qubits(const std::unordered_map<int, int> &qubit_map) const

Copy with qubit indices remapped by {old: new}; an occupied target that is not itself remapped is swapped (as PauliString::map_qubits).

inline SparsePauliString diff(const std::string &name) const

Derivative of the coefficient by the symbol name (symbolic coefficients).

template<typename Values>
inline SparsePauliString substitute(const Values &values) const

Values or polynomials substituted for symbols of the coefficient.

Public Members

std::vector<Site> sites

The non-identity sites, by ascending qubit (see detail/SparseSites.h).

Coeff coeff = pauliengine_detail::coeff_one<Coeff>()
template<typename Coeff>
class SparsePauliStringSum

Public Types

using Site = pauliengine_detail::Site
using Terms = pauliengine_detail::SparseTerms<Coeff>
using String = SparsePauliString<Coeff>

Public Functions

SparsePauliStringSum() = default

The zero operator.

inline explicit SparsePauliStringSum(const std::vector<String> &terms)

From terms that may repeat operators: equal operators are merged, zeros dropped.

inline explicit SparsePauliStringSum(const std::vector<PauliString<Coeff>> &terms)

From Pauli strings with bit vectors.

inline explicit SparsePauliStringSum(const Hamiltonian_structure<Coeff> &data)

From a list of (coefficient, {qubit: “X”/”Y”/”Z”}) pairs.

inline explicit SparsePauliStringSum(const QubitHamiltonian<Coeff> &dense)

The same operator as a QubitHamiltonian.

Its terms are compacted already, so the sites are read off the words row by row, in parallel, and nothing is merged.

inline const Terms &terms() const
inline size_t size() const
inline String term(size_t i) const
inline std::vector<String> paulistrings() const
inline QubitHamiltonian<Coeff> to_dense() const

The operator as a QubitHamiltonian (bit vectors up to the highest qubit).

inline Hamiltonian_structure<Coeff> to_dictionary() const
inline std::string to_string() const
inline SparsePauliStringSum operator+(const SparsePauliStringSum &other) const
inline SparsePauliStringSum operator-(const SparsePauliStringSum &other) const
inline SparsePauliStringSum operator-() const
inline SparsePauliStringSum operator*(std::complex<double> s) const
inline SparsePauliStringSum operator*(double s) const
inline SparsePauliStringSum operator*(int s) const
inline SparsePauliStringSum operator*(const SparsePauliStringSum &other) const

Product this * other; h * h (the same object) uses the square formula.

inline SparsePauliStringSum square() const

this * this = sum_i a_i^2 I + 2 sum_{i < j, commuting} a_i a_j P_i P_j: the anti-commuting pairs cancel, so each unordered pair is visited once.

inline SparsePauliStringSum power(int n) const
inline SparsePauliStringSum commutator(const SparsePauliStringSum &other) const

[this, other] = 2 sum over anti-commuting pairs of a_i b_j P_i P_j.

When few pairs share a qubit, only those are visited.

inline Coeff targeted_commutator(const SparsePauliStringSum &other, const String &target) const

Coefficient of target’s operator in [this, other], via a hash join: for each term P of the smaller sum the only partner that can give target is Q = P * target (up to phase).

O((|A| + |B|) w) expected.

inline Coeff contains(const String &ps) const

Coefficient of the operator of ps (0 if absent); its coefficient is ignored.

inline Coeff inner_product(const SparsePauliStringSum &other) const

Normalized Hilbert-Schmidt inner product sum_P conj(a_P) b_P.

inline bool operator==(const SparsePauliStringSum &other) const
inline std::vector<int> qubits() const
inline int n_qubits() const
inline size_t span() const

Highest qubit index + 1 (0 for the zero operator or the identity).

inline bool fits_sparse() const

Whether the operator still fits the sparse representation (see pauliengine_detail::sparse_stays); otherwise to_dense() is the better form.

inline bool is_all_z() const
inline int count_measurements() const
inline SparsePauliStringSum simplify(double threshold = 0.0) const

Terms with |coefficient| <= threshold dropped (symbolic ones that do not evaluate to a number are kept).

inline SparsePauliStringSum set_all_coeff(const Coeff &value) const
inline SparsePauliStringSum dagger() const

Hermitian conjugate: every coefficient conjugated.

inline SparsePauliStringSum conjugate() const

Complex conjugate: conjugated coefficients, sign flipped for an odd number of Y.

inline SparsePauliStringSum transpose() const

Transpose: sign flipped for an odd number of Y.

inline SparsePauliStringSum diff(const std::string &name) const
template<typename Values>
inline SparsePauliStringSum substitute(const Values &values) const

Values or polynomials substituted for symbols of every coefficient.

inline SparsePauliStringSum map_qubits(const std::unordered_map<int, int> &qubit_map) const

Copy with qubit indices remapped (see PauliString::map_qubits); terms that become equal are merged.

Public Static Functions

static inline SparsePauliStringSum from_terms(Terms terms)

Already compacted terms.

static inline SparsePauliStringSum zero()
static inline SparsePauliStringSum unit()

Symbolic coefficients

class Expression

A polynomial sum_k c_k * m_k with complex c_k and monomials m_k.

Storage: one buffer of 64-bit words. Term k is [nf, re, im, f_1 .. f_nf], with re and im the bits of the coefficient and f_i = (symbol id << 32) | exponent, sorted by id. Terms are sorted by monomial (lexicographic over the factor words, a prefix first), so the constant term comes first.

Public Types

using complex = std::complex<double>

Public Functions

Expression() = default
inline Expression(int v)
inline Expression(double v)
inline Expression(complex v)
inline explicit Expression(const std::string &text)

Parse “2*a*b + 3”, “a**2 - b/4”, “(1 + 2*I)*c”, “2a”, “conjugate(a)”.

inline explicit Expression(const char *text)
inline size_t n_terms() const

Number of terms; 0 for the zero polynomial.

inline bool is_zero() const
inline bool is_number() const

True if the expression has no symbols.

inline complex to_complex() const

The value of a numeric expression; throws if symbols are left.

inline bool operator==(const Expression &o) const
inline bool operator!=(const Expression &o) const
inline Expression operator-() const
inline Expression &operator+=(const Expression &o)
inline Expression &operator-=(const Expression &o)
inline Expression &operator*=(const Expression &o)
inline Expression &operator*=(complex s)
inline Expression &operator*=(double s)
inline Expression &operator*=(int s)
inline Expression pow(unsigned n) const

this**n for n >= 0, by repeated squaring.

inline Expression diff(const std::string &name) const

Derivative with respect to the symbol name (0 if it does not occur).

conjugate(name) counts as the same variable unless the symbol is complex.

inline Expression diff(const DiffTarget &target) const
inline Expression diff(uint32_t id) const
inline Expression subs(const std::unordered_map<std::string, complex> &values) const

Substitute numbers for symbols, given by name (see prepare_subs).

inline Expression subs(const Substitution &s) const
inline Expression subs(const std::unordered_map<std::string, Expression> &values) const

Substitute polynomials for symbols, given by name, all at once: {“a”: b, “b”: a} swaps a and b.

Numbers are constant polynomials, so this also covers subs with values.

inline Expression subs(const PolySubstitution &s) const
inline Expression conjugate() const

Complex conjugate: conjugated coefficients, every symbol swapped with its partner conjugate(a), except real symbols, which stay.

inline Expression conjugate(const std::vector<uint32_t> &conj) const

conjugate() with the map from conjugation_map(); empty means all real.

inline std::vector<std::string> free_symbols() const

Names of the symbols that occur, sorted by name.

inline std::string str() const

Readable form that the parser reads back exactly, e.g. “2 + 3*a*b**2 - I*c”.

template<typename F>
inline void for_each_term(F &&f) const

Calls f(coefficient, factors, n_factors) for every term in canonical order; a factor is a word read with symbol_of() and exponent_of().

inline void append_term_unchecked(complex c, const uint64_t *f, size_t nf)

Appends a term without any check: the coefficient must be non-zero, the factors sorted by symbol, and the monomial greater than all present ones.

inline size_t hash() const

Public Static Functions

static inline Expression symbol(const std::string &name)
static inline Expression symbol(uint32_t id)
template<typename Get>
static inline Expression sum_of(size_t n, Get &&get)

get(0) + … + get(n - 1) in one merge; get(k) returns a const Expression&.

Adds equal monomials in index order, like repeated + would.

static inline bool prepare_diff(const std::string &name, DiffTarget &target)

Resolves name for diff(); false if the symbol does not exist.

static inline Substitution prepare_subs(const std::unordered_map<std::string, complex> &values)

Resolves {name: value}.

Unknown names are ignored. For a complex symbol a, conjugate(a) gets conj(value) unless it is given too.

static inline PolySubstitution prepare_subs(const std::unordered_map<std::string, Expression> &values)

Resolves {name: polynomial}.

Unknown names are ignored. For a complex symbol a, conjugate(a) gets the conjugated polynomial unless it is given too.

static inline std::vector<uint32_t> conjugation_map()

The conjugation map for conjugate(map); fetch it once for many conjugations.

It covers every symbol that exists when it is fetched.

static inline uint32_t symbol_of(uint64_t f)
static inline uint32_t exponent_of(uint64_t f)
static inline uint64_t make_factor(uint32_t id, uint32_t exp)

Friends

inline friend Expression operator+(const Expression &a, const Expression &b)
inline friend Expression operator-(const Expression &a, const Expression &b)
inline friend Expression operator*(const Expression &a, const Expression &b)
inline friend Expression operator*(const Expression &a, complex s)
inline friend Expression operator*(complex s, const Expression &a)
inline friend Expression operator*(const Expression &a, double s)
inline friend Expression operator*(double s, const Expression &a)
inline friend Expression operator*(const Expression &a, int s)
inline friend Expression operator*(int s, const Expression &a)
inline friend Expression operator+(const Expression &a, complex s)
inline friend Expression operator+(complex s, const Expression &a)
inline friend Expression operator-(const Expression &a, complex s)
inline friend Expression operator-(complex s, const Expression &a)
inline friend Expression operator+(const Expression &a, double s)
inline friend Expression operator+(double s, const Expression &a)
inline friend Expression operator-(const Expression &a, double s)
inline friend Expression operator-(double s, const Expression &a)
struct DiffTarget

The symbols a derivative by one name acts on: the symbol, and its partner conjugate(a) unless the symbol is complex (then the two are independent).

Public Members

uint32_t id = SymbolTable::kNone
uint32_t partner = SymbolTable::kNone
struct Substitution

Values for symbols, resolved to ids once for many subs() calls.

Public Functions

inline bool empty() const
inline const complex *find(uint32_t id) const

Public Members

std::vector<std::pair<uint32_t, complex>> by_id
struct PolySubstitution

Polynomials for symbols, resolved to ids once for many subs() calls.

Public Functions

inline bool empty() const
inline const Expression *find(uint32_t id) const

Public Members

std::vector<std::pair<uint32_t, Expression>> by_id

Pauli cycles

template<typename Coeff>
class PauliCycle

Represents a Pauli string with a coefficient of type Coeff (std::complex<double> or symbolic::Expression) together with all of its cyclic rotations on n_qubits qubits, i.e.

the operator

C(P) = coeff * sum_{k=0}^{n-1} R^k P        (R = cyclic shift by one qubit)

For example, n = 3 and the base string XIY stands for XIY + YXI + IYX. The sum always runs over all n rotations, so a base with a shorter cyclic period d (n % d == 0) counts each distinct rotation n/d times: the cycle of ZZZ on 3 qubits is 3*ZZZ, and that of XIXI on 4 qubits is 2*(XIXI + IXIX). This is what makes C(P) C(Q) = sum_k C(P R^k Q) hold for every base. https://arxiv.org/pdf/2604.16701

Public Functions

inline PauliCycle(int qubits, const std::unordered_map<int, std::string> &data, Coeff coeff = pauliengine_detail::coeff_one<Coeff>())

Cycle of the base {qubit: “X”/”Y”/”Z”} on qubits qubits with coefficient coeff.

Throws std::invalid_argument if qubits < 1 or the base acts on a qubit >= qubits.

inline PauliCycle(int qubits, const std::string &pauli_string, Coeff coeff = pauliengine_detail::coeff_one<Coeff>())

Cycle of a dense base string such as “XIY” (character i acts on qubit i).

inline PauliCycle(int qubits, const PauliString<Coeff> &base)

Cycle of an existing PauliString; its coefficient becomes the cycle’s.

inline void unify()

Rewrite Base to the unified representative of the orbit (see ensure_unified()).

Optional and idempotent. operator==/hash are rotation-invariant on their own, so this is only needed when you want the stored Base (and hence rotate()/repr) to be unified.

inline PauliString<Coeff> rotate(int k) const

Cyclic shift by k qubits: new[(i + k) mod n] = old[i].

Word-parallel, O(n / 64).

inline std::vector<PauliString<Coeff>> rotations() const

All n rotations rotate(0), …, rotate(n - 1) of the stored base.

inline QubitHamiltonian<Coeff> to_qubit_hamiltonian() const

The cycle expanded into an explicit QubitHamiltonian (sum of all rotations).

template<typename C>
inline bool operator==(const PauliCycle<C> &other) const

Orbit-level equality: two cycles are equal iff they describe the same rotation orbit.

Both sides are unified on demand (cached), so no explicit unify() call is required. The coefficient (and its type) is ignored (structural equality of the cycle).

inline int period() const

Period d of the base: the smallest d > 0 with R^d P = P, i.e.

the number of distinct rotations (d divides n_qubits). The sum over all n rotations counts each distinct one n/d times. Computed in O(n/64 + w) from the cyclic (gap, Pauli) sequence: if that sequence consists of r repetitions of a block, then d = n / r.

template<typename C>
inline bool operator!=(const PauliCycle<C> &other) const

Negation of operator==.

inline size_t hash() const

Hash of the unified representative, consistent with operator==.

inline bool prefers_signature() const

Whether rotation_signature() is cheaper than the unified form: the base has many sites for its number of words.

The same for all rotations of the base.

inline size_t rotation_signature() const

Hash of quantities that do not change under rotation: n_qubits, the numbers of X, Y and Z sites and of every pair of cyclically adjacent Paulis.

Equal cycles (bases that are rotations of each other) always share it; cycles with different signatures differ. O(n / 64), against O(n / 64 + w) for the unified form, so merging can skip unifying cycles whose signatures differ.

inline std::vector<uint64_t> get_hash() const

Invertible, coefficient-independent key of the cycle, consistent with operator==: key = string_key * 2^32 + n_qubits, where string_key is PauliString::get_hash() of the unified representative (so all rotations share it).

Little-endian 64-bit limbs.

inline PauliCycleSum<Coeff> operator*(const PauliCycle &other) const

Product C(P) C(Q) = sum_{b=0}^{n-1} C(P R^b Q), as a PauliCycleSum.

Products that are rotations of each other are merged.

inline PauliCycle operator*(double scale) const

The cycle with its coefficient multiplied by scale.

inline PauliCycle operator*(int scale) const

The cycle with its coefficient multiplied by scale.

inline PauliCycle diff(const std::string &name) const

Derivative of the coefficient by the symbol name (symbolic coefficients).

template<typename Values>
inline PauliCycle substitute(const Values &values) const

The cycle with values or polynomials substituted for symbols of its coefficient (see QubitHamiltonian::substitute).

inline PauliCycle operator*(std::complex<double> scale) const

The cycle with its coefficient multiplied by a complex scale.

inline PauliCycleSum<Coeff> commutator(const PauliCycle &other) const

[C(P), C(Q)] = sum_k C([P, R^k Q]) (Eq.

40). Only shifts k at which the supports overlap can contribute (Corollary 3); different shifts landing in the same rotation orbit are merged by PauliCycleSum.

template<typename C>
inline Coeff targeted_commutator(const PauliCycle &other, const PauliCycle<C> &target) const

Coefficient of a single target cycle in [this, other], i.e.

the sum of the coefficients of the cycles in the full commutator whose rotation orbit equals target. Uses the same support-difference shift pruning as commutator() but, instead of constructing and merging every output cycle, only accumulates the shifts whose product belongs to the target’s cyclic class (PauliEngine Features). Per shift, the operator of P R^k Q is the XOR of the words; its weight is checked first (rotations preserve it), then one phase pass gives the commutation parity and i^k, and only the remaining candidates are unified and compared with the target. No product string, rotated cycle or coefficient is formed for a shift that cannot contribute.

Public Members

int n_qubits

Fixed at construction: the cached unified form below depends on it, so it must not change afterwards (exposed read-only to Python).

PauliString<Coeff> Base

Base holds one representative of the rotation orbit: exactly the Pauli string the cycle was constructed from.

Construction does NOT unify. operator== and hash() compare unified forms on demand (via a cached representative), so two cycles from the same rotation orbit compare equal without an explicit unify() call; the latter just rewrites Base itself to the unified representative.

Public Static Functions

static inline PauliCycle from_hash(const std::vector<uint64_t> &limbs, Coeff coeff = pauliengine_detail::coeff_one<Coeff>())

Inverse of get_hash(): the cycle with that key and the given coefficient (its base is the unified representative).

template<typename Coeff>
class PauliCycleSum

A sum of PauliCycles, sum_i data[i].

Each cycle carries its own coefficient (in its base string); there is no separate global prefactor. The sum is kept merged: no two summands describe the same rotation orbit, and summands whose coefficient cancelled to zero are dropped. Coeff is std::complex<double> or symbolic::Expression.

Public Functions

PauliCycleSum() = default

The empty sum (zero operator).

explicit PauliCycleSum(std::vector<PauliCycle<Coeff>> cycles)

Merges summands from the same rotation orbit and drops zeros.

QubitHamiltonian<Coeff> to_qubit_hamiltonian() const

The sum expanded into a QubitHamiltonian (all rotations of all summands).

template<typename C>
Coeff contains(const PauliCycle<C> &cycle) const

Coefficient of the summand in the same rotation orbit as cycle (its own coefficient is ignored), or 0 if there is none.

PauliCycleSum commutator(const PauliCycleSum &other) const

Commutator [this, other] = sum_{i,j} [c_i, d_j], merged.

PauliCycleSum operator*(const PauliCycleSum &other) const

Product this * other = sum_{i,j} c_i d_j, merged.

PauliCycleSum operator*(double scale) const

Every coefficient multiplied by scale; scaling by 0 gives the empty sum.

inline PauliCycleSum operator*(int scale) const
inline PauliCycleSum operator*(std::complex<double> scale) const

Every coefficient multiplied by a complex scale; scaling by 0 gives the empty sum.

inline PauliCycleSum diff(const std::string &name) const

Derivative of every coefficient by the symbol name; summands whose derivative is zero are dropped (symbolic coefficients).

template<typename Values>
inline PauliCycleSum substitute(const Values &values) const

The sum with values or polynomials substituted for symbols; summands that become zero are dropped.

template<typename C>
Coeff targeted_commutator(const PauliCycleSum &other, const PauliCycle<C> &target) const

Coefficient of target’s orbit in [this, other], without building the full commutator.

void unify()

Rewrite every summand’s base to the unified representative of its orbit (in place).

Cheap: the unified forms are already cached by the merging in the constructor.

inline size_t size() const

Number of summands.

Public Members

std::vector<PauliCycle<Coeff>> data

The summands; no two lie in the same rotation orbit.

Pauli orbits

class PauliOrbit

Permutation-invariant Pauli orbit basis (arXiv:2604.16701, Section V).

B_{p,q,r} on n qubits is the normalized sum of all Pauli strings with p X-, q Y- and r Z-sites. This header provides the orbit data structure and its algebra; the PauliOrbitSum data structure lives in PauliOrbitSum.h.

Public Types

using Coeff = std::complex<double>

Public Functions

inline PauliOrbit(int n, int p_, int q_, int r_)

Orbit B_{p,q,r} on n qubits.

Throws std::invalid_argument unless 0 <= n <= kMaxQubits, p, q, r >= 0 and p + q + r <= n.

inline int identities() const

Number of identity sites, n - p - q - r.

inline bool is_valid() const

Always true for a constructed orbit (the constructor rejects anything else); kept as a public query.

inline double n_terms() const

Number of distinct Pauli strings in the orbit, n! / (p! q! r! (n-p-q-r)!) (Eq. 52).

inline bool operator==(const PauliOrbit &o) const

Equal iff n_qubits and the label (p, q, r) agree.

inline bool operator!=(const PauliOrbit &o) const

Negation of operator==.

inline size_t hash() const

Fixed-size hash, consistent with operator==.

inline QubitHamiltonian<Coeff> to_qubit_hamiltonian() const

The orbit expanded into an explicit QubitHamiltonian: (i / n_terms()) * sum of all Pauli strings with p X-, q Y- and r Z-sites.

The expansion has n_terms() strings; throws std::length_error above kMaxExpandedTerms.

inline uint64_t get_hash() const

Invertible key of the orbit label, consistent with operator==: n * 2^24 + p * 2^16 + q * 2^8 + r (all four are <= kMaxQubits < 256).

inline PauliOrbitSum<Coeff> commutator(const PauliOrbit &other) const

Commutator [this, other] in the orbit basis (Theorem 1 / Prop.

7), via the contingency tables of the two labels; never expands the strings.

inline PauliOrbitSum<Coeff> operator*(double scale) const

scale * this as a PauliOrbitSum, since an orbit has no coefficient of its own; scaling by 0 gives the empty sum.

inline PauliOrbitSum<Coeff> operator*(int scale) const

scale * this as a PauliOrbitSum.

inline PauliOrbitSum<Coeff> operator*(std::complex<double> scale) const

scale * this as a PauliOrbitSum, for a complex scale.

inline PauliOrbitSum<Coeff> operator*(const PauliOrbit &other) const

Product this * other in the orbit basis (complex coefficients).

inline Coeff targeted_commutator(const PauliOrbit &other, const PauliOrbit &target) const

The structure constant is real; it is returned as Coeff (complex) for a uniform interface with PauliCycle and the sum classes.

Public Members

int n_qubits

Fixed at construction (exposed read-only to Python): they define equality and the hash, so changing them would corrupt sets/dicts.

int p

number of X sites

int q

number of Y sites

int r

number of Z sites

Public Static Functions

static inline PauliOrbit from_hash(uint64_t key)

Inverse of get_hash(); the constructor validates the decoded label.

Public Static Attributes

static constexpr int kMaxQubits = 170

The structure constants are built from n!

and its divisors in float64, which overflows at 171! (> 1.8e308). Beyond this the coefficients would silently become inf/nan, so construction is refused instead.

static constexpr double kMaxExpandedTerms = 2.0e7

Largest number of Pauli strings to_qubit_hamiltonian() will materialize; beyond it the expansion would need several GB, so it throws instead.

template<typename Coeff>
class PauliOrbitSum

A scalar-weighted sum of Pauli orbits, sum_i coeffs[i] * data[i] (arXiv:2604.16701, Section V).

See PauliOrbit.h for the orbit data structure.

Coeff is std::complex<double> or symbolic::Expression. Commutators of the (anti-Hermitian) orbits have real structure constants, but ordinary products carry a full complex Pauli phase (see PauliOrbit::operator*); a symbolic sum multiplies these numeric constants into its symbolic coefficients.

Public Types

using Orbit = typename pauliengine_detail::orbit_type<Coeff>::type

PauliOrbit (see pauliengine_detail::orbit_type).

Public Functions

explicit PauliOrbitSum(int n)

Empty sum on n qubits. Throws std::invalid_argument unless 0 <= n <= PauliOrbit::kMaxQubits.

void add(const Orbit &orbit, Coeff coeff)

Adds coeff * orbit; merges into an existing term with the same label in O(1).

Coeff contains(const Orbit &orbit) const

Coefficient of orbit in the sum, or 0 if it does not occur.

void prune_zeros()

Drop terms whose coefficient is zero.

inline PauliOrbitSum diff(const std::string &name) const

Derivative of every coefficient by the symbol name; terms whose derivative is zero are dropped (symbolic coefficients).

template<typename Values>
inline PauliOrbitSum substitute(const Values &values) const

The sum with values or polynomials substituted for symbols; terms that become zero are dropped.

PauliOrbitSum commutator(const PauliOrbitSum &other) const

Commutator [this, other] = sum_{i,j} a_i b_j [B_i, B_j].

PauliOrbitSum operator*(const PauliOrbitSum &other) const

Product this * other = sum_{i,j} a_i b_j B_i B_j.

PauliOrbitSum operator*(double scale) const

Every coefficient multiplied by scale; scaling by 0 gives the empty sum.

inline PauliOrbitSum operator*(int scale) const
inline PauliOrbitSum operator*(std::complex<double> scale) const

Every coefficient multiplied by a complex scale; scaling by 0 gives the empty sum.

Coeff targeted_commutator(const PauliOrbitSum &other, const Orbit &target) const

Coefficient of target in [this, other], without building the full commutator.

QubitHamiltonian<Coeff> to_qubit_hamiltonian() const

The sum expanded into an explicit QubitHamiltonian.

inline size_t size() const

Number of terms.

Public Members

int n_qubits

Fixed at construction (exposed read-only to Python).

std::vector<Orbit> data

the orbits, each label at most once

std::vector<Coeff> coeffs

coefficients, parallel to data

Arithmetic between representations

include/pauliengine/Arithmetic.h overloads +, -, * and provides the free functions commutator(a, b) and inner_product(a, b) for every pair of representations.

Arithmetic between all operator representations.

PauliString, QubitHamiltonian, PauliCycle, PauliCycleSum, PauliOrbit and PauliOrbitSum all describe linear combinations of Pauli strings. This header overloads +, -, * and provides a free commutator(a, b) for every pair:

Functions

template<typename C>
QubitHamiltonian<C> as_hamiltonian(const PauliString<C> &ps)

A single Pauli string as a one-term Hamiltonian.

template<typename C>
const QubitHamiltonian<C> &as_hamiltonian(const QubitHamiltonian<C> &h)

A Hamiltonian is already one.

template<typename C>
QubitHamiltonian<C> as_hamiltonian(const PauliCycle<C> &c)

A cycle expanded into the sum of all its rotations.

template<typename C>
QubitHamiltonian<C> as_hamiltonian(const PauliCycleSum<C> &s)

A cycle sum expanded into the sum of all rotations of all summands.

inline QubitHamiltonian<std::complex<double>> as_hamiltonian(const PauliOrbit &o)

An orbit expanded into all of its Pauli strings (see PauliOrbit::to_qubit_hamiltonian).

template<typename C>
QubitHamiltonian<C> as_hamiltonian(const SparsePauliString<C> &ps)

A sparse Pauli string as a one-term Hamiltonian with bit vectors.

template<typename C>
QubitHamiltonian<C> as_hamiltonian(const SparsePauliStringSum<C> &h)

A sparse Hamiltonian with bit vectors.

template<typename C>
QubitHamiltonian<C> as_hamiltonian(const PauliOrbitSum<C> &s)

An orbit sum expanded into all Pauli strings of all its orbits.

template<typename A, typename B>
auto operator+(const A &a, const B &b)

Sum of two cycle-family operators, as a PauliCycleSum.

template<typename A, typename B>
auto operator-(const A &a, const B &b)

Difference of two cycle-family operators, as a PauliCycleSum.

template<typename A, typename B>
auto operator*(const A &a, const B &b)

Product of two cycle-family operators, as a PauliCycleSum.

template<typename A, typename B>
auto operator+(const A &a, const B &b)

Sum of two orbit-family operators, as a PauliOrbitSum.

template<typename A, typename B>
auto operator-(const A &a, const B &b)

Difference of two orbit-family operators, as a PauliOrbitSum.

template<typename A, typename B>
auto operator*(const A &a, const B &b)

Product of two orbit-family operators, as a PauliOrbitSum.

template<typename A, typename B>
auto operator+(const A &a, const B &b)

a + b with a sparse operand, as a SparsePauliStringSum.

template<typename A, typename B>
auto operator-(const A &a, const B &b)

a - b with a sparse operand, as a SparsePauliStringSum.

template<typename A, typename B>
auto operator*(const A &a, const B &b)

a * b with a sparse operand: a SparsePauliString for two strings, otherwise a SparsePauliStringSum.

template<typename A, typename B>
auto operator+(const A &a, const B &b)

a + b for operators of different kinds, as a QubitHamiltonian.

template<typename A, typename B>
auto operator-(const A &a, const B &b)

a - b for operators of different kinds, as a QubitHamiltonian.

template<typename A, typename B>
auto operator*(const A &a, const B &b)

a * b for operators of different kinds, as a QubitHamiltonian.

template<typename A, typename B>
auto commutator(const A &a, const B &b)

Commutator [a, b] = a b - b a for any two operators.

Same type with a commutator member: that member (PauliString stays a PauliString, cycles and orbits stay sums). Two Pauli strings of different coefficient types: a symbolic PauliString, like their product. Same family: the family’s sum commutator. Otherwise both sides are expanded and the result is a QubitHamiltonian.

template<typename A, typename B>
pauliengine_detail::inner_t<A, B> inner_product(const A &a, const B &b)

Normalized Hilbert-Schmidt inner product Tr(a^dagger b) / 2^n of any two operators (arXiv:2604.16701, Corollary 1 and Lemma 1).

Conjugate-linear in a; symbolic if either operand is symbolic, otherwise complex. Orbits are never expanded, so this also works for orbits too large to write out.

inline PauliString<std::complex<double>> as_weighted(const unweighted_pauliString &s)

An unweighted string as a weighted one, with coefficient 1.0 * i^phase.

inline QubitHamiltonian<std::complex<double>> as_weighted(const unweighted_QubitHamiltonian &h)

An unweighted set as a Hamiltonian, with coefficient 1.0 * i^k on every element.

inline unweighted_QubitHamiltonian as_unweighted_set(const unweighted_pauliString &s)

An unweighted string as a set with one element.

template<typename A, typename B>
auto operator+(const A &a, const B &b)
template<typename A, typename B>
auto operator+(const A &a, const B &b)
template<typename A, typename B>
auto operator-(const A &a, const B &b)
template<typename A, typename B>
auto operator-(const A &a, const B &b)
template<typename A, typename B>
auto operator*(const A &a, const B &b)
template<typename A, typename B>
auto operator*(const A &a, const B &b)
template<typename A, typename B>
auto commutator(const A &a, const B &b)
template<typename A, typename B>
auto commutator(const A &a, const B &b)
template<typename A, typename B>
auto inner_product(const A &a, const B &b)
template<typename A, typename B>
auto inner_product(const A &a, const B &b)
template<typename S>
QubitHamiltonian<std::complex<double>> operator*(const unweighted_QubitHamiltonian &h, S scale)

An unweighted set times a number, as a weighted Hamiltonian.

template<typename S>
QubitHamiltonian<std::complex<double>> operator*(S scale, const unweighted_QubitHamiltonian &h)
inline unweighted_QubitHamiltonian operator+(const unweighted_QubitHamiltonian &h, const unweighted_pauliString &s)

Union of a set and a string; the string is added unless its operator is present.

inline unweighted_QubitHamiltonian operator+(const unweighted_pauliString &s, const unweighted_QubitHamiltonian &h)
inline unweighted_QubitHamiltonian operator*(const unweighted_QubitHamiltonian &h, const unweighted_pauliString &s)

Every element times the string.

inline unweighted_QubitHamiltonian operator*(const unweighted_pauliString &s, const unweighted_QubitHamiltonian &h)
inline unweighted_QubitHamiltonian commutator(const unweighted_QubitHamiltonian &h, const unweighted_pauliString &s)

Commutators without their factor 2, see unweighted_QubitHamiltonian::commutator.

inline unweighted_QubitHamiltonian commutator(const unweighted_pauliString &s, const unweighted_QubitHamiltonian &h)
template<typename A, typename B>
std::complex<double> inner_product(const A &a, const B &b)

<a, b> for two unweighted operators, each counted with coefficient 1.0 * i^phase.

template<typename C>
PauliString<pauliengine::symbolic::Expression> scaled_by(const PauliString<C> &a, const pauliengine::symbolic::Expression &e)
template<typename C>
QubitHamiltonian<pauliengine::symbolic::Expression> scaled_by(const QubitHamiltonian<C> &h, const pauliengine::symbolic::Expression &e)
template<typename C>
PauliCycle<pauliengine::symbolic::Expression> scaled_by(const PauliCycle<C> &c, const pauliengine::symbolic::Expression &e)
template<typename C>
PauliCycleSum<pauliengine::symbolic::Expression> scaled_by(const PauliCycleSum<C> &s, const pauliengine::symbolic::Expression &e)
inline PauliOrbitSum<pauliengine::symbolic::Expression> scaled_by(const PauliOrbit &o, const pauliengine::symbolic::Expression &e)
template<typename C>
PauliOrbitSum<pauliengine::symbolic::Expression> scaled_by(const PauliOrbitSum<C> &s, const pauliengine::symbolic::Expression &e)
inline PauliString<pauliengine::symbolic::Expression> scaled_by(const unweighted_pauliString &s, const pauliengine::symbolic::Expression &e)
inline QubitHamiltonian<pauliengine::symbolic::Expression> scaled_by(const unweighted_QubitHamiltonian &h, const pauliengine::symbolic::Expression &e)
template<typename C>
SparsePauliString<pauliengine::symbolic::Expression> scaled_by(const SparsePauliString<C> &a, const pauliengine::symbolic::Expression &e)
template<typename C>
SparsePauliStringSum<pauliengine::symbolic::Expression> scaled_by(const SparsePauliStringSum<C> &h, const pauliengine::symbolic::Expression &e)

Pauli decomposition

Pauli decomposition: any 2^n x 2^n matrix as a QubitHamiltonian.

Implements the method of T. N. Georges et al., New J. Phys. (2025), https://doi.org/10.1088/1367-2630/adb44d: a XOR permutation of the matrix entries followed by a Walsh-Hadamard transform of every row. It needs O(4^n n) time and works on one copy of the matrix.

Conventions match QubitHamiltonian::to_matrix(): qubit 0 is the most significant bit of the row and column index, so pauli_decomposition(H.to_matrix(false)) reproduces H.

Functions

inline QubitHamiltonian<std::complex<double>> pauli_decomposition(std::vector<std::complex<double>> data, size_t dim, double threshold = 0.0)

Pauli decomposition of a 2^n x 2^n matrix given row-major in data (length dim * dim).

The input is not modified.

Terms with |coefficient| <= threshold are dropped; the default 0 drops only exact zeros. Throws std::invalid_argument unless dim is a power of two and data has dim * dim entries.

inline QubitHamiltonian<std::complex<double>> pauli_decomposition(const Matrix2D &matrix, double threshold = 0.0)

Pauli decomposition of a square matrix given as rows (the format returned by QubitHamiltonian::to_matrix).

The input is not modified.