Loading...
Searching...
No Matches
HarmonicBathResponse.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Cholesky>
8#include <Eigen/Eigenvalues>
9#include <Eigen/SVD>
10#include <algorithm>
11#include <cmath>
12#include <limits>
13#include <stdexcept>
14#include <utility>
16using Matrix = Eigen::MatrixXd;
17using Vector = Eigen::VectorXd;
18struct Model {
20 // With state (u,p), d(state)/dt = -gleDrift()*state + gleInput()*a.
21 // This Hamiltonian bath has no thermostat or fitted dissipation.
23 {
24 const auto n = stiffness.rows();
25 Matrix drift = Matrix::Zero(2 * n, 2 * n);
26 drift.topRightCorner(n, n) = -Matrix::Identity(n, n);
27 drift.bottomLeftCorner(n, n) = stiffness;
28 return drift;
29 }
31 {
32 Matrix input = Matrix::Zero(2 * stiffness.rows(), coupling.cols());
33 input.bottomRows(stiffness.rows()) = coupling;
34 return input;
35 }
37 {
38 Matrix output = Matrix::Zero(coupling.cols(), 2 * stiffness.rows());
39 output.leftCols(stiffness.rows()) = coupling.transpose();
40 return output;
41 }
42 Matrix response(double shift) const
43 {
44 Matrix shifted = stiffness;
45 shifted.diagonal().array() += shift;
46 Eigen::LLT<Matrix> factor(shifted);
47 if (factor.info() != Eigen::Success)
48 throw std::domain_error("Positive shifted bath stiffness required");
49 const Matrix result = coupling.transpose() * factor.solve(coupling);
50 if (!result.allFinite())
51 throw std::overflow_error("Bath response exceeds range");
52 return .5 * (result + result.transpose());
53 }
54};
61// A is self-adjoint and A >= alpha I. Q is orthonormal, AQ contains
62// native operator actions, and range(B) lies in range(Q). The caller
63// supplies the global lower bound; projected Ritz values cannot prove it.
64inline Bounds boundResponse(const Matrix& B, const Matrix& Q, const Matrix& AQ,
65 double alpha, double shift)
66{
67 const auto n = B.rows(), r = B.cols(), m = Q.cols();
68 if (n <= 0 || r <= 0 || m <= 0 || m > n || Q.rows() != n ||
69 AQ.rows() != n || AQ.cols() != m || !B.allFinite() || !Q.allFinite() ||
70 !AQ.allFinite() || !std::isfinite(alpha) || alpha < 0 ||
71 !std::isfinite(shift) || !(shift + alpha > 0))
72 throw std::invalid_argument(
73 "Finite compatible bath data and positive spectral shift required");
74 const double epsilon = 256 * std::numeric_limits<double>::epsilon() * n;
75 if ((Q.transpose() * Q - Matrix::Identity(m, m)).norm() > epsilon)
76 throw std::invalid_argument("Orthonormal bath response basis required");
77 const Matrix G = Q.transpose() * B;
78 const Matrix missing = B - Q * G;
79 for (Eigen::Index j = 0; j < r; ++j)
80 if (missing.col(j).stableNorm() > epsilon * B.col(j).stableNorm())
81 throw std::invalid_argument(
82 "Bath coupling lies outside the supplied basis");
83 Matrix T = Q.transpose() * AQ;
84 const double operatorScale = AQ.stableNorm();
85 if ((T - T.transpose()).norm() > epsilon * operatorScale)
86 throw std::invalid_argument(
87 "Self-adjoint bath operator actions required");
88 T = (.5 * T + .5 * T.transpose()).eval();
89 Matrix positive = T;
90 positive.diagonal().array() -= alpha;
91 Eigen::SelfAdjointEigenSolver<Matrix> spectrum(positive);
92 const double tolerance = epsilon * std::max(operatorScale, std::abs(alpha));
93 if (spectrum.info() != Eigen::Success ||
94 spectrum.eigenvalues().minCoeff() < -tolerance)
95 throw std::domain_error(
96 "Bath actions violate the declared spectral lower bound");
97 Bounds result;
98 result.lowerModel = {T, G};
99 result.lower = result.lowerModel.response(shift);
100 if (m == n) {
101 result.upperModel = result.lowerModel;
102 result.upper = result.lower;
103 result.residualDirections = Matrix(n, 0);
104 result.residualSingularValues = Vector(0);
105 return result;
106 }
107 Matrix residual = AQ - Q * T;
108 for (int pass = 0; pass < 2; ++pass)
109 residual.noalias() -= Q * (Q.transpose() * residual).eval();
110 Eigen::JacobiSVD<Matrix> factor(residual,
111 Eigen::ComputeThinU | Eigen::ComputeThinV);
112 if (factor.info() != Eigen::Success)
113 throw std::runtime_error("Bath residual factorization failed");
114 result.residualDirections = factor.matrixU();
115 result.residualSingularValues = factor.singularValues();
116 const Matrix F =
117 factor.singularValues().asDiagonal() * factor.matrixV().transpose();
118 Vector inverse = Vector::Zero(m);
119 for (Eigen::Index j = 0; j < m; ++j) {
120 if (spectrum.eigenvalues()[j] > tolerance)
121 inverse[j] = 1 / spectrum.eigenvalues()[j];
122 else if ((F * spectrum.eigenvectors().col(j)).stableNorm() >
123 epsilon * operatorScale)
124 throw std::domain_error(
125 "Bath residual leaves the supported stiffness range");
126 }
127 const Matrix pseudoinverse = spectrum.eigenvectors() *
128 inverse.asDiagonal() *
129 spectrum.eigenvectors().transpose();
130 Matrix extended = Matrix::Zero(2 * m, 2 * m);
131 extended.topLeftCorner(m, m) = T;
132 extended.bottomLeftCorner(m, m) = F;
133 extended.topRightCorner(m, m) = F.transpose();
134 extended.bottomRightCorner(m, m) = F * pseudoinverse * F.transpose();
135 extended.bottomRightCorner(m, m).diagonal().array() += alpha;
136 Matrix coupling = Matrix::Zero(2 * m, r);
137 coupling.topRows(m) = G;
138 result.upperModel = {std::move(extended), std::move(coupling)};
139 result.upper = result.upperModel.response(shift);
140 return result;
141}
142} // namespace gpr::quantum::bathresponse
Bounds boundResponse(const Matrix &B, const Matrix &Q, const Matrix &AQ, double alpha, double shift)
Matrix response(double shift) const