Loading...
Searching...
No Matches
SpectralDeterminant.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Dense>
8#include <Eigen/SparseCholesky>
9#include <Eigen/SparseCore>
10#include <cmath>
11#include <functional>
12#include <limits>
13#include <numbers>
14#include <random>
15#include <stdexcept>
16#include <vector>
17
18// Determinants of large curvature operators without forming or diagonalising
19// them, and the partition function of a soft mode that is not harmonic.
20//
21// softModePartition: a mode of curvature lambda that is periodic with mode
22// volume a (a lattice translation, a soft rotation of a defect) is modelled by
23// E(x) = A [1 - cos(2 pi x / a)], A = lambda a^2 / (4 pi^2). Its classical
24// configurational integral over one period is
25//
26// Z = a exp(-kappa lambda) I_0(kappa lambda), kappa = a^2 / (4 pi^2 k_B T),
27//
28// which tends to the harmonic sqrt(2 pi k_B T / lambda) when kappa lambda is
29// large and to the free a when it is small. A mode is harmonic when
30// T < a^2 lambda / (2 pi k_B), roughly. This is the classical model; a stiff
31// mode with hbar w >> k_B T is quantum and harmonic regardless.
32//
33// shiftedLogDetPlus: log det+ H, H with a few eigenvalues lambda_i at or near
34// zero that must be excluded, from log det(H + alpha I) at several shifts.
35// f(alpha) = log det(H + alpha I) - sum_i log(lambda_i + alpha) is analytic at
36// alpha = 0 with f(0) = log det+ H and f'(0) = tr(H_+^-1), so a low-order
37// polynomial through f on a grid of shifts extrapolates to it; the spread
38// between fit orders is the extrapolation error.
39//
40// stochasticLogDet: log det A = tr log A for a symmetric positive A applied
41// only through products A v, by stochastic Lanczos quadrature with Rademacher
42// probes. Returns the estimate and its standard error over probes.
44using Matrix = Eigen::MatrixXd;
45using Vector = Eigen::VectorXd;
46using Sparse = Eigen::SparseMatrix<double>;
47
48// exp(-z) I_0(z) for z >= 0, finite for any z.
49inline double scaledBesselI0(double z)
50{
51 if (!(z >= 0)) throw std::invalid_argument("Nonnegative argument required");
52 // The library loses digits at large z; the series is converged below
53 // rounding past 50.
54 if (z < 50) return std::exp(-z) * std::cyl_bessel_i(0., z);
55 // Asymptotic series: e^-z I_0(z) = (2 pi z)^-1/2 sum_k ((2k-1)!!)^2 / (k!
56 // (8z)^k).
57 double term = 1, sum = 1;
58 // The series is asymptotic: stop at convergence or where the terms turn
59 // upward.
60 for (int k = 1; k < 200; ++k) {
61 const double next = term * (2 * k - 1) * (2 * k - 1) / (8. * z * k);
62 if (next >= term) break;
63 term = next;
64 sum += term;
65 if (term < 1e-17 * sum) break;
66 }
67 return sum / std::sqrt(2 * std::numbers::pi * z);
68}
69
70struct SoftMode {
71 double partition = 0; // Z over one period
72 double harmonic = 0; // sqrt(2 pi kT / lambda)
73 double ratio = 0; // Z / harmonic
74 bool harmonicRegime = false;
75};
76
77inline SoftMode softModePartition(double lambda, double volume, double kT)
78{
79 if (!(lambda >= 0) || !(volume > 0) || !(kT > 0))
80 throw std::invalid_argument("lambda >= 0, a > 0 and kT > 0 required");
81 const double z = volume * volume * lambda /
82 (4 * std::numbers::pi * std::numbers::pi * kT);
83 SoftMode m;
84 m.partition = volume * scaledBesselI0(z);
85 m.harmonic = lambda > 0 ? std::sqrt(2 * std::numbers::pi * kT / lambda)
86 : std::numeric_limits<double>::infinity();
87 m.ratio = lambda > 0 ? m.partition / m.harmonic : 0;
88 m.harmonicRegime = kT < volume * volume * lambda / (2 * std::numbers::pi);
89 return m;
90}
91
93 double value = 0, error = 0;
94};
95
96// excluded holds the eigenvalues to remove (from a partial diagonalisation).
98 const std::vector<double>& excluded,
99 double alphaMin, double alphaMax,
100 int shifts = 8)
101{
102 if (H.rows() != H.cols() || !(alphaMin > 0) || !(alphaMax > alphaMin) ||
103 shifts < 4)
104 throw std::invalid_argument(
105 "Square H, 0 < alphaMin < alphaMax and at least four shifts "
106 "required");
107 Sparse identity(H.rows(), H.cols());
108 identity.setIdentity();
109 Vector alpha(shifts), f(shifts);
110 for (int k = 0; k < shifts; ++k) {
111 alpha(k) = alphaMin * std::pow(alphaMax / alphaMin,
112 static_cast<double>(k) / (shifts - 1));
113 Eigen::SimplicialLDLT<Sparse> ldlt(Sparse(H + alpha(k) * identity));
114 if (ldlt.info() != Eigen::Success)
115 throw std::runtime_error("Shifted factorisation failed");
116 double logdet = 0;
117 for (Eigen::Index i = 0; i < ldlt.vectorD().size(); ++i) {
118 const double d = ldlt.vectorD()(i);
119 if (!(d > 0))
120 throw std::domain_error("The shift must make H positive");
121 logdet += std::log(d);
122 }
123 for (double l: excluded) {
124 if (!(l + alpha(k) > 0))
125 throw std::domain_error(
126 "An excluded eigenvalue is below minus the shift");
127 logdet -= std::log(l + alpha(k));
128 }
129 f(k) = logdet;
130 }
131 const auto fit = [&](int order) {
132 Matrix V(shifts, order + 1);
133 for (int k = 0; k < shifts; ++k)
134 for (int p = 0; p <= order; ++p)
135 V(k, p) = std::pow(alpha(k) / alphaMax, p);
136 return V.colPivHouseholderQr().solve(f)(0);
137 };
138 const double one = fit(1), two = fit(2), three = fit(3);
139 return {two, std::max(std::abs(two - one), std::abs(three - two))};
140}
141
143 double value = 0, standardError = 0;
144};
145
147 const std::function<Vector(const Vector&)>& apply, Eigen::Index n,
148 int probes, int steps, unsigned seed = 1)
149{
150 if (n < 1 || probes < 2 || steps < 1)
151 throw std::invalid_argument(
152 "Size, at least two probes and steps required");
153 std::mt19937_64 rng(seed);
154 std::bernoulli_distribution coin(.5);
155 std::vector<double> samples;
156 for (int p = 0; p < probes; ++p) {
157 Vector v(n);
158 for (Eigen::Index i = 0; i < n; ++i)
159 v(i) = coin(rng) ? 1. : -1.;
160 const double norm2 = v.squaredNorm();
161 const int m = static_cast<int>(std::min<Eigen::Index>(steps, n));
162 Matrix Q(n, m);
163 Vector a = Vector::Zero(m), b = Vector::Zero(m);
164 Q.col(0) = v / std::sqrt(norm2);
165 int used = m;
166 for (int j = 0; j < m; ++j) {
167 Vector w = apply(Q.col(j));
168 a(j) = Q.col(j).dot(w);
169 w -= a(j) * Q.col(j);
170 if (j > 0) w -= b(j - 1) * Q.col(j - 1);
171 for (int pass = 0; pass < 2; ++pass)
172 w -= Q.leftCols(j + 1) * (Q.leftCols(j + 1).transpose() * w);
173 if (j + 1 == m) break;
174 b(j) = w.norm();
175 if (!(b(j) > 1e-12 * std::abs(a(j)))) {
176 used = j + 1;
177 break;
178 }
179 Q.col(j + 1) = w / b(j);
180 }
181 Matrix T = Matrix::Zero(used, used);
182 for (int j = 0; j < used; ++j) {
183 T(j, j) = a(j);
184 if (j + 1 < used) T(j, j + 1) = T(j + 1, j) = b(j);
185 }
186 Eigen::SelfAdjointEigenSolver<Matrix> s(T);
187 if (s.info() != Eigen::Success || !(s.eigenvalues().minCoeff() > 0))
188 throw std::domain_error(
189 "Stochastic log-det needs a positive operator");
190 double quad = 0;
191 for (int j = 0; j < used; ++j)
192 quad += s.eigenvectors()(0, j) * s.eigenvectors()(0, j) *
193 std::log(s.eigenvalues()(j));
194 samples.push_back(norm2 * quad);
195 }
196 double mean = 0;
197 for (double x: samples)
198 mean += x;
199 mean /= probes;
200 double var = 0;
201 for (double x: samples)
202 var += (x - mean) * (x - mean);
203 var /= (probes - 1);
204 return {mean, std::sqrt(var / probes)};
205}
206} // namespace gpr::quantum::spectraldet
@ z
Definition Enums.h:28
@ x
Definition Enums.h:28
SoftMode softModePartition(double lambda, double volume, double kT)
Eigen::SparseMatrix< double > Sparse
Extrapolated shiftedLogDetPlus(const Sparse &H, const std::vector< double > &excluded, double alphaMin, double alphaMax, int shifts=8)
Stochastic stochasticLogDet(const std::function< Vector(const Vector &)> &apply, Eigen::Index n, int probes, int steps, unsigned seed=1)