-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampler.h
193 lines (148 loc) · 4.95 KB
/
sampler.h
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
#ifndef _SAMPLER_H_
#define _SAMPLER_H_
#include "itensor/all.h"
#include <cstdlib>
#include <random>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
using namespace itensor;
// TODO: bug when rotating wavefunction (not conserving QNs)
// bug in hadamard / kadamard when setting matrix elements
class Sampler
{
private:
int N_; // number of sites (currently 1D only)
int num_samples_; // number of samples to generate in every input basis
complex<double> I_; // i
vector<vector<string>> bases_; // bases to generate samples in
MPS psi_; // the MPS from DMRG calculation
string sample_path_; // contains generated samples
string sample_bases_path_; // contains the corresponding basis of each sample
SiteSet sites_; // SiteSet from DMRG calculation
public:
Sampler(int N, MPS &psi, SiteSet sites, int num_samples, string sample_path, string sample_bases_path) : N_(N), psi_(psi), sites_(sites), num_samples_(num_samples), sample_path_(sample_path), sample_bases_path_(sample_bases_path), I_(0, 1){};
void Sample()
{
// perform sampling
vector<string> basis;
ITensor cap;
vector<int> sample(N_);
random_device rd;
mt19937 rng(rd());
ofstream sample_file(sample_path_);
ofstream sample_bases_file(sample_bases_path_);
for (int b = 0; b < bases_.size(); b++)
{
// generate num_samples samples for all bases
basis = bases_[b];
MPS psi = RotateMPS(basis);
psi.position(1);
for (int j = 0; j < num_samples_; j++)
{
for (auto i : range1(N_))
{
auto tensor = i == 1 ? psi.A(i) : cap * psi.A(i);
auto rho = prime(dag(tensor), "Site") * tensor;
auto I = sites_(i);
auto Ip = prime(I);
vector<Real> weights(dim(I) + 1);
for (auto idx : range1(dim(I)))
{
weights[idx] = rho.eltC(I(idx), Ip(idx)).real();
}
discrete_distribution<> d(weights.begin(), weights.end());
sample[i - 1] = d(rng);
cap = dag(setElt(I(sample[i - 1]))) * tensor;
} // N
// write samples and bases to files
for (int j = 0; j < N_; j++)
{
sample_file << sample[j] - 1 << " ";
}
sample_file << endl;
for (int j = 0; j < N_; j++)
{
sample_bases_file << bases_[b][j] << " ";
}
sample_bases_file << endl;
} // num_samples
} // bases
}
ITensor Hadamard(const ITensor &A)
{
// Diagonalizes pauliX
auto s = findIndex(A, "Site");
auto sp = prime(s);
ITensor hadamard(s, sp);
hadamard.set(s(1), sp(1), 1.0 / sqrt(2));
hadamard.set(s(1), sp(2), 1.0 / sqrt(2));
hadamard.set(s(2), sp(1), 1.0 / sqrt(2));
hadamard.set(s(2), sp(2), -1.0 / sqrt(2));
return hadamard;
}
ITensor Kadamard(const ITensor &A)
{
// Diagonalizes pauliY
auto s = findIndex(A, "Site");
auto sp = prime(s);
ITensor kadamard(s, sp);
kadamard.set(s(1), sp(1), 1.0 / sqrt(2));
kadamard.set(s(1), sp(2), 1.0 / sqrt(2));
kadamard.set(s(2), sp(1), I_ / sqrt(2));
kadamard.set(s(2), sp(2), -I_ / sqrt(2));
return kadamard;
}
MPS RotateMPS(vector<string> &basis)
{
// rotate the MPS to sample in a desired basis
// placeholder to do the rotations
auto psi = psi_;
ITensor rotation;
for (int j = 1; j <= N_; j++)
{
if (basis[j - 1] != "Z")
{
psi.position(j);
auto wf = psi.A(j);
if (basis[j - 1] == "X")
{
rotation = Hadamard(wf);
}
if (basis[j - 1] == "Y")
rotation = dag(Kadamard(wf));
wf *= rotation;
wf.noPrime();
psi.setA(j, wf);
}
}
return psi;
}
void LoadOnlyCompBasis()
{
// bases_ will only contain "Z"
bases_.resize(1, vector<string>(N_));
for (int j = 0; j < N_; j++)
{
bases_[0][j] = "Z";
}
}
void LoadBases(ifstream &bases_file)
{
// append information from bases_file
int num_bases;
bases_file >> num_bases;
bases_.resize(num_bases, vector<string>(N_));
for (int i = 0; i < num_bases; i++)
{
for (int j = 0; j < N_; j++)
{
bases_file >> bases_[i][j];
}
}
}
};
#endif