forked from smorita/Tensordot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdt.py
executable file
·316 lines (261 loc) · 9.95 KB
/
tdt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
import sys
import argparse
import logging
import time
import config
import netcon
# Global variables
TENSOR_NAMES = []
TENSOR_MATH_NAMES = []
BOND_NAMES = []
BOND_DIMS = []
VECTORS = []
FINAL_ORDER = None
class Tensor:
def __init__(self, name = None, bonds = []):
if name == None:
self.name = []
elif isinstance(name, list):
self.name = name[:]
else:
self.name = [name]
self.bonds = bonds[:]
def __repr__(self):
return "Tensor(" + str(self.name) + ", " + str(self.bonds) + ")"
def __str__(self):
return str(self.name) + ", " + str(self.bonds)
class Bond:
def __init__(self, t0 = -1, t1 = -1):
self.t0 = t0
self.t1 = t1
def __str__(self):
return "({0}, {1})".format(self.t0, self.t1)
def isFree(self):
return (self.t0 < 0 or self.t1 < 0)
def connect(self, tensor_index):
assert self.isFree(), "edge already connected to two tensors"
if self.t0 < 0:
self.t0 = tensor_index
else:
assert not self.t0 == tensor_index, "edge connects to the same tensor"
self.t1 = tensor_index
def has(self, tensor_index):
return (self.t0 == tensor_index or self.t1 == tensor_index)
class TensorNetwork:
def __init__(self):
self.tensors = []
self.bonds = []
self.total_memory = 0.0
self.max_memory = 0.0
self.cpu_cost = 0.0
def __str__(self):
s = ""
for i, t in enumerate(self.tensors):
s += "tensor {0} : {1}\n".format(i, t)
for i, b in enumerate(self.bonds):
s += "bond {0} : {1}, {2} {3}\n".format(i, BOND_NAMES[i], b, BOND_DIMS[i])
s += "memory : {0}\n".format(self.total_memory)
s += "cpu : {0}\n".format(self.cpu_cost)
return s
def clone(self):
tn = TensorNetwork()
tn.total_memory = self.total_memory
tn.max_memory = self.max_memory
tn.cpu_cost = self.cpu_cost
tn.bonds = [Bond(b.t0, b.t1) for b in self.bonds]
tn.tensors = [Tensor(t.name, t.bonds) for t in self.tensors]
return tn
def output_log(self, prefix=""):
if not prefix == "": prefix += " "
for i, t in enumerate(self.tensors):
logging.info(prefix + "tensor{0} : {1} {2}".format(i, TENSOR_NAMES[i], t.bonds))
for i,b in enumerate(self.bonds):
logging.info(prefix + "bond{0} : {1} {2} {3}".format(i, BOND_NAMES[i], b, BOND_DIMS[i]))
def add_tensor(self, t_name, b_names):
t_index = len(self.tensors)
b_indexs = []
for b in b_names:
if b not in BOND_NAMES:
self.bonds.append(Bond())
BOND_NAMES.append(b)
BOND_DIMS.append(config.DEFAULT_BOND_DIM)
i = BOND_NAMES.index(b)
self.bonds[i].connect(t_index)
b_indexs.append(i)
TENSOR_NAMES.append(t_name)
self.tensors.append(Tensor(t_index, b_indexs))
def find_bonds(self, tensor_a, tensor_b):
bonds_a = self.tensors[tensor_a].bonds
bonds_b = self.tensors[tensor_b].bonds
contract = [b for b in bonds_a if b in bonds_b]
replaced_a = [b for b in bonds_a if b not in bonds_b]
replaced_b = [b for b in bonds_b if b not in bonds_a]
return contract, replaced_a, replaced_b
def contract(self, t0, t1, bc, br0, br1):
tn = self.clone()
# create the contracted tensor
t_new = tn.tensors[t0]
## change names of tensors using Reverse Polish Notation
t_new.name = self.tensors[t0].name+self.tensors[t1].name+[-1]
## remove contracted bonds
for b in bc: t_new.bonds.remove(b)
## add bonds from deleted tensor
for b in br1: t_new.bonds.append(b)
# clear the removed tensor
tn.tensors[t1] = Tensor()
# update bonds
bonds = tn.bonds
## remove contracted bonds from the bond list
for b in bc: bonds[b].t0 = bonds[b].t1 = -1
## change bond connections
old_idx = t1
new_idx = t0
for b in br1:
if bonds[b].t0 == old_idx: bonds[b].t0 = new_idx
elif bonds[b].t1 == old_idx: bonds[b].t1 = new_idx
return tn
"""Generate mathematical formula from Reverse Polish Notation"""
stack = []
for c in rpn:
if c == -1:
t1 = stack.pop()
t0 = stack.pop()
new_name = "(" + t0 + "*" + t1 + ")"
stack.append(new_name)
else:
stack.append(TENSOR_MATH_NAMES[c])
return stack[0]
#def get_connect_list_in(tn_orig, rpn, connect_list_in):
def get_connect_list_in(tn_orig, rpn):
"""Generate tensordot script from Reverse Polish Notation"""
tn = tn_orig.clone()
index = []
name = []
i = 1
for c in rpn:
#print(index)
if c == -1:
index1 = index.pop()
index0 = index.pop()
#print("index0", index0, "index1", index1)
findices = sum([tn.tensors[index0].bonds,tn.tensors[index1].bonds], [])#flatten
print([x for x in set(findices) if findices.count(x) > 1])
for d in [x for x in set(findices) if findices.count(x) > 1]:
for j in range(lenTN):
for k in range(len(connect_list_in[j])):
if connect_list_in[j][k] == BOND_NAMES[d]:
connect_list_in[j][k] = i
i += 1
#print(index)
undep = [x for x in set(findices) if findices.count(x) == 1]
tn.tensors[index0].bonds = undep
tn.tensors[index1].bonds = []
#t0 = tn.tensors[index0]
#t1 = tn.tensors[index1]
#bc, br0, br1 = tn.find_bonds(index0, index1)
#axes0 = [t0.bonds.index(b) for b in bc]
#axes1 = [t1.bonds.index(b) for b in bc]
#tn = tn.contract(index0, index1, bc, br0, br1)
#print(undep)
#print(tn)
#exit()
index.append(index0)
else:
index.append(c)
return
#return connect_list_in
def read_file(infile, tn):
"""Read input file"""
global FINAL_ORDER
for line in infile:
data = line.split()
if data == []: continue
command = data[0].lower()
if command == "style":
set_style(data[1].lower())
elif command == "numpy":
config.NUMPY = data[1]
elif command == "indent":
config.INDENT = " " * int(data[1])
elif command == "default_dimension":
# Should be set the top of input file.
config.DEFAULT_BOND_DIM = int(data[1])
elif command == "debug" or command == "verbose":
config.LOGGING_LEVEL = logging.DEBUG
elif command == "tensor":
tn.add_tensor(data[1], data[2:])
elif command == "bond":
for b in data[1:-1]: set_bond_dim(b, int(data[-1]))
elif command == "bond_dim":
for b in data[2:]: set_bond_dim(b, int(data[1]))
elif command == "order":
FINAL_ORDER = data[1:]
elif command == "vector":
VECTORS.append((data[1], data[2]))
infile.close()
def check_bond_order(tn):
return FINAL_ORDER == None or \
frozenset(FINAL_ORDER) == frozenset(BOND_NAMES[i] for i, b in enumerate(tn.bonds) if b.isFree())
def check_vector():
for v in VECTORS:
if v[1] not in BOND_NAMES: return False
return True
def parse_args():
parser = argparse.ArgumentParser(description = "Code generator for tensor contraction")
parser.add_argument('-s', metavar = 'style', dest = 'style',
type = str, default = None,
choices = ['numpy', 'mptensor', 'julia'],
help = 'set output style ("numpy" or "mptensor" or "julia")')
parser.add_argument('-v', '--verbose', action = 'store_true', dest = 'verbose',
help = 'verbose mode')
parser.add_argument('-o', metavar = 'outfile', dest = 'outfile',
type = argparse.FileType('w'), default = sys.stdout,
help = 'write the result to outfile')
parser.add_argument('infile',
type = argparse.FileType('r'),
help = 'tensor-network definition file')
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
tn = TensorNetwork()
# Read input file
read_file(args.infile, tn)
# Overwrite by command-line option
set_style(args.style)
if args.verbose:
config.LOGGING_LEVEL = logging.DEBUG
assert len(tn.tensors) > 0, "No tensor."
assert len(tn.bonds) > 0, "No bond."
assert check_bond_order(tn), "Final bond order is invalid."
assert check_vector(), "Vectors will be put on non-existent bond."
logging.basicConfig(format = "%(levelname)s:%(message)s", level = config.LOGGING_LEVEL)
tn.output_log("input")
rpn, cpu = netcon.NetconOptimizer(tn.tensors, BOND_DIMS).optimize()
#set tensor_list
tensor_list = "Any["
lenTN = len(TENSOR_NAMES)
for i in range(lenTN):
tensor_list += TENSOR_NAMES[i]
if i != lenTN - 1:
tensor_list += ", "
tensor_list += "]"
connect_list_in = [[BOND_NAMES[i] for i in tn.tensors[j].bonds] for j in range(len(tn.tensors))]
#set FINAL_ORDER if it is None
if FINAL_ORDER == None:
fcli = sum(connect_list_in, []) #list flatten
FINAL_ORDER = [x for x in set(fcli) if fcli.count(x) == 1]
#set output minus index
for i in range(len(FINAL_ORDER)):
for j in range(lenTN):
for k in range(len(connect_list_in[j])):
if connect_list_in[j][k] == FINAL_ORDER[i]:
connect_list_in[j][k] = -i-1
else:
continue
break
else:
continue
break
get_connect_list_in(tn, rpn)
print(connect_list_in)