summaryrefslogtreecommitdiff
path: root/p-spin.hpp
blob: e5cd195c6fa15d32dfe5820040b202fe8ce6a462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once

#include <eigen3/Eigen/Dense>

#include "types.hpp"
#include "tensor.hpp"
#include "factorial.hpp"

template <typename Derived>
Vector<typename Derived::Scalar> normalize(const Eigen::MatrixBase<Derived>& z) {
  return z * sqrt((Real)z.size() / (typename Derived::Scalar)(z.transpose() * z));
}

template <class Scalar, int p>
std::tuple<Scalar, Vector<Scalar>, Matrix<Scalar>> hamGradHess(const Tensor<Scalar, p>& J, const Vector<Scalar>& z) {
  Matrix<Scalar> Jz = contractDown(J, z); // Contracts J into p - 2 copies of z.
  Vector<Scalar> Jzz = Jz * z;
  Scalar Jzzz = Jzz.transpose() * z;

  Real pBang = factorial(p);

  Matrix<Scalar> hessian = ((p - 1) * p / pBang) * Jz;
  Vector<Scalar> gradient = (p / pBang) * Jzz;
  Scalar hamiltonian = Jzzz / pBang;

  return {hamiltonian, gradient, hessian};
}

template <class Scalar, int p>
Scalar getHamiltonian(const Tensor<Scalar, p>& J, const Vector<Scalar>& z) {
  Scalar H;
  std::tie(H, std::ignore, std::ignore) = hamGradHess(J, z);
  return H;
}

template <class Scalar, int p>
Vector<Scalar> getGradient(const Tensor<Scalar, p>& J, const Vector<Scalar>& z) {
  Vector<Scalar> dH;
  std::tie(std::ignore, dH, std::ignore) = hamGradHess(J, z);
  return dH;
}

template <class Scalar, int p>
Matrix<Scalar> getHessian(const Tensor<Scalar, p>& J, const Vector<Scalar>& z) {
  Matrix<Scalar> ddH;
  std::tie(std::ignore, std::ignore, ddH) = hamGradHess(J, z);
  return ddH;
}

template <class Scalar>
Real getThresholdEnergyDensity(unsigned p, Scalar κ, Scalar ε, Real a) {
  Real apm2 = pow(a, p - 2);
  Scalar δ = κ / apm2;
  Real θ = arg(κ) + 2 * arg(ε);

  return (p - 1) * apm2 / (2 * p) * pow(1 - norm(δ), 2) / (1 + norm(δ) - 2 * abs(δ) * cos(θ));
}

template <class Scalar, int p>
Real getProportionOfThreshold(Scalar κ, const Tensor<Scalar, p>& J, const Vector<Scalar>& z) {
  Real N = z.size();
  Scalar ε = getHamiltonian(J, z) / N;
  Real a = z.squaredNorm() / N;

  return norm(ε) / getThresholdEnergyDensity(p, κ, ε, a);
}

template <class Scalar>
Vector<Scalar> zDot(const Vector<Scalar>& z, const Vector<Scalar>& dH) {
  return -dH.conjugate() + (dH.dot(z) / z.squaredNorm()) * z.conjugate();
}

template <class Scalar, int p>
std::tuple<Real, Vector<Scalar>> WdW(const Tensor<Scalar, p>& J, const Vector<Scalar>& z) {
  Vector<Scalar> dH;
  Matrix<Scalar> ddH;
  std::tie(std::ignore, dH, ddH) = hamGradHess(J, z);

  Vector<Scalar> dzdt = zDot(z, dH);

  Real a = z.squaredNorm();
  Scalar A = (Scalar)(z.transpose() * dzdt) / a;
  Scalar B = dH.dot(z) / a;

  Real W = dzdt.squaredNorm();
  Vector<Scalar> dW = ddH * (dzdt - A * z.conjugate())
    + 2 * (conj(A) * B * z).real()
    - conj(B) * dzdt - conj(A) * dH.conjugate();

  return {W, dW};
}

template <class Scalar>
Matrix<Scalar> dzDot(const Vector<Scalar>& z, const Vector<Scalar>& dH) {
  Real z² = z.squaredNorm();
  return (dH.conjugate() - (dH.dot(z) /) * z.conjugate()) * z.adjoint() /;
}

template <class Scalar>
Matrix<Scalar> dzDotConjugate(const Vector<Scalar>& z, const Vector<Scalar>& dH, const Matrix<Scalar>& ddH) {
  Real z² = z.squaredNorm();
  return -ddH + (ddH * z.conjugate()) * z.transpose() /+ (z.dot(dH) /) * (
              Matrix<Scalar>::Identity(ddH.rows(), ddH.cols()) - z.conjugate() * z.transpose() /);
}