ExaTN
tensor_network.hpp
1 
52 #ifndef EXATN_NUMERICS_TENSOR_NETWORK_HPP_
53 #define EXATN_NUMERICS_TENSOR_NETWORK_HPP_
54 
55 #include "tensor_basic.hpp"
56 #include "tensor_connected.hpp"
57 #include "tensor_op_factory.hpp"
58 #include "network_build_factory.hpp"
59 #include "contraction_seq_optimizer.hpp"
60 
61 #include <unordered_map>
62 #include <map>
63 #include <vector>
64 #include <list>
65 #include <string>
66 #include <memory>
67 
68 namespace exatn{
69 
70 namespace numerics{
71 
73 public:
74 
75  using Iterator = typename std::unordered_map<unsigned int, TensorConn>::iterator; //iterator
76  using ConstIterator = typename std::unordered_map<unsigned int, TensorConn>::const_iterator; //constant iterator
77 
79  TensorNetwork();
81  TensorNetwork(const std::string & name);
83  TensorNetwork(const std::string & name, //in: tensor network name
84  std::shared_ptr<Tensor> output_tensor, //in: fully specified output tensor of the tensor network
85  const std::vector<TensorLeg> & output_legs); //in: fully specified output tensor legs
87  TensorNetwork(const std::string & name, //in: tensor network name
88  const std::string & tensor_network, //in: tensor network expression (symbolic math expression)
89  const std::map<std::string,std::shared_ptr<Tensor>> & tensors); //in: participating tensors identified by their names
91  TensorNetwork(const std::string & name, //in: tensor network name
92  std::shared_ptr<Tensor> output_tensor, //in: output tensor of the tensor network
93  NetworkBuilder & builder); //in: specific tensor network builder
94 
95  TensorNetwork(const TensorNetwork &) = default;
96  TensorNetwork & operator=(const TensorNetwork &) = default;
97  TensorNetwork(TensorNetwork &&) noexcept = default;
98  TensorNetwork & operator=(TensorNetwork &&) noexcept = default;
99  virtual ~TensorNetwork() = default;
100 
102  void printIt() const;
103 
105  bool isEmpty() const;
106 
108  bool isExplicit() const;
109 
111  bool isFinalized() const;
112 
114  unsigned int getRank() const;
115 
118  unsigned int getNumTensors() const;
119 
121  unsigned int getMaxTensorId() const;
122 
124  const std::string & getName() const;
125 
127  void rename(const std::string & name);
128 
131  std::shared_ptr<Tensor> getTensor(unsigned int tensor_id,
132  bool * conjugated = nullptr);
133 
135  const std::vector<TensorLeg> * getTensorConnections(unsigned int tensor_id);
136 
138  inline Iterator begin() {return tensors_.begin();}
140  inline Iterator end() {return tensors_.end();}
142  inline ConstIterator cbegin() const {return tensors_.cbegin();}
144  inline ConstIterator cend() const {return tensors_.cend();}
145 
148  bool finalize(bool check_validity = false);
149 
156  bool appendTensor(unsigned int tensor_id, //in: appended tensor id (unique within the tensor network)
157  std::shared_ptr<Tensor> tensor, //in: appended tensor
158  const std::vector<TensorLeg> & connections, //in: tensor connections (fully specified)
159  bool conjugated = false, //in: complex conjugation flag for the appended tensor
160  bool leg_matching_check = true); //in: tensor leg matching check
161 
169  bool appendTensor(unsigned int tensor_id, //in: appended tensor id (unique within the tensor network)
170  std::shared_ptr<Tensor> tensor, //in: appended tensor
171  const std::vector<std::pair<unsigned int, unsigned int>> & pairing, //in: leg pairing: output tensor mode -> appended tensor mode
172  const std::vector<LegDirection> & leg_dir = std::vector<LegDirection>{}, //in: optional leg directions (for all tensor modes)
173  bool conjugated = false); //in: complex conjugation flag for the appended tensor
174 
178  bool appendTensorGate(unsigned int tensor_id, //in: appended tensor id (unique within the tensor network)
179  std::shared_ptr<Tensor> tensor, //in: appended tensor gate (operator)
180  const std::vector<unsigned int> & pairing, //in: leg pairing: output tensor modes (half-rank)
181  bool conjugated = false); //in: complex conjugation flag for the appended tensor gate
182 
192  bool appendTensorNetwork(TensorNetwork && network, //in: appended tensor network
193  const std::vector<std::pair<unsigned int, unsigned int>> & pairing); //in: leg pairing: output tensor mode (primary) -> output tensor mode (appended)
194 
206  bool appendTensorNetworkGate(TensorNetwork && network, //in: appended tensor network gate (operator)
207  const std::vector<unsigned int> & pairing); //in: leg pairing: output tensor modes of the primary network (half-rank)
208 
211  bool reorderOutputModes(const std::vector<unsigned int> & order); //in: new order of the output tensor modes (N2O)
212 
217  bool deleteTensor(unsigned int tensor_id); //in: id of the tensor to be deleted
218 
223  bool mergeTensors(unsigned int left_id, //in: left tensor id (present in the tensor network)
224  unsigned int right_id, //in: right tensor id (present in the tensor network)
225  unsigned int result_id, //in: result tensor id (absent in the tensor network, to be appended)
226  std::string * contr_pattern = nullptr); //inout: corresponding tensor contraction pattern (owned by the caller)
227 
232  bool splitTensor(unsigned int tensor_id, //in: id of the tensor to be split into two tensors
233  unsigned int left_tensor_id, //in: id of the left tensor obtained via splitting
234  const std::string & left_tensor_name, //in: name of the left tensor
235  unsigned int right_tensor_id, //in: id of the right tensor obtained via splitting
236  const std::string & right_tensor_name, //in: name of the right tensor
237  const TensorShape & contracted_dims, //in: dimension extents of the contracted (new) dimensions connecting two tensors after splitting
238  const std::vector<int> & right_dims); //in: assignment of original tensor dimensions to new tensors (0: left, 1: right tensor)
239 
242  void conjugate();
243 
248  double getContractionCost(unsigned int left_id, //in: left tensor id (present in the tensor network)
249  unsigned int right_id, //in: right tensor id (present in the tensor network)
250  double * arithm_intensity = nullptr, //out: arithmetic intensity of the tensor contraction
251  bool adjust_cost = false); //in: whether or not to adjust the flops cost due to arithmetic intensity
252 
254  std::list<std::shared_ptr<TensorOperation>> & getOperationList(const std::string & contr_seq_opt_name = "dummy");
255 
256 protected:
257 
260  TensorConn * getTensorConn(unsigned int tensor_id);
261 
264  std::vector<TensorConn*> getTensorConnAll();
265 
267  bool checkConnections(unsigned int tensor_id);
269  bool checkConnections();
270 
273  void updateConnections(unsigned int tensor_id); //in: id of the tensor whose connections were modified
274 
278 
281 
286  double determineContractionSequence(ContractionSeqOptimizer & contr_seq_optimizer);
287 
288 private:
289 
290  int explicit_output_; //whether or not the output tensor has been fully specified during construction
291  int finalized_; //finalization status of the tensor network
292  std::string name_; //tensor network name
293  std::unordered_map<unsigned int, TensorConn> tensors_; //tensors connected to each other via legs (tensor connections)
294  //map: Non-negative tensor id --> Connected tensor
295  double contraction_seq_flops_; //flop estimate for the determined tensor contraction sequence
296  std::list<ContrTriple> contraction_seq_; //cached tensor contraction sequence
297  std::list<std::shared_ptr<TensorOperation>> operations_; //cached tensor operations required for evaluating the tensor network
298 };
299 
300 } //namespace numerics
301 
302 template<typename... Args>
303 inline std::shared_ptr<numerics::TensorNetwork> makeSharedTensorNetwork(Args&&... args)
304 {
305  return std::make_shared<numerics::TensorNetwork>(args...);
306 }
307 
308 } //namespace exatn
309 
310 #endif //EXATN_NUMERICS_TENSOR_NETWORK_HPP_
exatn::numerics::TensorNetwork::getMaxTensorId
unsigned int getMaxTensorId() const
Definition: tensor_network.cpp:213
exatn::numerics::TensorNetwork
Definition: tensor_network.hpp:72
exatn::numerics::TensorNetwork::conjugate
void conjugate()
Definition: tensor_network.cpp:1093
exatn::numerics::TensorNetwork::updateConnectionsFromInputTensors
void updateConnectionsFromInputTensors()
Definition: tensor_network.cpp:344
exatn::numerics::TensorNetwork::getNumTensors
unsigned int getNumTensors() const
Definition: tensor_network.cpp:207
exatn::numerics::TensorNetwork::getName
const std::string & getName() const
Definition: tensor_network.cpp:223
exatn::numerics::TensorNetwork::finalize
bool finalize(bool check_validity=false)
Definition: tensor_network.cpp:272
exatn::numerics::TensorNetwork::deleteTensor
bool deleteTensor(unsigned int tensor_id)
Definition: tensor_network.cpp:856
exatn::numerics::TensorNetwork::cbegin
ConstIterator cbegin() const
Definition: tensor_network.hpp:142
exatn::numerics::TensorNetwork::appendTensorGate
bool appendTensorGate(unsigned int tensor_id, std::shared_ptr< Tensor > tensor, const std::vector< unsigned int > &pairing, bool conjugated=false)
Definition: tensor_network.cpp:541
exatn::numerics::TensorShape
Definition: tensor_shape.hpp:29
exatn::numerics::TensorNetwork::checkConnections
bool checkConnections()
Definition: tensor_network.cpp:313
exatn::numerics::TensorNetwork::appendTensorNetworkGate
bool appendTensorNetworkGate(TensorNetwork &&network, const std::vector< unsigned int > &pairing)
Definition: tensor_network.cpp:736
exatn
Definition: DriverClient.hpp:10
exatn::numerics::TensorNetwork::getRank
unsigned int getRank() const
Definition: tensor_network.cpp:200
exatn::numerics::TensorNetwork::appendTensorNetwork
bool appendTensorNetwork(TensorNetwork &&network, const std::vector< std::pair< unsigned int, unsigned int >> &pairing)
Definition: tensor_network.cpp:640
exatn::numerics::TensorNetwork::getContractionCost
double getContractionCost(unsigned int left_id, unsigned int right_id, double *arithm_intensity=nullptr, bool adjust_cost=false)
Definition: tensor_network.cpp:1100
exatn::numerics::TensorNetwork::begin
Iterator begin()
Definition: tensor_network.hpp:138
exatn::numerics::TensorNetwork::mergeTensors
bool mergeTensors(unsigned int left_id, unsigned int right_id, unsigned int result_id, std::string *contr_pattern=nullptr)
Definition: tensor_network.cpp:922
exatn::numerics::TensorNetwork::rename
void rename(const std::string &name)
Definition: tensor_network.cpp:229
exatn::numerics::TensorNetwork::end
Iterator end()
Definition: tensor_network.hpp:140
exatn::numerics::TensorNetwork::getTensorConnections
const std::vector< TensorLeg > * getTensorConnections(unsigned int tensor_id)
Definition: tensor_network.cpp:264
exatn::numerics::TensorNetwork::invalidateContractionSequence
void invalidateContractionSequence()
Definition: tensor_network.cpp:353
exatn::numerics::TensorNetwork::cend
ConstIterator cend() const
Definition: tensor_network.hpp:144
exatn::numerics::TensorNetwork::reorderOutputModes
bool reorderOutputModes(const std::vector< unsigned int > &order)
Definition: tensor_network.cpp:828
exatn::numerics::TensorNetwork::isExplicit
bool isExplicit() const
Definition: tensor_network.cpp:188
exatn::numerics::TensorNetwork::printIt
void printIt() const
Definition: tensor_network.cpp:168
exatn::numerics::TensorNetwork::TensorNetwork
TensorNetwork()
Definition: tensor_network.cpp:30
exatn::numerics::TensorNetwork::getTensor
std::shared_ptr< Tensor > getTensor(unsigned int tensor_id, bool *conjugated=nullptr)
Definition: tensor_network.cpp:255
exatn::numerics::TensorNetwork::appendTensor
bool appendTensor(unsigned int tensor_id, std::shared_ptr< Tensor > tensor, const std::vector< TensorLeg > &connections, bool conjugated=false, bool leg_matching_check=true)
Definition: tensor_network.cpp:374
exatn::numerics::TensorNetwork::isFinalized
bool isFinalized() const
Definition: tensor_network.cpp:194
exatn::numerics::TensorNetwork::splitTensor
bool splitTensor(unsigned int tensor_id, unsigned int left_tensor_id, const std::string &left_tensor_name, unsigned int right_tensor_id, const std::string &right_tensor_name, const TensorShape &contracted_dims, const std::vector< int > &right_dims)
Definition: tensor_network.cpp:1007
exatn::numerics::TensorNetwork::updateConnections
void updateConnections(unsigned int tensor_id)
Definition: tensor_network.cpp:323
exatn::numerics::TensorNetwork::getOperationList
std::list< std::shared_ptr< TensorOperation > > & getOperationList(const std::string &contr_seq_opt_name="dummy")
Definition: tensor_network.cpp:1134
exatn::numerics::TensorNetwork::isEmpty
bool isEmpty() const
Definition: tensor_network.cpp:182
exatn::numerics::TensorNetwork::getTensorConn
TensorConn * getTensorConn(unsigned int tensor_id)
Definition: tensor_network.cpp:236
exatn::numerics::TensorNetwork::determineContractionSequence
double determineContractionSequence(ContractionSeqOptimizer &contr_seq_optimizer)
Definition: tensor_network.cpp:362
exatn::numerics::NetworkBuilder
Definition: network_builder.hpp:25
exatn::numerics::TensorNetwork::getTensorConnAll
std::vector< TensorConn * > getTensorConnAll()
Definition: tensor_network.cpp:244