﻿ 基于numpy.einsum的张量网络计算_python_开心洋葱网
• 欢迎访问开心洋葱网站，在线教程，推荐使用最新版火狐浏览器和Chrome浏览器访问本网站，欢迎加入开心洋葱 QQ群
• 为方便开心洋葱网用户，开心洋葱官网已经开启复制功能！
• 欢迎访问开心洋葱网站，手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~！
• 由于近期流量激增，小站的ECS没能经的起亲们的访问，本站依然没有盈利，如果各位看如果觉着文字不错，还请看官给小站打个赏~~~~~~~~~~~~~！

2074次浏览

[隐藏]

# 1.张量与张量网络

x = [1, 0]
y = [0, 1, 0]
z = [1, 2, 3, 4]


M = [[1, -1], [-1, 1]]
N = [[1, 3], [2, 4], [5, 6]]


pi = 3.14
P = [[[1]]]
Q = [[[1, 1, 1], [1, 1, 1], [1, 1, 1]]]


print (P[0][0][0])


import numpy as np
M = np.random.rand(2, 2)
v = np.random.rand(2)
w = np.dot(M, v)
print (M)
print (v)
print (w)


[[0.09660039 0.55849787]
[0.93007524 0.32329559]]
[0.74966152 0.59573188]
[0.40513259 0.88983912]


import numpy as np
A = np.random.rand(1, 2, 2, 2)
B = np.random.rand(2, 2, 2)
C = np.einsum('ijkl,klm->ijm', A, B)
print ('A:', A)
print ('B:', B)
print ('C:', C)


A: [[[[0.85939221 0.43684494]
[0.71895754 0.31222944]]

[[0.49276976 0.13639093]
[0.04176578 0.14400289]]]]
B: [[[0.21157005 0.58052674]
[0.59166167 0.21243451]]

[[0.11420572 0.98995349]
[0.1145634  0.97101076]]]
C: [[[0.5581652  1.60661377]
[0.20621996 0.49621469]]]


# 2.张量缩并顺序与计算复杂性

import numpy as np
M = np.random.rand(2, 2)
v = np.random.rand(2)
path_info = np.einsum_path('ij,j->i', M, v, optimize='greedy')
print(path_info[0])
print(path_info[1])


['einsum_path', (0, 1)]
Complete contraction:  ij,j->i
Naive scaling:  2
Optimized scaling:  2
Naive FLOP count:  8.000e+00
Optimized FLOP count:  9.000e+00
Theoretical speedup:  0.889
Largest intermediate:  2.000e+00 elements
--------------------------------------------------------------------------
scaling                  current                                remaining
--------------------------------------------------------------------------
2                     j,ij->i                                     i->i



import numpy as np
A = np.random.rand(1, 2, 2, 2)
B = np.random.rand(2, 2, 2)
path_info = np.einsum_path('ijkl,klm->ijm', A, B, optimize='greedy')
print(path_info[0])
print(path_info[1])


['einsum_path', (0, 1)]
Complete contraction:  ijkl,klm->ijm
Naive scaling:  5
Optimized scaling:  5
Naive FLOP count:  3.200e+01
Optimized FLOP count:  3.300e+01
Theoretical speedup:  0.970
Largest intermediate:  4.000e+00 elements
--------------------------------------------------------------------------
scaling                  current                                remaining
--------------------------------------------------------------------------
5               klm,ijkl->ijm                                 ijm->ijm


import numpy as np
np.random.seed(123)
a = np.random.rand(2, 2, 2)
b = np.random.rand(2, 2, 2, 2)
c = np.random.rand(2, 2, 2)
d = np.random.rand(2, 2)
path_info = np.einsum_path('ijk,jlmn,klo,mo->in', a, b, c, d, optimize='greedy')
print(path_info[0])
print(path_info[1])


['einsum_path', (2, 3), (1, 2), (0, 1)]
Complete contraction:  ijk,jlmn,klo,mo->in
Naive scaling:  7
Optimized scaling:  5
Naive FLOP count:  5.120e+02
Optimized FLOP count:  1.290e+02
Theoretical speedup:  3.969
Largest intermediate:  8.000e+00 elements
--------------------------------------------------------------------------
scaling                  current                                remaining
--------------------------------------------------------------------------
4                 mo,klo->klm                         ijk,jlmn,klm->in
5               klm,jlmn->jkn                              ijk,jkn->in
4                 jkn,ijk->in                                   in->in


# 3.张量分割对张量网络缩并复杂性的影响

import numpy as np
np.random.seed(123)
a = np.random.rand(2)
b = np.random.rand(2)
c = np.random.rand(2, 2, 2, 2)
d = np.random.rand(2)
e = np.random.rand(2)
path_info = np.einsum_path('i,j,ijkl,k,l', a, b, c, d, e, optimize='greedy')
print(path_info[0])
print(path_info[1])


['einsum_path', (0, 2), (0, 3), (0, 2), (0, 1)]
Complete contraction:  i,j,ijkl,k,l->
Naive scaling:  4
Optimized scaling:  4
Naive FLOP count:  8.000e+01
Optimized FLOP count:  6.100e+01
Theoretical speedup:  1.311
Largest intermediate:  8.000e+00 elements
--------------------------------------------------------------------------
scaling                  current                                remaining
--------------------------------------------------------------------------
4                 ijkl,i->jkl                              j,k,l,jkl->
3                   jkl,j->kl                                 k,l,kl->
2                     kl,k->l                                    l,l->
1                       l,l->                                       ->


import numpy as np
np.random.seed(123)
a = np.random.rand(2)
b = np.random.rand(2)
c = np.random.rand(2, 2, 2)
d = np.random.rand(2, 2, 2)
e = np.random.rand(2)
f = np.random.rand(2)
path_info = np.einsum_path('i,j,imk,jml,k,l', a, b, c, d, e, f, optimize='greedy')
print(path_info[0])
print(path_info[1])


['einsum_path', (0, 2), (0, 1), (0, 2), (0, 1), (0, 1)]
Complete contraction:  i,j,imk,jml,k,l->
Naive scaling:  5
Optimized scaling:  3
Naive FLOP count:  1.920e+02
Optimized FLOP count:  5.300e+01
Theoretical speedup:  3.623
Largest intermediate:  4.000e+00 elements
--------------------------------------------------------------------------
scaling                  current                                remaining
--------------------------------------------------------------------------
3                   imk,i->km                           j,jml,k,l,km->
3                   jml,j->lm                              k,l,km,lm->
2                     km,k->m                                 l,lm,m->
2                     lm,l->m                                    m,m->
1                       m,m->                                       ->


# 4.补充测试

import networkx as nx
graph = nx.Graph()
nx.draw_networkx(graph)


import networkx as nx
graph = nx.Graph()
nx.draw_networkx(graph)


import numpy as np
np.random.seed(123)
a = np.random.rand(2)
b = np.random.rand(2)
c = np.random.rand(2)
d = np.random.rand(2, 2, 2, 2)
e = np.random.rand(2, 2, 2, 2)
f = np.random.rand(2, 2, 2, 2)
g = np.random.rand(2)
h = np.random.rand(2)
i = np.random.rand(2)
path_info = np.einsum_path('i,j,k,ijlm,mnko,lpoq,p,n,q', a, b, c, d, e, f, g, h, i, optimize='greedy')
print(path_info[0])
print(path_info[1])


['einsum_path', (0, 3), (1, 2), (1, 2), (0, 3), (0, 2), (0, 1), (0, 1), (0, 1)]
Complete contraction:  i,j,k,ijlm,mnko,lpoq,p,n,q->
Naive scaling:  9
Optimized scaling:  4
Naive FLOP count:  4.608e+03
Optimized FLOP count:  1.690e+02
Theoretical speedup:  27.266
Largest intermediate:  8.000e+00 elements
--------------------------------------------------------------------------
scaling                  current                                remaining
--------------------------------------------------------------------------
4                 ijlm,i->jlm                j,k,mnko,lpoq,p,n,q,jlm->
4                 mnko,k->mno                   j,lpoq,p,n,q,jlm,mno->
4                 p,lpoq->loq                      j,n,q,jlm,mno,loq->
3                   jlm,j->lm                         n,q,mno,loq,lm->
3                   mno,n->mo                            q,loq,lm,mo->
3                   loq,q->lo                               lm,mo,lo->
3                   mo,lm->lo                                  lo,lo->
2                     lo,lo->                                       ->


import numpy as np
np.random.seed(123)
a = np.random.rand(2)
b = np.random.rand(2)
c = np.random.rand(2)
d = np.random.rand(2, 2, 2)
e = np.random.rand(2, 2, 2)
f = np.random.rand(2, 2, 2)
g = np.random.rand(2, 2, 2)
h = np.random.rand(2, 2, 2)
i = np.random.rand(2, 2, 2)
j = np.random.rand(2)
k = np.random.rand(2)
l = np.random.rand(2)
path_info = np.einsum_path('i,j,k,iml,jmn,nop,kpq,lrs,sqt,r,o,t', a, b, c, d, e, f, g, h, i, j, k, l, optimize='greedy')
print(path_info[0])
print(path_info[1])


['einsum_path', (0, 3), (0, 2), (0, 2), (0, 4), (0, 2), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)]
Complete contraction:  i,j,k,iml,jmn,nop,kpq,lrs,sqt,r,o,t->
Naive scaling:  12
Optimized scaling:  3
Naive FLOP count:  4.915e+04
Optimized FLOP count:  1.690e+02
Theoretical speedup:  290.840
Largest intermediate:  4.000e+00 elements
--------------------------------------------------------------------------
scaling                  current                                remaining
--------------------------------------------------------------------------
3                   iml,i->lm       j,k,jmn,nop,kpq,lrs,sqt,r,o,t,lm->
3                   jmn,j->mn          k,nop,kpq,lrs,sqt,r,o,t,lm,mn->
3                   kpq,k->pq             nop,lrs,sqt,r,o,t,lm,mn,pq->
3                   o,nop->np                lrs,sqt,r,t,lm,mn,pq,np->
3                   r,lrs->ls                   sqt,t,lm,mn,pq,np,ls->
3                   t,sqt->qs                      lm,mn,pq,np,ls,qs->
3                   mn,lm->ln                         pq,np,ls,qs,ln->
3                   np,pq->nq                            ls,qs,ln,nq->
3                   qs,ls->lq                               ln,nq,lq->
3                   nq,ln->lq                                  lq,lq->
2                     lq,lq->                                       ->


# 7.参考链接

1. 什么是张量网络（tensor network）？ – 何史提的回答 – 知乎 https://www.zhihu.com/question/54786880/answer/147099121
2. Michael Streif1, Martin Leib，”Training the Quantum Approximate Optimization Algorithm without access to a Quantum Processing Unit“, 2019, arXiv:1908.08862

[开心洋葱]

• 版权声明

本站的文章和资源来自互联网或者站长的原创，按照 CC BY -NC -SA 3.0 CN协议发布和共享，转载或引用本站文章应遵循相同协议。如果有侵犯版权的资源请尽快联系站长，我们会在24h内删除有争议的资源。
• 合作网站

• 友情链接

• 关于我们

一群热爱思考，热爱生活，有理想的新社会主义接班人的多维思维学习平台，天行健，君子以自强不息。地势坤，君子以厚德载物。
……