Loading...
Searching...
No Matches
GaussianPolynomial.cpp
Go to the documentation of this file.
2
3#include <Eigen/Eigenvalues>
4#include <algorithm>
5#include <cfenv>
6#include <cmath>
7#include <limits>
8#include <map>
9#include <stdexcept>
10
11namespace gpr::quantum {
12namespace {
13using Real = long double;
14using Powers = std::vector<unsigned>;
15constexpr Real epsilon = std::numeric_limits<Real>::epsilon();
16
17Real upward(Real value)
18{
19 if (!std::isfinite(value))
20 throw std::overflow_error(
21 "Gaussian polynomial arithmetic is not finite");
22 return std::nextafter(value, std::numeric_limits<Real>::infinity());
23}
24
25Real addUp(Real a, Real b)
26{
27 return upward(a + b);
28}
29Real multiplyUp(Real a, Real b)
30{
31 return upward(a * b);
32}
33
34Real rounding(Real value)
35{
36 return addUp(multiplyUp(epsilon / (1 - epsilon), std::abs(value)),
37 std::numeric_limits<Real>::denorm_min());
38}
39
41{
42 const Real value = a.value + b.value;
43 return {value,
44 addUp(addUp(a.absoluteRoundoffBound, b.absoluteRoundoffBound),
45 rounding(value))};
46}
47
49{
50 const Real value = a.value * b.value;
51 Real error = addUp(multiplyUp(std::abs(a.value), b.absoluteRoundoffBound),
52 multiplyUp(std::abs(b.value), a.absoluteRoundoffBound));
53 error = addUp(error,
54 multiplyUp(a.absoluteRoundoffBound, b.absoluteRoundoffBound));
55 return {value, addUp(error, rounding(value))};
56}
57
58class Moments {
59 const GaussianLaw& law;
60 std::map<Powers, GaussianMoment> cache;
61
62public:
63 explicit Moments(const GaussianLaw& input) : law(input) { }
64
65 GaussianMoment get(Powers powers)
66 {
67 if (auto it = cache.find(powers); it != cache.end()) return it->second;
68 std::size_t first = 0;
69 while (first < powers.size() && !powers[first])
70 ++first;
71 if (first == powers.size()) return {1, 0};
72 --powers[first];
73 GaussianMoment value =
74 multiply({static_cast<Real>(law.mean[first]), 0}, get(powers));
75 for (std::size_t j = 0; j < powers.size(); ++j) {
76 if (!powers[j]) continue;
77 const auto count = powers[j];
78 --powers[j];
79 value = add(
80 value,
81 multiply(
82 multiply({static_cast<Real>(count), 0},
83 {static_cast<Real>(law.covariance(first, j)), 0}),
84 get(powers)));
85 ++powers[j];
86 }
87 ++powers[first];
88 cache.emplace(std::move(powers), value);
89 return value;
90 }
91};
92
93void validateLaw(const GaussianLaw& law, GaussianPolynomialMoments& result)
94{
95 const auto dimension = law.mean.size();
96 if (!dimension || law.covariance.rows() != dimension ||
97 law.covariance.cols() != dimension || !law.mean.allFinite() ||
98 !law.covariance.allFinite())
99 throw std::invalid_argument(
100 "Expected a finite Gaussian mean and matching covariance");
101 const double scale = law.covariance.cwiseAbs().maxCoeff();
102 result.covarianceArithmeticTolerance =
103 64 * std::numeric_limits<double>::epsilon() * dimension * scale;
104 result.covarianceSymmetryDefect =
105 (law.covariance - law.covariance.transpose()).cwiseAbs().maxCoeff();
106 if (!std::isfinite(result.covarianceArithmeticTolerance) ||
107 result.covarianceSymmetryDefect > result.covarianceArithmeticTolerance)
108 throw std::invalid_argument(
109 "Gaussian covariance is not symmetric to arithmetic precision");
110 for (Eigen::Index i = 0; i < dimension; ++i)
111 if (law.covariance(i, i) < 0)
112 throw std::invalid_argument("Negative Gaussian variance");
113 const Eigen::MatrixXd symmetric =
114 (.5 * law.covariance + .5 * law.covariance.transpose()).eval();
115 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eig(symmetric);
116 if (eig.info() != Eigen::Success || !eig.eigenvalues().allFinite())
117 throw std::runtime_error("Gaussian covariance eigensolve failed");
118 const double residual =
119 (symmetric * eig.eigenvectors() -
120 eig.eigenvectors() * eig.eigenvalues().asDiagonal())
121 .norm();
122 const double budget = residual + result.covarianceArithmeticTolerance;
123 if (!std::isfinite(budget))
124 throw std::overflow_error("Gaussian validation residual overflow");
125 if (eig.eigenvalues()[0] < -budget)
126 throw std::invalid_argument(
127 "Gaussian covariance is not positive semidefinite");
128 result.covarianceHasArithmeticUnresolvedEigenvalues =
129 scale != 0 && eig.eigenvalues()[0] <= budget;
130}
131} // namespace
132
134 long double probabilityBound)
135{
136 if (!std::isfinite(probabilityBound) || probabilityBound < 0 ||
137 probabilityBound > 1)
138 throw std::invalid_argument(
139 "Expected an event probability bound between zero and one");
140 if (!std::isfinite(moments.second.value) ||
141 !std::isfinite(moments.second.absoluteRoundoffBound) ||
142 moments.second.absoluteRoundoffBound < 0)
143 throw std::invalid_argument(
144 "Invalid Gaussian second moment or error allowance");
145 const Real upper =
146 addUp(moments.second.value, moments.second.absoluteRoundoffBound);
147 if (upper < 0)
148 throw std::runtime_error("Negative Gaussian polynomial second moment");
149 if (probabilityBound == 0) return 0;
150 return upward(std::sqrt(multiplyUp(upper, probabilityBound)));
151}
152
154 const GaussianLaw& law, const SparsePolynomial& polynomial,
155 std::optional<long double> eventProbabilityBound)
156{
157 if (std::fegetround() != FE_TONEAREST)
158 throw std::runtime_error(
159 "Gaussian polynomial arithmetic requires round-to-nearest");
161 validateLaw(law, result);
162 const auto dimension = static_cast<std::size_t>(law.mean.size());
163 for (const auto& term: polynomial) {
164 if (!std::isfinite(term.coefficient) || term.powers.size() != dimension)
165 throw std::invalid_argument(
166 "Polynomial coefficient or multi-index dimension is invalid");
167 for (unsigned power: term.powers)
168 if (power > std::numeric_limits<unsigned>::max() / 2)
169 throw std::overflow_error(
170 "Polynomial square multi-index exceeds its integer range");
171 }
172 Moments moments(law);
173 for (const auto& term: polynomial) {
174 if (term.coefficient == 0) continue;
175 result.first = add(result.first, multiply({term.coefficient, 0},
176 moments.get(term.powers)));
177 for (const auto& other: polynomial) {
178 if (other.coefficient == 0) continue;
179 Powers combined = term.powers;
180 for (std::size_t i = 0; i < dimension; ++i)
181 combined[i] += other.powers[i];
182 result.second =
183 add(result.second, multiply(multiply({term.coefficient, 0},
184 {other.coefficient, 0}),
185 moments.get(std::move(combined))));
186 }
187 }
188 if (addUp(result.second.value, result.second.absoluteRoundoffBound) < 0)
189 throw std::runtime_error("Negative Gaussian polynomial second moment");
192 if (eventProbabilityBound)
194 weightedEventBound(result, *eventProbabilityBound);
195 return result;
196}
197
198} // namespace gpr::quantum
double addUp(double a, double b)
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
GaussianPolynomialMoments evaluateGaussianPolynomial(const GaussianLaw &law, const SparsePolynomial &polynomial, std::optional< long double > eventProbabilityBound)
long double weightedEventBound(const GaussianPolynomialMoments &moments, long double probabilityBound)
std::vector< PolynomialTerm > SparsePolynomial
std::optional< long double > weightedEventAbsoluteBound