Loading...
Searching...
No Matches
gpr::quantum::spectraldet Namespace Reference

Classes

struct  SoftMode
struct  Extrapolated
struct  Stochastic

Typedefs

using Matrix = Eigen::MatrixXd
using Vector = Eigen::VectorXd
using Sparse = Eigen::SparseMatrix<double>

Functions

double scaledBesselI0 (double z)
SoftMode softModePartition (double lambda, double volume, double kT)
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)

Typedef Documentation

◆ Matrix

using gpr::quantum::spectraldet::Matrix = Eigen::MatrixXd

Definition at line 44 of file SpectralDeterminant.h.

◆ Sparse

using gpr::quantum::spectraldet::Sparse = Eigen::SparseMatrix<double>

Definition at line 46 of file SpectralDeterminant.h.

◆ Vector

using gpr::quantum::spectraldet::Vector = Eigen::VectorXd

Definition at line 45 of file SpectralDeterminant.h.

Function Documentation

◆ scaledBesselI0()

double gpr::quantum::spectraldet::scaledBesselI0 ( double z)
inline

Definition at line 49 of file SpectralDeterminant.h.

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}
@ z
Definition Enums.h:28

◆ shiftedLogDetPlus()

Extrapolated gpr::quantum::spectraldet::shiftedLogDetPlus ( const Sparse & H,
const std::vector< double > & excluded,
double alphaMin,
double alphaMax,
int shifts = 8 )
inline

Definition at line 97 of file SpectralDeterminant.h.

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}
Eigen::SparseMatrix< double > Sparse

◆ softModePartition()

SoftMode gpr::quantum::spectraldet::softModePartition ( double lambda,
double volume,
double kT )
inline

Definition at line 77 of file SpectralDeterminant.h.

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}
STL namespace.

◆ stochasticLogDet()

Stochastic gpr::quantum::spectraldet::stochasticLogDet ( const std::function< Vector(const Vector &)> & apply,
Eigen::Index n,
int probes,
int steps,
unsigned seed = 1 )
inline

Definition at line 146 of file SpectralDeterminant.h.

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}
@ x
Definition Enums.h:28