-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncon.py
196 lines (166 loc) · 7.26 KB
/
ncon.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
# ncon network contractor
# by Glen Evenbly (c) for www.tensors.net, (v1.2) - last modified 6/2020
# ncon.py
import numpy as np
from typing import List, Union, Tuple, Optional
def ncon(tensors: List[np.ndarray],
connects: List[Union[List[int], Tuple[int]]],
con_order: Optional[Union[List[int], str]] = None,
check_network: Optional[bool] = True,
which_env: Optional[int] = 0):
"""
Network CONtractor: contracts a tensor network of N tensors via a sequence
of (N-1) tensordot operations. More detailed instructions and examples can
be found at: https://arxiv.org/abs/1402.0939.
Args:
tensors: list of the tensors in the network.
connects: length-N list of lists (or tuples) specifying the network
connections. The jth entry of the ith list in connects labels the edge
connected to the jth index of the ith tensor. Labels should be positive
integers for internal indices and negative integers for free indices.
con_order: optional argument to specify the order for contracting the
positive indices. Defaults to ascending order if omitted. Can also be
set at "greedy" or "full" to call a solver to automatically determine
the order.
check_network: if true then the input network is checked for consistency;
this can catch many common user mistakes for defining networks.
which_env: if provided, ncon will produce the environment of the requested
tensor (i.e. the network given by removing the specified tensor from
the original network). Only valid for networks with no open indices.
Returns:
Union[np.ndarray,float]: the result of the network contraction; an
np.ndarray if the network contained open indices, otherwise a scalar.
"""
num_tensors = len(tensors)
tensor_list = [tensors[ele] for ele in range(num_tensors)]
connect_list = [np.array(connects[ele]) for ele in range(num_tensors)]
# generate contraction order if necessary
flat_connect = np.concatenate(connect_list)
if con_order is None:
con_order = np.unique(flat_connect[flat_connect > 0])
else:
con_order = np.array(con_order)
# check inputs if enabled
if check_network:
dims_list = [list(tensor.shape) for tensor in tensor_list]
check_inputs(connect_list, flat_connect, dims_list, con_order)
# do all partial traces
for ele in range(len(tensor_list)):
num_cont = len(connect_list[ele]) - len(np.unique(connect_list[ele]))
if num_cont > 0:
tensor_list[ele], connect_list[ele], cont_ind = partial_trace(
tensor_list[ele], connect_list[ele])
con_order = np.delete(
con_order,
np.intersect1d(con_order, cont_ind, return_indices=True)[1])
# do all binary contractions
while len(con_order) > 0:
# identify tensors to be contracted
cont_ind = con_order[0]
locs = [
ele for ele in range(len(connect_list))
if sum(connect_list[ele] == cont_ind) > 0
]
# do binary contraction
cont_many, A_cont, B_cont = np.intersect1d(
connect_list[locs[0]],
connect_list[locs[1]],
assume_unique=True,
return_indices=True)
if np.size(tensor_list[locs[0]]) < np.size(tensor_list[locs[1]]):
ind_order = np.argsort(A_cont)
else:
ind_order = np.argsort(B_cont)
tensor_list.append(
np.tensordot(
tensor_list[locs[0]],
tensor_list[locs[1]],
axes=(A_cont[ind_order], B_cont[ind_order])))
connect_list.append(
np.append(
np.delete(connect_list[locs[0]], A_cont),
np.delete(connect_list[locs[1]], B_cont)))
# remove contracted tensors from list and update con_order
del tensor_list[locs[1]]
del tensor_list[locs[0]]
del connect_list[locs[1]]
del connect_list[locs[0]]
con_order = np.delete(
con_order,
np.intersect1d(con_order, cont_many, return_indices=True)[1])
# do all outer products
while len(tensor_list) > 1:
s1 = tensor_list[-2].shape
s2 = tensor_list[-1].shape
tensor_list[-2] = np.outer(tensor_list[-2].reshape(np.prod(s1)),
tensor_list[-1].reshape(np.prod(s2))).reshape(
np.append(s1, s2))
connect_list[-2] = np.append(connect_list[-2], connect_list[-1])
del tensor_list[-1]
del connect_list[-1]
# do final permutation
if len(connect_list[0]) > 0:
return np.transpose(tensor_list[0], np.argsort(-connect_list[0]))
else:
return tensor_list[0].item()
def partial_trace(A, A_label):
""" Partial trace on tensor A over repeated labels in A_label """
num_cont = len(A_label) - len(np.unique(A_label))
if num_cont > 0:
dup_list = []
for ele in np.unique(A_label):
if sum(A_label == ele) > 1:
dup_list.append([np.where(A_label == ele)[0]])
cont_ind = np.array(dup_list).reshape(2 * num_cont, order='F')
free_ind = np.delete(np.arange(len(A_label)), cont_ind)
cont_dim = np.prod(np.array(A.shape)[cont_ind[:num_cont]])
free_dim = np.array(A.shape)[free_ind]
B_label = np.delete(A_label, cont_ind)
cont_label = np.unique(A_label[cont_ind])
B = np.zeros(np.prod(free_dim))
A = A.transpose(np.append(free_ind, cont_ind)).reshape(
np.prod(free_dim), cont_dim, cont_dim)
for ip in range(cont_dim):
B = B + A[:, ip, ip]
return B.reshape(free_dim), B_label, cont_label
else:
return A, A_label, []
def check_inputs(connect_list, flat_connect, dims_list, con_order):
""" Check consistancy of NCON inputs"""
pos_ind = flat_connect[flat_connect > 0]
neg_ind = flat_connect[flat_connect < 0]
# check that lengths of lists match
if len(dims_list) != len(connect_list):
raise ValueError(
('mismatch between %i tensors given but %i index sublists given') %
(len(dims_list), len(connect_list)))
# check that tensors have the right number of indices
for ele in range(len(dims_list)):
if len(dims_list[ele]) != len(connect_list[ele]):
raise ValueError((
'number of indices does not match number of labels on tensor %i: '
'%i-indices versus %i-labels')
% (ele, len(dims_list[ele]), len(connect_list[ele])))
# check that contraction order is valid
if not np.array_equal(np.sort(con_order), np.unique(pos_ind)):
raise ValueError(('NCON error: invalid contraction order'))
# check that negative indices are valid
for ind in np.arange(-1, -len(neg_ind) - 1, -1):
if sum(neg_ind == ind) == 0:
raise ValueError(('NCON error: no index labelled %i') % (ind))
elif sum(neg_ind == ind) > 1:
raise ValueError(('NCON error: more than one index labelled %i') % (ind))
# check that positive indices are valid and contracted tensor dimensions match
flat_dims = np.array([item for sublist in dims_list for item in sublist])
for ind in np.unique(pos_ind):
if sum(pos_ind == ind) == 1:
raise ValueError(('NCON error: only one index labelled %i') % (ind))
elif sum(pos_ind == ind) > 2:
raise ValueError(
('NCON error: more than two indices labelled %i') % (ind))
cont_dims = flat_dims[flat_connect == ind]
if cont_dims[0] != cont_dims[1]:
raise ValueError(
('NCON error: tensor dimension mismatch on index labelled %i: '
'dim-%i versus dim-%i') % (ind, cont_dims[0], cont_dims[1]))
return True