Cytnx v1.0.0
Loading...
Searching...
No Matches
Bond.hpp
Go to the documentation of this file.
1#ifndef CYTNX_BOND_H_
2#define CYTNX_BOND_H_
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
15namespace cytnx {
16
31 enum bondType : int {
32 BD_KET = -1,
33 BD_BRA = 1,
34 BD_REG = 0,
35 BD_NONE = 0,
36 BD_IN = -1,
37 BD_OUT = 1
38 };
39
41 class Bond_impl : public intrusive_ptr_base<Bond_impl> {
42 private:
43 public:
44 friend class Bond;
45 cytnx_uint64 _dim;
46 bondType _type;
47 std::vector<cytnx_uint64> _degs; // this only works for Qnum
48 /*
49 [Note], _degs has size only when the Bond is defined with Qnum, deg !!
50 Use this size to check if the bond is type-2 (new type)
51 */
52 std::vector<std::vector<cytnx_int64>> _qnums; //(dim, # of sym)
53 std::vector<Symmetry> _syms;
54
55 Bond_impl() : _dim(0), _type(bondType::BD_REG){};
56
57 void _rm_qnum(const cytnx_uint64 &q_index) {
58 // this will not check, so check it before using this internal function!!
59 this->_dim -= this->_degs[q_index];
60 this->_degs.erase(this->_degs.begin() + q_index);
61 this->_qnums.erase(this->_qnums.begin() + q_index);
62 }
63
64 void Init(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG);
65
66 // new added
67 void Init(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
68 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {});
69
70 bondType type() const { return this->_type; };
71 const std::vector<std::vector<cytnx_int64>> &qnums() const { return this->_qnums; }
72 std::vector<std::vector<cytnx_int64>> &qnums() { return this->_qnums; }
73 const cytnx_uint64 &dim() const { return this->_dim; }
74 cytnx_uint32 Nsym() const { return this->_syms.size(); }
75 const std::vector<Symmetry> &syms() const { return this->_syms; }
76 std::vector<Symmetry> &syms() { return this->_syms; }
77
78 // this is clone return.
79 std::vector<std::vector<cytnx_int64>> qnums_clone() const { return this->_qnums; }
80 std::vector<Symmetry> syms_clone() const { return vec_clone(this->_syms); }
81
82 bool has_duplicate_qnums() const {
83 if (this->_degs.size()) {
84 auto tmp = this->_qnums;
85 std::sort(tmp.begin(), tmp.end());
86 return std::adjacent_find(tmp.begin(), tmp.end()) != tmp.end();
87 } else {
88 return false;
89 }
90 }
91
92 void set_type(const bondType &new_bondType) {
93 if ((this->_type != BD_REG)) {
94 if (new_bondType == BD_REG) {
95 cytnx_error_msg(this->_qnums.size(),
96 "[ERROR] Cannot change type to BD_REG for a symmetry bond.%s", "\n");
97 }
98 if (std::abs(int(this->_type)) != std::abs(int(new_bondType))) {
99 cytnx_error_msg(this->_qnums.size(),
100 "[ERROR] Cannot exchange BDtype between BD_* <-> gBD_* .%s", "\n");
101 }
102 }
103
104 this->_type = new_bondType;
105 }
106
107 void clear_type() {
108 if (this->_type != BD_REG) {
109 cytnx_error_msg(this->_qnums.size(), "[ERROR] Cannot clear type for a symmetry bond.%s",
110 "\n");
111 }
112 this->_type = bondType::BD_REG;
113 }
114
115 boost::intrusive_ptr<Bond_impl> clone() const {
116 boost::intrusive_ptr<Bond_impl> out(new Bond_impl());
117 out->_dim = this->dim();
118 out->_type = this->type();
119 out->_qnums = this->qnums_clone();
120 out->_syms = this->syms_clone(); // return a clone of vec!
121 out->_degs = this->_degs;
122 return out;
123 }
124
125 // [NOTE] for UniTensor internal, we might need to return the QNpool (unordered map for further
126 // info on block arrangement!)
127 void combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in, const bool &is_grp = true);
128
129 boost::intrusive_ptr<Bond_impl> combineBond(const boost::intrusive_ptr<Bond_impl> &bd_in,
130 const bool &is_grp = true) {
131 boost::intrusive_ptr<Bond_impl> out = this->clone();
132 out->combineBond_(bd_in, is_grp);
133 return out;
134 }
135
136 // return a sorted qnums by removing all duplicates, sorted from large to small.
137 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts,
138 const bool &return_counts);
139 // checked [KHW] ^^
140 // return the degeneracy of the specify qnum set.
141 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum, const bool &return_indices,
142 std::vector<cytnx_uint64> &indices);
143
144 // return the effective qnums when Bra-Ket mismatch.
145 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums();
146
147 std::vector<cytnx_uint64> &getDegeneracies() { return this->_degs; };
148 const std::vector<cytnx_uint64> &getDegeneracies() const { return this->_degs; };
149
150 fermionParity get_fermion_parity(const std::vector<cytnx_int64> &qnum);
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
202 Bond(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG)
203 : _impl(new Bond_impl()) {
204 this->_impl->Init(dim, bd_type);
205 }
206
228 Bond(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
229 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
230 : _impl(new Bond_impl()) {
231 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
232 }
233
241 Bond(const bondType &bd_type, const std::initializer_list<std::vector<cytnx_int64>> &in_qnums,
242 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
243 : _impl(new Bond_impl()) {
244 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
245 }
246
247 // this is needed for python binding!
255 Bond(const bondType &bd_type, const std::vector<cytnx::Qs> &in_qnums,
256 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
257 : _impl(new Bond_impl()) {
258 vec2d<cytnx_int64> qnums(in_qnums.begin(), in_qnums.end());
259 this->_impl->Init(bd_type, qnums, degs, in_syms);
260 }
261
269 Bond(const bondType &bd_type,
270 const std::vector<std::pair<std::vector<cytnx_int64>, cytnx_uint64>> &in_qnums_dims,
271 const std::vector<Symmetry> &in_syms = {})
272 : _impl(new Bond_impl()) {
273 this->Init(bd_type, in_qnums_dims, in_syms);
274 }
275
303 void Init(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG) {
304 this->_impl->Init(dim, bd_type);
305 }
306
342 void Init(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
343 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {}) {
344 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
345 }
346
352 void Init(const bondType &bd_type,
353 const std::vector<std::pair<std::vector<cytnx_int64>, cytnx_uint64>> &in_qnums_dims,
354 const std::vector<Symmetry> &in_syms = {}) {
355 vec2d<cytnx_int64> qnums(in_qnums_dims.size());
356 std::vector<cytnx_uint64> degs(in_qnums_dims.size());
357 for (int i = 0; i < in_qnums_dims.size(); i++) {
358 qnums[i] = in_qnums_dims[i].first;
359 degs[i] = in_qnums_dims[i].second;
360 }
361 this->_impl->Init(bd_type, qnums, degs, in_syms);
362 }
363
368 bondType type() const { return this->_impl->type(); };
369
375 const std::vector<std::vector<cytnx_int64>> &qnums() const { return this->_impl->qnums(); };
376
382 std::vector<std::vector<cytnx_int64>> qnums_clone() const {
383 return this->_impl->qnums_clone();
384 };
385
390 cytnx_uint64 dim() const { return this->_impl->dim(); };
391
396 cytnx_uint32 Nsym() const { return this->_impl->syms().size(); };
397
403 const std::vector<Symmetry> &syms() const { return this->_impl->syms(); };
404
410 std::vector<Symmetry> syms_clone() const { return this->_impl->syms_clone(); };
411
421 Bond retype(const bondType &new_bondType) const {
422 Bond out = this->clone();
423 out._impl->set_type(new_bondType);
424 return out;
425 }
426
431 Bond redirect() const {
432 Bond out = this->clone();
433 out._impl->set_type(bondType(int(out.type()) * -1));
434 return out;
435 }
436
451 Bond clone() const {
452 Bond out;
453 out._impl = this->_impl->clone();
454 return out;
455 }
456
481 Bond combineBond(const Bond &bd_in, const bool &is_grp = true) const {
482 Bond out;
483 out._impl = this->_impl->combineBond(bd_in._impl, is_grp);
484 return out;
485 }
486
511 Bond combineBond(const std::vector<Bond> &bds, const bool &is_grp = true) const {
512 Bond out = this->clone();
513 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
514 out._impl->combineBond_(bds[i]._impl, is_grp);
515 }
516 return out;
517 }
518
546 [[deprecated(
547 "Please use "
548 "Bond combineBond(const Bond &bd_in, const bool &is_grp) const "
549 "instead.")]] Bond
550 combineBonds(const std::vector<Bond> &bds, const bool &is_grp = true) const {
551 Bond out = this->clone();
552 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
553 out._impl->combineBond_(bds[i]._impl, is_grp);
554 }
555 return out;
556 }
557
565 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts) {
566 return this->_impl->getUniqueQnums(counts, true);
567 }
568
575 std::vector<std::vector<cytnx_int64>> getUniqueQnums() {
576 std::vector<cytnx_uint64> tmp;
577 return this->_impl->getUniqueQnums(tmp, false);
578 }
579
593 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum) const {
594 std::vector<cytnx_uint64> tmp;
595 return this->_impl->getDegeneracy(qnum, false, tmp);
596 }
597
611 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum,
612 std::vector<cytnx_uint64> &indices) const {
613 indices.clear();
614 return this->_impl->getDegeneracy(qnum, true, indices);
615 }
616
624 const std::vector<cytnx_uint64> &getDegeneracies() const {
625 return this->_impl->getDegeneracies();
626 }
627
633 fermionParity get_fermion_parity(const std::vector<cytnx_int64> &qnum) {
634 return this->_impl->get_fermion_parity(qnum);
635 }
636
652 Bond group_duplicates(std::vector<cytnx_uint64> &mapper) const {
653 Bond out;
654 out._impl = this->_impl->group_duplicates(mapper);
655 return out;
656 }
657
666 bool has_duplicate_qnums() const { return this->_impl->has_duplicate_qnums(); }
667
676 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums() {
677 return this->_impl->calc_reverse_qnums();
678 }
679
687 void Save(const std::string &fname) const;
688
692 void Save(const char *fname) const;
693
700 static cytnx::Bond Load(const std::string &fname);
701
705 static cytnx::Bond Load(const char *fname);
706
708 void _Save(std::fstream &f) const;
709 void _Load(std::fstream &f);
711
720 bool operator==(const Bond &rhs) const;
721
728 bool operator!=(const Bond &rhs) const;
729
746 Bond operator*(const Bond &rhs) const { return this->combineBond(rhs); }
747 };
748
750 std::ostream &operator<<(std::ostream &os, const Bond &bin);
752} // namespace cytnx
753
754#endif // CYTNX_BOND_H_
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:178
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:593
Bond combineBonds(const std::vector< Bond > &bds, const bool &is_grp=true) const
combine multiple input bonds with self, and return a new combined Bond instance.
Definition Bond.hpp:550
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:352
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:403
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:481
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return the clone (deep copy) of the current quantum number set(s)
Definition Bond.hpp:382
Bond operator*(const Bond &rhs) const
The multiplication operator of the Bond object.
Definition Bond.hpp:746
Bond group_duplicates(std::vector< cytnx_uint64 > &mapper) const
Group the duplicated quantum number and return the new instance of the Bond object.
Definition Bond.hpp:652
void Init(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
init a bond object
Definition Bond.hpp:303
Bond retype(const bondType &new_bondType) const
create a new instance of Bond with type changed to the new tag-type.
Definition Bond.hpp:421
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:255
Bond redirect() const
create a new instance of Bond with type changed in between bondType.BD_BRA / bondType....
Definition Bond.hpp:431
const std::vector< cytnx_uint64 > & getDegeneracies() const
return all degeneracies.
Definition Bond.hpp:624
fermionParity get_fermion_parity(const std::vector< cytnx_int64 > &qnum)
return fermionic parity for a given quantum number
Definition Bond.hpp:633
bondType type() const
return the current bond type (see cytnx::bondType).
Definition Bond.hpp:368
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:451
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:611
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:410
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:390
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:575
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:375
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:269
void Save(const char *fname) const
cytnx_uint32 Nsym() const
return the number of the symmetries
Definition Bond.hpp:396
bool has_duplicate_qnums() const
Check whether there is duplicated quantum numbers in the Bond.
Definition Bond.hpp:666
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:565
Bond combineBond(const std::vector< Bond > &bds, const bool &is_grp=true) const
combine multiple input bonds with self, and return a new combined Bond instance.
Definition Bond.hpp:511
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Calculate the reverse of the quantum numbers.
Definition Bond.hpp:676
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:228
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:342
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:241
bool operator==(const Bond &rhs) const
The comparison operator 'equal to'.
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:115
Definition Accessor.hpp:12
bondType
bond type
Definition Bond.hpp:31
@ BD_IN
Definition Bond.hpp:36
@ BD_OUT
Definition Bond.hpp:37
@ BD_NONE
Definition Bond.hpp:35
@ BD_REG
Definition Bond.hpp:34
@ BD_BRA
Definition Bond.hpp:33
@ BD_KET
Definition Bond.hpp:32
fermionParity
fermionParity
Definition Symmetry.hpp:38