-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathindex.h
More file actions
548 lines (409 loc) · 10.3 KB
/
index.h
File metadata and controls
548 lines (409 loc) · 10.3 KB
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//
// Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef __ITENSOR_INDEX_H
#define __ITENSOR_INDEX_H
#include "itensor/global.h"
#include "itensor/tagset.h"
#include "itensor/arrow.h"
#include "itensor/qn.h"
#include <thread>
namespace itensor {
//Forward declarations
class IndexVal;
using QNInt = std::pair<QN,long>;
namespace detail {
struct RandomID
{
using rng_type = std::mt19937_64;
using result_type = typename rng_type::result_type;
std::hash<std::thread::id> hasher;
RandomID()
: rng(std::clock() + hasher(std::this_thread::get_id()))
{ }
result_type
operator()() { return rng(); }
private:
rng_type rng;
};
struct SequentialID
{
using result_type = std::uint_fast32_t;
SequentialID() : id(1) { }
result_type
operator()()
{
auto res = id;
id += 1;
return res;
}
private:
result_type id = 0;
};
} //namespace detail
//
// Index
//
// Represents a tensor index of fixed bond dimension m.
//
// Can be compared with == operator (returns true if both
// are copies of the same Index instance).
//
class IQIndexDat;
class Index
{
public:
using IDGenerator = detail::RandomID;
using id_type = IDGenerator::result_type;
using extent_type = int;
using qnstorage = std::vector<QNInt>;
using qn_ptr = std::shared_ptr<IQIndexDat>;
private:
id_type id_;
extent_type dim_;
Arrow dir_ = Out;
qn_ptr pd;
TagSet tags_;
public:
Index();
explicit
Index(long dim,
TagSet const& ts = TagSet("0"));
// Deprecated
explicit
Index(std::string s,
long m)
{
Error("Index(string,int) constructor deprecated, use Index(int,...) instead");
}
template<typename... QN_Sizes>
Index(QN const& q1, long size1,
QN_Sizes const&... qnsizes);
Index(qnstorage && qns,
TagSet const& ts = TagSet("0"));
Index(qnstorage && qns,
Arrow dir,
TagSet const& ts = TagSet("0"));
// Returns the dimension of this Index
long
dim() const { return dim_; }
// Returns the prime level
int
primeLevel() const { return tags_.primeLevel(); }
// Returns the TagSet
TagSet
tags() const { return tags_; }
id_type
id() const { return id_; }
// Evaluates to false if Index is default constructed.
explicit operator bool() const;
// (Explicitly) convertible to integer types
explicit operator int() const { return dim(); }
explicit operator long() const { return dim(); }
explicit operator size_t() const { return dim(); }
// Add tags
Index&
addTags(const TagSet& t) { tags_.addTags(t); return *this; }
// Remove tags
Index&
removeTags(const TagSet& t) { tags_.removeTags(t); return *this; }
// Set tags
Index&
setTags(const TagSet& t) { tags_.setTags(t); return *this; }
// Remove all tags
Index&
noTags() { tags_.noTags(); return *this; }
// Set tags
Index&
replaceTags(const TagSet& tsold, const TagSet& tsnew) { tags_.replaceTags(tsold,tsnew); return *this; }
// Sets the prime level to a specified value.
Index&
setPrime(int p);
// Sets the prime level to 0.
Index&
noPrime();
// Increase primelevel by 1 (or by optional amount inc)
Index&
prime(int inc = 1);
//Return an IndexVal with specified value
IndexVal
operator()(long val) const;
IndexVal
operator=(long val) const;
//define size()==dim() in order to do
//for(auto n : range(I)) { ... } for some Index I
long
size() const { return dim(); }
// Write Index to binary output stream.
void
write(std::ostream& s) const;
// Read Index from binary input stream.
Index&
read(std::istream& s);
//
// QN related functions
//
//number of quantum number blocks
long
nblock() const;
//1-indexed
long
blocksize(long i) const;
//1-indexed
QN const&
qn(long i) const;
Arrow
dir() const { return dir_; }
void
setDir(Arrow ndir) { dir_ = ndir; }
Index&
dag() { dir_ = -dir_; return *this; }
qn_ptr const&
store() const { return pd; }
Index&
sim(Index const& I)
{
*this = I;
id_ = generateID();
return *this;
}
private:
void
makeStorage(qnstorage && qi);
Index::id_type
generateID();
public:
//
// Advanced / developer methods.
// Not intended for normal usage.
//
// Constructor taking a QN pointer
Index(qn_ptr const& p,
TagSet const& tags = TagSet());
Index(qn_ptr const& p,
Arrow dir,
TagSet const& tags);
Index(id_type id,
long dim,
Arrow dir,
TagSet const& ts);
//0-indexed
long
blocksize0(long i) const;
void
removeQNs() { pd.reset(); }
public:
//Deprecated: prefer to use I.dim() or dim(I)
//long
//m() const
// {
// Global::warnDeprecated(".m() is deprecated in favor of dim(Index)");
// return this->dim();
// }
}; //class Index
// i1 compares equal to i2 if i2 is a copy of i1 with same primelevel
bool
operator==(Index const& i1, Index const& i2);
bool
operator!=(Index const& i1, Index const& i2);
// Useful for sorting Index objects
bool
operator<(Index const& i1, Index const& i2);
bool
operator>(Index const& i1, Index const& i2);
//
// IndexVal
//
// Class pairing an Index of dimension m
// with a specific value i where 1 <= i <= m
//
class IndexVal
{
public:
Index index;
long val;
IndexVal();
IndexVal(const Index& index, long val_);
long
dim() const { return index.dim(); }
explicit operator bool() const { return bool(index); }
// Add tags
IndexVal&
addTags(const TagSet& t) { index.addTags(t); return *this; }
// Remove tags
IndexVal&
removeTags(const TagSet& t) { index.removeTags(t); return *this; }
// Set tags
IndexVal&
setTags(const TagSet& t) { index.setTags(t); return *this; }
// Remove all tags
IndexVal&
noTags() { index.noTags(); return *this; }
// Set tags
IndexVal&
replaceTags(const TagSet& tsold, const TagSet& tsnew) { index.replaceTags(tsold,tsnew); return *this; }
// Sets the prime level to a specified value.
IndexVal&
setPrime(int p) { index.setPrime(p); return *this; }
// Sets the prime level to 0.
IndexVal&
noPrime() { index.noPrime(); return *this; }
// Increase primelevel by 1 (or by optional amount inc)
IndexVal&
prime(int inc = 1) { index.prime(inc); return *this; }
IndexVal&
dag();
QN const&
qn() const;
//Deprecated: prefer to use .dim()
long
m() const
{
Global::warnDeprecated(".m() is deprecated in favor of dim(IndexVal)");
return this->dim();
}
};
bool
operator==(IndexVal const& iv1, IndexVal const& iv2);
bool
operator!=(IndexVal const& iv1, IndexVal const& iv2);
bool
operator==(IndexVal const& iv, Index const& I);
bool
operator==(Index const& I, IndexVal const& iv);
Index::id_type
id(Index const& I);
long
dim(Index const& I);
long
dim(IndexVal const& I);
int
primeLevel(Index const& I);
TagSet
tags(Index const& I);
//
// Check if Index I contains the tags tsmatch.
//
bool
hasTags(Index I, TagSet const& tsmatch);
bool
hasQNs(Index const& I);
Index
removeQNs(Index I);
bool
hasQNs(IndexVal const& iv);
Index
dag(Index res);
IndexVal
dag(IndexVal res);
QN const&
qn(Index const& i, long b);
QN const&
qn(IndexVal const& iv);
Arrow
dir(Index const& res);
Arrow
dir(IndexVal const& res);
Index const&
index(IndexVal const& res);
long
val(IndexVal const& res);
long
nblock(Index const& i);
long
blocksize(Index const& i, long b);
//
// Tag functions
//
Index
addTags(Index I, TagSet const& t);
Index
removeTags(Index I, TagSet const& t);
Index
setTags(Index I, TagSet const& t);
Index
noTags(Index I);
Index
replaceTags(Index I, TagSet const& tsold, TagSet const& tsnew);
Index
prime(Index I, int plinc = 1);
Index
setPrime(Index I, int plev);
Index
noPrime(Index I);
IndexVal
addTags(IndexVal I, TagSet const& t);
IndexVal
removeTags(IndexVal I, TagSet const& t);
IndexVal
setTags(IndexVal I, TagSet const& t);
IndexVal
noTags(IndexVal I);
IndexVal
replaceTags(IndexVal I, TagSet const& tsold, TagSet const& tsnew);
IndexVal
prime(IndexVal I, int plinc = 1);
IndexVal
setPrime(IndexVal I, int plev);
IndexVal
noPrime(IndexVal I);
// Get the direct sum of two indices
Index
directSum(Index const& i,
Index const& j,
Args const& args = Args::global());
//Make a new index with same properties as I,
//but a different id number (will not compare equal)
Index
sim(Index const& I);
//Returns a string version of this Index's bond dimension.
std::string
showDim(Index const& I);
std::ostream&
operator<<(std::ostream & s, Index const& t);
std::ostream&
operator<<(std::ostream& s, IndexVal const& iv);
void
add(Args& args,
Args::Name const& name,
TagSet const& ts);
TagSet
getTagSet(Args const& args,
Args::Name const& name);
TagSet
getTagSet(Args const& args,
Args::Name const& name,
TagSet const& default_val);
long
QNblock(Index const& I,
QN const& Q);
long
QNblockSize(Index const& I,
QN const& Q);
void
write(std::ostream & s, QNInt const& q);
void
read(std::istream & s, QNInt & q);
bool
isFermionic(Index const& I);
#ifdef ITENSOR_USE_HDF5
void
h5_write(h5::group parent, std::string const& name, Index const& I);
void
h5_read(h5::group parent, std::string const& name, Index & I);
#endif
} //namespace itensor
#include "itensor/index_impl.h"
#endif