Cytnx v0.9.5
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 "cytnx_error.hpp"
6#include "Symmetry.hpp"
7#include <initializer_list>
8#include <vector>
9#include <fstream>
10#include <map>
11#include <algorithm>
13#include "utils/vec_clone.hpp"
14
15#ifdef BACKEND_TORCH
16#else
17namespace cytnx {
18
33 enum bondType : int {
34 BD_KET = -1,
35 BD_BRA = 1,
36 BD_REG = 0,
37 BD_NONE = 0,
38 BD_IN = -1,
39 BD_OUT = 1
40 };
41
43 class Bond_impl : public intrusive_ptr_base<Bond_impl> {
44 private:
45 public:
46 friend class Bond;
47 cytnx_uint64 _dim;
48 bondType _type;
49 std::vector<cytnx_uint64> _degs; // this only works for Qnum
50 /*
51 [Note], _degs has size only when the Bond is defined with Qnum, deg !!
52 Use this size to check if the bond is type-2 (new type)
53 */
54 std::vector<std::vector<cytnx_int64>> _qnums; //(dim, # of sym)
55 std::vector<Symmetry> _syms;
56
57 Bond_impl() : _dim(0), _type(bondType::BD_REG){};
58
59 void _rm_qnum(const cytnx_uint64 &q_index) {
60 // this will not check, so check it before using this internal function!!
61 this->_dim -= this->_degs[q_index];
62 this->_degs.erase(this->_degs.begin() + q_index);
63 this->_qnums.erase(this->_qnums.begin() + q_index);
64 }
65
66 void Init(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG);
67
68 // new added
69 void Init(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
70 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {});
71
72 bondType type() const { return this->_type; };
73 const std::vector<std::vector<cytnx_int64>> &qnums() const { return this->_qnums; }
74 std::vector<std::vector<cytnx_int64>> &qnums() { return this->_qnums; }
75 const cytnx_uint64 &dim() const { return this->_dim; }
76 cytnx_uint32 Nsym() const { return this->_syms.size(); }
77 const std::vector<Symmetry> &syms() const { return this->_syms; }
78 std::vector<Symmetry> &syms() { return this->_syms; }
79
80 // this is clone return.
81 std::vector<std::vector<cytnx_int64>> qnums_clone() const { return this->_qnums; }
82 std::vector<Symmetry> syms_clone() const { return vec_clone(this->_syms); }
83
84 bool has_duplicate_qnums() const {
85 if (this->_degs.size()) {
86 auto tmp = this->_qnums;
87 std::sort(tmp.begin(), tmp.end());
88 return std::adjacent_find(tmp.begin(), tmp.end()) != tmp.end();
89 } else {
90 return false;
91 }
92 }
93
94 void set_type(const bondType &new_bondType) {
95 if ((this->_type != BD_REG)) {
96 if (new_bondType == BD_REG) {
97 cytnx_error_msg(this->_qnums.size(),
98 "[ERROR] cannot change type to BD_REG for a symmetry bond.%s", "\n");
99 }
100 if (std::abs(int(this->_type)) != std::abs(int(new_bondType))) {
101 cytnx_error_msg(this->_qnums.size(),
102 "[ERROR] cannot exchange BDtype between BD_* <-> gBD_* .%s", "\n");
103 }
104 }
105
106 this->_type = new_bondType;
107 }
108
109 void clear_type() {
110 if (this->_type != BD_REG) {
111 cytnx_error_msg(this->_qnums.size(), "[ERROR] cannot clear type for a symmetry bond.%s",
112 "\n");
113 }
114 this->_type = bondType::BD_REG;
115 }
116
117 boost::intrusive_ptr<Bond_impl> clone() const {
118 boost::intrusive_ptr<Bond_impl> out(new Bond_impl());
119 out->_dim = this->dim();
120 out->_type = this->type();
121 out->_qnums = this->qnums_clone();
122 out->_syms = this->syms_clone(); // return a clone of vec!
123 out->_degs = this->_degs;
124 return out;
125 }
126
127 // [NOTE] for UniTensor iinternal, we might need to return the QNpool (unordered map for further
128 // info on block arrangement!)
129 void combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in, const bool &is_grp = true);
130
131 boost::intrusive_ptr<Bond_impl> combineBond(const boost::intrusive_ptr<Bond_impl> &bd_in,
132 const bool &is_grp = true) {
133 boost::intrusive_ptr<Bond_impl> out = this->clone();
134 out->combineBond_(bd_in, is_grp);
135 return out;
136 }
137
138 // return a sorted qnums by removing all duplicates, sorted from large to small.
139 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts,
140 const bool &return_counts);
141 // checked [KHW] ^^
142 // return the degeneracy of the specify qnum set.
143 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum, const bool &return_indices,
144 std::vector<cytnx_uint64> &indices);
145
146 // return the effective qnums when Bra-Ket mismatch.
147 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums();
148
149 std::vector<cytnx_uint64> &getDegeneracies() { return this->_degs; };
150 const std::vector<cytnx_uint64> &getDegeneracies() const { return this->_degs; };
151
152 std::vector<cytnx_uint64> group_duplicates_();
153
154 boost::intrusive_ptr<Bond_impl> group_duplicates(std::vector<cytnx_uint64> &mapper) const {
155 boost::intrusive_ptr<Bond_impl> out = this->clone();
156 mapper = out->group_duplicates_();
157 return out;
158 }
159
160 void force_combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in, const bool &is_grp);
161
162 }; // Bond_impl
164
178 class Bond {
179 public:
181 boost::intrusive_ptr<Bond_impl> _impl;
182 Bond() : _impl(new Bond_impl()){};
183 Bond(const Bond &rhs) { this->_impl = rhs._impl; }
184 Bond &operator=(const Bond &rhs) {
185 this->_impl = rhs._impl;
186 return *this;
187 }
189
203 : _impl(new Bond_impl()) {
204 this->_impl->Init(dim, bd_type);
205 }
206
227 Bond(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
228 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
229 : _impl(new Bond_impl()) {
230 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
231 }
232
240 Bond(const bondType &bd_type, const std::initializer_list<std::vector<cytnx_int64>> &in_qnums,
241 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
242 : _impl(new Bond_impl()) {
243 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
244 }
245
246 // this is needed for python binding!
254 Bond(const bondType &bd_type, const std::vector<cytnx::Qs> &in_qnums,
255 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
256 : _impl(new Bond_impl()) {
257 vec2d<cytnx_int64> qnums(in_qnums.begin(), in_qnums.end());
258 this->_impl->Init(bd_type, qnums, degs, in_syms);
259 }
260
268 Bond(const bondType &bd_type,
269 const std::vector<std::pair<std::vector<cytnx_int64>, cytnx_uint64>> &in_qnums_dims,
270 const std::vector<Symmetry> &in_syms = {})
271 : _impl(new Bond_impl()) {
272 this->Init(bd_type, in_qnums_dims, in_syms);
273 }
274
302 void Init(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG) {
303 this->_impl->Init(dim, bd_type);
304 }
305
341 void Init(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
342 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {}) {
343 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
344 }
345
351 void Init(const bondType &bd_type,
352 const std::vector<std::pair<std::vector<cytnx_int64>, cytnx_uint64>> &in_qnums_dims,
353 const std::vector<Symmetry> &in_syms = {}) {
354 vec2d<cytnx_int64> qnums(in_qnums_dims.size());
355 std::vector<cytnx_uint64> degs(in_qnums_dims.size());
356 for (int i = 0; i < in_qnums_dims.size(); i++) {
357 qnums[i] = in_qnums_dims[i].first;
358 degs[i] = in_qnums_dims[i].second;
359 }
360 this->_impl->Init(bd_type, qnums, degs, in_syms);
361 }
362
367 bondType type() const { return this->_impl->type(); };
368
370
375 const std::vector<std::vector<cytnx_int64>> &qnums() const { return this->_impl->qnums(); };
379 std::vector<std::vector<cytnx_int64>> &qnums() { return this->_impl->qnums(); };
381
387 std::vector<std::vector<cytnx_int64>> qnums_clone() const {
388 return this->_impl->qnums_clone();
389 };
390
395 cytnx_uint64 dim() const { return this->_impl->dim(); };
396
401 cytnx_uint32 Nsym() const { return this->_impl->syms().size(); };
402
404
409 const std::vector<Symmetry> &syms() const { return this->_impl->syms(); };
413 std::vector<Symmetry> &syms() { return this->_impl->syms(); };
415
421 std::vector<Symmetry> syms_clone() const { return this->_impl->syms_clone(); };
422
430 Bond &set_type(const bondType &new_bondType) {
431 this->_impl->set_type(new_bondType);
432 return *this;
433 }
434
444 Bond retype(const bondType &new_bondType) {
445 auto out = this->clone();
446 out.set_type(new_bondType);
447 return out;
448 }
449
454 Bond redirect() const {
455 auto out = this->clone();
456 out.set_type(bondType(int(out.type()) * -1));
457 return out;
458 }
459
465 this->set_type(bondType(int(this->type()) * -1));
466 return *this;
467 }
468
474 void clear_type() { this->_impl->clear_type(); }
475
490 Bond clone() const {
491 Bond out;
492 out._impl = this->_impl->clone();
493 return out;
494 }
495
522 void combineBond_(const Bond &bd_in, const bool &is_grp = true) {
523 this->_impl->combineBond_(bd_in._impl, is_grp);
524 }
525
553 Bond combineBond(const Bond &bd_in, const bool &is_grp = true) const {
554 Bond out;
555 out._impl = this->_impl->combineBond(bd_in._impl, is_grp);
556 return out;
557 }
558
586 Bond combineBonds(const std::vector<Bond> &bds, const bool &is_grp = true) {
587 Bond out = this->clone();
588 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
589 out.combineBond_(bds[i], is_grp);
590 }
591 return out;
592 }
593
620 void combineBonds_(const std::vector<Bond> &bds, const bool &is_grp = true) {
621 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
622 this->combineBond_(bds[i], is_grp);
623 }
624 }
625
633 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts) {
634 return this->_impl->getUniqueQnums(counts, true);
635 }
636
643 std::vector<std::vector<cytnx_int64>> getUniqueQnums() {
644 std::vector<cytnx_uint64> tmp;
645 return this->_impl->getUniqueQnums(tmp, false);
646 }
647
661 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum) const {
662 std::vector<cytnx_uint64> tmp;
663 return this->_impl->getDegeneracy(qnum, false, tmp);
664 }
665
679 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum,
680 std::vector<cytnx_uint64> &indices) const {
681 indices.clear();
682 return this->_impl->getDegeneracy(qnum, true, indices);
683 }
684
693 std::vector<cytnx_uint64> &getDegeneracies() { return this->_impl->getDegeneracies(); }
694
698 const std::vector<cytnx_uint64> &getDegeneracies() const {
699 return this->_impl->getDegeneracies();
700 }
701
718 std::vector<cytnx_uint64> group_duplicates_() { return this->_impl->group_duplicates_(); }
719
740 Bond group_duplicates(std::vector<cytnx_uint64> &mapper) const {
741 Bond out;
742 out._impl = this->_impl->group_duplicates(mapper);
743 return out;
744 }
745
756 bool has_duplicate_qnums() const { return this->_impl->has_duplicate_qnums(); }
757
766 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums() {
767 return this->_impl->calc_reverse_qnums();
768 }
769
777 void Save(const std::string &fname) const;
778
782 void Save(const char *fname) const;
783
790 static cytnx::Bond Load(const std::string &fname);
791
795 static cytnx::Bond Load(const char *fname);
796
798 void _Save(std::fstream &f) const;
799 void _Load(std::fstream &f);
801
810 bool operator==(const Bond &rhs) const;
811
818 bool operator!=(const Bond &rhs) const;
819
837 Bond operator*(const Bond &rhs) const { return this->combineBond(rhs); }
838
856 Bond &operator*=(const Bond &rhs) {
857 this->combineBond_(rhs);
858 return *this;
859 }
860 };
861
863 std::ostream &operator<<(std::ostream &os, const Bond &bin);
865} // namespace cytnx
866#endif // BACKEND_TORCH
867
868#endif
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:178
std::vector< std::vector< cytnx_int64 > > & qnums()
Definition Bond.hpp:379
Bond & set_type(const bondType &new_bondType)
change the tag-type of the instance Bond
Definition Bond.hpp:430
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:661
std::vector< cytnx_uint64 > group_duplicates_()
Group the duplicated quantum number, inplacely.
Definition Bond.hpp:718
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:351
Bond retype(const bondType &new_bondType)
create a new instance of Bond with type changed to the new tag-type.
Definition Bond.hpp:444
void combineBonds_(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:620
Bond & operator*=(const Bond &rhs)
The multiplication assignment operator of the Bond object.
Definition Bond.hpp:856
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:409
Bond(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
The constructor of the Bond object.
Definition Bond.hpp:202
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:553
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return the clone (deep copy) of the current quantum number set(s)
Definition Bond.hpp:387
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:586
Bond operator*(const Bond &rhs) const
The multiplication operator of the Bond object.
Definition Bond.hpp:837
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:740
void Init(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
init a bond object
Definition Bond.hpp:302
void clear_type()
change the tag-type to the default value bondType.BD_REG.
Definition Bond.hpp:474
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:254
Bond redirect() const
create a new instance of Bond with type changed in between bondType.BD_BRA / bondType....
Definition Bond.hpp:454
const std::vector< cytnx_uint64 > & getDegeneracies() const
Definition Bond.hpp:698
std::vector< cytnx_uint64 > & getDegeneracies()
return all degeneracies.
Definition Bond.hpp:693
bondType type() const
return the current bond type (see cytnx::bondType).
Definition Bond.hpp:367
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:490
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:679
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:421
Bond & redirect_()
Change the bond type between bondType.BD_BRA and bondType.BD_KET in the Bond.
Definition Bond.hpp:464
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:395
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:643
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:375
void combineBond_(const Bond &bd_in, const bool &is_grp=true)
Combine the input bond with self, inplacely.
Definition Bond.hpp:522
std::vector< Symmetry > & syms()
Definition Bond.hpp:413
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:268
void Save(const char *fname) const
cytnx_uint32 Nsym() const
return the number of the symmetries
Definition Bond.hpp:401
bool has_duplicate_qnums() const
Check whether there is duplicated quantum numbers in the Bond.
Definition Bond.hpp:756
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:633
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Calculate the reverse of the quantum numbers.
Definition Bond.hpp:766
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:227
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:341
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:240
bool operator==(const Bond &rhs) const
The comparison operator 'equal to'.
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Helper function to print vector with ODT:
Definition Accessor.hpp:12
uint32_t cytnx_uint32
Definition Type.hpp:56
uint64_t cytnx_uint64
Definition Type.hpp:55
std::vector< std::vector< T > > vec2d
Definition Type.hpp:51
bondType
bond type
Definition Bond.hpp:33
@ BD_IN
Definition Bond.hpp:38
@ BD_OUT
Definition Bond.hpp:39
@ BD_NONE
Definition Bond.hpp:37
@ BD_REG
Definition Bond.hpp:36
@ BD_BRA
Definition Bond.hpp:35
@ BD_KET
Definition Bond.hpp:34
tmp
Definition sp.py:8