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, const std::vector<std::vector<std::string>> &lbls, const std::vector<std::string> &outlbl, const cytnx_int64 &outrk, const std::string &order, const bool optim);
89 virtual void PrintNet(std::ostream &os);
90 virtual boost::intrusive_ptr<Network_base> clone();
91 virtual void Savefile(const std::string &fname);
92 virtual ~Network_base(){};
93
94 }; // Network_base
95
96 class RegularNetwork : public Network_base {
97 public:
98 RegularNetwork() { this->nwrktype_id = NtType.Regular; };
99 void Fromfile(const std::string &fname);
100 void FromString(const std::vector<std::string> &contents);
101 void PutUniTensor(const std::string &name, const UniTensor &utensor);
102 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor);
103 void PutUniTensors(const std::vector<std::string> &name,
104 const std::vector<UniTensor> &utensors);
105 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
106 const std::vector<std::string> &alias = {},
107 const std::string &contract_order = "");
108 void clear() {
109 this->tensors.clear();
110 this->name2pos.clear();
111 this->CtTree.clear();
112 this->names.clear();
113 this->iBondNums.clear();
114 this->label_arr.clear();
115 this->TOUT_labels.clear();
116 this->TOUT_iBondNum = 0;
117 this->ORDER_tokens.clear();
118 }
119 std::string getOptimalOrder();
120 UniTensor Launch(const bool &optimal = false, const std::string &contract_order = "");
121 void construct(const std::vector<std::string> &alias, const std::vector<std::vector<std::string>> &lbls, const std::vector<std::string> &outlbl, const cytnx_int64 &outrk, const std::string &order, const bool optim);
122 boost::intrusive_ptr<Network_base> clone() {
123 RegularNetwork *tmp = new RegularNetwork();
124 tmp->name2pos = this->name2pos;
125 tmp->CtTree = this->CtTree;
126 tmp->names = this->names;
127 tmp->iBondNums = this->iBondNums;
128 tmp->label_arr = this->label_arr;
129 tmp->TOUT_labels = this->TOUT_labels;
130 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
131 tmp->ORDER_tokens = this->ORDER_tokens;
132 boost::intrusive_ptr<Network_base> out(tmp);
133 return out;
134 }
135 void PrintNet(std::ostream &os);
136 void Savefile(const std::string &fname);
137 ~RegularNetwork(){};
138 };
139
140 // Under dev!!
141 class FermionNetwork : public Network_base {
142 protected:
143 // [Future] Swap gates.
144
145 public:
146 FermionNetwork() { this->nwrktype_id = NtType.Fermion; };
147 void Fromfile(const std::string &fname){};
148 void FromString(const std::vector<std::string> &contents){};
149 void PutUniTensor(const std::string &name, const UniTensor &utensor){};
150 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor){};
151 void PutUniTensors(const std::vector<std::string> &name,
152 const std::vector<UniTensor> &utensors){};
153 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout,
154 const std::vector<std::string> &alias = {},
155 const std::string &contract_order = ""){};
156 void clear() {
157 this->name2pos.clear();
158 this->CtTree.clear();
159 this->names.clear();
160 this->iBondNums.clear();
161 this->label_arr.clear();
162 this->TOUT_labels.clear();
163 this->TOUT_iBondNum = 0;
164 this->ORDER_tokens.clear();
165 }
166 UniTensor Launch(const bool &optimal = false) { return UniTensor(); };
167 boost::intrusive_ptr<Network_base> clone() {
168 FermionNetwork *tmp = new FermionNetwork();
169 tmp->name2pos = this->name2pos;
170 tmp->CtTree = this->CtTree;
171 tmp->names = this->names;
172 tmp->iBondNums = this->iBondNums;
173 tmp->label_arr = this->label_arr;
174 tmp->TOUT_labels = this->TOUT_labels;
175 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
176 tmp->ORDER_tokens = this->ORDER_tokens;
177 boost::intrusive_ptr<Network_base> out(tmp);
178 return out;
179 }
180 void PrintNet(std::ostream &os){};
181 void Savefile(const std::string &fname){};
182 ~FermionNetwork(){};
183 };
184
186
187 /* @brief the Network object for easy build tensor network.
188
189 The Network is an object that allow one to create a complex network from a pre-defined
190 Network file. By putting the Tensors into the Network, the user simply call “Network.Launch()”
191 to get the out-come.
192 */
193 class Network {
194 public:
196 boost::intrusive_ptr<Network_base> _impl;
197 Network() : _impl(new Network_base()){};
198 Network(const Network &rhs) { this->_impl = rhs._impl; }
199 Network &operator=(const Network &rhs) {
200 this->_impl = rhs._impl;
201 return *this;
202 }
204
252 void Fromfile(const std::string &fname, const int &network_type = NtType.Regular) {
253 if (network_type == NtType.Regular) {
254 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
255 this->_impl = tmp;
256 } else {
257 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
258 }
259 this->_impl->Fromfile(fname);
260 }
261
289 void FromString(const std::vector<std::string> &contents,
290 const int &network_type = NtType.Regular) {
291 if (network_type == NtType.Regular) {
292 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
293 this->_impl = tmp;
294 } else {
295 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
296 }
297 this->_impl->FromString(contents);
298 }
299 // void Savefile(const std::string &fname);
300
301 static Network Contract(const std::vector<UniTensor> &tensors, const std::string &Tout,
302 const std::vector<std::string> &alias = {},
303 const std::string &contract_order = "") {
304 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
305 Network out;
306 out._impl = tmp;
307 out._impl->Contract_plan(tensors, Tout, alias, contract_order);
308 return out;
309 }
310
311 Network(const std::string &fname, const int &network_type = NtType.Regular) {
312 this->Fromfile(fname, network_type);
313 }
314
315 void PutUniTensor(const std::string &name, const UniTensor &utensor, const std::vector<std::string> &lbl_order={}) {
316 if(lbl_order.size()){
317 auto tmpu = utensor.permute(lbl_order);
318 this->_impl->PutUniTensor(name, tmpu);
319 }else
320 this->_impl->PutUniTensor(name, utensor);
321 }
322 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const std::vector<std::string> &lbl_order={}) {
323 if(lbl_order.size()){
324 auto tmpu = utensor.permute(lbl_order);
325 this->_impl->PutUniTensor(idx, tmpu);
326 }else
327 this->_impl->PutUniTensor(idx, utensor);
328 }
329
330
331
332
333
334
335 void PutUniTensors(const std::vector<std::string> &name,
336 const std::vector<UniTensor> &utensors) {
337 this->_impl->PutUniTensors(name, utensors);
338 }
339 std::string getOptimalOrder(const int &network_type = NtType.Regular) {
340 if (network_type == NtType.Regular) {
341 return this->_impl->getOptimalOrder();
342 } else {
343 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
344 }
345 }
346 UniTensor Launch(const bool &optimal, const std::string &contract_order = "",
347 const int &network_type = NtType.Regular) {
348 if (network_type == NtType.Regular) {
349 return this->_impl->Launch(optimal);
350 } else {
351 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
352 }
353 }
354
355 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){
356 if (network_type == NtType.Regular) {
357 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
358 this->_impl = tmp;
359 } else {
360 cytnx_error_msg(true, "[Developing] currently only support regular type network.%s", "\n");
361 }
362 this->_impl->construct(alias, lbls, outlbl, outrk, order, optim);
363 }
364
365 void clear() {
366 // boost::intrusive_ptr<Network_base> tmp(new Network_base());
367 this->_impl->clear();
368 }
369
371 Network out;
372 out._impl = this->_impl->clone();
373 return out;
374 }
375 void PrintNet() { this->_impl->PrintNet(std::cout); }
376
377 void Savefile(const std::string &fname) { this->_impl->Savefile(fname); }
378 };
379
381 std::ostream &operator<<(std::ostream &os, const Network &bin);
383} // namespace cytnx
384
385#endif
Definition Network.hpp:193
void Fromfile(const std::string &fname, const int &network_type=NtType.Regular)
Construct Network from network file.
Definition Network.hpp:252
void PutUniTensor(const std::string &name, const UniTensor &utensor, const std::vector< std::string > &lbl_order={})
Definition Network.hpp:315
Network(const std::string &fname, const int &network_type=NtType.Regular)
Definition Network.hpp:311
Network clone()
Definition Network.hpp:370
void PrintNet()
Definition Network.hpp:375
std::string getOptimalOrder(const int &network_type=NtType.Regular)
Definition Network.hpp:339
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:355
void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const std::vector< std::string > &lbl_order={})
Definition Network.hpp:322
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:301
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:289
UniTensor Launch(const bool &optimal, const std::string &contract_order="", const int &network_type=NtType.Regular)
Definition Network.hpp:346
void clear()
Definition Network.hpp:365
void PutUniTensors(const std::vector< std::string > &name, const std::vector< UniTensor > &utensors)
Definition Network.hpp:335
void Savefile(const std::string &fname)
Definition Network.hpp:377
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:2449
UniTensor permute(const std::vector< cytnx_int64 > &mapper, const cytnx_int64 &rowrank=-1, const bool &by_label=false) const
permute the lags of the UniTensor
Definition UniTensor.hpp:3087
#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
GnconType_class NtType
Definition Gncon.cpp:27
uint64_t cytnx_uint64
Definition Type.hpp:45
int64_t cytnx_int64
Definition Type.hpp:48
tmp
Definition sp.py:8