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
26 enum bondType : int {
27 BD_KET = -1,
28 BD_BRA = 1,
29 BD_REG = 0,
30 BD_NONE = 0,
31 BD_IN = -1,
32 BD_OUT = 1
33 };
34
36 class Bond_impl : public intrusive_ptr_base<Bond_impl> {
37 private:
38 public:
39 friend class Bond;
40 cytnx_uint64 _dim;
41 bondType _type;
42 std::vector<cytnx_uint64> _degs; // this only works for Qnum
43 /*
44 [Note], _degs has size only when the Bond is defined with Qnum, deg !!
45 Use this size to check if the bond is type-2 (new type)
46 */
47 std::vector<std::vector<cytnx_int64>> _qnums; //(dim, # of sym)
48 std::vector<Symmetry> _syms;
49
50 Bond_impl() : _dim(0), _type(bondType::BD_REG){};
51
52 void _rm_qnum(const cytnx_uint64 &q_index){
53 // this will not check, so check it before using this internal function!!
54 this->_dim -= this->_degs[q_index];
55 this->_degs.erase(this->_degs.begin() + q_index);
56 this->_qnums.erase(this->_qnums.begin() + q_index);
57 }
58
59 void Init(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG,
60 const std::vector<std::vector<cytnx_int64>> &in_qnums = {},
61 const std::vector<Symmetry> &in_syms = {});
62
63 // new added
64 void Init(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
65 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {});
66
67 bondType type() const { return this->_type; };
68 const std::vector<std::vector<cytnx_int64>> &qnums() const { return this->_qnums; }
69 std::vector<std::vector<cytnx_int64>> &qnums() { return this->_qnums; }
70 const cytnx_uint64 &dim() const { return this->_dim; }
71 cytnx_uint32 Nsym() const { return this->_syms.size(); }
72 const std::vector<Symmetry> &syms() const { return this->_syms; }
73 std::vector<Symmetry> &syms() { return this->_syms; }
74
75 // this is clone return.
76 std::vector<std::vector<cytnx_int64>> qnums_clone() const { return this->_qnums; }
77 std::vector<Symmetry> syms_clone() const { return vec_clone(this->_syms); }
78
79 bool has_duplicate_qnums() const{
80 if(this->_degs.size()){
81 auto tmp = this->_qnums;
82 std::sort(tmp.begin(),tmp.end());
83 return std::adjacent_find(tmp.begin(), tmp.end()) != tmp.end();
84 }else{
85 return false;
86 }
87 }
88
89 void set_type(const bondType &new_bondType) {
90 if ((this->_type != BD_REG)) {
91 if (new_bondType == BD_REG) {
92 cytnx_error_msg(this->_qnums.size(),
93 "[ERROR] cannot change type to BD_REG for a symmetry bond.%s", "\n");
94 }
95 if (std::abs(int(this->_type)) != std::abs(int(new_bondType))) {
96 cytnx_error_msg(this->_qnums.size(),
97 "[ERROR] cannot exchange BDtype between BD_* <-> gBD_* .%s", "\n");
98 }
99 }
100
101 this->_type = new_bondType;
102 }
103
104 void clear_type() {
105 if (this->_type != BD_REG) {
106 cytnx_error_msg(this->_qnums.size(), "[ERROR] cannot clear type for a symmetry bond.%s",
107 "\n");
108 }
109 this->_type = bondType::BD_REG;
110 }
111
112 boost::intrusive_ptr<Bond_impl> clone() const {
113 boost::intrusive_ptr<Bond_impl> out(new Bond_impl());
114 out->_dim = this->dim();
115 out->_type = this->type();
116 out->_qnums = this->qnums_clone();
117 out->_syms = this->syms_clone(); // return a clone of vec!
118 out->_degs = this->_degs;
119 return out;
120 }
121
122 // [NOTE] for UniTensor iinternal, we might need to return the QNpool (unordered map for further
123 // info on block arrangement!)
124 void combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in, const bool &is_grp = true);
125
126 boost::intrusive_ptr<Bond_impl> combineBond(const boost::intrusive_ptr<Bond_impl> &bd_in,
127 const bool &is_grp = true) {
128 boost::intrusive_ptr<Bond_impl> out = this->clone();
129 out->combineBond_(bd_in, is_grp);
130 return out;
131 }
132
133 // return a sorted qnums by removing all duplicates, sorted from large to small.
134 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts,
135 const bool &return_counts);
136 // checked [KHW] ^^
137 // return the degeneracy of the specify qnum set.
138 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum, const bool &return_indices,
139 std::vector<cytnx_uint64> &indices);
140
141 // return the effective qnums when Bra-Ket mismatch.
142 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums();
143
144
145 std::vector<cytnx_uint64>& getDegeneracies(){ return this->_degs;};
146 const std::vector<cytnx_uint64>& getDegeneracies() const{return this->_degs;};
147
148 std::vector<cytnx_uint64> group_duplicates_();
149
150 boost::intrusive_ptr<Bond_impl> group_duplicates(std::vector<cytnx_uint64> &mapper) const{
151 boost::intrusive_ptr<Bond_impl> out = this->clone();
152 mapper = out->group_duplicates_();
153 return out;
154 }
155
156 void force_combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in, const bool &is_grp);
157
158
159
160 }; // Bond_impl
162
174 class Bond {
175 public:
177 boost::intrusive_ptr<Bond_impl> _impl;
178 Bond() : _impl(new Bond_impl()){};
179 Bond(const Bond &rhs) { this->_impl = rhs._impl; }
180 Bond &operator=(const Bond &rhs) {
181 this->_impl = rhs._impl;
182 return *this;
183 }
185
210 const std::vector<std::vector<cytnx_int64>> &in_qnums = {},
211 const std::vector<Symmetry> &in_syms = {})
212 : _impl(new Bond_impl()) {
213 this->_impl->Init(dim, bd_type, in_qnums, in_syms);
214 }
215
236 Bond(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
237 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
238 : _impl(new Bond_impl()) {
239 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
240 }
241
242
250 Bond(const bondType &bd_type, const std::initializer_list<std::vector<cytnx_int64>> &in_qnums,
251 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
252 : _impl(new Bond_impl()) {
253 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
254 }
255
256 // this is needed for python binding!
264 Bond(const bondType &bd_type, const std::vector<cytnx::Qs> &in_qnums,
265 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {})
266 : _impl(new Bond_impl()) {
267 vec2d<cytnx_int64> qnums(in_qnums.begin(),in_qnums.end());
268 this->_impl->Init(bd_type, qnums, degs, in_syms);
269 }
270
278 Bond(const bondType &bd_type,
279 const std::vector< std::pair<std::vector<cytnx_int64>, cytnx_uint64> > &in_qnums_dims,
280 const std::vector<Symmetry> &in_syms = {})
281 : _impl(new Bond_impl()) {
282 this->Init(bd_type, in_qnums_dims, in_syms);
283 }
284
285
325 void Init(const cytnx_uint64 &dim, const bondType &bd_type = bondType::BD_REG,
326 const std::vector<std::vector<cytnx_int64>> &in_qnums = {},
327 const std::vector<Symmetry> &in_syms = {}) {
328 this->_impl->Init(dim, bd_type, in_qnums, in_syms);
329 }
330
366 void Init(const bondType &bd_type, const std::vector<std::vector<cytnx_int64>> &in_qnums,
367 const std::vector<cytnx_uint64> &degs, const std::vector<Symmetry> &in_syms = {}) {
368 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
369 }
370
376 void Init(const bondType &bd_type,
377 const std::vector< std::pair<std::vector<cytnx_int64>, cytnx_uint64> > &in_qnums_dims,
378 const std::vector<Symmetry> &in_syms = {}) {
379 vec2d<cytnx_int64> qnums(in_qnums_dims.size());
380 std::vector<cytnx_uint64> degs(in_qnums_dims.size());
381 for(int i=0;i<in_qnums_dims.size();i++){
382 qnums[i] = in_qnums_dims[i].first;
383 degs[i] = in_qnums_dims[i].second;
384 }
385 this->_impl->Init(bd_type, qnums, degs, in_syms);
386 }
387
392 bondType type() const { return this->_impl->type(); };
393
395
400 const std::vector<std::vector<cytnx_int64>> &qnums() const { return this->_impl->qnums(); };
404 std::vector<std::vector<cytnx_int64>> &qnums() { return this->_impl->qnums(); };
406
412 std::vector<std::vector<cytnx_int64>> qnums_clone() const {
413 return this->_impl->qnums_clone();
414 };
415
420 cytnx_uint64 dim() const { return this->_impl->dim(); };
421
426 cytnx_uint32 Nsym() const { return this->_impl->syms().size(); };
427
429
434 const std::vector<Symmetry> &syms() const { return this->_impl->syms(); };
438 std::vector<Symmetry> &syms() { return this->_impl->syms(); };
440
446 std::vector<Symmetry> syms_clone() const { return this->_impl->syms_clone(); };
447
455 Bond &set_type(const bondType &new_bondType) {
456 this->_impl->set_type(new_bondType);
457 return *this;
458 }
459
469 Bond retype(const bondType &new_bondType) {
470 auto out = this->clone();
471 out.set_type(new_bondType);
472 return out;
473 }
474
479 Bond redirect() const {
480 auto out = this->clone();
481 out.set_type(bondType(int(out.type()) * -1));
482 return out;
483 }
484
490 this->set_type(bondType(int(this->type()) * -1));
491 return *this;
492 }
493
499 void clear_type() { this->_impl->clear_type(); }
500
515 Bond clone() const {
516 Bond out;
517 out._impl = this->_impl->clone();
518 return out;
519 }
520
543 void combineBond_(const Bond &bd_in, const bool &is_grp = true) {
544 this->_impl->combineBond_(bd_in._impl, is_grp);
545 }
546
570 Bond combineBond(const Bond &bd_in, const bool &is_grp = true) const {
571 Bond out;
572 out._impl = this->_impl->combineBond(bd_in._impl, is_grp);
573 return out;
574 }
575
599 Bond combineBonds(const std::vector<Bond> &bds, const bool &is_grp = true) {
600 Bond out = this->clone();
601 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
602 out.combineBond_(bds[i], is_grp);
603 }
604 return out;
605 }
606
629 void combineBonds_(const std::vector<Bond> &bds, const bool &is_grp = true) {
630 for (cytnx_uint64 i = 0; i < bds.size(); i++) {
631 this->combineBond_(bds[i], is_grp);
632 }
633 }
634
642 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts) {
643 return this->_impl->getUniqueQnums(counts, true);
644 }
645
652 std::vector<std::vector<cytnx_int64>> getUniqueQnums() {
653 std::vector<cytnx_uint64> tmp;
654 return this->_impl->getUniqueQnums(tmp, false);
655 }
656
670 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum) const {
671 std::vector<cytnx_uint64> tmp;
672 return this->_impl->getDegeneracy(qnum, false, tmp);
673 }
674
688 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum,
689 std::vector<cytnx_uint64> &indices) const {
690 indices.clear();
691 return this->_impl->getDegeneracy(qnum, true, indices);
692 }
693
702 std::vector<cytnx_uint64> & getDegeneracies(){
703 return this->_impl->getDegeneracies();
704 }
705
709 const std::vector<cytnx_uint64> & getDegeneracies() const{
710 return this->_impl->getDegeneracies();
711 }
712
729 std::vector<cytnx_uint64> group_duplicates_(){
730 return this->_impl->group_duplicates_();
731 }
732
753 Bond group_duplicates(std::vector<cytnx_uint64> &mapper) const{
754 Bond out;
755 out._impl = this->_impl->group_duplicates(mapper);
756 return out;
757 }
758
769 bool has_duplicate_qnums() const {
770 return this->_impl->has_duplicate_qnums();
771 }
772
781 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums() {
782 return this->_impl->calc_reverse_qnums();
783 }
784
791 void Save(const std::string &fname) const;
792
796 void Save(const char *fname) const;
797
803 static cytnx::Bond Load(const std::string &fname);
804
808 static cytnx::Bond Load(const char *fname);
809
811 void _Save(std::fstream &f) const;
812 void _Load(std::fstream &f);
814
821 bool operator==(const Bond &rhs) const;
822
829 bool operator!=(const Bond &rhs) const;
830
848 Bond operator*(const Bond &rhs) const { return this->combineBond(rhs); }
849
867 Bond &operator*=(const Bond &rhs) {
868 this->combineBond_(rhs);
869 return *this;
870 }
871 };
872
874 std::ostream &operator<<(std::ostream &os, const Bond &bin);
876} // namespace cytnx
877
878#endif
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:174
std::vector< std::vector< cytnx_int64 > > & qnums()
Definition Bond.hpp:404
Bond & set_type(const bondType &new_bondType)
change the tag-type of the instance Bond
Definition Bond.hpp:455
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:670
std::vector< cytnx_uint64 > group_duplicates_()
Group the duplicated quantum number, inplacely.
Definition Bond.hpp:729
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:376
Bond retype(const bondType &new_bondType)
create a new instance of Bond with type changed to the new tag-type.
Definition Bond.hpp:469
void combineBonds_(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:629
Bond & operator*=(const Bond &rhs)
The multiplication assignment operator of the Bond object.
Definition Bond.hpp:867
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:434
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:570
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return the clone (deep copy) of the current quantum number set(s)
Definition Bond.hpp:412
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:599
static cytnx::Bond Load(const std::string &fname)
Load the Bond object from the file.
Definition Bond.cpp:604
Bond operator*(const Bond &rhs) const
The multiplication operator of the Bond object.
Definition Bond.hpp:848
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:753
void clear_type()
change the tag-type to the default value bondType.BD_REG.
Definition Bond.hpp:499
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:264
Bond redirect() const
create a new instance of Bond with type changed in between bondType.BD_BRA / bondType....
Definition Bond.hpp:479
void Init(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG, const std::vector< std::vector< cytnx_int64 > > &in_qnums={}, const std::vector< Symmetry > &in_syms={})
init a bond object
Definition Bond.hpp:325
const std::vector< cytnx_uint64 > & getDegeneracies() const
Definition Bond.hpp:709
std::vector< cytnx_uint64 > & getDegeneracies()
return all degeneracies.
Definition Bond.hpp:702
Bond(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG, const std::vector< std::vector< cytnx_int64 > > &in_qnums={}, const std::vector< Symmetry > &in_syms={})
The constructor of the Bond object.
Definition Bond.hpp:209
bondType type() const
return the current tag type
Definition Bond.hpp:392
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:515
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:688
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:446
Bond & redirect_()
Change the bond type between bondType.BD_BRA and bondType.BD_KET in the Bond.
Definition Bond.hpp:489
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:420
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:652
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:400
void combineBond_(const Bond &bd_in, const bool &is_grp=true)
Combine the input bond with self, inplacely.
Definition Bond.hpp:543
std::vector< Symmetry > & syms()
Definition Bond.hpp:438
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:278
cytnx_uint32 Nsym() const
return the number of the symmetries
Definition Bond.hpp:426
bool has_duplicate_qnums() const
Check whether there is duplicated quantum numbers in the Bond.
Definition Bond.hpp:769
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:642
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Calculate the reverse of the quantum numbers.
Definition Bond.hpp:781
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:236
bool operator!=(const Bond &rhs) const
The comparison operator 'not equal to'.
Definition Bond.cpp:582
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:366
void Save(const std::string &fname) const
Save the Bond object to the file.
Definition Bond.cpp:584
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:250
bool operator==(const Bond &rhs) const
The comparison operator 'equal to'.
Definition Bond.cpp:563
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
uint32_t cytnx_uint32
Definition Type.hpp:46
std::ostream & operator<<(std::ostream &os, const Scalar &in)
The stream operator for Scalar objects.
Definition Scalar.cpp:10
uint64_t cytnx_uint64
Definition Type.hpp:45
std::vector< std::vector< T > > vec2d
Definition Type.hpp:40
bondType
bond type
Definition Bond.hpp:26
@ BD_IN
Definition Bond.hpp:31
@ BD_OUT
Definition Bond.hpp:32
@ BD_NONE
Definition Bond.hpp:30
@ BD_REG
Definition Bond.hpp:29
@ BD_BRA
Definition Bond.hpp:28
@ BD_KET
Definition Bond.hpp:27
tmp
Definition sp.py:8