Cytnx v0.7.4
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,
16 BD_REG =0
17 };
19 class Bond_impl: public intrusive_ptr_base<Bond_impl>{
20 private:
21
22 public:
23 friend class Bond;
24 cytnx_uint64 _dim;
25 bondType _type;
26 std::vector< std::vector<cytnx_int64> > _qnums; //(dim, # of sym)
27 std::vector<Symmetry> _syms;
28
29 Bond_impl(): _type(bondType::BD_REG) {};
30
31 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={});
32
33
34 bondType type() const{return this->_type;};
35 const std::vector<std::vector<cytnx_int64> >& qnums() const{return this->_qnums;}
36 std::vector<std::vector<cytnx_int64> >& qnums(){return this->_qnums;}
37 const cytnx_uint64& dim() const{return this->_dim;}
38 cytnx_uint32 Nsym() const{return this->_syms.size();}
39 const std::vector<Symmetry>& syms() const{return this->_syms;}
40 std::vector<Symmetry>& syms(){return this->_syms;}
41
42 //this is clone return.
43 std::vector<std::vector<cytnx_int64> > qnums_clone() const{return this->_qnums;}
44 std::vector<Symmetry> syms_clone() const{return vec_clone(this->_syms);}
45
46
47 void set_type(const bondType &new_bondType){
48 if((this->_type != BD_REG) && (new_bondType == BD_REG)){
49 cytnx_error_msg(this->_qnums.size(),"[ERROR] cannot change type to BD_REG for a symmetry bond.%s","\n");
50 }
51 this->_type = new_bondType;
52 }
53
54 void clear_type(){
55 if(this->_type != BD_REG){
56 cytnx_error_msg(this->_qnums.size(),"[ERROR] cannot clear type for a symmetry bond.%s","\n");
57 }
58 this->_type = bondType::BD_REG;
59 }
60
61
62 boost::intrusive_ptr<Bond_impl> clone(){
63 boost::intrusive_ptr<Bond_impl> out(new Bond_impl());
64 out->_dim = this->dim();
65 out->_type = this->type();
66 out->_qnums = this->qnums_clone();
67 out->_syms = this->syms_clone();// return a clone of vec!
68 return out;
69 }
70
71 void combineBond_(const boost::intrusive_ptr<Bond_impl> &bd_in);
72
73
74 boost::intrusive_ptr<Bond_impl> combineBond(const boost::intrusive_ptr<Bond_impl> &bd_in){
75 boost::intrusive_ptr<Bond_impl> out = this->clone();
76 out->combineBond_(bd_in);
77 return out;
78 }
79
80 // return a sorted qnums by removing all duplicates, sorted from large to small.
81 std::vector<std::vector<cytnx_int64> > getUniqueQnums(std::vector<cytnx_uint64> &counts, const bool &return_counts);
82
83 // return the degeneracy of the specify qnum set.
84 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64> &qnum, const bool &return_indices,std::vector<cytnx_uint64> &indices);
85
86 // return the effective qnums when Bra-Ket mismatch.
87 std::vector<std::vector<cytnx_int64> > calc_reverse_qnums();
88
89
90 };//Bond_impl
92
94 class Bond{
95 public:
97 boost::intrusive_ptr<Bond_impl> _impl;
98 Bond(): _impl(new Bond_impl()){};
99 Bond(const Bond&rhs){this->_impl = rhs._impl;}
100 Bond& operator=(const Bond &rhs){this->_impl = rhs._impl; return *this;}
102
103 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()){
104 this->_impl->Init(dim,bd_type,in_qnums,in_syms);
105 }
106
132 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={}){
133 this->_impl->Init(dim,bd_type,in_qnums,in_syms);
134 }
135
141 bondType type() const{return this->_impl->type();};
142
144
150 const std::vector<std::vector<cytnx_int64> >& qnums() const{return this->_impl->qnums();};
151 std::vector<std::vector<cytnx_int64> >& qnums(){return this->_impl->qnums();};
153
159 std::vector<std::vector<cytnx_int64> > qnums_clone() const{return this->_impl->qnums_clone();};
160
166 cytnx_uint64 dim() const{return this->_impl->dim();};
167
173 cytnx_uint32 Nsym() const{return this->_impl->syms().size();};
174
175
177
182 const std::vector<Symmetry>& syms() const{return this->_impl->syms();};
183 std::vector<Symmetry>& syms(){return this->_impl->syms();};
185
191 std::vector<Symmetry> syms_clone() const{return this->_impl->syms_clone();};
192
193
194
195
201 Bond& set_type(const bondType &new_bondType){
202 this->_impl->set_type(new_bondType);
203 return *this;
204 }
205
214 Bond retype(const bondType &new_bondType){
215 auto out = this->clone();
216 out.set_type(new_bondType);
217 return out;
218 }
219
224 Bond redirect() const{
225 auto out = this->clone();
226 out.set_type(bondType(int(out.type())*-1));
227 return out;
228 }
229
230
231
237 this->_impl->clear_type();
238 }
239
254 Bond clone() const{
255 Bond out;
256 out._impl = this->_impl->clone();
257 return out;
258 }
259
260
275 void combineBond_(const Bond &bd_in){
276 this->_impl->combineBond_(bd_in._impl);
277 }
278
294 Bond combineBond(const Bond &bd_in) const{
295 Bond out;
296 out._impl = this->_impl->combineBond(bd_in._impl);
297 return out;
298 }
299
315 Bond combineBonds(const std::vector<Bond> &bds){
316 Bond out = this->clone();
317 for(cytnx_uint64 i=0;i<bds.size();i++){
318 out.combineBond_(bds[i]);
319 }
320 return out;
321 }
322
337 void combineBonds_(const std::vector<Bond> &bds){
338 for(cytnx_uint64 i=0;i<bds.size();i++){
339 this->combineBond_(bds[i]);
340 }
341 }
342
348 std::vector<std::vector<cytnx_int64> > getUniqueQnums(std::vector<cytnx_uint64> &counts){
349 return this->_impl->getUniqueQnums(counts,true);
350 }
351 std::vector<std::vector<cytnx_int64> > getUniqueQnums(){
352 std::vector<cytnx_uint64> tmp;
353 return this->_impl->getUniqueQnums(tmp,false);
354 }
355
364 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64>& qnum) const{
365 std::vector<cytnx_uint64> tmp;
366 return this->_impl->getDegeneracy(qnum,false,tmp);
367 }
368 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64>& qnum, std::vector<cytnx_uint64> &indices) const{
369 indices.clear();
370 return this->_impl->getDegeneracy(qnum,true,indices);
371 }
372
373 std::vector<std::vector<cytnx_int64> > calc_reverse_qnums(){
374 return this->_impl->calc_reverse_qnums();
375 }
376
377
378 void Save(const std::string &fname) const;
379 void Save(const char* fname) const;
380 static cytnx::Bond Load(const std::string &fname);
381 static cytnx::Bond Load(const char* fname);
382
383
385 void _Save(std::fstream &f) const;
386 void _Load(std::fstream &f);
388
389 bool operator==(const Bond &rhs) const;
390 bool operator!=(const Bond &rhs) const;
391
392 Bond operator*(const Bond &rhs) const{
393 return this->combineBond(rhs);
394 }
395
396 Bond& operator*=(const Bond &rhs){
397 this->combineBond_(rhs);
398 return *this;
399 }
400
401
402
403 };
404
406 std::ostream& operator<<(std::ostream &os,const Bond &bin);
408}
409
410
411#endif
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:94
std::vector< std::vector< cytnx_int64 > > & qnums()
Definition Bond.hpp:151
Bond & set_type(const bondType &new_bondType)
change the tag-type of the instance Bond
Definition Bond.hpp:201
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:364
void combineBonds_(const std::vector< Bond > &bds)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:337
Bond retype(const bondType &new_bondType)
create a new instance of Bond with type changed to the new tag-type:
Definition Bond.hpp:214
Bond & operator*=(const Bond &rhs)
Definition Bond.hpp:396
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:182
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return copy of the current quantum number set(s)
Definition Bond.hpp:159
Bond combineBonds(const std::vector< Bond > &bds)
combine multiple input bonds with self, and return a new combined Bond instance.
Definition Bond.hpp:315
static cytnx::Bond Load(const std::string &fname)
Definition Bond.cpp:238
Bond operator*(const Bond &rhs) const
Definition Bond.hpp:392
void clear_type()
change the tag-type to the default value BD_REG
Definition Bond.hpp:236
Bond redirect() const
create a new instance of Bond with type changed in btwn BRA / KET:
Definition Bond.hpp:224
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:132
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:103
bondType type() const
return the current tag type
Definition Bond.hpp:141
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:254
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum, std::vector< cytnx_uint64 > &indices) const
Definition Bond.hpp:368
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:191
Bond combineBond(const Bond &bd_in) const
combine the input bond with self, and return a new combined Bond instance.
Definition Bond.hpp:294
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:166
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
Definition Bond.hpp:351
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:150
std::vector< Symmetry > & syms()
Definition Bond.hpp:183
void combineBond_(const Bond &bd_in)
combine the input bond with self, inplacely
Definition Bond.hpp:275
cytnx_uint32 Nsym() const
return the number of symmetries
Definition Bond.hpp:173
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:348
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Definition Bond.hpp:373
bool operator!=(const Bond &rhs) const
Definition Bond.cpp:213
void Save(const std::string &fname) const
Definition Bond.cpp:218
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_REG
Definition Bond.hpp:16
@ BD_BRA
Definition Bond.hpp:15
@ BD_KET
Definition Bond.hpp:14