我有一个计算选定维度上的部分跟踪的 python 实现。
def partial_trace(rho, keep, dims):
"""Calculate the partial trace
Parameters
----------
ρ : 2D array
Matrix to trace
keep : array
An array of indices of the spaces to keep after
being traced. For instance, if the space is
A x B x C x D and we want to trace out B and D,
keep = [0,2]
dims : array
An array of the dimensions of each space.
For instance, if the space is A x B x C x D,
dims = [dim_A, dim_B, dim_C, dim_D]
Returns
-------
ρ_a : 2D array
Traced matrix
"""
dims = np.asarray(dims)
N = dims.size
# Indices to trace
rest = np.delete(np.arange(N), keep)
# Reshape into tensor
rho_a = rho.reshape(np.tile(dims, 2))
# Trace indices
for i,d in enumerate(rest[::-1]):
rho_a = np.trace(rho_a, axis1=d, axis2=N-i+d)
# Reshape into matrix
N = np.prod(dims[keep])
return rho_a.reshape(N,N)
我想计算部分痕迹但是当我尝试构建外部产品时遇到了内存不足错误。相反,我想直接从.
我将如何在 Python 中做到这一点?