summaryrefslogtreecommitdiff
path: root/p-spin.hpp
blob: bd3cacc893b15e300aee350c353bcbcd63ab565e (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
#pragma once

#include <eigen3/Eigen/Dense>

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

template <typename Derived>
Vector<typename Derived::Scalar> normalize(const Eigen::MatrixBase<Derived>& z) {
  return z * sqrt((double)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;

  double 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>
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<double, 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);

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

  double 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};
}