Loading...
Searching...
No Matches
ReflectionEvenBand.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Core>
8#include <algorithm>
9#include <cmath>
10#include <limits>
11#include <stdexcept>
12#include <vector>
13
14namespace gpr::quantum {
15template <class Constraints>
16void validateReflectionEvenBand(const std::vector<Eigen::VectorXd>& path,
17 const std::vector<double>& weights,
18 const Constraints& constraints,
19 double tolerance)
20{
21 const auto n = path.size();
22 if (n < 3 || n % 2 == 0 || weights.size() + 1 != n || !(tolerance > 0) ||
23 !std::isfinite(tolerance))
24 throw std::invalid_argument(
25 "An odd reflected band, matching links and positive tolerance "
26 "required");
27 const auto d = path.front().size();
28 if (d == 0)
29 throw std::invalid_argument("Nonempty reflection coordinates required");
30 const double roundoff = 128 * std::numeric_limits<double>::epsilon();
31 for (std::size_t j = 0; j < n; ++j) {
32 const auto& q = path[j];
33 const auto& reflected = path[n - 1 - j];
34 if (q.size() != d || reflected.size() != d || !q.allFinite() ||
35 !reflected.allFinite() ||
36 (q - reflected).norm() >
37 roundoff * std::max({1., q.norm(), reflected.norm()}))
38 throw std::invalid_argument(
39 "Native reflection search requires an explicitly reflected CON "
40 "seed");
41 }
42 for (std::size_t j = 0; j < weights.size(); ++j)
43 if (!(weights[j] > 0) || !std::isfinite(weights[j]) ||
44 std::abs(weights[j] - weights[weights.size() - 1 - j]) >
45 roundoff * std::max(1., std::abs(weights[j])))
46 throw std::invalid_argument(
47 "Reflection search requires palindromic positive "
48 "imaginary-time links");
49 const auto k = constraints.target.size();
50 if (!constraints.target.allFinite() ||
51 constraints.target.norm() > tolerance ||
52 (k > 0 && constraints.normals.size() != n))
53 throw std::invalid_argument(
54 "Reflection search requires compatible odd phase constraints");
55 if (k > 0)
56 for (std::size_t j = 0; j < n; ++j) {
57 const auto& a = constraints.normals[j];
58 const auto& b = constraints.normals[n - 1 - j];
59 if (a.rows() != d || a.cols() != k || b.rows() != d ||
60 b.cols() != k || !a.allFinite() || !b.allFinite() ||
61 (a + b).norm() > roundoff * std::max({1., a.norm(), b.norm()}))
62 throw std::invalid_argument(
63 "Reflection search requires odd phase normals");
64 }
65}
66
67// Retain the outgoing leg exactly. Its reflected copies share one native
68// geometry, while the physical fluctuation operator keeps both parity sectors.
69inline double reflectOutgoingBand(std::vector<Eigen::VectorXd>& path)
70{
71 if (path.size() < 3 || path.size() % 2 == 0)
72 throw std::invalid_argument(
73 "An odd closed band is required for outgoing reflection");
74 double correction = 0;
75 for (std::size_t j = 0; j < path.size() / 2; ++j) {
76 if (path[j].size() == 0 ||
77 path[j].size() != path[path.size() - 1 - j].size() ||
78 !path[j].allFinite() || !path[path.size() - 1 - j].allFinite())
79 throw std::invalid_argument(
80 "Matching finite outgoing and return coordinates required");
81 correction =
82 std::max(correction, (path[path.size() - 1 - j] - path[j]).norm());
83 path[path.size() - 1 - j] = path[j];
84 }
85 return correction;
86}
87
88inline double projectReflectionEvenBand(std::vector<Eigen::VectorXd>& path)
89{
90 if (path.size() < 3 || path.size() % 2 == 0)
91 throw std::invalid_argument(
92 "An odd closed band is required for reflection projection");
93 const auto d = path.front().size();
94 for (const auto& point: path)
95 if (d == 0 || point.size() != d || !point.allFinite())
96 throw std::invalid_argument(
97 "Matching finite reflection coordinates required");
98 double correction = 0;
99 for (std::size_t j = 0; j < path.size() / 2; ++j) {
100 const auto k = path.size() - 1 - j;
101 const Eigen::VectorXd average = .5 * path[j] + .5 * path[k];
102 correction = std::max({correction, (average - path[j]).stableNorm(),
103 (average - path[k]).stableNorm()});
104 if (!std::isfinite(correction))
105 throw std::overflow_error(
106 "Reflection correction exceeds arithmetic range");
107 path[j] = average;
108 path[k] = average;
109 }
110 return correction;
111}
112} // namespace gpr::quantum
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
void validateReflectionEvenBand(const std::vector< Eigen::VectorXd > &path, const std::vector< double > &weights, const Constraints &constraints, double tolerance)
double reflectOutgoingBand(std::vector< Eigen::VectorXd > &path)
double projectReflectionEvenBand(std::vector< Eigen::VectorXd > &path)