Loading...
Searching...
No Matches
MidpointTaylorAction.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
3
4#include <Eigen/Eigenvalues>
5#include <cmath>
6#include <stdexcept>
7
9double midpointTaylorAction(const Matrix &hessian, const Vector &gradient,
10 const Vector &displacement, double potential,
11 double duration)
12{
13 // The segment validation includes symmetry, finite inputs and caustics.
14 exactSegment(hessian, duration);
15 if (gradient.size() != hessian.rows() ||
16 displacement.size() != hessian.rows() || !gradient.allFinite() ||
17 !displacement.allFinite() || !std::isfinite(potential))
18 throw std::invalid_argument("Invalid midpoint Taylor action data");
19 Eigen::SelfAdjointEigenSolver<Matrix> eig(.5 * hessian +
20 .5 * hessian.transpose());
21 if (eig.info() != Eigen::Success)
22 throw std::runtime_error("Midpoint action eigendecomposition failed");
23 const Vector g = eig.eigenvectors().transpose() * gradient;
24 const Vector d = eig.eigenvectors().transpose() * displacement;
25 long double action = duration * potential;
26 for (Eigen::Index i = 0; i < g.size(); ++i) {
27 const double lambda = eig.eigenvalues()[i];
28 const double x = duration * std::sqrt(std::abs(lambda));
29 double kinetic, affine;
30 if (x < 1e-2) {
31 const double z = std::copysign(x * x, lambda);
32 kinetic =
33 (1 + z / 12 - z * z / 720 + z * z * z / 30240) / (2 * duration);
34 affine = duration * duration * duration *
35 (-1. / 24 + z / 240 - 17 * z * z / 40320 +
36 31 * z * z * z / 725760);
37 } else {
38 const double t = lambda > 0 ? std::tanh(x / 2) : std::tan(x / 2);
39 kinetic = std::sqrt(std::abs(lambda)) / (4 * t);
40 affine = duration * (2 * t / x - 1) / (2 * lambda);
41 }
42 action += static_cast<long double>(kinetic) * d[i] * d[i] +
43 static_cast<long double>(affine) * g[i] * g[i];
44 }
45 const double result = static_cast<double>(action);
46 if (!std::isfinite(result))
47 throw std::overflow_error("Nonfinite midpoint Taylor action");
48 return result;
49}
50} // namespace gpr::quantum::gaussian
@ z
Definition Enums.h:28
@ x
Definition Enums.h:28
Segment exactSegment(const Matrix &hessian, double duration)
double midpointTaylorAction(const Matrix &hessian, const Vector &gradient, const Vector &displacement, double potential, double duration)