Cytnx v0.9.1
Loading...
Searching...
No Matches
Gncon.hpp
Go to the documentation of this file.
1#ifndef _H_Gncon_
2#define _H_Gncon_
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
6#include <initializer_list>
7#include <vector>
8#include <map>
9#include <fstream>
11#include "utils/utils.hpp"
12#include "UniTensor.hpp"
13#include "contraction_tree.hpp"
14namespace cytnx {
16 // struct __ntwk {
17 // enum __GNType { Void = -1, Regular = 0, Fermion = 1 };
18 // };
19 class GnconType_class {
20 public:
21 enum : int { Void = -1, Regular = 0, Fermion = 1 };
22 std::string getname(const int &nwrktype_id);
23 };
24 extern GnconType_class GNType;
26
28 class Gncon_base : public intrusive_ptr_base<Gncon_base> {
29 // protected:
30 public:
31 int nwrktype_id;
32 std::string filename;
33 // std::vector<std::vector<string>> tasks; // ex: "A:a-B:b",... => [["A","a","B","b"],...]
34
35 std::vector<UniTensor> tensors;
36 std::vector<cytnx_int64> TOUT_labels;
37
38 std::vector<std::vector<std::pair<std::string,std::string>>> table; // table[i] = i-th tensor's leg names to be contracted, and its target label.
39
40 cytnx_uint64 TOUT_iBondNum;
41
42 // bool ordered;
43
44 // Contraction order.
45 ContractionTree CtTree;
46 std::vector<std::string> ORDER_tokens;
47
48 // labels corr to the tn list.
49 std::vector<std::vector<std::string>> label_arr;
50 std::vector<cytnx_int64> iBondNums;
51
52 // name of tn.
53 std::vector<std::string> names;
54 std::map<std::string, cytnx_uint64> name2pos;
55
56 friend class FermionGncon;
57 friend class RegularGncon;
58 friend class Gncon;
59 Gncon_base() : nwrktype_id(GNType.Void){};
60
61 bool HasPutAllUniTensor() {
62 for (cytnx_uint64 i = 0; i < this->tensors.size(); i++) {
63 if (this->tensors[i].uten_type() == UTenType.Void) return false;
64 }
65 return true;
66 }
67
68 // void print_Gncon() const;
69
70 // void PreConstruct(bool force = true);
71
72 // void PutTensor(cytnx_int64 idx, const UniTensor& UniT, bool force = true);
73
74 // void PutTensor(const std::string &name, const UniTensor &UniT, bool force = true);
75
76 // UniTensor Launch(const std::string &Tname="");
77
78 // std::string GetContractOrder() const;
79 virtual void PutUniTensor(const std::string &name, const UniTensor &utensor);
80 virtual void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor);
81 virtual void PutUniTensors(const std::vector<std::string> &name,
82 const std::vector<UniTensor> &utensors);
83 virtual void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
84 const std::vector<std::string> &alias,
85 const std::string &contract_order);
86
87 virtual void Fromfile(const std::string &fname);
88 virtual void FromString(const std::vector<std::string> &content);
89 virtual void clear();
90 virtual std::string getOptimalOrder();
91 virtual UniTensor Launch(const bool &optimal = false, const std::string &contract_order = "");
92 virtual void PrintNet(std::ostream &os);
93 virtual boost::intrusive_ptr<Gncon_base> clone();
94 virtual void Savefile(const std::string &fname);
95 virtual ~Gncon_base(){};
96
97 }; // Gncon_base
98
99 class RegularGncon : public Gncon_base {
100 public:
101 RegularGncon() { this->nwrktype_id = GNType.Regular; };
102 void Fromfile(const std::string &fname);
103 void FromString(const std::vector<std::string> &contents);
104 void PutUniTensor(const std::string &name, const UniTensor &utensor);
105 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor);
106 void PutUniTensors(const std::vector<std::string> &name,
107 const std::vector<UniTensor> &utensors);
108 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
109 const std::vector<std::string> &alias = {},
110 const std::string &contract_order = "");
111 void clear() {
112 this->name2pos.clear();
113 this->CtTree.clear();
114 this->names.clear();
115 this->iBondNums.clear();
116 this->label_arr.clear();
117 this->TOUT_labels.clear();
118 this->TOUT_iBondNum = 0;
119 this->ORDER_tokens.clear();
120 }
121 std::string getOptimalOrder();
122 UniTensor Launch(const bool &optimal = false, const std::string &contract_order = "");
123 boost::intrusive_ptr<Gncon_base> clone() {
124 RegularGncon *tmp = new RegularGncon();
125 tmp->name2pos = this->name2pos;
126 tmp->CtTree = this->CtTree;
127 tmp->names = this->names;
128 tmp->iBondNums = this->iBondNums;
129 tmp->label_arr = this->label_arr;
130 tmp->TOUT_labels = this->TOUT_labels;
131 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
132 tmp->ORDER_tokens = this->ORDER_tokens;
133 boost::intrusive_ptr<Gncon_base> out(tmp);
134 return out;
135 }
136 void PrintNet(std::ostream &os);
137 void Savefile(const std::string &fname);
138 ~RegularGncon(){};
139 };
140
141 // Under dev!!
142 class FermionGncon : public Gncon_base {
143 protected:
144 // [Future] Swap gates.
145
146 public:
147 FermionGncon() { this->nwrktype_id = GNType.Fermion; };
148 void Fromfile(const std::string &fname){};
149 void FromString(const std::vector<std::string> &contents){};
150 void PutUniTensor(const std::string &name, const UniTensor &utensor){};
151 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor){};
152 void PutUniTensors(const std::vector<std::string> &name,
153 const std::vector<UniTensor> &utensors){};
154 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
155 const std::vector<std::string> &alias = {},
156 const std::string &contract_order = ""){};
157 void clear() {
158 this->name2pos.clear();
159 this->CtTree.clear();
160 this->names.clear();
161 this->iBondNums.clear();
162 this->label_arr.clear();
163 this->TOUT_labels.clear();
164 this->TOUT_iBondNum = 0;
165 this->ORDER_tokens.clear();
166 }
167 UniTensor Launch(const bool &optimal = false) { return UniTensor(); };
168 boost::intrusive_ptr<Gncon_base> clone() {
169 FermionGncon *tmp = new FermionGncon();
170 tmp->name2pos = this->name2pos;
171 tmp->CtTree = this->CtTree;
172 tmp->names = this->names;
173 tmp->iBondNums = this->iBondNums;
174 tmp->label_arr = this->label_arr;
175 tmp->TOUT_labels = this->TOUT_labels;
176 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
177 tmp->ORDER_tokens = this->ORDER_tokens;
178 boost::intrusive_ptr<Gncon_base> out(tmp);
179 return out;
180 }
181 void PrintNet(std::ostream &os){};
182 void Savefile(const std::string &fname){};
183 ~FermionGncon(){};
184 };
185
187
188 /* @brief the Gncon object for easy build tensor Gncon.
189
190 The Gncon is an object that allow one to create a complex Gncon from a pre-defined
191 Gncon file. By putting the Tensors into the Gncon, the user simply call “Gncon.Launch()”
192 to get the out-come.
193 */
194 class Gncon {
195 public:
197 boost::intrusive_ptr<Gncon_base> _impl;
198 Gncon() : _impl(new Gncon_base()){};
199 Gncon(const Gncon &rhs) { this->_impl = rhs._impl; }
200 Gncon &operator=(const Gncon &rhs) {
201 this->_impl = rhs._impl;
202 return *this;
203 }
205
253 void Fromfile(const std::string &fname, const int &Gncon_type = GNType.Regular) {
254 if (Gncon_type == GNType.Regular) {
255 boost::intrusive_ptr<Gncon_base> tmp(new RegularGncon());
256 this->_impl = tmp;
257 } else {
258 cytnx_error_msg(true, "[Developing] currently only support regular type Gncon.%s", "\n");
259 }
260 this->_impl->Fromfile(fname);
261 }
262
290 void FromString(const std::vector<std::string> &contents,
291 const int &Gncon_type = GNType.Regular) {
292 if (Gncon_type == GNType.Regular) {
293 boost::intrusive_ptr<Gncon_base> tmp(new RegularGncon());
294 this->_impl = tmp;
295 } else {
296 cytnx_error_msg(true, "[Developing] currently only support regular type Gncon.%s", "\n");
297 }
298 this->_impl->FromString(contents);
299 }
300 // void Savefile(const std::string &fname);
301
302 static Gncon Contract(const std::vector<UniTensor> &tensors, const std::string &Tout,
303 const std::vector<std::string> &alias = {},
304 const std::string &contract_order = "") {
305 boost::intrusive_ptr<Gncon_base> tmp(new RegularGncon());
306 Gncon out;
307 out._impl = tmp;
308 out._impl->Contract_plan(tensors, Tout, alias, contract_order);
309 return out;
310 }
311
312 Gncon(const std::string &fname, const int &Gncon_type = GNType.Regular) {
313 this->Fromfile(fname, Gncon_type);
314 }
315
316 void PutUniTensor(const std::string &name, const UniTensor &utensor) {
317 this->_impl->PutUniTensor(name, utensor);
318 }
319 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor) {
320 this->_impl->PutUniTensor(idx, utensor);
321 }
322 void PutUniTensors(const std::vector<std::string> &name,
323 const std::vector<UniTensor> &utensors) {
324 this->_impl->PutUniTensors(name, utensors);
325 }
326 std::string getOptimalOrder(const int &Gncon_type = GNType.Regular) {
327 if (Gncon_type == GNType.Regular) {
328 return this->_impl->getOptimalOrder();
329 } else {
330 cytnx_error_msg(true, "[Developing] currently only support regular type Gncon.%s", "\n");
331 }
332 }
333 UniTensor Launch(const bool &optimal, const std::string &contract_order = "",
334 const int &Gncon_type = GNType.Regular) {
335 if (Gncon_type == GNType.Regular) {
336 return this->_impl->Launch(optimal);
337 } else {
338 cytnx_error_msg(true, "[Developing] currently only support regular type Gncon.%s", "\n");
339 }
340 }
341 void clear() {
342 // boost::intrusive_ptr<Gncon_base> tmp(new Gncon_base());
343 this->_impl->clear();
344 }
345
347 Gncon out;
348 out._impl = this->_impl->clone();
349 return out;
350 }
351 void PrintNet() { this->_impl->PrintNet(std::cout); }
352
353 void Savefile(const std::string &fname) { this->_impl->Savefile(fname); }
354 };
355
357 std::ostream &operator<<(std::ostream &os, const Gncon &bin);
359} // namespace cytnx
360
361#endif
Definition Gncon.hpp:194
Gncon(const std::string &fname, const int &Gncon_type=GNType.Regular)
Definition Gncon.hpp:312
void Fromfile(const std::string &fname, const int &Gncon_type=GNType.Regular)
Construct Gncon from Gncon file.
Definition Gncon.hpp:253
Gncon clone()
Definition Gncon.hpp:346
static Gncon Contract(const std::vector< UniTensor > &tensors, const std::string &Tout, const std::vector< std::string > &alias={}, const std::string &contract_order="")
Definition Gncon.hpp:302
void PrintNet()
Definition Gncon.hpp:351
void clear()
Definition Gncon.hpp:341
void PutUniTensors(const std::vector< std::string > &name, const std::vector< UniTensor > &utensors)
Definition Gncon.hpp:322
std::string getOptimalOrder(const int &Gncon_type=GNType.Regular)
Definition Gncon.hpp:326
void Savefile(const std::string &fname)
Definition Gncon.hpp:353
void FromString(const std::vector< std::string > &contents, const int &Gncon_type=GNType.Regular)
Construct Gncon from a list of strings, where each string is the same as each line in Gncon file.
Definition Gncon.hpp:290
void PutUniTensor(const std::string &name, const UniTensor &utensor)
Definition Gncon.hpp:316
void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor)
Definition Gncon.hpp:319
UniTensor Launch(const bool &optimal, const std::string &contract_order="", const int &Gncon_type=GNType.Regular)
Definition Gncon.hpp:333
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:2449
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
UniTensorType_class UTenType
UniTensor type.
Definition UniTensor_base.cpp:24
std::ostream & operator<<(std::ostream &os, const Scalar &in)
The stream operator for Scalar objects.
Definition Scalar.cpp:10
uint64_t cytnx_uint64
Definition Type.hpp:45
tmp
Definition sp.py:8