Cytnx v0.9.1
Loading...
Searching...
No Matches
Bond.hpp
Go to the documentation of this file.
1#ifndef _H_Bond_
2#define _H_Bond_
3
4#include "Type.hpp"
5#include "Symmetry.hpp"
6#include "cytnx_error.hpp"
7#include <initializer_list>
8#include <vector>
9#include <fstream>
10#include <map>
11#include <algorithm>
13#include "utils/vec_clone.hpp"
14namespace cytnx {
15
30 enum bondType : int {
31 BD_KET = -1,
32 BD_BRA = 1,
33 BD_REG = 0,
34 BD_NONE = 0,
35 BD_IN = -1,
36 BD_OUT = 1
37 };
38
40 class Bond_impl : public intrusive_ptr_base<Bond_impl> {
41 private:
42 public:
43 friend class Bond;
44 cytnx_uint64 _dim;
45 bondType _type;
46 std::vector<cytnx_uint64> _degs; // this only works for Qnum
47 /*
48 [Note], _degs has size only when the Bond is defined with Qnum, deg !!
49 Use this size to check if the bond is type-2 (new type)
50 */
51 std::vector<std::vector<cytnx_int64>> _qnums; //(dim, # of sym)
52 std::vector<Symmetry> _syms;
53
54 Bond_impl() : _dim(0), _type(bondType::BD_REG){};
55
56 void _rm_qnum(const cytnx_uint64 &q_index) {
57 // this will not check, so check it before using this internal function!!
58 this->_dim -= this->_degs[q_index];
59 this->_degs.erase(this->_degs.begin() + q_index);
60 this->_qnums.erase(this->_qnums.begin() + q_index);
61 }
62
63 void Init(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG);
64
65 // new added
66 void Init(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
67 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {});
68
69 bondType type() const { return this->_type; };
70 const std::vector<std::vector<cytnx_int64>> &qnums() const { return this->_qnums; }
71 std::vector<std::vector<cytnx_int64>> &qnums() { return this->_qnums; }
72 const cytnx_uint64 &dim() const { return this->_dim; }
73 cytnx_uint32 Nsym() const { return this->_syms.size(); }
74 const std::vector<Symmetry> &syms() const { return this->_syms; }
75 std::vector<Symmetry> &syms() { return this->_syms; }
76
77 // this is clone return.
78 std::vector<std::vector<cytnx_int64>> qnums_clone() const { return this->_qnums; }
79 std::vector<Symmetry> syms_clone() const { return vec_clone(this->_syms); }
80
81 bool has_duplicate_qnums() const {
82 if (this->_degs.size()) {
83 auto tmp = this->_qnums;
84 std::sort(tmp.begin(), tmp.end());
85 return std::adjacent_find(tmp.begin(), tmp.end()) != tmp.end();
86 } else {
87 return false;
88 }
89 }
90
91 void set_type(const bondType &new_bondType) {
92 if ((this->_type != BD_REG)) {
93 if (new_bondType == BD_REG) {
94 cytnx_error_msg(this->_qnums.size(),
95 "[ERROR] cannot change type to BD_REG for a symmetry bond.%s", "\n");
96 }
97 if (std::abs(int(this->_type)) != std::abs(int(new_bondType))) {
98 cytnx_error_msg(this->_qnums.size(),
99 "[ERROR] cannot exchange BDtype between BD_* <-> gBD_* .%s", "\n");
100 }
101 }
102
103 this->_type = new_bondType;
104 }
105
106 void clear_type() {
107 if (this->_type != BD_REG) {
108 cytnx_error_msg(this->_qnums.size(), "[ERROR] cannot clear type for a symmetry bond.%s",
109 "\n");
110 }
111 this->_type = bondType::BD_REG;
112 }
113
114 boost::intrusive_ptr<Bond_impl> clone() const {
115 boost::intrusive_ptr<Bond_impl> out(new Bond_impl());
116 out->_dim = this->dim();
117 out->_type = this->type();
118 out->_qnums = this->qnums_clone();
119 out->_syms = this->syms_clone(); // return a clone of vec!
120 out->_degs = this->_degs;
121 return out;
122 }
123
124 // [NOTE] for UniTensor iinternal, we might need to return the QNpool (unordered map for further
125 // info on block arrangement!)
126 void combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in, const bool &is_grp = true);
127
128 boost::intrusive_ptr<Bond_impl> combineBond(const boost::intrusive_ptr<Bond_impl> &bd_in,
129 const bool &is_grp = true) {
130 boost::intrusive_ptr<Bond_impl> out = this->clone();
131 out->combineBond_(bd_in, is_grp);
132 return out;
133 }
134
135 // return a sorted qnums by removing all duplicates, sorted from large to small.
136 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts,
137 const bool &return_counts);
138 // checked [KHW] ^^
139 // return the degeneracy of the specify qnum set.
140 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum, const bool &return_indices,
141 std::vector<cytnx_uint64> &indices);
142
143 // return the effective qnums when Bra-Ket mismatch.
144 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums();
145
146 std::vector<cytnx_uint64> &getDegeneracies() { return this->_degs; };
147 const std::vector<cytnx_uint64> &getDegeneracies() const { return this->_degs; };
148
149 std::vector<cytnx_uint64> group_duplicates_();
150
151 boost::intrusive_ptr<Bond_impl> group_duplicates(std::vector<cytnx_uint64> &mapper) const {
152 boost::intrusive_ptr<Bond_impl> out = this->clone();
153 mapper = out->group_duplicates_();
154 return out;
155 }
156
157 void force_combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in, const bool &is_grp);
158
159 }; // Bond_impl
161
175 class Bond {
176 public:
178 boost::intrusive_ptr<Bond_impl> _impl;
179 Bond() : _impl(new Bond_impl()){};
180 Bond(const Bond &rhs) { this->_impl = rhs._impl; }
181 Bond &operator=(const Bond &rhs) {
182 this->_impl = rhs._impl;
183 return *this;
184 }
186
200 : _impl(new Bond_impl()) {
201 this->_impl->Init(dim, bd_type);
202 }
203
224 Bond(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
225 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
226 : _impl(new Bond_impl()) {
227 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
228 }
229
237 Bond(const bondType &bd_type, const std::initializer_list<std::vector<cytnx_int64>> &in_qnums,
238 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
239 : _impl(new Bond_impl()) {
240 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
241 }
242
243 // this is needed for python binding!
251 Bond(const bondType &bd_type, const std::vector<cytnx::Qs> &in_qnums,
252 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
253 : _impl(new Bond_impl()) {
254 vec2d<cytnx_int64> qnums(in_qnums.begin(), in_qnums.end());
255 this->_impl->Init(bd_type, qnums, degs, in_syms);
256 }
257
265 Bond(const bondType &bd_type,
266 const std::vector<std::pair<std::vector<cytnx_int64>, cytnx_uint64>> &in_qnums_dims,
267 const std::vector<Symmetry> &in_syms = {})
268 : _impl(new Bond_impl()) {
269 this->Init(bd_type, in_qnums_dims, in_syms);
270 }
271
299 void Init(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG) {
300 this->_impl->Init(dim, bd_type);
301 }
302
338 void Init(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
339 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {}) {
340 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
341 }
342
348 void Init(const bondType &bd_type,
349 const std::vector<std::pair<std::vector<cytnx_int64>, cytnx_uint64>> &in_qnums_dims,
350 const std::vector<Symmetry> &in_syms = {}) {
351 vec2d<cytnx_int64> qnums(in_qnums_dims.size());
352 std::vector<cytnx_uint64> degs(in_qnums_dims.size());
353 for (int i = 0; i < in_qnums_dims.size(); i++) {
354 qnums[i] = in_qnums_dims[i].first;
355 degs[i] = in_qnums_dims[i].second;
356 }
357 this->_impl->Init(bd_type, qnums, degs, in_syms);
358 }
359
364 bondType type() const { return this->_impl->type(); };
365
367
372 const std::vector<std::vector<cytnx_int64>> &qnums() const { return this->_impl->qnums(); };
376 std::vector<std::vector<cytnx_int64>> &qnums() { return this->_impl->qnums(); };
378
384 std::vector<std::vector<cytnx_int64>> qnums_clone() const {
385 return this->_impl->qnums_clone();
386 };
387
392 cytnx_uint64 dim() const { return this->_impl->dim(); };
393
398 cytnx_uint32 Nsym() const { return this->_impl->syms().size(); };
399
401
406 const std::vector<Symmetry> &syms() const { return this->_impl->syms(); };
410 std::vector<Symmetry> &syms() { return this->_impl->syms(); };
412
418 std::vector<Symmetry> syms_clone() const { return this->_impl->syms_clone(); };
419
427 Bond &set_type(const bondType &new_bondType) {
428 this->_impl->set_type(new_bondType);
429 return *this;
430 }
431
441 Bond retype(const bondType &new_bondType) {
442 auto out = this->clone();
443 out.set_type(new_bondType);
444 return out;
445 }
446
451 Bond redirect() const {
452 auto out = this->clone();
453 out.set_type(bondType(int(out.type()) * -1));
454 return out;
455 }
456
462 this->set_type(bondType(int(this->type()) * -1));
463 return *this;
464 }
465
471 void clear_type() { this->_impl->clear_type(); }
472
487 Bond clone() const {
488 Bond out;
489 out._impl = this->_impl->clone();
490 return out;
491 }
492
519 void combineBond_(const Bond &bd_in, const bool &is_grp = true) {
520 this->_impl->combineBond_(bd_in._impl, is_grp);
521 }
522
550 Bond combineBond(const Bond &bd_in, const bool &is_grp = true) const {
551 Bond out;
552 out._impl = this->_impl->combineBond(bd_in._impl, is_grp);
553 return out;
554 }
555
583 Bond combineBonds(const std::vector<Bond> &bds, const bool &is_grp = true) {
584 Bond out = this->clone();
585 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
586 out.combineBond_(bds[i], is_grp);
587 }
588 return out;
589 }
590
617 void combineBonds_(const std::vector<Bond> &bds, const bool &is_grp = true) {
618 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
619 this->combineBond_(bds[i], is_grp);
620 }
621 }
622
630 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts) {
631 return this->_impl->getUniqueQnums(counts, true);
632 }
633
640 std::vector<std::vector<cytnx_int64>> getUniqueQnums() {
641 std::vector<cytnx_uint64> tmp;
642 return this->_impl->getUniqueQnums(tmp, false);
643 }
644
658 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum) const {
659 std::vector<cytnx_uint64> tmp;
660 return this->_impl->getDegeneracy(qnum, false, tmp);
661 }
662
676 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum,
677 std::vector<cytnx_uint64> &indices) const {
678 indices.clear();
679 return this->_impl->getDegeneracy(qnum, true, indices);
680 }
681
690 std::vector<cytnx_uint64> &getDegeneracies() { return this->_impl->getDegeneracies(); }
691
695 const std::vector<cytnx_uint64> &getDegeneracies() const {
696 return this->_impl->getDegeneracies();
697 }
698
715 std::vector<cytnx_uint64> group_duplicates_() { return this->_impl->group_duplicates_(); }
716
737 Bond group_duplicates(std::vector<cytnx_uint64> &mapper) const {
738 Bond out;
739 out._impl = this->_impl->group_duplicates(mapper);
740 return out;
741 }
742
753 bool has_duplicate_qnums() const { return this->_impl->has_duplicate_qnums(); }
754
763 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums() {
764 return this->_impl->calc_reverse_qnums();
765 }
766
774 void Save(const std::string &fname) const;
775
779 void Save(const char *fname) const;
780
787 static cytnx::Bond Load(const std::string &fname);
788
792 static cytnx::Bond Load(const char *fname);
793
795 void _Save(std::fstream &f) const;
796 void _Load(std::fstream &f);
798
807 bool operator==(const Bond &rhs) const;
808
815 bool operator!=(const Bond &rhs) const;
816
834 Bond operator*(const Bond &rhs) const { return this->combineBond(rhs); }
835
853 Bond &operator*=(const Bond &rhs) {
854 this->combineBond_(rhs);
855 return *this;
856 }
857 };
858
860 std::ostream &operator<<(std::ostream &os, const Bond &bin);
862} // namespace cytnx
863
864#endif
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:175
std::vector< std::vector< cytnx_int64 > > & qnums()
Definition Bond.hpp:376
Bond & set_type(const bondType &new_bondType)
change the tag-type of the instance Bond
Definition Bond.hpp:427
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:658
std::vector< cytnx_uint64 > group_duplicates_()
Group the duplicated quantum number, inplacely.
Definition Bond.hpp:715
static cytnx::Bond Load(const std::string &fname)
Load the Bond object from the file.
void Init(const bondType &bd_type, const std::vector< std::pair< std::vector< cytnx_int64 >, cytnx_uint64 > > &in_qnums_dims, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:348
Bond retype(const bondType &new_bondType)
create a new instance of Bond with type changed to the new tag-type.
Definition Bond.hpp:441
void combineBonds_(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:617
Bond & operator*=(const Bond &rhs)
The multiplication assignment operator of the Bond object.
Definition Bond.hpp:853
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:406
Bond(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
The constructor of the Bond object.
Definition Bond.hpp:199
Bond combineBond(const Bond &bd_in, const bool &is_grp=true) const
combine the input bond with self, and return a new combined Bond instance.
Definition Bond.hpp:550
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return the clone (deep copy) of the current quantum number set(s)
Definition Bond.hpp:384
Bond combineBonds(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, and return a new combined Bond instance.
Definition Bond.hpp:583
Bond operator*(const Bond &rhs) const
The multiplication operator of the Bond object.
Definition Bond.hpp:834
Bond group_duplicates(std::vector< cytnx_uint64 > &mapper) const
Group the duplicated quantum number and return the new instance of the Bond ojbect.
Definition Bond.hpp:737
void Init(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
init a bond object
Definition Bond.hpp:299
void clear_type()
change the tag-type to the default value bondType.BD_REG.
Definition Bond.hpp:471
Bond(const bondType &bd_type, const std::vector< cytnx::Qs > &in_qnums, const std::vector< cytnx_uint64 > &degs, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:251
Bond redirect() const
create a new instance of Bond with type changed in between bondType.BD_BRA / bondType....
Definition Bond.hpp:451
const std::vector< cytnx_uint64 > & getDegeneracies() const
Definition Bond.hpp:695
std::vector< cytnx_uint64 > & getDegeneracies()
return all degeneracies.
Definition Bond.hpp:690
bondType type() const
return the current bond type (see cytnx::bondType).
Definition Bond.hpp:364
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:487
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum, std::vector< cytnx_uint64 > &indices) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:676
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:418
Bond & redirect_()
Change the bond type between bondType.BD_BRA and bondType.BD_KET in the Bond.
Definition Bond.hpp:461
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:392
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:640
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:372
void combineBond_(const Bond &bd_in, const bool &is_grp=true)
Combine the input bond with self, inplacely.
Definition Bond.hpp:519
std::vector< Symmetry > & syms()
Definition Bond.hpp:410
Bond(const bondType &bd_type, const std::vector< std::pair< std::vector< cytnx_int64 >, cytnx_uint64 > > &in_qnums_dims, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:265
void Save(const char *fname) const
cytnx_uint32 Nsym() const
return the number of the symmetries
Definition Bond.hpp:398
bool has_duplicate_qnums() const
Check whether there is duplicated quantum numbers in the Bond.
Definition Bond.hpp:753
std::vector< std::vector< cytnx_int64 > > getUniqueQnums(std::vector< cytnx_uint64 > &counts)
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:630
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Calculate the reverse of the quantum numbers.
Definition Bond.hpp:763
Bond(const bondType &bd_type, const std::vector< std::vector< cytnx_int64 > > &in_qnums, const std::vector< cytnx_uint64 > &degs, const std::vector< Symmetry > &in_syms={})
The constructor of the Bond object.
Definition Bond.hpp:224
bool operator!=(const Bond &rhs) const
The comparison operator 'not equal to'.
void Init(const bondType &bd_type, const std::vector< std::vector< cytnx_int64 > > &in_qnums, const std::vector< cytnx_uint64 > &degs, const std::vector< Symmetry > &in_syms={})
init a bond object
Definition Bond.hpp:338
void Save(const std::string &fname) const
Save the Bond object to the file.
static cytnx::Bond Load(const char *fname)
Bond(const bondType &bd_type, const std::initializer_list< std::vector< cytnx_int64 > > &in_qnums, const std::vector< cytnx_uint64 > &degs, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:237
bool operator==(const Bond &rhs) const
The comparison operator 'equal to'.
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
uint32_t cytnx_uint32
Definition Type.hpp:46
uint64_t cytnx_uint64
Definition Type.hpp:45
std::vector< std::vector< T > > vec2d
Definition Type.hpp:41
bondType
bond type
Definition Bond.hpp:30
@ BD_IN
Definition Bond.hpp:35
@ BD_OUT
Definition Bond.hpp:36
@ BD_NONE
Definition Bond.hpp:34
@ BD_REG
Definition Bond.hpp:33
@ BD_BRA
Definition Bond.hpp:32
@ BD_KET
Definition Bond.hpp:31
tmp
Definition sp.py:8