Cytnx v0.7.6
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>
11#include "utils/vec_clone.hpp"
12namespace cytnx{
13 enum bondType: int{
14 BD_KET = -1,
15 BD_BRA = 1,
18 BD_IN = -1,
19 BD_OUT = 1
20 };
22 class Bond_impl: public intrusive_ptr_base<Bond_impl>{
23 private:
24
25 public:
26 friend class Bond;
27 cytnx_uint64 _dim;
28 bondType _type;
29 std::vector< std::vector<cytnx_int64> > _qnums; //(dim, # of sym)
30 std::vector<Symmetry> _syms;
31
32 Bond_impl(): _type(bondType::BD_REG) {};
33
34 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={});
35
36
37 bondType type() const{return this->_type;};
38 const std::vector<std::vector<cytnx_int64> >& qnums() const{return this->_qnums;}
39 std::vector<std::vector<cytnx_int64> >& qnums(){return this->_qnums;}
40 const cytnx_uint64& dim() const{return this->_dim;}
41 cytnx_uint32 Nsym() const{return this->_syms.size();}
42 const std::vector<Symmetry>& syms() const{return this->_syms;}
43 std::vector<Symmetry>& syms(){return this->_syms;}
44
45 //this is clone return.
46 std::vector<std::vector<cytnx_int64> > qnums_clone() const{return this->_qnums;}
47 std::vector<Symmetry> syms_clone() const{return vec_clone(this->_syms);}
48
49
50 void set_type(const bondType &new_bondType){
51 if((this->_type != BD_REG) && (new_bondType == BD_REG)){
52 cytnx_error_msg(this->_qnums.size(),"[ERROR] cannot change type to BD_REG for a symmetry bond.%s","\n");
53 }
54 this->_type = new_bondType;
55 }
56
57 void clear_type(){
58 if(this->_type != BD_REG){
59 cytnx_error_msg(this->_qnums.size(),"[ERROR] cannot clear type for a symmetry bond.%s","\n");
60 }
61 this->_type = bondType::BD_REG;
62 }
63
64
65 boost::intrusive_ptr<Bond_impl> clone(){
66 boost::intrusive_ptr<Bond_impl> out(new Bond_impl());
67 out->_dim = this->dim();
68 out->_type = this->type();
69 out->_qnums = this->qnums_clone();
70 out->_syms = this->syms_clone();// return a clone of vec!
71 return out;
72 }
73
74 void combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in);
75
76
77 boost::intrusive_ptr<Bond_impl> combineBond(const boost::intrusive_ptr<Bond_impl> &bd_in){
78 boost::intrusive_ptr<Bond_impl> out = this->clone();
79 out->combineBond_(bd_in);
80 return out;
81 }
82
83 // return a sorted qnums by removing all duplicates, sorted from large to small.
84 std::vector<std::vector<cytnx_int64> > getUniqueQnums(std::vector<cytnx_uint64> &counts, const bool &return_counts);
85
86 // return the degeneracy of the specify qnum set.
87 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum, const bool &return_indices,std::vector<cytnx_uint64> &indices);
88
89 // return the effective qnums when Bra-Ket mismatch.
90 std::vector<std::vector<cytnx_int64> > calc_reverse_qnums();
91
92
93 };//Bond_impl
95
97 class Bond{
98 public:
100 boost::intrusive_ptr<Bond_impl> _impl;
101 Bond(): _impl(new Bond_impl()){};
102 Bond(const Bond&rhs){this->_impl = rhs._impl;}
103 Bond& operator=(const Bond &rhs){this->_impl = rhs._impl; return *this;}
105
106 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={}): _impl(new Bond_impl()){
107 this->_impl->Init(dim,bd_type,in_qnums,in_syms);
108 }
109
135 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={}){
136 this->_impl->Init(dim,bd_type,in_qnums,in_syms);
137 }
138
144 bondType type() const{return this->_impl->type();};
145
147
153 const std::vector<std::vector<cytnx_int64> >& qnums() const{return this->_impl->qnums();};
154 std::vector<std::vector<cytnx_int64> >& qnums(){return this->_impl->qnums();};
156
162 std::vector<std::vector<cytnx_int64> > qnums_clone() const{return this->_impl->qnums_clone();};
163
169 cytnx_uint64 dim() const{return this->_impl->dim();};
170
176 cytnx_uint32 Nsym() const{return this->_impl->syms().size();};
177
178
180
185 const std::vector<Symmetry>& syms() const{return this->_impl->syms();};
186 std::vector<Symmetry>& syms(){return this->_impl->syms();};
188
194 std::vector<Symmetry> syms_clone() const{return this->_impl->syms_clone();};
195
196
197
198
204 Bond& set_type(const bondType &new_bondType){
205 this->_impl->set_type(new_bondType);
206 return *this;
207 }
208
217 Bond retype(const bondType &new_bondType){
218 auto out = this->clone();
219 out.set_type(new_bondType);
220 return out;
221 }
222
227 Bond redirect() const{
228 auto out = this->clone();
229 out.set_type(bondType(int(out.type())*-1));
230 return out;
231 }
232
233
234
240 this->_impl->clear_type();
241 }
242
257 Bond clone() const{
258 Bond out;
259 out._impl = this->_impl->clone();
260 return out;
261 }
262
263
278 void combineBond_(const Bond &bd_in){
279 this->_impl->combineBond_(bd_in._impl);
280 }
281
297 Bond combineBond(const Bond &bd_in) const{
298 Bond out;
299 out._impl = this->_impl->combineBond(bd_in._impl);
300 return out;
301 }
302
318 Bond combineBonds(const std::vector<Bond> &bds){
319 Bond out = this->clone();
320 for(cytnx_uint64 i=0;i<bds.size();i++){
321 out.combineBond_(bds[i]);
322 }
323 return out;
324 }
325
340 void combineBonds_(const std::vector<Bond> &bds){
341 for(cytnx_uint64 i=0;i<bds.size();i++){
342 this->combineBond_(bds[i]);
343 }
344 }
345
351 std::vector<std::vector<cytnx_int64> > getUniqueQnums(std::vector<cytnx_uint64> &counts){
352 return this->_impl->getUniqueQnums(counts,true);
353 }
354 std::vector<std::vector<cytnx_int64> > getUniqueQnums(){
355 std::vector<cytnx_uint64> tmp;
356 return this->_impl->getUniqueQnums(tmp,false);
357 }
358
367 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64>& qnum) const{
368 std::vector<cytnx_uint64> tmp;
369 return this->_impl->getDegeneracy(qnum,false,tmp);
370 }
371 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64>& qnum, std::vector<cytnx_uint64> &indices) const{
372 indices.clear();
373 return this->_impl->getDegeneracy(qnum,true,indices);
374 }
375
376 std::vector<std::vector<cytnx_int64> > calc_reverse_qnums(){
377 return this->_impl->calc_reverse_qnums();
378 }
379
380
381 void Save(const std::string &fname) const;
382 void Save(const char* fname) const;
383 static cytnx::Bond Load(const std::string &fname);
384 static cytnx::Bond Load(const char* fname);
385
386
388 void _Save(std::fstream &f) const;
389 void _Load(std::fstream &f);
391
392 bool operator==(const Bond &rhs) const;
393 bool operator!=(const Bond &rhs) const;
394
395 Bond operator*(const Bond &rhs) const{
396 return this->combineBond(rhs);
397 }
398
399 Bond& operator*=(const Bond &rhs){
400 this->combineBond_(rhs);
401 return *this;
402 }
403
404
405
406 };
407
409 std::ostream& operator<<(std::ostream &os,const Bond &bin);
411}
412
413
414#endif
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:97
std::vector< std::vector< cytnx_int64 > > & qnums()
Definition Bond.hpp:154
Bond & set_type(const bondType &new_bondType)
change the tag-type of the instance Bond
Definition Bond.hpp:204
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:367
void combineBonds_(const std::vector< Bond > &bds)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:340
Bond retype(const bondType &new_bondType)
create a new instance of Bond with type changed to the new tag-type:
Definition Bond.hpp:217
Bond & operator*=(const Bond &rhs)
Definition Bond.hpp:399
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:185
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return copy of the current quantum number set(s)
Definition Bond.hpp:162
Bond combineBonds(const std::vector< Bond > &bds)
combine multiple input bonds with self, and return a new combined Bond instance.
Definition Bond.hpp:318
static cytnx::Bond Load(const std::string &fname)
Definition Bond.cpp:240
Bond operator*(const Bond &rhs) const
Definition Bond.hpp:395
void clear_type()
change the tag-type to the default value BD_REG
Definition Bond.hpp:239
Bond redirect() const
create a new instance of Bond with type changed in btwn BRA / KET:
Definition Bond.hpp:227
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:135
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={})
Definition Bond.hpp:106
bondType type() const
return the current tag type
Definition Bond.hpp:144
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:257
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum, std::vector< cytnx_uint64 > &indices) const
Definition Bond.hpp:371
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:194
Bond combineBond(const Bond &bd_in) const
combine the input bond with self, and return a new combined Bond instance.
Definition Bond.hpp:297
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:169
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
Definition Bond.hpp:354
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:153
std::vector< Symmetry > & syms()
Definition Bond.hpp:186
void combineBond_(const Bond &bd_in)
combine the input bond with self, inplacely
Definition Bond.hpp:278
cytnx_uint32 Nsym() const
return the number of symmetries
Definition Bond.hpp:176
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:351
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Definition Bond.hpp:376
bool operator!=(const Bond &rhs) const
Definition Bond.cpp:215
void Save(const std::string &fname) const
Definition Bond.cpp:220
bool operator==(const Bond &rhs) const
Definition Bond.cpp:203
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:18
Definition Accessor.hpp:12
uint32_t cytnx_uint32
Definition Type.hpp:23
std::ostream & operator<<(std::ostream &os, const Scalar &in)
Definition Scalar.cpp:14
uint64_t cytnx_uint64
Definition Type.hpp:22
bondType
Definition Bond.hpp:13
@ BD_IN
Definition Bond.hpp:18
@ BD_OUT
Definition Bond.hpp:19
@ BD_NONE
Definition Bond.hpp:17
@ BD_REG
Definition Bond.hpp:16
@ BD_BRA
Definition Bond.hpp:15
@ BD_KET
Definition Bond.hpp:14