Loading...
Searching...
No Matches
GaussianTwistedTrace.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
3
4#include <Eigen/Eigenvalues>
5#include <algorithm>
6#include <cmath>
7#include <limits>
8
10namespace {
11double magnitude(const Matrix &matrix)
12{
13 return matrix.size() ? matrix.cwiseAbs().maxCoeff() : 0;
14}
15} // namespace
16
18{
19 if (!logAbsNormalizedDet || !std::isfinite(*logAbsNormalizedDet) ||
20 inertia.negative || inertia.zero || unresolvedSchurEigenvalues ||
21 inertia.positive != static_cast<std::size_t>(dimension))
22 throw std::domain_error(
23 "Twisted Gaussian trace is not resolved and positive");
24 return -.5 * *logAbsNormalizedDet;
25}
26
27TwistedTraceResult traceDeterminant(const std::vector<Segment> &segments,
28 const Matrix &twist)
29{
30 const Factorization open(segments);
31 const Eigen::Index d = open.blockDimension();
32 constexpr double eps = std::numeric_limits<double>::epsilon();
33 if (twist.rows() != d || twist.cols() != d || !twist.allFinite())
34 throw std::invalid_argument(
35 "Twist must be a finite matrix matching the segment dimension");
36 const Matrix gram = twist.transpose() * twist;
37 if (!gram.allFinite() || magnitude(gram - Matrix::Identity(d, d)) >
38 64 * eps * d * std::max(1.0, magnitude(gram)))
39 throw std::invalid_argument(
40 "Twist must be orthogonal in the mass-weighted metric");
41 if (segments.size() >
42 static_cast<std::size_t>(std::numeric_limits<Eigen::Index>::max() / d))
43 throw std::overflow_error(
44 "Twisted trace dimension exceeds index range");
45
46 const Matrix endpoint =
47 segments.front().A +
48 twist.transpose() * segments.back().rightEndpoint() * twist;
49 Matrix correction;
50 if (segments.size() == 1) {
51 correction = segments.front().L * twist +
52 twist.transpose() * segments.front().L.transpose();
53 } else {
54 Matrix coupling = Matrix::Zero(open.interiorDimension(), d);
55 coupling.topRows(d) -= segments.front().L.transpose();
56 coupling.bottomRows(d) -= segments.back().L * twist;
57 Matrix response(coupling.rows(), d);
58 for (Eigen::Index col = 0; col < d; ++col)
59 response.col(col) = open.solve(Vector(coupling.col(col)));
60 correction = coupling.transpose() * response;
61 }
62 Matrix schur = endpoint - correction;
63 const double scale = magnitude(endpoint) + magnitude(correction);
64 const double tolerance = 64 * eps * d * scale;
65 if (!schur.allFinite() || !std::isfinite(tolerance) ||
66 magnitude(schur - schur.transpose()) > tolerance)
67 throw std::runtime_error(
68 "Twisted Schur matrix exceeds arithmetic roundoff");
69 schur = (.5 * schur + .5 * schur.transpose()).eval();
70 Eigen::SelfAdjointEigenSolver<Matrix> eig(schur);
71 if (eig.info() != Eigen::Success || !eig.eigenvalues().allFinite())
72 throw std::runtime_error("Twisted Schur eigendecomposition failed");
73
74 TwistedTraceResult result;
75 result.inertia = open.inertia();
76 result.dimension = static_cast<Eigen::Index>(segments.size()) * d;
77 result.duration = open.duration();
78 double logDet = open.logAbsD();
79 for (double value: eig.eigenvalues()) {
80 if (std::abs(value) <= tolerance) {
82 ++result.inertia.zero;
83 } else {
84 if (value > 0)
85 ++result.inertia.positive;
86 else
87 ++result.inertia.negative;
88 logDet += std::log(std::abs(value));
89 }
90 }
91 if (!result.unresolvedSchurEigenvalues) {
92 if (!std::isfinite(logDet))
93 throw std::overflow_error(
94 "Nonfinite normalized twisted determinant");
95 result.logAbsNormalizedDet = logDet;
96 }
97 return result;
98}
99} // namespace gpr::quantum::gaussian
Vector solve(const Vector &rhs) const
TwistedTraceResult traceDeterminant(const std::vector< Segment > &segments, const Matrix &twist)