Cytnx v0.9.5
Loading...
Searching...
No Matches
Symmetry.hpp
Go to the documentation of this file.
1#ifndef _H_Symmetry
2#define _H_Symmetry
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
7#include <string>
8#include <cstdio>
9#include <iostream>
10#include <ostream>
11#include "utils/vec_clone.hpp"
12
13#ifdef BACKEND_TORCH
14#else
15namespace cytnx {
17 struct __sym {
18 enum __stype { U = -1, Z = 0 };
19 };
20
21 class SymmetryType_class {
22 public:
23 enum : int {
24 Void = -99,
25 U = -1,
26 Z = 0,
27 };
28 std::string getname(const int &stype);
29 };
31
44 extern SymmetryType_class SymType;
45
46 // helper class, has implicitly conversion to vector<int64>!
47 class Qs {
48 private:
49 std::vector<cytnx_int64> tmpQs;
50
51 public:
52 template <class... Ts>
53 Qs(const cytnx_int64 &e1, const Ts... elems) {
54 this->tmpQs = dynamic_arg_int64_resolver(e1, elems...);
55 }
56
57 Qs(const std::vector<cytnx_int64> &qin) { this->tmpQs = qin; }
58
59 // interprete as 2d vector directly implicitly convert!
60 explicit operator std::vector<cytnx_int64>() const { return this->tmpQs; };
61
62 std::pair<std::vector<cytnx_int64>, cytnx_uint64> operator>>(const cytnx_uint64 &dim) {
63 return make_pair(this->tmpQs, dim);
64 }
65 };
66
68 class Symmetry_base : public intrusive_ptr_base<Symmetry_base> {
69 public:
70 int stype_id;
71 int n;
72 Symmetry_base() : stype_id(SymType.Void){};
73 Symmetry_base(const int &n) : stype_id(SymType.Void) { this->Init(n); };
74 Symmetry_base(const Symmetry_base &rhs);
75 Symmetry_base &operator=(const Symmetry_base &rhs);
76
77 std::vector<cytnx_int64> combine_rule(const std::vector<cytnx_int64> &inL,
78 const std::vector<cytnx_int64> &inR);
79 cytnx_int64 combine_rule(const cytnx_int64 &inL, const cytnx_int64 &inR,
80 const bool &is_reverse);
81
82 cytnx_int64 reverse_rule(const cytnx_int64 &in);
83
84 virtual void Init(const int &n){};
85 virtual boost::intrusive_ptr<Symmetry_base> clone() { return nullptr; };
86 virtual bool check_qnum(
87 const cytnx_int64 &in_qnum); // check the passed in qnums satisfy the symmetry requirement.
88 virtual bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
89 virtual void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
90 const std::vector<cytnx_int64> &inR);
91 virtual void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
92 const bool &is_reverse);
93 virtual void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
94 virtual void print_info() const;
95 // virtual std::vector<cytnx_int64>& combine_rule(const std::vector<cytnx_int64> &inL, const
96 // std::vector<cytnx_int64> &inR);
97 };
99
101 class U1Symmetry : public Symmetry_base {
102 public:
103 U1Symmetry() { this->stype_id = SymType.U; };
104 U1Symmetry(const int &n) { this->Init(n); };
105 void Init(const int &n) {
106 this->stype_id = SymType.U;
107 this->n = n;
108 if (n != 1) cytnx_error_msg(1, "%s", "[ERROR] U1Symmetry should set n = 1");
109 }
110 boost::intrusive_ptr<Symmetry_base> clone() {
111 boost::intrusive_ptr<Symmetry_base> out(new U1Symmetry(this->n));
112 return out;
113 }
114 bool check_qnum(const cytnx_int64 &in_qnum);
115 bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
116 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
117 const std::vector<cytnx_int64> &inR);
118 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
119 const bool &is_reverse);
120 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
121 void print_info() const;
122 };
124
126 class ZnSymmetry : public Symmetry_base {
127 public:
128 ZnSymmetry() { this->stype_id = SymType.Z; };
129 ZnSymmetry(const int &n) { this->Init(n); };
130 void Init(const int &n) {
131 this->stype_id = SymType.Z;
132 this->n = n;
133 if (n <= 1) cytnx_error_msg(1, "%s", "[ERROR] ZnSymmetry can only have n > 1");
134 }
135 boost::intrusive_ptr<Symmetry_base> clone() {
136 boost::intrusive_ptr<Symmetry_base> out(new ZnSymmetry(this->n));
137 return out;
138 }
139 bool check_qnum(const cytnx_int64 &in_qnum);
140 bool check_qnums(const std::vector<cytnx_int64> &in_qnums);
141 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
142 const std::vector<cytnx_int64> &inR);
143 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
144 const bool &is_reverse);
145 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in);
146 void print_info() const;
147 };
149
150 //=====================================
151 // this is API
153 class Symmetry {
154 public:
155 //[Note] these two are hide from user.
157 boost::intrusive_ptr<Symmetry_base> _impl;
158
159 Symmetry(const int &stype = -1, const int &n = 0) : _impl(new Symmetry_base()) {
160 this->Init(stype, n);
161 }; // default is U1Symmetry
162
163 void Init(const int &stype = -1, const int &n = 0) {
164 if (stype == SymType.U) {
165 boost::intrusive_ptr<Symmetry_base> tmp(new U1Symmetry(1));
166 this->_impl = tmp;
167 } else if (stype == SymType.Z) {
168 boost::intrusive_ptr<Symmetry_base> tmp(new ZnSymmetry(n));
169 this->_impl = tmp;
170 } else {
171 cytnx_error_msg(1, "%s", "[ERROR] invalid symmetry type.");
172 }
173 }
174 Symmetry &operator=(const Symmetry &rhs) {
175 this->_impl = rhs._impl;
176 return *this;
177 }
178 Symmetry(const Symmetry &rhs) { this->_impl = rhs._impl; }
180
181 //[genenrators]
210 static Symmetry U1() { return Symmetry(SymType.U, 1); }
211
241 static Symmetry Zn(const int &n) { return Symmetry(SymType.Z, n); }
242
257 Symmetry clone() const {
258 Symmetry out;
259 out._impl = this->_impl->clone();
260 return out;
261 }
262
269 int stype() const { return this->_impl->stype_id; }
270
280 int &n() const { return this->_impl->n; }
281
289 std::string stype_str() const {
290 return SymType.getname(this->_impl->stype_id) + std::to_string(this->_impl->n);
291 }
292
299 bool check_qnum(const cytnx_int64 &qnum) { return this->_impl->check_qnum(qnum); }
300
308 bool check_qnums(const std::vector<cytnx_int64> &qnums) {
309 return this->_impl->check_qnums(qnums);
310 }
311
318 std::vector<cytnx_int64> combine_rule(const std::vector<cytnx_int64> &inL,
319 const std::vector<cytnx_int64> &inR) {
320 return this->_impl->combine_rule(inL, inR);
321 }
322
330 void combine_rule_(std::vector<cytnx_int64> &out, const std::vector<cytnx_int64> &inL,
331 const std::vector<cytnx_int64> &inR) {
332 this->_impl->combine_rule_(out, inL, inR);
333 }
334
342 const bool &is_reverse = false) const {
343 return this->_impl->combine_rule(inL, inR, is_reverse);
344 }
345
353 void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR,
354 const bool &is_reverse = false) {
355 this->_impl->combine_rule_(out, inL, inR, is_reverse);
356 }
357
366 void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in) {
367 this->_impl->reverse_rule_(out, in);
368 }
369
377 cytnx_int64 reverse_rule(const cytnx_int64 &in) const { return this->_impl->reverse_rule(in); }
378
384 void Save(const std::string &fname) const;
385
389 void Save(const char *fname) const;
390
397 static Symmetry Load(const std::string &fname);
398
402 static Symmetry Load(const char *fname);
403
405 void _Save(std::fstream &f) const;
406 void _Load(std::fstream &f);
408
412 void print_info() const { this->_impl->print_info(); }
413
417 bool operator==(const Symmetry &rhs) const;
418
422 bool operator!=(const Symmetry &rhs) const;
423 };
424
426 std::ostream &operator<<(std::ostream &os, const Symmetry &in);
428
429} // namespace cytnx
430#endif // BACKEND_TORCH
431
432#endif
Definition Symmetry.hpp:47
Qs(const cytnx_int64 &e1, const Ts... elems)
Definition Symmetry.hpp:53
Qs(const std::vector< cytnx_int64 > &qin)
Definition Symmetry.hpp:57
std::pair< std::vector< cytnx_int64 >, cytnx_uint64 > operator>>(const cytnx_uint64 &dim)
Definition Symmetry.hpp:62
the symmetry object
Definition Symmetry.hpp:153
static Symmetry Zn(const int &n)
create a Zn descrete symmetry object with
Definition Symmetry.hpp:241
bool check_qnum(const cytnx_int64 &qnum)
check the quantum number qnum is within the valid value range of current Symmetry.
Definition Symmetry.hpp:299
std::vector< cytnx_int64 > combine_rule(const std::vector< cytnx_int64 > &inL, const std::vector< cytnx_int64 > &inR)
apply combine rule of current symmetry to two quantum number lists.
Definition Symmetry.hpp:318
std::string stype_str() const
return the symmetry type name of current Symmetry object in string form, see cytnx::SymType.
Definition Symmetry.hpp:289
void Save(const char *fname) const
Same as Save(const std::string &fname) const;.
cytnx_int64 combine_rule(const cytnx_int64 &inL, const cytnx_int64 &inR, const bool &is_reverse=false) const
apply combine rule of current symmetry to two quantum numbers.
Definition Symmetry.hpp:341
Symmetry clone() const
return a clone instance of current Symmetry object.
Definition Symmetry.hpp:257
bool check_qnums(const std::vector< cytnx_int64 > &qnums)
check all the quantum numbers \qnums are within the valid value range of current Symmetry.
Definition Symmetry.hpp:308
int stype() const
return the symmetry type-id of current Symmetry object, see cytnx::SymType.
Definition Symmetry.hpp:269
int & n() const
return the descrete n of current Symmetry object.
Definition Symmetry.hpp:280
bool operator==(const Symmetry &rhs) const
the equality operator of the Symmetry object.
void combine_rule_(std::vector< cytnx_int64 > &out, const std::vector< cytnx_int64 > &inL, const std::vector< cytnx_int64 > &inR)
apply combine rule of current symmetry to two quantum number lists, and store it into parameter out.
Definition Symmetry.hpp:330
void combine_rule_(cytnx_int64 &out, const cytnx_int64 &inL, const cytnx_int64 &inR, const bool &is_reverse=false)
apply combine rule of current symmetry to two quantum numbers, and store the combined quntun number i...
Definition Symmetry.hpp:353
void print_info() const
Print the information of current Symmetry object.
Definition Symmetry.hpp:412
static Symmetry U1()
create a U1 symmetry object
Definition Symmetry.hpp:210
cytnx_int64 reverse_rule(const cytnx_int64 &in) const
Apply reverse rule of current symmetry to a given quantum number and return the result.
Definition Symmetry.hpp:377
static Symmetry Load(const char *fname)
Same as static Symmetry Load(const std::string &fname);.
void reverse_rule_(cytnx_int64 &out, const cytnx_int64 &in)
Apply reverse rule of current symmetry to a given quantum number and store in parameter out.
Definition Symmetry.hpp:366
bool operator!=(const Symmetry &rhs) const
the inequality operator of the Symmetry object.
void Save(const std::string &fname) const
Save the current Symmetry object to a file.
static Symmetry Load(const std::string &fname)
Load a Symmetry object from a file.
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Helper function to print vector with ODT:
Definition Accessor.hpp:12
uint64_t cytnx_uint64
Definition Type.hpp:55
int64_t cytnx_int64
Definition Type.hpp:58
SymmetryType_class SymType
Symmetry type.