-
Notifications
You must be signed in to change notification settings - Fork 3
/
learn.cc
239 lines (196 loc) · 5.53 KB
/
learn.cc
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <algorithm>
#include "bitstring.h"
using namespace itensor;
using std::vector;
using std::string;
template<typename Container>
void
randshuffle(Container & C)
{
static std::random_device rd;
static std::mt19937 g(rd());
std::shuffle(C.begin(), C.end(), g);
}
MPS
makeMPS(SiteSet const& sites,
vector<BitString> data,
Args const& args = Args::global())
{
auto N = length(sites);
auto maxDim = args.getInt("MaxDim",100);
auto phi = [&data,&sites](int i,int n) -> ITensor
{
auto s = sites(n);
auto p = ITensor(s);
auto elt = 1+bit(data[i],n);
p.set(s=elt, 1.);
return p;
};
int ndata = data.size();
auto env = vector<ITensor>(ndata);
for(auto i : range(ndata))
{
env[i] = phi(i,1);
}
auto eind = sites(1);
auto psi = MPS(sites);
for(int n : range1(1,N-1))
{
printf(" %d",n);
ITensor rho;
for(auto i : range(ndata))
{
auto wfi = env[i]*phi(i,n+1);
for(int j = i; j < ndata; j += 1)
{
bool include = compFrom(n+2,data[i],data[j]);
if(include)
{
auto wfj = env[j]*phi(j,n+1);
rho += wfi*prime(wfj);
if(i != j) rho += wfj*prime(wfi);
}
}
}
auto Tr = (rho * delta(eind,prime(eind))
* delta(sites(n+1),prime(sites(n+1)))).real();
rho /= Tr;
//println();
//PrintData(rho);
//EXIT;
ITensor U,D;
diagPosSemiDef(rho,U,D,{"Tags=","Link","MaxDim=",maxDim});
//PrintData(D);
psi.set(1+n,U);
eind = commonIndex(U,D);
for(auto i : range(ndata))
{
env[i] = U*env[i]*phi(i,n+1);
}
}
println();
ITensor U(sites(1)),D,V;
svd(psi(2),U,D,V,{"Tags=","Link"});
psi.set(1,U);
psi.set(2,D*V);
ITensor P;
for(auto e : env) P += e;
P /= norm(P);
psi.set(N,psi(N)*P);
psi.position(1);
return psi;
}
//
// Returns the exact MPS for the even parity data set
// It is a theoretical construction and known to
// have bond dimension (tensor-train rank) equal to 2
//
MPS
exactMPS(SiteSet const& sites)
{
auto N = length(sites);
auto psi = MPS(sites);
auto link = vector<Index>(1+N);
for(auto n : range(link)) link.at(n) = Index(2,tinyformat::format("Link,l=%d",n));
for(auto n : range1(N))
{
auto l = link.at(n-1);
auto r = link.at(n);
auto s = sites(n);
auto A = ITensor(l,s,r);
//1 means even parity and 2 means odd
auto val = n == 1 ? 1. : ISqrt2;
A.set(1,1,1, val);
A.set(2,1,2, val);
A.set(2,2,1, val);
A.set(1,2,2, val);
psi.set(n, A);
}
auto L = ITensor(link.at(0));
L.set(1, 1.0);
psi.set(1, L*psi(1));
auto R = ITensor(link.at(N));
R.set(1, 1.0);
psi.set(N, psi(N)*R);
return psi;
}
Real
bhattDist(MPS const& psi, SiteSet const& sites)
{
return fabs(inner(psi,exactMPS(sites)));
}
int
main(int argc, char* argv[])
{
if(argc != 2) return printfln("Usage: %s inputfile",argv[0]),0;
auto in = InputGroup(argv[1],"input");
auto N = in.getInt("N");
auto maxDim = in.getInt("maxDim");
auto rho_threshold = in.getReal("rho_threshold",0.1);
auto minsamples = in.getInt("minsamples",0);
auto samplestep = in.getInt("samplestep",1);
auto pause_step = in.getYesNo("pause_step",false);
auto nrepeat = in.getInt("nrepeat",1);
auto fraction = in.getReal("fraction",1.);
auto amount = in.getInt("amount",0);
println("\n");
auto data = allEvenStrings(N);
printfln("Data set size = %d",data.size());
auto sites = SiteSet(N,2);
std::ofstream f("out.dat");
int fsize = 0;
if(amount != 0)
{
fsize = amount;
}
else
{
fsize = fraction*data.size();
}
auto frac_data = decltype(data)(fsize);
printfln("Actual number of training samples = %d",frac_data.size());
printfln("Actual fraction of train samples to total data = %.5f",1.*frac_data.size()/data.size());
vector<Real> distances;
for(int step = 1; step <= nrepeat; ++step)
{
println("Shuffling data");
randshuffle(data);
for(auto n : range(fsize)) frac_data[n] = data[n];
println("Computing MPS");
auto psi = makeMPS(sites,
frac_data,
{"MaxDim=",maxDim});
auto dist = bhattDist(psi,sites);
//Print(dist);
if(not isnan(dist))
{
distances.push_back(dist);
printfln(f,"%.12f",dist);
}
println("Distances so far = ");
Real avg = 0;
Real avg2 = 0;
Real Nt = distances.size();
for(auto [n,d] : enumerate(distances))
{
printfln("%.12f",d);
avg += d;
avg2 += d*d;
}
avg /= Nt;
avg2 /= Nt;
println();
printfln("avg = %.12f",avg);
printfln("std dev = %.12f",sqrt(avg2-avg*avg));
printfln("std err = %.12f",sqrt(avg2-avg*avg)/sqrt(Nt));
}
f.close();
//for(auto n : range1(20))
// {
// auto s = binarySample(psi);
// printf("%02d: ",n);
// for(auto b : s) print(b);
// println();
// }
return 0;
}