/
basis.hpp
447 lines (387 loc) · 10 KB
/
basis.hpp
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#ifndef __BASIS_HPP__
#define __BASIS_HPP__
#include <stdint.h>
#include <vector>
#include <map>
#include <bitset>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <cassert>
#include <cmath>
#include <Eigen/Sparse>
#include <Eigen/Dense>
using namespace Eigen;
#include "eigenHelpers.hpp"
class FermionicStateException : public std::logic_error
{
public:
FermionicStateException (const std::string& what)
: std::logic_error(what)
{}
};
template<typename T=uint64_t>
class FermionicState
{
public:
typedef T num_t;
typedef std::bitset<sizeof(T)*8> bitset;
FermionicState (size_t N_) : N(N_)
{
assert(N<=sizeof(T)*8);
}
FermionicState (const std::string& sString)
{
N = sString.size();
for (size_t i=0; i<N; i++)
if (sString[i] == '1')
s[i] = 1;
else if (sString[i] == '0')
s[i] = 0;
else
throw FermionicStateException("Can't construct FermionicState from string '" +
sString + "'.");
}
void operator= (const T& v)
{
s = v;
}
bool operator== (const FermionicState<T>& fs) const
{
return s == fs.s && N == fs.N;
}
bool operator[] (size_t pos) const
{
return s[pos];
}
typename bitset::reference operator[] (size_t pos)
{
//return s[pos];
return typename bitset::reference(s, pos);
}
size_t size() const
{
return N;
}
size_t count(size_t fromPos, size_t toPos) const
{
if (toPos<fromPos) std::swap(toPos,fromPos);
assert (fromPos <= toPos);
size_t sum = 0;
for (size_t i=fromPos; i<toPos; i++)
sum += s[i];
return sum;
}
size_t count(size_t toPos) const
{
size_t sum = 0;
for (size_t i=0; i<toPos; i++)
sum += s[i];
return sum;
}
size_t count() const
{
return s.count();
}
bool operator< (const FermionicState<T>& state2) const
{
return s.to_ulong() < state2.s.to_ulong();
}
std::string toString() const
{
std::stringstream ss;
for (size_t i=0; i<N; i++)
ss << s[i];
return ss.str();
}
private:
size_t N;
bitset s;
};
template<typename T>
std::ostream& operator<< (std::ostream& o, const FermionicState<T>& fs)
{
o << fs.toString();
return o;
}
#ifndef STORAGE_TYPE_OF_FERMIONIC_STATE
#define STORAGE_TYPE_OF_FERMIONIC_STATE uint16_t
#endif
typedef FermionicState<STORAGE_TYPE_OF_FERMIONIC_STATE> State;
class SymmetryOperator
{
public:
virtual ~SymmetryOperator() {}
virtual int operator() (const State&) const = 0;
};
class ParticleNumberSymmetryOperator : public SymmetryOperator
{
public:
virtual ~ParticleNumberSymmetryOperator() {}
virtual int operator() (const State& s) const
{
return s.count();
}
};
class SpinSymmetryOperator : public SymmetryOperator
{
public:
virtual ~SpinSymmetryOperator() {}
virtual int operator() (const State& s) const
{
// N_down = N - N_up with N the total particle number
// spin = N_up - N_down = 2 N_up - N
const int N = s.count();
int N_up=0;
for (size_t i=0; i<s.size(); i+=2)
N_up += s[i];
return 2*N_up - N;
}
};
struct Range
{
Range () : a(0), b(0) {}
Range (int a_, int b_) : a(a_), b(b_) {}
bool operator== (const Range& r)
{
return a==r.a && b==r.b;
}
int a;
int b;
};
typedef std::vector<int> Sector;
/*
* Some small helper functions for convenient construction of Sector.
*/
Sector constructSector (int a)
{
Sector s;
s.push_back(a);
return s;
}
Sector constructSector (int a, int b)
{
Sector s;
s.push_back(a);
s.push_back(b);
return s;
}
Sector constructSector (int a, int b, int c)
{
Sector s;
s.push_back(a);
s.push_back(b);
s.push_back(c);
return s;
}
Sector constructSector (int a, int b, int c, int d)
{
Sector s;
s.push_back(a);
s.push_back(b);
s.push_back(c);
s.push_back(d);
return s;
}
Sector constructSector (int a, int b, int c, int d, int e)
{
Sector s;
s.push_back(a);
s.push_back(b);
s.push_back(c);
s.push_back(d);
s.push_back(e);
return s;
}
Sector constructSector (int a, int b, int c, int d, int e, int f)
{
Sector s;
s.push_back(a);
s.push_back(b);
s.push_back(c);
s.push_back(d);
s.push_back(e);
s.push_back(f);
return s;
}
class BasisException : public std::logic_error
{
public:
BasisException (const std::string& what)
: std::logic_error(what)
{}
};
class Basis
{
private:
struct SectorAndState
{
SectorAndState (Sector sector_, State state_)
: sector(sector_), state(state_) {}
Sector sector;
State state;
};
class SymmetrySorter
{
public:
bool operator() (const SectorAndState& sas1, const SectorAndState& sas2) const
{
const Sector& s1 = sas1.sector;
const Sector& s2 = sas2.sector;
assert(s1.size() == s2.size());
for (size_t i=0; i<s1.size(); i++)
{
if (s1[i] < s2[i])
return true;
else if (s1[i] > s2[i])
return false;
}
//They are equal. Has to be false, otherwise sort keeps on
//reordering indefinitely.
return false;
}
};
public:
typedef State::num_t num_t;
Basis (size_t N_orbital_) : N_orbital(N_orbital_), filterSet(false) {}
void construct ()
{
/*
* Note: sizeof(N_basis) must be bigger than sizeof(num_t), because a
* FermionicState<num_t> can hold N = 2^(sizeof(num_t)) different
* states. However this number N is not representable in num_t (num_t's
* biggest number is N-1).
*/
size_t N_basis;
assert(sizeof(N_basis) > sizeof(num_t));
N_basis = std::pow(2.0, static_cast<int>(N_orbital));
State state(N_orbital);
std::vector<SectorAndState> sas;
for (size_t num=0; num<N_basis; num++)
{
state = static_cast<num_t>(num);
Sector sector = computeSectorForState(state);
if (!filterSet || applyFilter(sector))
sas.push_back(SectorAndState(sector, state));
}
std::sort(sas.begin(), sas.end(), SymmetrySorter());
constructSectorsAndRanges(sas);
states.resize(sas.size(), State(N_orbital));
indices.clear();
for (size_t i=0; i<sas.size(); i++)
{
states[i] = sas[i].state;
indices[states[i]] = i;
}
}
Basis& addSymmetryOperator (const SymmetryOperator* so)
{
symmetryOperators.push_back(so);
return *this;
}
void setFilter (Sector filter_)
{
if (filter_.size() > symmetryOperators.size())
throw BasisException("Invalid filter specified.");
filter = filter_;
if (filter.size() == 0) filterSet = false;
else filterSet = true;
}
Range getRangeOfSector (const Sector& s) const
{
assert(sectors.size() == ranges.size());
for (size_t i=0; i<sectors.size(); i++)
if (sectors[i] == s)
return ranges[i];
return Range(0,0);
}
Range getRangeOfSector (int a, int b=0, int c=0, int d=0, int e=0) const
{
size_t size = symmetryOperators.size();
Sector s;
if (size > 0) s.push_back(a);
if (size > 1) s.push_back(b);
if (size > 2) s.push_back(c);
if (size > 3) s.push_back(d);
if (size > 4) s.push_back(e);
return getRangeOfSector(s);
}
const std::vector<Range>& getRanges () const
{
return ranges;
}
const State& operator() (num_t index) const
{
return states[index];
}
num_t operator() (const State& state) const
{
typedef std::map<State, num_t>::const_iterator CIT;
CIT i = indices.find(state);
if (i != indices.end())
return i->second;
throw BasisException("No such state in basis.");
}
size_t size() const
{
return states.size();
}
size_t numberOfOrbitals () const
{
return N_orbital;
}
void dump () const
{
for (size_t i=0; i<states.size(); i++)
std::cerr << states[i] << std::endl;
}
private:
Sector computeSectorForState (const State& s) const
{
Sector sector(symmetryOperators.size());
for (size_t i=0; i<symmetryOperators.size(); i++)
sector[i] = symmetryOperators[i]->operator()(s);
return sector;
}
void constructSectorsAndRanges(const std::vector<SectorAndState>& sas)
{
if (sas.size() == 0)
return;
Sector const * currentSector = &(sas[0].sector);
sectors.clear();
ranges.clear();
int a = 0;
int b = 0;
for (size_t i=1; i<sas.size(); i++)
{
Sector const * newSector = &(sas[i].sector);
if (*newSector != *currentSector)
{
b = i;
sectors.push_back(*currentSector);
ranges.push_back(Range(a,b));
a = i;
currentSector = newSector;
}
}
sectors.push_back(*currentSector);
ranges.push_back(Range(b, sas.size()));
}
bool applyFilter (const Sector& sector) const
{
assert(filter.size() <= sector.size());
for (size_t i=0; i<filter.size(); i++)
if (filter[i] != sector[i])
return false;
return true;
}
size_t N_orbital;
std::vector<State> states;
std::map<State, num_t> indices;
std::vector<Sector> sectors;
std::vector<Range> ranges;
std::vector<const SymmetryOperator*> symmetryOperators;
Sector filter;
bool filterSet;
};
#endif // __BASIS_HPP__