为什么在 tensordot 中没有保留跟踪

问题描述

我正在尝试取三个密度矩阵的张量积并在乘积基础上表达它。这些矩阵中的每一个都有迹线 1,理论上,乘积矩阵也应该如此。但是在 numpy 中这样做似乎有一些意想不到的效果。甚至将中间数组重塑为二维形式也能给出相同的答案。

In [31]: rho1
Out[31]: 
array([[0.,0.,0. ],[0.,0.1,0.2,0.3,0.4]])

In [32]: np.trace(rho1)
Out[32]: 1.0

In [33]: rho2
Out[33]: 
array([[0.2,0.2]])

In [34]: np.trace(rho2)
Out[34]: 1.0

In [35]: rho3
Out[35]: 
array([[0.5,0.5,0. ]])

In [36]: np.trace(rho3)
Out[36]: 1.0

In [37]: rho = np.tensordot(rho1,np.tensordot(rho2,rho3,axes=0),axes=0)

In [38]: np.trace(rho.reshape(125,125))
Out[38]: 0.010000000000000002

In [39]: rho = np.tensordot(rho1,axes=0).reshape(25,25),axes=0)

In [40]: np.trace(rho.reshape(125,125))
Out[40]: 0.010000000000000002

我真的在这代码中找不到任何错误,所以我觉得我误解了 tensordot 和 reshape 是如何工作的。但我并没有真正从文档中得到任何东西。有人可以帮我解决这个问题吗?

解决方法

简答:

我猜您正在寻找 kronecker 张量积:np.kron():

rho = np.kron(rho1,np.kron(rho2,rho3))

这次:

np.trace(rho)
Out[22]: 1.0000000000000002

详情:

为什么您的解决方案不起作用?

因为 np.reshape() 不会以“正确”的维度顺序重塑您的数组,您最终可以排列某些维度以获得所需的结果:

rho = np.moveaxis(np.tensordot(rho1,np.moveaxis(np.tensordot(rho1,rho2,axes=0),[0,1,2,3],3,1]).reshape((5,5)),1]).reshape((125,125))

会输出正确的结果,但是由于存在np.kron,所以有点矫枉过正。

实际上您认为 np.tensordot(rho1,axes=0) 相当于:

np.einsum('ik,jl',rho1,rho2)

但实际上 np.tensordot(rho1,axes=0) 计算:

np.einsum('ij,kl',rho2)

因此获得正确答案的另一种方法是使用:

np.einsum('ik,rho2).reshape(5,5)
,

tensordot 文档确实说轴 = 0 给出了张量积:

axes = 0 : tensor product :math:`a\otimes b`,

数组的外积。但是你必须注意结果维度是如何排列的。

但它说:

The shape of the result consists of the non-contracted axes of the
first tensor,followed by the non-contracted axes of the second.

使用 axes=0,它实际上创建了 rho2[:,:,None]rho3[:,None,:],并在法向 dot 轴上计算乘积总和。

In [37]: x=np.tensordot(rho2,rho3,0)
In [38]: x.shape
Out[38]: (5,5,5)

将其与具有默认“ijkl”输出的 einsum 进行比较:

In [40]: y=np.einsum('ij,rho3)
In [41]: y.shape
Out[41]: (5,5)
In [42]: np.allclose(x,y)
Out[42]: True

dot 等价物:

In [44]: z=np.dot(rho2[:,None],rho3[:,:])
In [45]: np.allclose(x,z)
Out[45]: True

前两个维度来自 rho2

如果您真正想要的是 kron,我们必须首先转置维度,将 2 个数组中的维度交错:

In [46]: K=np.kron(rho2,rho3)
In [47]: K.shape
Out[47]: (25,25)
In [48]: np.trace?
In [49]: np.allclose(x.transpose(0,3).reshape(25,25),K)
Out[49]: True

测试跟踪:

In [50]: K.trace()
Out[50]: 1.0

einsum 相同。注意索引的顺序:

In [53]: np.einsum('ij,kl->ikjl',rho3).reshape(25,25).trace()
Out[53]: 1.0

另一种看待这个的方法是从 4d 数组中提取 2d 对角线值:

In [60]: j = np.arange(5); i=j[:,None]
In [61]: x[i,i,j,j]
Out[61]: 
array([[0.1,0.1,0.,0. ],[0.1,0. ]])
In [62]: _.sum()
Out[62]: 1.0

简单地重塑 x,将这些值放在对角线上:

In [63]: x[i,j]
Out[63]: 
array([[0.1,[0.,0. ]])

等效于 2 步跟踪:

In [75]: x.trace(0,1)
Out[75]: 
array([[0.5,0.5,0. ]])
In [76]: x.trace(0,1).trace()
Out[76]: 1.0
,

np.tensordot 不是张量积,而是张量的点积。张量积类似于外积。