Cytnx v0.7.3
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
211 this->_impl->clear_type();
212 }
213
228 Bond clone() const{
229 Bond out;
230 out._impl = this->_impl->clone();
231 return out;
232 }
233
234
249 void combineBond_(const Bond &bd_in){
250 this->_impl->combineBond_(bd_in._impl);
251 }
252
268 Bond combineBond(const Bond &bd_in) const{
269 Bond out;
270 out._impl = this->_impl->combineBond(bd_in._impl);
271 return out;
272 }
273
289 Bond combineBonds(const std::vector<Bond> &bds){
290 Bond out = this->clone();
291 for(cytnx_uint64 i=0;i<bds.size();i++){
292 out.combineBond_(bds[i]);
293 }
294 return out;
295 }
296
311 void combineBonds_(const std::vector<Bond> &bds){
312 for(cytnx_uint64 i=0;i<bds.size();i++){
313 this->combineBond_(bds[i]);
314 }
315 }
316
322 std::vector<std::vector<cytnx_int64> > getUniqueQnums(std::vector<cytnx_uint64> &counts){
323 return this->_impl->getUniqueQnums(counts,true);
324 }
325 std::vector<std::vector<cytnx_int64> > getUniqueQnums(){
326 std::vector<cytnx_uint64> tmp;
327 return this->_impl->getUniqueQnums(tmp,false);
328 }
329
338 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64>& qnum) const{
339 std::vector<cytnx_uint64> tmp;
340 return this->_impl->getDegeneracy(qnum,false,tmp);
341 }
342 cytnx_uint64 getDegeneracy(const std::vector<cytnx_int64>& qnum, std::vector<cytnx_uint64> &indices) const{
343 indices.clear();
344 return this->_impl->getDegeneracy(qnum,true,indices);
345 }
346
347 std::vector<std::vector<cytnx_int64> > calc_reverse_qnums(){
348 return this->_impl->calc_reverse_qnums();
349 }
350
351
352 void Save(const std::string &fname) const;
353 void Save(const char* fname) const;
354 static cytnx::Bond Load(const std::string &fname);
355 static cytnx::Bond Load(const char* fname);
356
357
359 void _Save(std::fstream &f) const;
360 void _Load(std::fstream &f);
362
363 bool operator==(const Bond &rhs) const;
364 bool operator!=(const Bond &rhs) const;
365
366 Bond operator*(const Bond &rhs) const{
367 return this->combineBond(rhs);
368 }
369
370 Bond& operator*=(const Bond &rhs){
371 this->combineBond_(rhs);
372 return *this;
373 }
374
375
376
377 };
378
380 std::ostream& operator<<(std::ostream &os,const Bond &bin);
382}
383
384
385#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:338
void combineBonds_(const std::vector< Bond > &bds)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:311
Bond & operator*=(const Bond &rhs)
Definition Bond.hpp:370
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:289
static cytnx::Bond Load(const std::string &fname)
Definition Bond.cpp:237
Bond operator*(const Bond &rhs) const
Definition Bond.hpp:366
void clear_type()
change the tag-type to the default value BD_REG
Definition Bond.hpp:210
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:228
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum, std::vector< cytnx_uint64 > &indices) const
Definition Bond.hpp:342
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:268
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:166
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
Definition Bond.hpp:325
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:249
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:322
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Definition Bond.hpp:347
bool operator!=(const Bond &rhs) const
Definition Bond.cpp:212
void Save(const std::string &fname) const
Definition Bond.cpp:217
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