Cytnx v0.7.6
Loading...
Searching...
No Matches
Network.hpp
Go to the documentation of this file.
1#ifndef _H_Network_
2#define _H_Network_
3
4
5
6#include "Type.hpp"
7#include "cytnx_error.hpp"
8#include <initializer_list>
9#include <vector>
10#include <map>
11#include <fstream>
13#include "utils/utils.hpp"
14#include "UniTensor.hpp"
15#include "contraction_tree.hpp"
16namespace cytnx{
18 struct __ntwk{
19 enum __nttype{
20 Void = -1,
21 Regular = 0,
22 Fermion= 1
23 };
24 };
25 class NetworkType_class{
26 public:
27 enum : int{
28 Void= -1,
29 Regular= 0,
30 Fermion= 1
31 };
32 std::string getname(const int &nwrktype_id);
33 };
34 extern NetworkType_class NtType;
36
38 class Network_base: public intrusive_ptr_base<Network_base>{
39 //protected:
40 public:
41 int nwrktype_id;
42 std::string filename;
43 std::vector<UniTensor> tensors;
44 std::vector<cytnx_int64> TOUT_labels;
45 cytnx_uint64 TOUT_iBondNum;
46
47 //bool ordered;
48
49 //Contraction order.
50 ContractionTree CtTree;
51 std::vector<std::string> ORDER_tokens;
52
53 // labels corr to the tn list.
54 std::vector< std::vector<cytnx_int64> > label_arr;
55 std::vector< cytnx_int64 > iBondNums;
56
57
58 // name of tn.
59 std::vector< std::string > names;
60 std::map<std::string,cytnx_uint64> name2pos;
61
62
63
64 friend class FermionNetwork;
65 friend class RegularNetwork;
66 friend class Network;
67 Network_base():nwrktype_id(NtType.Void){};
68
69
70 bool HasPutAllUniTensor(){
71 for(cytnx_uint64 i=0;i<this->tensors.size();i++){
72 if(this->tensors[i].uten_type()==UTenType.Void) return false;
73 }
74 return true;
75 }
76
77 //void print_network() const;
78
79 //void PreConstruct(bool force = true);
80
81 //void PutTensor(cytnx_int64 idx, const UniTensor& UniT, bool force = true);
82
83 //void PutTensor(const std::string &name, const UniTensor &UniT, bool force = true);
84
85 //UniTensor Launch(const std::string &Tname="");
86
87 //std::string GetContractOrder() const;
88 virtual void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone);
89 virtual void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone);
90 virtual void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone);
91 virtual void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout, const std::vector<bool> &is_clone, const std::vector<std::string> &alias, const std::string &contract_order);
92
93 virtual void Fromfile(const std::string& fname);
94 virtual void FromString(const std::vector<std::string> &content);
95 virtual void clear();
96 virtual std::string getOptimalOrder();
97 virtual UniTensor Launch(const bool &optimal=false, const std::string& contract_order="");
98 virtual void PrintNet(std::ostream &os);
99 virtual boost::intrusive_ptr<Network_base> clone();
100 virtual void Savefile(const std::string &fname);
101 virtual ~Network_base(){};
102
103
104 };//Network_base
105
106 class RegularNetwork : public Network_base{
107
108 public:
109 RegularNetwork(){this->nwrktype_id = NtType.Regular;};
110 void Fromfile(const std::string &fname);
111 void FromString(const std::vector<std::string> &contents);
112 void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true);
113 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true);
114 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true);
115 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout, const std::vector<bool> &is_clone={}, const std::vector<std::string> &alias={}, const std::string &contract_order="");
116 void clear(){
117 this->name2pos.clear();
118 this->CtTree.clear();
119 this->names.clear();
120 this->iBondNums.clear();
121 this->label_arr.clear();
122 this->TOUT_labels.clear();
123 this->TOUT_iBondNum = 0;
124 this->ORDER_tokens.clear();
125 }
126 std::string getOptimalOrder();
127 UniTensor Launch(const bool &optimal=false, const std::string& contract_order="");
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
147 //Under dev!!
148 class FermionNetwork : public Network_base{
149 protected:
150 // [Future] Swap gates.
151
152 public:
153 FermionNetwork(){this->nwrktype_id=NtType.Fermion;};
154 void Fromfile(const std::string &fname){};
155 void FromString(const std::vector<std::string> &contents){};
156 void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true){};
157 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true){};
158 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true){};
159 void Contract_plan(const std::vector<UniTensor> &utensors, const std::string &Tout, const std::vector<bool> &is_clone={}, const std::vector<std::string> &alias={}, const std::string &contract_order=""){};
160 void clear(){
161 this->name2pos.clear();
162 this->CtTree.clear();
163 this->names.clear();
164 this->iBondNums.clear();
165 this->label_arr.clear();
166 this->TOUT_labels.clear();
167 this->TOUT_iBondNum = 0;
168 this->ORDER_tokens.clear();
169 }
170 UniTensor Launch(const bool &optimal=false){return UniTensor();};
171 boost::intrusive_ptr<Network_base> clone(){
172 FermionNetwork *tmp = new FermionNetwork();
173 tmp->name2pos = this->name2pos;
174 tmp->CtTree = this->CtTree;
175 tmp->names = this->names;
176 tmp->iBondNums = this->iBondNums;
177 tmp->label_arr = this->label_arr;
178 tmp->TOUT_labels = this->TOUT_labels;
179 tmp->TOUT_iBondNum = this->TOUT_iBondNum;
180 tmp->ORDER_tokens = this->ORDER_tokens;
181 boost::intrusive_ptr<Network_base> out(tmp);
182 return out;
183 }
184 void PrintNet(std::ostream &os){};
185 void Savefile(const std::string &fname){};
186 ~FermionNetwork(){};
187 };
188
190
191 /* @brief the Network object for easy build tensor network.
192
193 The Network is an object that allow one to create a complex network from a pre-defined Network file. By putting the Tensors into the Network, the user simply call “Network.Launch()” to get the out-come.
194 */
195 class Network{
196 public:
198 boost::intrusive_ptr<Network_base> _impl;
199 Network(): _impl(new Network_base()){};
200 Network(const Network&rhs){this->_impl = rhs._impl;}
201 Network& operator=(const Network &rhs){this->_impl = rhs._impl; return *this;}
203
204
205
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
288 void FromString(const std::vector<std::string> &contents, const int &network_type=NtType.Regular){
289 if(network_type==NtType.Regular){
290 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
291 this->_impl = tmp;
292 }else{
293 cytnx_error_msg(true,"[Developing] currently only support regular type network.%s","\n");
294 }
295 this->_impl->FromString(contents);
296 }
297 //void Savefile(const std::string &fname);
298
299
300
301 static Network Contract(const std::vector<UniTensor> &tensors, const std::string &Tout, const std::vector<bool> &is_clone={}, const std::vector<std::string> &alias={}, const std::string &contract_order=""){
302 boost::intrusive_ptr<Network_base> tmp(new RegularNetwork());
303 Network out;
304 out._impl = tmp;
305 out._impl->Contract_plan(tensors,Tout,is_clone,alias,contract_order);
306 return out;
307 }
308
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 bool &is_clone=true){
316 this->_impl->PutUniTensor(name,utensor,is_clone);
317 }
318 void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true){
319 this->_impl->PutUniTensor(idx,utensor,is_clone);
320 }
321 void PutUniTensors(const std::vector<std::string> &name, const std::vector<UniTensor> &utensors, const bool &is_clone=true){
322 this->_impl->PutUniTensors(name,utensors,is_clone);
323 }
324 UniTensor Launch(const bool &optimal=false){
325 return this->_impl->Launch(optimal);
326 }
327 void clear(){
328 //boost::intrusive_ptr<Network_base> tmp(new Network_base());
329 this->_impl->clear();
330 }
331
333 Network out;
334 out._impl = this->_impl->clone();
335 return out;
336 }
337 void PrintNet(){
338 this->_impl->PrintNet(std::cout);
339 }
340
341 void Savefile(const std::string &fname){
342 this->_impl->Savefile(fname);
343 }
344
345 };
346
348 std::ostream& operator<<(std::ostream &os,const Network &bin);
350}
351
352
353#endif
Definition Network.hpp:195
void Fromfile(const std::string &fname, const int &network_type=NtType.Regular)
Construct Network from network file.
Definition Network.hpp:252
Network(const std::string &fname, const int &network_type=NtType.Regular)
Definition Network.hpp:311
Network clone()
Definition Network.hpp:332
static Network Contract(const std::vector< UniTensor > &tensors, const std::string &Tout, const std::vector< bool > &is_clone={}, const std::vector< std::string > &alias={}, const std::string &contract_order="")
Definition Network.hpp:301
void PrintNet()
Definition Network.hpp:337
UniTensor Launch(const bool &optimal=false)
Definition Network.hpp:324
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:288
void PutUniTensor(const std::string &name, const UniTensor &utensor, const bool &is_clone=true)
Definition Network.hpp:315
void clear()
Definition Network.hpp:327
void PutUniTensors(const std::vector< std::string > &name, const std::vector< UniTensor > &utensors, const bool &is_clone=true)
Definition Network.hpp:321
void PutUniTensor(const cytnx_uint64 &idx, const UniTensor &utensor, const bool &is_clone=true)
Definition Network.hpp:318
void Savefile(const std::string &fname)
Definition Network.hpp:341
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1122
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:18
Definition Accessor.hpp:12
UniTensorType_class UTenType
Definition UniTensor_base.cpp:21
std::ostream & operator<<(std::ostream &os, const Scalar &in)
Definition Scalar.cpp:14
NetworkType_class NtType
Definition Network.cpp:21
uint64_t cytnx_uint64
Definition Type.hpp:22