Cytnx v1.0.0
Loading...
Searching...
No Matches
contraction_tree.hpp
Go to the documentation of this file.
1#ifndef CYTNX_CONTRACTION_TREE_H_
2#define CYTNX_CONTRACTION_TREE_H_
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
6#include "UniTensor.hpp"
7#include "utils/utils.hpp"
8#include <vector>
9#include <map>
10#include <string>
11#include <memory>
12
13namespace cytnx {
15 class Node : public std::enable_shared_from_this<Node> {
16 public:
17 UniTensor utensor;
18 bool is_assigned;
19 std::shared_ptr<Node> left;
20 std::shared_ptr<Node> right;
21 std::weak_ptr<Node> root;
22 std::string name;
23
24 Node() : is_assigned(false) {}
25
26 Node(const Node& rhs)
27 : utensor(rhs.utensor),
28 is_assigned(rhs.is_assigned),
29 left(rhs.left),
30 right(rhs.right),
31 name(rhs.name) {
32 // Only copy root if it exists
33 if (auto r = rhs.root.lock()) {
34 root = r;
35 }
36 }
37
38 Node& operator=(const Node& rhs) {
39 if (this != &rhs) {
40 utensor = rhs.utensor;
41 is_assigned = rhs.is_assigned;
42 left = rhs.left;
43 right = rhs.right;
44 name = rhs.name;
45 if (auto r = rhs.root.lock()) {
46 root = r;
47 }
48 }
49 return *this;
50 }
51
52 Node(std::shared_ptr<Node> in_left, std::shared_ptr<Node> in_right,
53 const UniTensor& in_uten = UniTensor())
54 : is_assigned(false), left(in_left), right(in_right) {
55 // Set name based on children
56 if (left && right) {
57 name = "(" + left->name + "," + right->name + ")";
58 }
59
60 if (in_uten.uten_type() != UTenType.Void) {
61 utensor = in_uten;
62 }
63 }
64
65 void set_root_ptrs() {
66 try {
67 auto self = shared_from_this();
68
69 if (left) {
70 left->root = self;
71 left->set_root_ptrs();
72 }
73
74 if (right) {
75 right->root = self;
76 right->set_root_ptrs();
77 }
78 } catch (const std::bad_weak_ptr& e) {
79 std::cerr << "Failed to set root ptrs for node " << name << ": " << e.what() << std::endl;
80 throw;
81 }
82 }
83
84 void clear_utensor() {
85 if (left) {
86 left->clear_utensor();
87 left->root.reset();
88 }
89 if (right) {
90 right->clear_utensor();
91 right->root.reset();
92 }
93 is_assigned = false;
94 utensor = UniTensor();
95 }
96
97 void assign_utensor(const UniTensor& in_uten) {
98 utensor = in_uten;
99 is_assigned = true;
100 }
101 };
102
103 class ContractionTree {
104 public:
105 std::vector<std::shared_ptr<Node>> nodes_container; // intermediate layer
106 std::vector<std::shared_ptr<Node>> base_nodes; // bottom layer
107
108 ContractionTree() = default;
109 ContractionTree(const ContractionTree&) = default;
110 ContractionTree& operator=(const ContractionTree&) = default;
111
112 void clear() {
113 nodes_container.clear();
114 base_nodes.clear();
115 }
116
117 void reset_contraction_order() {
118 // First clear all root pointers
119 for (auto& node : base_nodes) {
120 if (node) node->root.reset();
121 }
122 // Then clear the container
123 nodes_container.clear();
124 }
125
126 void reset_nodes() {
127 // Clear from root down if we have nodes
128 if (!nodes_container.empty() && nodes_container.back()) {
129 nodes_container.back()->clear_utensor();
130 }
131 nodes_container.clear();
132
133 // Reset base nodes
134 for (auto& node : base_nodes) {
135 if (node) {
136 node->is_assigned = false;
137 node->utensor = UniTensor();
138 }
139 }
140 }
141
142 void build_default_contraction_tree();
143 void build_contraction_tree_by_tokens(const std::map<std::string, cytnx_uint64>& name2pos,
144 const std::vector<std::string>& tokens);
145 };
147} // namespace cytnx
148
149#endif // CYTNX_CONTRACTION_TREE_H_
Definition Accessor.hpp:12
UniTensorType_class UTenType
UniTensor type.