Cytnx v0.9.1
Loading...
Searching...
No Matches
Network.hpp
Go to the documentation of this file.
1#ifndef _H_Network_
2#define _H_Network_
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 __nttype { Void = -1, Regular = 0, Fermion = 1 };
18 };
19 class NetworkType_class {
20 public:
21 enum : int { Void = -1, Regular = 0, Fermion = 1 };
22 std::string getname(const int &nwrktype_id);
23 };
24 extern NetworkType_class NtType;
26
28 class Network_base : public intrusive_ptr_base<Network_base> {
29 // protected:
30 public:
31 int nwrktype_id;
32 std::string filename;
33 std::vector<UniTensor> tensors;
34 std::vector<std::string> TOUT_labels;
35
36 cytnx_uint64 TOUT_iBondNum;
37
38 // bool ordered;
39
40 // Contraction order.
41 ContractionTree CtTree;
42 std::vector<std::string> ORDER_tokens;
43
44 // labels corr to the tn list.
45 std::vector<std::vector<std::string>> label_arr;
46 std::vector<cytnx_int64> iBondNums;
47
48 // name of tn.
49 std::vector<std::string> names;
50 std::map<std::string, cytnx_uint64> name2pos;
51
52 friend class FermionNetwork;
53 friend class RegularNetwork;
54 friend class Network;
55 Network_base() : nwrktype_id(NtType.Void){};
56
57 bool HasPutAllUniTensor() {
58 for (cytnx_uint64 i = 0; i < this->tensors.size(); i++) {
59 if (this->tensors[i].uten_type() == UTenType.Void) return false;
60 }
61 return true;
62 }
63
64 // void print_network() const;
65
66 // void PreConstruct(bool force = true);
67
68 // void PutTensor(cytnx_int64 idx, const UniTensor& UniT, bool force = true);
69
70 // void PutTensor(const std::string &name, const UniTensor &UniT, bool force = true);
71
72 // UniTensor Launch(const std::string &Tname="");
73
74 // std::string GetContractOrder() const;
75 virtual void PutUniTensor(const std::string &name, const UniTensor &utensor);
76 virtual void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor);
77 virtual void PutUniTensors(const std::vector<std::string> &name,
78 const std::vector<UniTensor> &utensors);
79 virtual void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
80 const std::vector<std::string> &alias,
81 const std::string &contract_order);
82
83 virtual void Fromfile(const std::string &fname);
84 virtual void FromString(const std::vector<std::string> &content);
85 virtual void clear();
86 virtual std::string getOptimalOrder();
87 virtual UniTensor Launch(const bool &optimal = false, const std::string &contract_order = "");
88 virtual void construct(const std::vector<std::string> &alias,
89 const std::vector<std::vector<std::string>> &lbls,
90 const std::vector<std::string> &outlbl, const cytnx_int64 &outrk,
91 const std::string &order, const bool optim);
92 virtual void PrintNet(std::ostream &os);
93 virtual boost::intrusive_ptr<Network_base> clone();
94 virtual void Savefile(const std::string &fname);
95 virtual ~Network_base(){};
96
97 }; // Network_base
98
99 class RegularNetwork : public Network_base {
100 public:
101 RegularNetwork() { this->nwrktype_id = NtType.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->tensors.clear();
113 this->name2pos.clear();
114 this->CtTree.clear();
115 this->names.clear();
116 this->iBondNums.clear();
117 this->label_arr.clear();
118 this->TOUT_labels.clear();
119 this->TOUT_iBondNum = 0;
120 this->ORDER_tokens.clear();
121 }
122 std::string getOptimalOrder();
123 UniTensor Launch(const bool &optimal = false, const std::string &contract_order = "");
124 void construct(const std::vector<std::string> &alias,
125 const std::vector<std::vector<std::string>> &lbls,
126 const std::vector<std::string> &outlbl, const cytnx_int64 &outrk,
127 const std::string &order, const bool optim);
128 boost::intrusive_ptr<Network_base> clone() {
129 RegularNetwork *tmp = new RegularNetwork();
130 tmp->name2pos = this->name2pos;
131 tmp->CtTree = this->CtTree;
132 tmp->names = this->names;
133 tmp->iBondNums = this->iBondNums;
134 tmp->label_arr = this->label_arr;
135 tmp->TOUT_labels = this->TOUT_labels;
136 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
137 tmp->ORDER_tokens = this->ORDER_tokens;
138 boost::intrusive_ptr<Network_base> out(tmp);
139 return out;
140 }
141 void PrintNet(std::ostream &os);
142 void Savefile(const std::string &fname);
143 ~RegularNetwork(){};
144 };
145
146 // Under dev!!
147 class FermionNetwork : public Network_base {
148 protected:
149 // [Future] Swap gates.
150
151 public:
152 FermionNetwork() { this->nwrktype_id = NtType.Fermion; };
153 void Fromfile(const std::string &fname){};
154 void FromString(const std::vector<std::string> &contents){};
155 void PutUniTensor(const std::string &name, const UniTensor &utensor){};
156 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor){};
157 void PutUniTensors(const std::vector<std::string> &name,
158 const std::vector<UniTensor> &utensors){};
159 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
160 const std::vector<std::string> &alias = {},
161 const std::string &contract_order = ""){};
162 void clear() {
163 this->name2pos.clear();
164 this->CtTree.clear();
165 this->names.clear();
166 this->iBondNums.clear();
167 this->label_arr.clear();
168 this->TOUT_labels.clear();
169 this->TOUT_iBondNum = 0;
170 this->ORDER_tokens.clear();
171 }
172 UniTensor Launch(const bool &optimal = false, const std::string &contract_order = "") {
173 return UniTensor();
174 };
175 boost::intrusive_ptr<Network_base> clone() {
176 FermionNetwork *tmp = new FermionNetwork();
177 tmp->name2pos = this->name2pos;
178 tmp->CtTree = this->CtTree;
179 tmp->names = this->names;
180 tmp->iBondNums = this->iBondNums;
181 tmp->label_arr = this->label_arr;
182 tmp->TOUT_labels = this->TOUT_labels;
183 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
184 tmp->ORDER_tokens = this->ORDER_tokens;
185 boost::intrusive_ptr<Network_base> out(tmp);
186 return out;
187 }
188 void PrintNet(std::ostream &os){};
189 void Savefile(const std::string &fname){};
190 ~FermionNetwork(){};
191 };
192
194
195 /* @brief the Network object for easy build tensor network.
196
197 The Network is an object that allow one to create a complex network from a pre-defined
198 Network file. By putting the Tensors into the Network, the user simply call “Network.Launch()”
199 to get the out-come.
200 */
201 class Network {
202 public:
204 boost::intrusive_ptr<Network_base> _impl;
205 Network() : _impl(new Network_base()){};
206 Network(const Network &rhs) { this->_impl = rhs._impl; }
207 Network &operator=(const Network &rhs) {
208 this->_impl = rhs._impl;
209 return *this;
210 }
212
260 void Fromfile(const std::string &fname, const int &network_type = NtType.Regular) {
261 if (network_type == NtType.Regular) {
262 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
263 this->_impl = tmp;
264 } else {
265 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
266 }
267 this->_impl->Fromfile(fname);
268 }
269
297 void FromString(const std::vector<std::string> &contents,
298 const int &network_type = NtType.Regular) {
299 if (network_type == NtType.Regular) {
300 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
301 this->_impl = tmp;
302 } else {
303 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
304 }
305 this->_impl->FromString(contents);
306 }
307 // void Savefile(const std::string &fname);
308
309 static Network Contract(const std::vector<UniTensor> &tensors, const std::string &Tout,
310 const std::vector<std::string> &alias = {},
311 const std::string &contract_order = "") {
312 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
313 Network out;
314 out._impl = tmp;
315 out._impl->Contract_plan(tensors, Tout, alias, contract_order);
316 return out;
317 }
318
319 Network(const std::string &fname, const int &network_type = NtType.Regular) {
320 this->Fromfile(fname, network_type);
321 }
322
323 void PutUniTensor(const std::string &name, const UniTensor &utensor,
324 const std::vector<std::string> &lbl_order = {}) {
325 if (lbl_order.size()) {
326 auto tmpu = utensor.permute(lbl_order);
327 this->_impl->PutUniTensor(name, tmpu);
328 } else
329 this->_impl->PutUniTensor(name, utensor);
330 }
331 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor,
332 const std::vector<std::string> &lbl_order = {}) {
333 if (lbl_order.size()) {
334 auto tmpu = utensor.permute(lbl_order);
335 this->_impl->PutUniTensor(idx, tmpu);
336 } else
337 this->_impl->PutUniTensor(idx, utensor);
338 }
339
340 void PutUniTensors(const std::vector<std::string> &name,
341 const std::vector<UniTensor> &utensors) {
342 this->_impl->PutUniTensors(name, utensors);
343 }
344 std::string getOptimalOrder(const int &network_type = NtType.Regular) {
345 if (network_type == NtType.Regular) {
346 return this->_impl->getOptimalOrder();
347 } else {
348 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
349 }
350 }
351 UniTensor Launch(const bool &optimal, const std::string &contract_order = "",
352 const int &network_type = NtType.Regular) {
353 if (network_type == NtType.Regular) {
354 return this->_impl->Launch(optimal);
355 } else {
356 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
357 }
358 }
359
360 void construct(const std::vector<std::string> &alias,
361 const std::vector<std::vector<std::string>> &lbls,
362 const std::vector<std::string> &outlbl = std::vector<std::string>(),
363 const cytnx_int64 &outrk = 0, const std::string &order = "",
364 const bool optim = false, const int &network_type = NtType.Regular) {
365 if (network_type == NtType.Regular) {
366 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
367 this->_impl = tmp;
368 } else {
369 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
370 }
371 this->_impl->construct(alias, lbls, outlbl, outrk, order, optim);
372 }
373
374 void clear() {
375 // boost::intrusive_ptr<Network_base> tmp(new Network_base());
376 this->_impl->clear();
377 }
378
380 Network out;
381 out._impl = this->_impl->clone();
382 return out;
383 }
384 void PrintNet() { this->_impl->PrintNet(std::cout); }
385
386 void Savefile(const std::string &fname) { this->_impl->Savefile(fname); }
387 };
388
390 std::ostream &operator<<(std::ostream &os, const Network &bin);
392} // namespace cytnx
393
394#endif
Definition Network.hpp:201
void Fromfile(const std::string &fname, const int &network_type=NtType.Regular)
Construct Network from network file.
Definition Network.hpp:260
void PutUniTensor(const std::string &name, const UniTensor &utensor, const std::vector< std::string > &lbl_order={})
Definition Network.hpp:323
Network(const std::string &fname, const int &network_type=NtType.Regular)
Definition Network.hpp:319
Network clone()
Definition Network.hpp:379
void PrintNet()
Definition Network.hpp:384
std::string getOptimalOrder(const int &network_type=NtType.Regular)
Definition Network.hpp:344
void construct(const std::vector< std::string > &alias, const std::vector< std::vector< std::string > > &lbls, const std::vector< std::string > &outlbl=std::vector< std::string >(), const cytnx_int64 &outrk=0, const std::string &order="", const bool optim=false, const int &network_type=NtType.Regular)
Definition Network.hpp:360
void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const std::vector< std::string > &lbl_order={})
Definition Network.hpp:331
static Network Contract(const std::vector< UniTensor > &tensors, const std::string &Tout, const std::vector< std::string > &alias={}, const std::string &contract_order="")
Definition Network.hpp:309
void FromString(const std::vector< std::string > &contents, const int &network_type=NtType.Regular)
Construct Network from a list of strings, where each string is the same as each line in network file.
Definition Network.hpp:297
UniTensor Launch(const bool &optimal, const std::string &contract_order="", const int &network_type=NtType.Regular)
Definition Network.hpp:351
void clear()
Definition Network.hpp:374
void PutUniTensors(const std::vector< std::string > &name, const std::vector< UniTensor > &utensors)
Definition Network.hpp:340
void Savefile(const std::string &fname)
Definition Network.hpp:386
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1705
UniTensor permute(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1) const
permute the lags of the UniTensor
Definition UniTensor.hpp:2354
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
UniTensorType_class UTenType
UniTensor type.
uint64_t cytnx_uint64
Definition Type.hpp:45
int64_t cytnx_int64
Definition Type.hpp:48
tmp
Definition sp.py:8