Add trace_moment: faster tr(H^k) via identity-multiset enumeration (#80) - #114
Open
thedaemon-wizard wants to merge 1 commit into
Open
Add trace_moment: faster tr(H^k) via identity-multiset enumeration (#80)#114thedaemon-wizard wants to merge 1 commit into
thedaemon-wizard wants to merge 1 commit into
Conversation
…icolasloizeau#80) Compute tr(o^k) and tr(A^k B^l) by enumerating only the identity-product Pauli multisets and summing the ordering phase analytically (peel + mixed-radix DP), without building o^(k/2). Covers plain Operator, the 4-arg trace(A^k B^l), translation-symmetric OperatorTS (translation folding), and multithreading. Validated against trace_product and the dense operator power to machine precision; tests pass on Julia 1.6 and 1.10. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
trace_moment: fastertr(H^k)via identity-multiset enumerationCloses #80.
Summary
Adds
trace_moment, which computes momentstr(o^k)(andtr(A^k B^l)) by enumerating onlythe Pauli-string multisets whose product is the identity and summing each ordering phase
analytically — without ever constructing
o^(k/2), exactly as requested in #80.This is a complete implementation covering:
trace_moment(o::Operator, k; scale=0)— the issue's example (plainOperator).trace_moment(A::Operator, k, B::Operator, l; scale=0)— thetr(A^k B^l)generalisation.trace_moment(o::Operator{<:PauliStringTS}, k; scale=0)— translation-symmetric operators,exploiting translation invariance by anchoring the first factor to a stored representative.
trace_productis left unchanged;trace_momentis added as a new, non-breaking function.How it works
From
tr(H^k) = Σ c_{l₁}…c_{lₖ} tr(P_{l₁}…P_{lₖ}), a term contributes only when the XOR of thechosen Pauli strings is the identity — a property of the multiset, not the order.
only branches that can still reach the target (support-reachability via a precomputed suffix
union, plus a
ceil(active_sites / max_support)lower bound; terms sorted by lowest activesite). This removes the
k!factor of the naive ordered sum.Σ_orderings σ = K_ref(M)·D̃(M),where
K_refis the sign of one canonical ordering (O(k)) andD̃depends only on theanticommutation graph: terms commuting with all others peel off as binomial coefficients,
leaving a small "anticommuting core" handled by a mixed-radix multiplicity DP.
tr(A^k B^l). The cheaper side is tabulated by XOR targetR(summing its multisetcontributions); the other side is enumerated for each
R, and the two blocks combine througha
(-1)^{ycount(R)}cross phase:tr(A^k B^l)=scale·Σ_R fA(R)·fB(R)·(-1)^{ycount(R)}.OperatorTS, anchoring the first factor to a representative andmultiplying by the number of translations folds the search by
~1/|G|(the stored-vs-physicalcoefficient relation makes the stabilizer factors cancel exactly).
the partial sums reduced in a fixed order, so the result is independent of the thread count.
The
i^ycountClifford phases are carried inside the stored coefficients, so the bookkeeping isreal
±1, exactly as intrace_product.Performance
Issue example — plain
Operator, TFIM, N=20, k=1:14,scale=1:trace_producttrace_moment(1 thr)trace_moment(8 thr)tr(H^k · O), H=TFIM(N=20), O=X₁, k=1:14 (the regime the moment method wins most):trace_producttrace_moment(8 thr)Odd / high moments are dramatically faster (the existing path builds
H^{(k+1)/2}; the multisetmethod gets odd moments almost for free and never materialises a high power). Even moments are
matched/beaten once multithreading is enabled. The translation-symmetric method gives the same
wins for
OperatorTSinputs.Results agree with
trace_productand with the dense operator power (trace(o^k),trace(A^k*B^l)) to machine precision.What's included
src/moments.jl:trace_moment(plainOperator, 4-arg, andOperatorTS) + self-containedhelpers (pruned DFS, peel + mixed-radix-DP phase, XOR-table for the 4-arg, threaded seeds).
src/PauliStrings.jl: exporttrace_moment.docs/src/docstrings.md: added under "Power and moments".test/moments.jlandtest/moments_ts.jl: test sets wired intoruntests.jl.benchmark/benchmarks.jl:trace_momentvstrace_productentries (plain, 4-arg, 1D/2D TS).Test plan
trace_momentvstrace_productand vs the dense operator power (trace(o^k),trace(A^k*B^l)), to machine precision — plain & TS, 1D/2D/mixed periodicity,k = 0…8,complex / non-Hermitian coefficients, the
scalekeyword,k=0/l=0, error handling.serial == multithreadedfor every method.Relation to #91
PR #91 also implements the moment method for
Operator. This PR was developed independently andadditionally provides the translation-symmetric
OperatorTSmethod and multithreading.Happy to consolidate with #91 in whatever way is most useful to the maintainers.
Limitations
k ≈ 20, beyond which the moment value itself exceedsthe
Float64range.Theory & references that informed the implementation
Quantum many-body simulations with PauliStrings.jl (arXiv:2410.09654),
states
Tr H^k = Σ h_{i₁}…h_{iₖ} Tr(τ_{i₁}…τ_{iₖ})and that this is tractable because the operatorsare sparse in the Pauli basis and the expansion contains large commuting sets — the basis for the
whole
trace_momentapproach.linear and quadratic operations over GF(2), Phys. Rev. A 68, 042318 (2003) — the
(v,w)representation (already used by this package) and its quadratic-form commutation phase. The whole
phase computation reduces to XOR (
p₁ ⊻ p₂) and the paritycount_ones(p₁.v & p₂.w) (mod 2),so all per-multiset phases are exact integer
±1.D̃(M)is a
(-1)-weighted (Mahonian / Gaussianq-binomial atq=-1) enumeration; terms commutingwith everything factor out as ordinary binomials, the rest are summed by a small mixed-radix DP.
(Standard enumerative combinatorics — e.g. Stanley, Enumerative Combinatorics Vol. 1, §1.7,
Gaussian binomial coefficients.)
A Universal Operator Growth Hypothesis, Phys. Rev. X 9, 041017 (2019)
(arXiv:1812.08657); and the numerical moment/Lanczos study
arXiv:2203.00533: Lanczos coefficients (hence Krylov/operator
complexity) are obtained directly from the Hamiltonian moments.
Green's Functions using a Cumulant Expansion of the Lanczos Method, Quantum 8, 1383 (2024)
(arXiv:2309.09685).
High-Performance Library for Processing Pauli Strings (2026,
arXiv:2605.25974), which independently argues for a bit-packed
symplectic representation, a sorted-array layout instead of hash maps, and multi-threaded
merging — matching the choices here (alphabet sorted by lowest active site, bitwise XOR algebra,
thread-local search seeds with a fixed-order reduction).
AI assistance disclosure
Per the unitaryHACK Ethical AI policy: I used Claude (Anthropic) as a coding co-pilot — for
drafting, the algebra of the phase factorisation / translation folding, and test/benchmark
scaffolding. I reviewed and understand every part and can explain it; correctness was verified by
me locally against two independent oracles (
trace_productand the dense operator power) with thefull suite passing on Julia 1.6 and 1.10. No unverified output was submitted.