-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensorcontract.py
81 lines (72 loc) · 2.56 KB
/
tensorcontract.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 8 15:02:33 2019
@author: User
"""
import numpy as np
class tensor:
def __init__(self,index):
self.index = index
noe = np.prod(index)
self.t = np.zeros(noe).reshape(index)
#------------------------------------partial trace----------------------------------------------------
def ptrace(self,i,j):
if(self.index[i] != self.index[j]):
print("dimensions dont match!")
newindex = self.index.copy()
del newindex[i]
del newindex[j-1]
if newindex == []:
d = sum(self.t[l,l] for l in range(self.index[i]))
return d
else :
d = tensor(newindex)
for k,value in np.ndenumerate(d.t):
m = list(k)
for l in range(self.index[i]):
if l == 0:
m.insert(i,l)
m.insert(j,l)
else:
m[i] = l
m[j] = l
n = tuple(m)
d.t[k] = d.t[k] + self.t[n]
return d
#---------------------------------contract two indices------------------------------------------------------------------
def contract(self,b,index):
newindex = self.index + b.index
d = tensor(newindex)
for j,v in np.ndenumerate(self.t):
for k,w in np.ndenumerate(b.t):
i = list(j+k)
m = tuple(i)
d.t[m] = (v*w)
if index == []:
return d
else:
return d.ptrace(index[0],index[1])
def contractmultiple(self,b,index):
newindex = self.index + b.index
d = tensor(newindex)
for j,v in np.ndenumerate(self.t):
for k,w in np.ndenumerate(b.t):
i = list(j+k)
m = tuple(i)
d.t[m] = (v*w)
if index == []:
return d
else:
for i in index:
d = d.ptrace(i[0],i[1])
return d
#-----------------------------delta------------------------------------------------------------------
class delta(tensor):
def __init__(self,index):
tensor.__init__(self,index)
for i,value in np.ndenumerate( self.t ):
if len(set(i))==1:
self.t[i] = 1
t1 = delta([2])
t2 = delta([2])
t3 = (t1.contract(t2,[0,1]))