summaryrefslogtreecommitdiff
path: root/hadamard.cpp
blob: 67a53f50aba2ebad8047f315abd73771bc0e784a (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

#include "hadamard_pt.hpp"
#include "matrices.hpp"

#include <execution>
#include <chrono>
#include <fstream>
#include <iostream>

class MeasureEnergy : public Measurement {
public:
  unsigned N;
  double totalE;
  double totalE2;
  unsigned n;
  std::vector<unsigned> ρ_dist;

  MeasureEnergy(unsigned n_bins = 1e4) : ρ_dist(n_bins + 1, 0) {
    n = n_bins;
    N = 0;
    totalE = 0;
    totalE2 = 0;
  }

  void after_sweep(double, double E, const Orthogonal& M) override {
    N++;
    totalE += E;
    totalE2 += pow(E, 2);
    double max = sqrt(M.size());
    for (unsigned i = 0; i < M.size(); i++) {
      for (unsigned j = 0; j < M.size(); j++) {
        ρ_dist[n * (M(i, j) + max) / (2 * max)]++;
      }
    }
  }

  double energy() const { return totalE / N; }
  double specific_heat() const { return totalE2 / N - pow(totalE / N, 2); }
};

class MeasureTransitionRates : public ParallelMeasurement {
public:
  std::vector<unsigned> nAccepted;
  std::vector<unsigned> total_steps;

  MeasureTransitionRates(unsigned n) : nAccepted(n - 1, 0), total_steps(n - 1, 0) {}

  void after_step(bool accepted, unsigned i, double, double, const MCMC&, const MCMC&) override {
    total_steps[i]++;
    if (accepted)
      nAccepted[i]++;
  }
};

int main(int argc, char* argv[]) {
  unsigned n_tuning = 1e2;
  double β₀ = 0.1;
  double β₁ = 10;
  unsigned N = 16;
  unsigned k = 2;
  double ε = 0.01;
  double ε2 = 0.01;

  unsigned M = 10;
  unsigned m = 1e4;

  bool loadBetasFromFile = false;
  std::string betaFilename;

  int opt;

  while ((opt = getopt(argc, argv, "k:b:c:n:t:N:M:e:m:f:F:")) != -1) {
    switch (opt) {
    case 'k':
      k = atoi(optarg);
      if (k == 0 || k > 8) {
        std::cout << "The size k must be an integer from 1 to 8!" << std::endl;
        exit(1);
      }
      break;
    case 'b':
      β₀ = atof(optarg);
      break;
    case 'c':
      β₁ = atof(optarg);
      break;
    case 'e':
      ε = atof(optarg);
      break;
    case 'f':
      ε2 = atof(optarg);
      break;
    case 'n':
      m = (unsigned)atof(optarg);
      break;
    case 't':
      n_tuning = (unsigned)atof(optarg);
      break;
    case 'N':
      N = (unsigned)atof(optarg);
      break;
    case 'M':
      M = (unsigned)atof(optarg);
      break;
    case 'F':
      loadBetasFromFile = true;
      betaFilename = optarg;
      break;
    default:
      exit(1);
    }
  }

  unsigned n = 4 * k;

  std::vector<Measurement*> As(N);
  for (Measurement*& A : As) {
    A = new MeasureEnergy();
  }
  MeasureTransitionRates B(N);

  PT p(β₀, β₁, N, n, B, As);

  for (MCMC& sim : p.Ms) {
    sim.M = hadamards[k - 1];
    sim.E = sim.M.energy();
  }

  std::vector<double> f;

  std::cout << "Beginning simulation of " << n << ".\n";
  if (loadBetasFromFile) {
    std::cout << "Loading βs from file.\n";
    std::ifstream betaFile(betaFilename);
    for (unsigned i = 0; i < N; i++) {
      double βtmp;
      betaFile >> βtmp;
      p.Ms[i].β = βtmp;
    }
    std::cout << "βs: ";
    for (const MCMC& M : p.Ms) {
      std::cout << M.β << " ";
    }
    std::cout << std::endl;
    std::cout << "Beginning " << n_tuning << " tuning tempering updates.\n";
    Rng rng;
    for (unsigned i = 0; i < n_tuning; i++) {
      std::for_each(std::execution::par_unseq, p.Ms.begin(), p.Ms.end(),
                    [M, ε](MCMC& MM) { MM.tune(M, ε); });
      for (unsigned j = 0; j < p.Ms.size(); j++) {
        unsigned k = rng.uniform((unsigned)0, (unsigned)(p.Ms.size() - 2));
        p.step(k, k + 1, true);
      }
    }
  } else {
    std::cout << "Beginning " << n_tuning << " tuning tempering updates of " << M
              << " sweeps each.\n";
    f = p.tune(n_tuning, M, ε, ε2);
    std::cout << "βs: ";
    for (const MCMC& M : p.Ms) {
      std::cout << M.β << " ";
    }
    std::cout << std::endl;
  }
  std::cout << "Running " << m << " PT swaps of " << M << " sweeps each.\n";
  p.run(m, M); 
  std::cout << "Finished " << n << ".\n";

  auto tag = std::chrono::high_resolution_clock::now();

  std::string filename = "hmm_" + std::to_string(n) + "_" + std::to_string(β₀) + "_" +
                         std::to_string(β₁) + "_" + std::to_string(N) + "_" +
                         std::to_string(tag.time_since_epoch().count()) + ".dat";

  std::ofstream file(filename);

  for (const MCMC& M : p.Ms) {
    file << M.β << " ";
  }
  file << std::endl;

  for (double ff : f) {
    file << ff << " ";
  }
  file << std::endl;

  for (unsigned i = 0; i < B.nAccepted.size(); i++) {
    file << std::fixed << B.nAccepted[i] / (double)B.total_steps[i] << " ";
  }
  file << std::endl;

  for (unsigned i = 0; i < As.size(); i++) {
    file << std::fixed << ((MeasureEnergy*)As[i])->energy() << " ";
  }

  file << std::endl;

  for (unsigned i = 0; i < As.size(); i++) {
    file << std::fixed << ((MeasureEnergy*)As[i])->specific_heat() << " ";
  }

  file << std::endl;

  for (unsigned i = 0; i < As.size(); i++) {
    for (unsigned j = 0; j < ((MeasureEnergy*)As[i])->ρ_dist.size(); j++) {
      file << std::fixed << ((MeasureEnergy*)As[i])->ρ_dist[j] << " ";
    }
    file << std::endl;
  }

  file.close();

  return 0;
}