forked from stoianmihail/Netzwerk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
388 lines (306 loc) · 9.96 KB
/
gen.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import argparse
seed = 123
import random
random.seed(seed)
def random_int(lower, upper):
return random.randint(lower, upper)
def random_leg_dim(f):
c = random_int(1, 100)
if c <= 50:
return random_int(f, f**2)
elif c <= 90:
return random_int(f**2, f**3)
return random_int(f**3, f**4)
class Generator:
def __init__(self, tn_type, size, leg_type, factor):
self.tn_type = tn_type
self.max_size = size
self.leg_type = leg_type
self.factor = factor
self.cache = {}
self.reset()
def makeEdge(self, u, v, leg_dim):
leg = (min(u, v), max(u, v))
if leg not in self.legs:
self.legs[leg] = leg_dim
else:
assert 0
# TODO: later put here min(., .) for powers of two.
pass
def set_leg_dim(self, leg_dim, count=1, open=False):
# Choose random leg dimension.
assert count >= 1
# Open leg?
if open:
return self.factor**count
# Otherwise, choose randomly.
if leg_dim is None:
leg_dim = random_leg_dim(self.factor)**count
return leg_dim
def get_vertex(self, c):
if c not in self.vertices:
self.vertices[c] = self.vertex_index
self.vertex_index += 1
return self.vertices[c]
def open(self, c, count, leg_dim = None):
# Set the leg dimension.
leg_dim = self.set_leg_dim(leg_dim, count, open=True)
# Get the vertex.
u = self.get_vertex(c)
# Insert the open leg.
self.open_legs[u] = leg_dim
def connect(self, c1, c2, leg_dim = None):
# Set the leg dimension.
leg_dim = self.set_leg_dim(leg_dim)
# Get the vertices. Note: the order matters for a nice output.
u = self.get_vertex(c2)
v = self.get_vertex(c1)
# Make the edge.
self.makeEdge(u, v, leg_dim)
def genFTPS(self):
print(f'Generate FTPS')
# The expected number of vertices.
def expected_size(w, h):
return w * (h + 1)
def buildFTPS(w, h):
print(f'[ftps] w={w}, h={h}')
# Reset.
self.reset()
# Compute the expected number of nodes.
num_nodes = expected_size(w, h)
for i in range(w):
# Bind to left on the main spine.
if i:
self.connect((i, 0), (i - 1, 0))
# Add an open leg for each node on the main spine.
self.open((i, 0), 1)
# Build the chain.
for j in range(1, h + 1):
# Connect with the previous node.
self.connect((i, j), (i, j - 1))
# Add an open leg.
self.open((i, j), 1)
# Assert correctness of the build process.
assert len(self.vertices) == num_nodes
assert len(self.legs) == num_nodes - 1
assert len(self.open_legs) == num_nodes
# Build all FTPS with size under `max_size`.
for w in range(2, self.max_size + 1):
for h in range(1, self.max_size + 1):
# Check if we need to build this.
if expected_size(w, h) > self.max_size:
continue
# Build.
buildFTPS(w, h)
# And flush.
self.flush(params = {
'w' : w,
'h' : h
})
# Flush all.
self.flush_all()
def genMERA(self):
print(f'Generate MERA')
# The expected number of vertices.
def expected_size(h):
num_nodes = 0
for i in range(h):
num_nodes += 2**i
extra_nodes = 0
for i in range(1, h):
extra_nodes += 2**i - 1
return num_nodes + extra_nodes, extra_nodes
def buildMERA(h):
print(f'[mera] h={h}')
# Reset.
self.reset()
num_nodes, extra_nodes = expected_size(h)
for i in range(h):
for j in range(2**i):
# Skip the first level.
if not i:
continue
# Connect to parent only if we are on the border.
if not j or j == (2**i) - 1:
self.connect((i, j), (i - 1, j // 2))
# Insert open leg only if we are on the border of the last layer.
if i == h - 1 and (not j or j == (2**i) - 1):
self.open((i, j), 1)
# Insert the intermediate nodes.
for i in range(1, h):
for j in range(1, 2**i):
# Connect to layer above.
self.connect((h + i, j), (i, j - 1))
self.connect((h + i, j), (i, j))
# Connect to layer below, if we are not on the last layer.
if i != h - 1:
self.connect((h + i, j), (i + 1, j))
self.connect((h + i, j), (i + 1, j + 1))
else:
# Otherwise insert an open leg.
self.open((h + i, j), 2)
assert len(self.vertices) == num_nodes
assert len(self.legs) == (num_nodes - extra_nodes) + 2 * extra_nodes - 1
assert len(self.open_legs) == 2**(h - 1) + 1
for h in range(2, self.max_size + 1):
# Check if we need to build this.
if expected_size(h)[0] > self.max_size:
continue
buildMERA(h)
self.flush(params = {
'h' : h
})
# Flush all.
self.flush_all()
def genTTN(self):
print(f'Generate TTN')
# The expected number of vertices.
def expected_size(h):
num_nodes = 0
for i in range(h):
num_nodes += 2**i
return num_nodes
def buildTTN(h):
print(f'[ttn] h={h}')
# Reset.
self.reset()
num_nodes = expected_size(h)
for i in range(h):
for j in range(2**i):
# Skip the first level.
if not i:
continue
print(f'i={i}, j={j}')
# Connect with the parent.
self.connect((i, j), (i - 1, j // 2))
if i == h - 1:
self.open((i, j), 2)
assert len(self.vertices) == num_nodes
assert len(self.legs) == num_nodes - 1
assert len(self.open_legs) == 2**(h - 1)
for h in range(2, self.max_size + 1):
# Check if we need to build this.
if expected_size(h) > self.max_size:
continue
buildTTN(h)
self.flush(params = {
'h' : h
})
# Flush all.
self.flush_all()
def genMPS(self):
print(f'Generate MEPS')
pass
def genPEPS(self):
# The expected number of vertices.
def expected_size(w, h):
return w * h
print(f'Generate PEPS')
def buildPEPS(w, h):
print(f'[peps] w={w}, h={h}')
# Reset.
self.reset()
num_nodes = expected_size(w, h)
for i in range(w):
for j in range(h):
if i:
self.connect((i, j), (i - 1, j))
if j:
self.connect((i, j), (i, j - 1))
# Add the open leg.
self.open((i, j), 1)
print(f'v={len(self.vertices)}, num_nodes={num_nodes}')
assert len(self.vertices) == num_nodes
assert len(self.legs) == (w - 1) * h + (h - 1) * w
assert len(self.open_legs) == num_nodes
for w in range(2, self.max_size + 1):
for h in range(2, self.max_size + 1):
# Check if we need to build this.
if expected_size(w, h) > self.max_size:
continue
# Build.
buildPEPS(w, h)
# Flush.
self.flush(params = {
'w' : w,
'h' : h
})
# Flush all.
self.flush_all()
def reset(self):
self.vertices = {}
self.vertex_index = 0
self.legs = {}
self.open_legs = {}
def flush_file(self, params, vertices, legs, open_legs):
# Ensure the directory exists.
import os
os.system(f'mkdir -p data/{self.tn_type}')
# Build the parameters.
params_str = '_'.join([f'{elem[0]}-{elem[1]}' for elem in params.items()])
# Create the file name.
file_name = f'{len(vertices)}_{len(legs)}_{self.tn_type}_{self.leg_type}_{params_str}'
# Open the file.
with open(f'data/{self.tn_type}/{file_name}.in', 'w') as f:
# Flush the number of vertices and legs.
f.write(f'{len(vertices)} {len(legs)} {len(open_legs) if self.leg_type == "open" else 0}\n')
# Flush the closed legs.
for edge in legs:
f.write(f'{edge[0]} {edge[1]} {legs[edge]}\n')
# Flush the open legs, if required.
if self.leg_type == 'open':
for leg in open_legs:
f.write(f'{leg} {open_legs[leg]}\n')
def flush_all(self):
print(f'Flushing to disk..')
for n in self.cache:
self.flush_file(self.cache[n]['params'],
self.cache[n]['vertices'],
self.cache[n]['legs'],
self.cache[n]['open_legs']
)
def flush(self, params):
assert len(self.vertices) <= self.max_size
# Do we store only the optimal parameters for special classes of tensor networks?
if self.tn_type in ['ftps', 'peps']:
if params['w'] > params['h']:
return
n = len(self.vertices)
if ((n in self.cache) and (params['w'] > self.cache[n]['params']['w'])) or (n not in self.cache):
self.cache[n] = {
'params' : params,
'vertices' : self.vertices,
'legs' : self.legs,
'open_legs' : self.open_legs
}
return
# Flush file.
self.flush_file(params, self.vertices, self.legs, self.open_legs)
def gen(tn_type, size, leg_type, factor):
# Create the generator.
gen = Generator(tn_type, size, leg_type, factor)
# Run the corresponding function.
getattr(gen, f'gen{tn_type.upper()}')()
def main():
parser = argparse.ArgumentParser(description='Generate Tensor Networks')
parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
optional = parser.add_argument_group('optional arguments')
# Required.
required.add_argument('-t', '--type', type=str,
choices=['mera', 'peps', 'mps', 'mpo', 'ftps', 'ttn'], help='Type of tensor network',
required=True)
required.add_argument('-s', '--size', type=int,
help='Maximal number of tensors',
required=True)
# Optional.
optional.add_argument('-l', '--leg_type', type=str, default='closed',
choices=['open', 'closed'], help='Type of legs')
optional.add_argument('-f', '--factor', type=int, default=2,
help='Start factor for bond dimensions')
# Parse.
args = parser.parse_args()
# Generate.
gen(args.type, args.size, args.leg_type, args.factor)
if __name__ == '__main__':
main()