34 {}) const
35 {
36 if (!positive_)
40 if (q.size() != n || native.size() != n ||
41 (!hessians.empty() && hessians.size() != n) ||
42 !(duration > 0 && duration <
period()) || !std::isfinite(duration))
43 throw std::invalid_argument(
44 "Matching positive arc nodes and duration required");
45 for (std::size_t j = 0; j < n; ++j)
46 if (q[j].size() != d || !q[j].allFinite() ||
47 native[j].gradient.size() != d ||
48 !native[j].gradient.allFinite() ||
49 !std::isfinite(native[j].energy))
50 throw std::invalid_argument(
51 "Finite matching positive arc values required");
53 const M identity = M::Identity(d, d);
54 ArcTailEvaluation
out;
55 out.gradient.assign(n, V::Zero(d));
56 out.durationCrossGradient.assign(n, V::Zero(d));
57 out.fluctuationHessianPhysical = !hessians.empty();
58 const double logScale = std::log(2 * std::acos(-1.) * hbar_);
59 for (std::size_t j = 0; j + 1 < n; ++j) {
61 const V delta = q[j + 1] - q[j];
62 const double kinetic = .5 * delta.squaredNorm() / h;
63 out.action += kinetic;
64 out.gradient[j] -= delta / h;
65 out.gradient[j + 1] += delta / h;
66 out.durationGradient -= kinetic / duration;
67 out.durationHessian += 2 * kinetic / (duration * duration);
68 out.durationCrossGradient[j] += delta / (h * duration);
69 out.durationCrossGradient[j + 1] -= delta / (h * duration);
70 out.logKernelNormalization += .5 * d * (-std::log(h) - logScale);
71 if (!hessians.empty()) {
72 if (hessians[j].rows() != d || hessians[j].cols() != d ||
73 !hessians[j].allFinite())
74 throw std::invalid_argument(
75 "Matching finite positive arc search curvature "
76 "required");
77 gaussian::Segment link;
78 link.A = identity / h + weights[j] * hessians[j];
79 link.L = identity / h;
80 link.rightA = identity / h;
81 link.duration = h;
82 link.logDetL = -d * std::log(h);
83 out.fluctuationSegments.push_back(std::move(link));
84 }
85 }
86 for (std::size_t j = 0; j < n; ++j) {
87 out.action += weights[j] * native[j].energy;
88 out.gradient[j] += weights[j] * native[j].gradient;
89 out.durationGradient += weights[j] * native[j].energy / duration;
90 out.durationCrossGradient[j] +=
91 weights[j] * native[j].gradient / duration;
92 }
93 const double tail =
period() - duration;
95 const auto& kernel = reference.kernel();
96 const Eigen::SelfAdjointEigenSolver<M> spectrum(
wellCurvature());
97 if (spectrum.info() != Eigen::Success)
98 throw std::runtime_error(
99 "Positive arc well spectrum is unresolved");
100 const M& modes = spectrum.eigenvectors();
101 const V omega = spectrum.eigenvalues().array().sqrt();
102 const V difference = modes.transpose() * (q.back() - q.front()),
103 sum = modes.transpose() * (q.back() + q.front());
104 V left(d), right(d), leftTime(d), rightTime(d);
105 for (Eigen::Index k = 0; k < d; ++k) {
106 const double w = omega[k],
x = w * tail, e = std::exp(-
x),
107 one = -std::expm1(-
x);
108 const double th = one / (1 + e), ct = (1 + e) / one,
109 sech2 = 4 * e / ((1 + e) * (1 + e)),
110 csch2 = 4 * e / (one * one);
111 const double a = difference[k], b = sum[k];
112 out.action += .25 * w * (ct * a * a + th * b * b);
113 left[k] = .5 * w * (ct * a + th * b);
114 right[k] = .5 * w * (-ct * a + th * b);
115 out.durationGradient -=
116 w * w / 8 * (-csch2 * a * a + sech2 * b * b);
117 out.durationHessian +=
118 w * w * w / 8 * (csch2 * ct * a * a - sech2 * th * b * b);
119 leftTime[k] = -.25 * w * w * (-csch2 * a + sech2 * b);
120 rightTime[k] = -.25 * w * w * (csch2 * a + sech2 * b);
121 }
122 out.gradient.back() += modes * left;
123 out.gradient.front() += modes * right;
124 out.durationCrossGradient.back() += modes * leftTime;
125 out.durationCrossGradient.front() += modes * rightTime;
126 out.logKernelNormalization += .5 * (kernel.logDetL - d * logScale);
127 if (!hessians.empty()) {
128 if (hessians.back().rows() != d || hessians.back().cols() != d ||
129 !hessians.back().allFinite())
130 throw std::invalid_argument(
131 "Matching finite positive arc endpoint curvature required");
132 auto link = kernel;
133 link.rightA = kernel.A;
134 link.A += weights[n - 1] * hessians.back();
135 out.fluctuationSegments.push_back(std::move(link));
136 }
137 if (!std::isfinite(
out.action) ||
138 !std::isfinite(
out.durationGradient) ||
139 !std::isfinite(
out.durationHessian))
140 throw std::overflow_error(
141 "Positive arc action exceeds arithmetic range");
143 }
const M & wellCurvature() const
ArcTailEvaluation evaluate(const std::vector< V > &q, const std::vector< gpr::quantum::PotentialEvaluation > &native, double arcDuration, const std::vector< M > &nativeHessians={}) const
const std::vector< double > & arcWeights() const
V nodalWeights(double duration) const
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out