Loading...
Searching...
No Matches
CentroidResponseFit.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Eigenvalues>
8#include <algorithm>
9#include <cmath>
10#include <limits>
11#include <stdexcept>
12
15 Eigen::MatrixXd response;
16 Eigen::MatrixXd conditionalCovariance;
17 Eigen::MatrixXd meanMomentResidual;
18 Eigen::MatrixXd scoreMomentResidual;
21};
22
23namespace centroid_detail {
24using Matrix = Eigen::MatrixXd;
25using Vector = Eigen::VectorXd;
26inline Eigen::SelfAdjointEigenSolver<Matrix> spectrum(const Matrix& value)
27{
28 if (!value.allFinite())
29 throw std::overflow_error(
30 "Centroid moment exceeds floating-point range");
31 const double tolerance = 128 * std::numeric_limits<double>::epsilon() *
32 value.rows() * value.stableNorm();
33 if ((value - value.transpose()).stableNorm() > tolerance)
34 throw std::invalid_argument("Symmetric centroid moments required");
35 Eigen::SelfAdjointEigenSolver<Matrix> eig(.5 * value +
36 .5 * value.transpose());
37 if (eig.info() != Eigen::Success)
38 throw std::runtime_error("Centroid moment eigendecomposition failed");
39 if (eig.eigenvalues().minCoeff() < -tolerance)
40 throw std::domain_error(
41 "Positive semidefinite centroid moments required");
42 return eig;
43}
44inline Matrix root(const Matrix& value)
45{
46 auto eig = spectrum(value);
47 const Vector values = eig.eigenvalues().cwiseMax(0.).cwiseSqrt();
48 return eig.eigenvectors() * values.asDiagonal() *
49 eig.eigenvectors().transpose();
50}
51inline double ratio(double numerator, double denominator)
52{
53 return denominator == 0. ? 0. : numerator / denominator;
54}
55inline double checked(long double value)
56{
57 const double result = static_cast<double>(value);
58 if (!std::isfinite(result))
59 throw std::overflow_error(
60 "Centroid response exceeds floating-point range");
61 return result;
62}
63} // namespace centroid_detail
64
71inline CentroidResponseFit fitCentroidResponse(const Eigen::MatrixXd& Caa,
72 const Eigen::MatrixXd& Cyy,
73 const Eigen::MatrixXd& Cya,
74 double beta)
75{
76 using namespace centroid_detail;
77 const auto n = Cyy.rows();
78 if (n <= 0 || Cyy.cols() != n || Caa.rows() != n || Caa.cols() != n ||
79 Cya.rows() != n || Cya.cols() != n || !Caa.allFinite() ||
80 !Cyy.allFinite() || !Cya.allFinite() || !std::isfinite(beta) ||
81 !(beta > 0.) || !std::isfinite(1. / beta) || !(1. / beta > 0.))
82 throw std::invalid_argument(
83 "Finite compatible centroid moments and positive beta required");
84 Vector scale(n);
85 for (Eigen::Index i = 0; i < n; ++i) {
86 if (Cyy(i, i) < 0.)
87 throw std::domain_error("Nonnegative centroid variance required");
88 if (Cyy(i, i) == 0. && ((Cyy.row(i).array() != 0.).any() ||
89 (Cyy.col(i).array() != 0.).any()))
90 throw std::domain_error(
91 "Zero centroid variance requires a zero covariance row");
92 scale[i] = Cyy(i, i) == 0. ? 1. : std::sqrt(Cyy(i, i));
93 }
94 Matrix aa(n, n), yy(n, n), ya(n, n);
95 for (Eigen::Index i = 0; i < n; ++i)
96 for (Eigen::Index j = 0; j < n; ++j) {
97 const long double si = scale[i], sj = scale[j];
98 aa(i, j) = checked(static_cast<long double>(Caa(i, j)) * si * sj);
99 yy(i, j) = checked(static_cast<long double>(Cyy(i, j)) / si / sj);
100 ya(i, j) = checked(static_cast<long double>(Cya(i, j)) * sj / si);
101 }
102 spectrum(aa);
103 const Matrix S = root(yy);
104 auto eig = spectrum(S * aa * S);
105 const double b = 1. / beta;
106 Vector weights(n);
107 for (Eigen::Index i = 0; i < n; ++i) {
108 const double lambda = std::max(0., eig.eigenvalues()[i]);
109 weights[i] = 1. / (.5 * b + .5 * std::hypot(b, 2 * std::sqrt(lambda)));
110 }
111 Matrix response = S * eig.eigenvectors() * weights.asDiagonal() *
112 eig.eigenvectors().transpose() * S;
113 response = (.5 * response + .5 * response.transpose()).eval();
114 const Matrix meanResidual = ya + response * aa;
115 const Matrix quadratic = response * aa * response;
116 const Matrix scoreResidual = quadratic + b * response - yy;
117 CentroidResponseFit result;
118 result.scaledMeanResidual =
119 ratio(meanResidual.stableNorm(),
120 std::max(ya.stableNorm(), (response * aa).stableNorm()));
121 result.relativeScoreResidual =
122 ratio(scoreResidual.stableNorm(),
123 std::max({yy.stableNorm(), quadratic.stableNorm(),
124 (b * response).stableNorm()}));
125 result.response.resize(n, n);
126 result.conditionalCovariance.resize(n, n);
127 result.meanMomentResidual.resize(n, n);
128 result.scoreMomentResidual.resize(n, n);
129 for (Eigen::Index i = 0; i < n; ++i)
130 for (Eigen::Index j = 0; j < n; ++j) {
131 const long double si = scale[i], sj = scale[j];
132 const long double entry =
133 static_cast<long double>(response(i, j)) * si * sj;
134 result.response(i, j) = checked(entry);
135 result.conditionalCovariance(i, j) = checked(entry / beta);
136 result.meanMomentResidual(i, j) =
137 checked(static_cast<long double>(meanResidual(i, j)) * si / sj);
138 result.scoreMomentResidual(i, j) = checked(
139 static_cast<long double>(scoreResidual(i, j)) * si * sj);
140 }
141 return result;
142}
143} // namespace gpr::quantum::bathresponse
CentroidResponseFit fitCentroidResponse(const Eigen::MatrixXd &Caa, const Eigen::MatrixXd &Cyy, const Eigen::MatrixXd &Cya, double beta)
Eigen::MatrixXd Matrix
Eigen::VectorXd Vector