Loading...
Searching...
No Matches
AmplitudeContinuation.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
3
4#include <algorithm>
5#include <cmath>
6#include <limits>
7#include <numeric>
8#include <stdexcept>
9#include <utility>
10
11namespace gpr::quantum {
12namespace {
13AmplitudeContinuationResult continueAmplitude(
14 const std::vector<Eigen::VectorXd>& initial, const MassMetric& metric,
15 const PathPotential& potential, const AffinePathBoundary& boundary,
16 const PathConstraints& constraints,
17 const AmplitudeContinuationOptions& options,
18 std::optional<Eigen::Index> phaseColumn)
19{
20 const Eigen::Index column = options.amplitudeColumn == -1
21 ? constraints.target.size() - 1
22 : options.amplitudeColumn;
23 if (column < 0 || column >= constraints.target.size() ||
24 options.maximumSamples < 2 || !(options.lowerFactor > 0) ||
25 !(options.upperFactor > options.lowerFactor) ||
26 !std::isfinite(options.lowerFactor) ||
27 !std::isfinite(options.upperFactor) ||
28 !(constraints.target[column] > 0) ||
29 !std::isfinite(constraints.target[column]))
30 throw std::invalid_argument(
31 "Positive amplitude scale, ordered factors and at least two "
32 "samples required");
33 if (phaseColumn &&
34 (*phaseColumn < 0 || *phaseColumn >= constraints.target.size() ||
35 *phaseColumn == column))
36 throw std::invalid_argument(
37 "A distinct retained phase column is required");
39 result.samples.reserve(options.maximumSamples);
40 auto current = constraints;
41 auto warm = initial;
42 auto pathOptions = options.path;
43 pathOptions.requireFullStationarity = false;
44 auto sample = [&](double coordinate) {
45 if (!(coordinate > 0) || !std::isfinite(coordinate))
46 throw std::overflow_error(
47 "Amplitude coordinate is not finite and positive");
48 current.target[column] = coordinate;
49 auto path = optimizePotentialPath(warm, metric, potential, boundary,
50 current, pathOptions);
51 warm = path.path;
52 AmplitudeSample value;
53 value.coordinate = coordinate;
54 if (path.conditionalStationarityQualified &&
55 path.normalForces.size() > column)
56 value.forceOverCoordinate = path.normalForces[column] / coordinate;
57 if (phaseColumn && path.conditionalStationarityQualified &&
58 path.fullGradient.size() == initial.size()) {
59 double coefficient = 0;
60 for (std::size_t j = 0; j < initial.size(); ++j)
61 coefficient += current.normals[j]
62 .col(*phaseColumn)
63 .dot(path.fullGradient[j]);
64 double maximum = 0;
65 for (std::size_t j = 0; j < initial.size(); ++j)
66 maximum = std::max(
67 maximum,
68 (path.fullGradient[j] -
69 coefficient * current.normals[j].col(*phaseColumn))
70 .norm());
71 value.phasePlaneForce = maximum;
72 }
73 value.path = std::move(path);
74 result.samples.push_back(std::move(value));
75 const auto index = result.samples.size() - 1;
76 auto merit = [&](std::size_t i) {
77 return phaseColumn ? result.samples[i].phasePlaneForce.value_or(
78 std::numeric_limits<double>::infinity())
79 : result.samples[i].path.fullForce;
80 };
81 if (result.samples[index].path.fullGradient.size() == initial.size() &&
82 (result.samples[result.selected].path.fullGradient.size() !=
83 initial.size() ||
84 merit(index) < merit(result.selected)))
85 result.selected = index;
86 if (!result.samples[index].forceOverCoordinate ||
87 !std::isfinite(*result.samples[index].forceOverCoordinate))
88 result.failure =
89 "Conditional amplitude path misses its stationarity "
90 "criterion: " +
91 result.samples[index].path.failure;
92 return index;
93 };
94 auto qualified = [&](std::size_t index) {
95 const auto& value = result.samples[index];
96 return value.path.conditionalStationarityQualified &&
97 (phaseColumn
98 ? value.phasePlaneForce &&
99 *value.phasePlaneForce <= options.path.forceTolerance
100 : value.path.fullStationarityQualified);
101 };
102 auto accept = [&](std::size_t index) {
103 result.selected = index;
104 result.converged =
105 result.samples[index].path.fullStationarityQualified &&
106 result.samples[index].path.conditionalStationarityQualified;
107 result.phasePlaneStationarityQualified = phaseColumn.has_value();
108 };
109 const double scale = constraints.target[column];
110 auto lower = sample(scale * options.lowerFactor);
111 if (!result.failure.empty()) return result;
112 if (qualified(lower)) {
113 accept(lower);
114 return result;
115 }
116 auto upper = sample(scale * options.upperFactor);
117 if (!result.failure.empty()) return result;
118 if (qualified(upper)) {
119 accept(upper);
120 return result;
121 }
122 auto force = [&](std::size_t index) {
123 return *result.samples[index].forceOverCoordinate;
124 };
125 while (!(force(lower) <= 0 && force(upper) >= 0) &&
126 result.samples.size() < options.maximumSamples) {
127 if (force(upper) < 0 || (force(upper) == 0 && force(lower) > 0)) {
128 lower = upper;
129 upper = sample(2 * result.samples[upper].coordinate);
130 } else {
131 upper = lower;
132 lower = sample(.5 * result.samples[lower].coordinate);
133 }
134 if (!result.failure.empty()) return result;
135 }
136 result.bracketed = force(lower) <= 0 && force(upper) >= 0;
137 if (!result.bracketed) {
138 result.failure =
139 "Potential amplitude forces do not bracket an ascending positive "
140 "root";
141 return result;
142 }
143 for (auto index: {lower, upper})
144 if (qualified(index)) {
145 accept(index);
146 return result;
147 }
148 double leftWeight = 1, rightWeight = 1;
149 int retainedSide = 0;
150 while (result.samples.size() < options.maximumSamples) {
151 const double left = result.samples[lower].coordinate,
152 right = result.samples[upper].coordinate;
153 const double x0 = left * left, x1 = right * right;
154 const double f0 = leftWeight * force(lower),
155 f1 = rightWeight * force(upper);
156 double x = (x0 * f1 - x1 * f0) / (f1 - f0);
157 if (!std::isfinite(x) || !(x > x0 && x < x1)) x = std::midpoint(x0, x1);
158 double coordinate = std::sqrt(x);
159 if (!(coordinate > left && coordinate < right))
160 coordinate = std::midpoint(left, right);
161 if (!(coordinate > left && coordinate < right)) {
162 result.failure =
163 "Positive amplitude bracket reaches floating-point resolution";
164 return result;
165 }
166 const auto index = sample(coordinate);
167 if (!result.failure.empty()) return result;
168 if (qualified(index)) {
169 accept(index);
170 return result;
171 }
172 if (force(index) < 0) {
173 lower = index;
174 leftWeight = 1;
175 rightWeight = retainedSide == -1 ? .5 * rightWeight : 1;
176 retainedSide = -1;
177 } else {
178 upper = index;
179 rightWeight = 1;
180 leftWeight = retainedSide == 1 ? .5 * leftWeight : 1;
181 retainedSide = 1;
182 }
183 }
184 result.failure = phaseColumn
185 ? "Amplitude continuation does not meet phase-plane "
186 "stationarity within its sample count"
187 : "Amplitude continuation does not meet full "
188 "stationarity within its sample count";
189 return result;
190}
191} // namespace
193 const std::vector<Eigen::VectorXd>& initial, const MassMetric& metric,
194 const PathPotential& potential, const AffinePathBoundary& boundary,
195 const PathConstraints& constraints,
196 const AmplitudeContinuationOptions& options)
197{
198 return continueAmplitude(initial, metric, potential, boundary, constraints,
199 options, std::nullopt);
200}
202 const std::vector<Eigen::VectorXd>& initial, const MassMetric& metric,
203 const PathPotential& potential, const AffinePathBoundary& boundary,
204 const PathConstraints& constraints, Eigen::Index phaseColumn,
205 const AmplitudeContinuationOptions& options)
206{
207 return continueAmplitude(initial, metric, potential, boundary, constraints,
208 options, phaseColumn);
209}
210} // namespace gpr::quantum
@ x
Definition Enums.h:28
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
PotentialPathResult optimizePotentialPath(const Nodes &initial, const MassMetric &metric, const PathPotential &potential, const AffinePathBoundary &boundary, const PathConstraints &constraints, const PotentialPathOptions &options)
AmplitudeContinuationResult continuePotentialPathAmplitudeInPhasePlane(const std::vector< Eigen::VectorXd > &initial, const MassMetric &metric, const PathPotential &potential, const AffinePathBoundary &boundary, const PathConstraints &constraints, Eigen::Index phaseColumn, const AmplitudeContinuationOptions &options)
AmplitudeContinuationResult continuePotentialPathAmplitude(const std::vector< Eigen::VectorXd > &initial, const MassMetric &metric, const PathPotential &potential, const AffinePathBoundary &boundary, const PathConstraints &constraints, const AmplitudeContinuationOptions &options)