7#include <initializer_list>
13#include "utils/vec_clone.hpp"
40 class Bond_impl :
public intrusive_ptr_base<Bond_impl> {
46 std::vector<cytnx_uint64> _degs;
51 std::vector<std::vector<cytnx_int64>> _qnums;
52 std::vector<Symmetry> _syms;
54 Bond_impl() : _dim(0), _type(bondType::BD_REG){};
56 void _rm_qnum(
const cytnx_uint64 &q_index) {
58 this->_dim -= this->_degs[q_index];
59 this->_degs.erase(this->_degs.begin() + q_index);
60 this->_qnums.erase(this->_qnums.begin() + q_index);
63 void Init(
const cytnx_uint64 &dim,
const bondType &bd_type = bondType::BD_REG);
66 void Init(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
67 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {});
69 bondType type()
const {
return this->_type; };
70 const std::vector<std::vector<cytnx_int64>> &qnums()
const {
return this->_qnums; }
71 std::vector<std::vector<cytnx_int64>> &qnums() {
return this->_qnums; }
74 const std::vector<Symmetry> &syms()
const {
return this->_syms; }
75 std::vector<Symmetry> &syms() {
return this->_syms; }
78 std::vector<std::vector<cytnx_int64>> qnums_clone()
const {
return this->_qnums; }
79 std::vector<Symmetry> syms_clone()
const {
return vec_clone(this->_syms); }
81 bool has_duplicate_qnums()
const {
82 if (this->_degs.size()) {
83 auto tmp = this->_qnums;
84 std::sort(
tmp.begin(),
tmp.end());
85 return std::adjacent_find(
tmp.begin(),
tmp.end()) !=
tmp.end();
91 void set_type(
const bondType &new_bondType) {
92 if ((this->_type != BD_REG)) {
93 if (new_bondType == BD_REG) {
95 "[ERROR] cannot change type to BD_REG for a symmetry bond.%s",
"\n");
97 if (std::abs(
int(this->_type)) != std::abs(
int(new_bondType))) {
99 "[ERROR] cannot exchange BDtype between BD_* <-> gBD_* .%s",
"\n");
103 this->_type = new_bondType;
107 if (this->_type != BD_REG) {
108 cytnx_error_msg(this->_qnums.size(),
"[ERROR] cannot clear type for a symmetry bond.%s",
111 this->_type = bondType::BD_REG;
114 boost::intrusive_ptr<Bond_impl> clone()
const {
115 boost::intrusive_ptr<Bond_impl> out(
new Bond_impl());
116 out->_dim = this->dim();
117 out->_type = this->type();
118 out->_qnums = this->qnums_clone();
119 out->_syms = this->syms_clone();
120 out->_degs = this->_degs;
126 void combineBond_(
const boost::intrusive_ptr<Bond_impl> &bd_in,
const bool &is_grp =
true);
128 boost::intrusive_ptr<Bond_impl> combineBond(
const boost::intrusive_ptr<Bond_impl> &bd_in,
129 const bool &is_grp =
true) {
130 boost::intrusive_ptr<Bond_impl> out = this->clone();
131 out->combineBond_(bd_in, is_grp);
136 std::vector<std::vector<cytnx_int64>> getUniqueQnums(std::vector<cytnx_uint64> &counts,
137 const bool &return_counts);
140 cytnx_uint64 getDegeneracy(
const std::vector<cytnx_int64> &qnum,
const bool &return_indices,
141 std::vector<cytnx_uint64> &indices);
144 std::vector<std::vector<cytnx_int64>> calc_reverse_qnums();
146 std::vector<cytnx_uint64> &getDegeneracies() {
return this->_degs; };
147 const std::vector<cytnx_uint64> &getDegeneracies()
const {
return this->_degs; };
149 std::vector<cytnx_uint64> group_duplicates_();
151 boost::intrusive_ptr<Bond_impl> group_duplicates(std::vector<cytnx_uint64> &mapper)
const {
152 boost::intrusive_ptr<Bond_impl> out = this->clone();
153 mapper = out->group_duplicates_();
157 void force_combineBond_(
const boost::intrusive_ptr<Bond_impl> &bd_in,
const bool &is_grp);
178 boost::intrusive_ptr<Bond_impl> _impl;
179 Bond() : _impl(
new Bond_impl()){};
180 Bond(
const Bond &rhs) { this->_impl = rhs._impl; }
182 this->_impl = rhs._impl;
200 : _impl(new Bond_impl()) {
201 this->_impl->Init(
dim, bd_type);
224 Bond(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
225 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
226 : _impl(new Bond_impl()) {
227 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
237 Bond(
const bondType &bd_type,
const std::initializer_list<std::vector<cytnx_int64>> &in_qnums,
238 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
239 : _impl(new Bond_impl()) {
240 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
252 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {})
253 : _impl(new Bond_impl()) {
255 this->_impl->Init(bd_type,
qnums, degs, in_syms);
266 const std::vector<std::pair<std::vector<cytnx_int64>,
cytnx_uint64>> &in_qnums_dims,
267 const std::vector<Symmetry> &in_syms = {})
268 : _impl(new Bond_impl()) {
269 this->
Init(bd_type, in_qnums_dims, in_syms);
300 this->_impl->Init(
dim, bd_type);
338 void Init(
const bondType &bd_type,
const std::vector<std::vector<cytnx_int64>> &in_qnums,
339 const std::vector<cytnx_uint64> °s,
const std::vector<Symmetry> &in_syms = {}) {
340 this->_impl->Init(bd_type, in_qnums, degs, in_syms);
349 const std::vector<std::pair<std::vector<cytnx_int64>,
cytnx_uint64>> &in_qnums_dims,
350 const std::vector<Symmetry> &in_syms = {}) {
352 std::vector<cytnx_uint64> degs(in_qnums_dims.size());
353 for (
int i = 0; i < in_qnums_dims.size(); i++) {
354 qnums[i] = in_qnums_dims[i].first;
355 degs[i] = in_qnums_dims[i].second;
357 this->_impl->Init(bd_type,
qnums, degs, in_syms);
372 const std::vector<std::vector<cytnx_int64>> &
qnums()
const {
return this->_impl->qnums(); };
376 std::vector<std::vector<cytnx_int64>> &
qnums() {
return this->_impl->qnums(); };
385 return this->_impl->qnums_clone();
406 const std::vector<Symmetry> &
syms()
const {
return this->_impl->syms(); };
410 std::vector<Symmetry> &
syms() {
return this->_impl->syms(); };
418 std::vector<Symmetry>
syms_clone()
const {
return this->_impl->syms_clone(); };
428 this->_impl->
set_type(new_bondType);
442 auto out = this->
clone();
443 out.set_type(new_bondType);
452 auto out = this->
clone();
453 out.set_type(
bondType(
int(out.type()) * -1));
489 out._impl = this->_impl->
clone();
520 this->_impl->combineBond_(bd_in._impl, is_grp);
552 out._impl = this->_impl->
combineBond(bd_in._impl, is_grp);
617 void combineBonds_(
const std::vector<Bond> &bds,
const bool &is_grp =
true) {
630 std::vector<std::vector<cytnx_int64>>
getUniqueQnums(std::vector<cytnx_uint64> &counts) {
631 return this->_impl->getUniqueQnums(counts,
true);
641 std::vector<cytnx_uint64> tmp;
642 return this->_impl->getUniqueQnums(tmp,
false);
659 std::vector<cytnx_uint64> tmp;
660 return this->_impl->getDegeneracy(qnum,
false, tmp);
677 std::vector<cytnx_uint64> &indices)
const {
679 return this->_impl->getDegeneracy(qnum,
true, indices);
690 std::vector<cytnx_uint64> &
getDegeneracies() {
return this->_impl->getDegeneracies(); }
696 return this->_impl->getDegeneracies();
764 return this->_impl->calc_reverse_qnums();
774 void Save(
const std::string &fname)
const;
779 void Save(
const char *fname)
const;
795 void _Save(std::fstream &f)
const;
796 void _Load(std::fstream &f);
860 std::ostream &operator<<(std::ostream &os,
const Bond &bin);
the object contains auxiliary properties for each Tensor rank (bond)
Definition Bond.hpp:175
std::vector< std::vector< cytnx_int64 > > & qnums()
Definition Bond.hpp:376
Bond & set_type(const bondType &new_bondType)
change the tag-type of the instance Bond
Definition Bond.hpp:427
cytnx_uint64 getDegeneracy(const std::vector< cytnx_int64 > &qnum) const
return the degeneracy of specify qnum set.
Definition Bond.hpp:658
std::vector< cytnx_uint64 > group_duplicates_()
Group the duplicated quantum number, inplacely.
Definition Bond.hpp:715
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:348
Bond retype(const bondType &new_bondType)
create a new instance of Bond with type changed to the new tag-type.
Definition Bond.hpp:441
void combineBonds_(const std::vector< Bond > &bds, const bool &is_grp=true)
combine multiple input bonds with self, inplacely
Definition Bond.hpp:617
Bond & operator*=(const Bond &rhs)
The multiplication assignment operator of the Bond object.
Definition Bond.hpp:853
const std::vector< Symmetry > & syms() const
return the vector of symmetry objects by reference.
Definition Bond.hpp:406
Bond(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
The constructor of the Bond object.
Definition Bond.hpp:199
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:550
std::vector< std::vector< cytnx_int64 > > qnums_clone() const
return the clone (deep copy) of the current quantum number set(s)
Definition Bond.hpp:384
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:583
Bond operator*(const Bond &rhs) const
The multiplication operator of the Bond object.
Definition Bond.hpp:834
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:737
void Init(const cytnx_uint64 &dim, const bondType &bd_type=bondType::BD_REG)
init a bond object
Definition Bond.hpp:299
void clear_type()
change the tag-type to the default value bondType.BD_REG.
Definition Bond.hpp:471
Bond(const bondType &bd_type, const std::vector< cytnx::Qs > &in_qnums, const std::vector< cytnx_uint64 > °s, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:251
Bond redirect() const
create a new instance of Bond with type changed in between bondType.BD_BRA / bondType....
Definition Bond.hpp:451
const std::vector< cytnx_uint64 > & getDegeneracies() const
Definition Bond.hpp:695
std::vector< cytnx_uint64 > & getDegeneracies()
return all degeneracies.
Definition Bond.hpp:690
bondType type() const
return the current bond type (see cytnx::bondType).
Definition Bond.hpp:364
Bond clone() const
return a copy of the instance Bond
Definition Bond.hpp:487
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:676
std::vector< Symmetry > syms_clone() const
return copy of the vector of symmetry objects.
Definition Bond.hpp:418
Bond & redirect_()
Change the bond type between bondType.BD_BRA and bondType.BD_KET in the Bond.
Definition Bond.hpp:461
cytnx_uint64 dim() const
return the dimension of the bond
Definition Bond.hpp:392
std::vector< std::vector< cytnx_int64 > > getUniqueQnums()
return a sorted qnum sets by removing all the duplicate qnum sets.
Definition Bond.hpp:640
const std::vector< std::vector< cytnx_int64 > > & qnums() const
return the current quantum number set(s) by reference
Definition Bond.hpp:372
void combineBond_(const Bond &bd_in, const bool &is_grp=true)
Combine the input bond with self, inplacely.
Definition Bond.hpp:519
std::vector< Symmetry > & syms()
Definition Bond.hpp:410
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:265
void Save(const char *fname) const
cytnx_uint32 Nsym() const
return the number of the symmetries
Definition Bond.hpp:398
bool has_duplicate_qnums() const
Check whether there is duplicated quantum numbers in the Bond.
Definition Bond.hpp:753
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:630
std::vector< std::vector< cytnx_int64 > > calc_reverse_qnums()
Calculate the reverse of the quantum numbers.
Definition Bond.hpp:763
Bond(const bondType &bd_type, const std::vector< std::vector< cytnx_int64 > > &in_qnums, const std::vector< cytnx_uint64 > °s, const std::vector< Symmetry > &in_syms={})
The constructor of the Bond object.
Definition Bond.hpp:224
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 > °s, const std::vector< Symmetry > &in_syms={})
init a bond object
Definition Bond.hpp:338
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 > °s, const std::vector< Symmetry > &in_syms={})
Definition Bond.hpp:237
bool operator==(const Bond &rhs) const
The comparison operator 'equal to'.
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
uint32_t cytnx_uint32
Definition Type.hpp:46
uint64_t cytnx_uint64
Definition Type.hpp:45
std::vector< std::vector< T > > vec2d
Definition Type.hpp:41
bondType
bond type
Definition Bond.hpp:30
@ BD_IN
Definition Bond.hpp:35
@ BD_OUT
Definition Bond.hpp:36
@ BD_NONE
Definition Bond.hpp:34
@ BD_REG
Definition Bond.hpp:33
@ BD_BRA
Definition Bond.hpp:32
@ BD_KET
Definition Bond.hpp:31