简体   繁体   English

在python中计算NxN矩阵的熵

[英]Calculating the Entropy of a NxN matrix in python

I have a NxN matrix where all the elements are having values between [-1, 1].我有一个 NxN 矩阵,其中所有元素的值都在 [-1, 1] 之间。 I can calculate Shannon's Entropy manually, but I want something like Von Neumann's Entropy.我可以手动计算香农熵,但我想要冯诺依曼熵之类的东西。 Is there any inbuilt function in Numpy/Scipy? Numpy/Scipy 中是否有任何内置函数? Manual method will also do.手动方法也行。 Matrix is usually of size 100x100.矩阵的大小通常为 100x100。 Something like this.像这样的东西。

[[-0.244608 -0.71395497 -0.36534627]  
[-0.44626849 -0.82385746 -0.74654582]
[ 0.38240205 -0.58970239  0.67858516]]

Thank You.谢谢你。

What about just finding eigenvalues?仅仅找到特征值怎么样? Untested pseudo-code未经测试的伪代码

import numpy as np
from numpy import linalg as LA

M = ... # this is your matrix

e, v = LA.eig(M)

t = e * np.log(e)

return -np.sum(t)

UPDATE更新

Looking at companion site, this answer might be of an interest to you查看配套站点,您可能会对这个答案感兴趣

https://cs.stackexchange.com/questions/56261/computing-von-neumann-entropy-efficiently https://cs.stackexchange.com/questions/56261/computing-von-neumann-entropy-efficiently

UPDATE更新

If you don't want to go via eigenvalues/polynomials, then you could compute log of the matrix (everything else is trivial) using Jordan decomposition to get Jordan normal form of a matrix .如果您不想通过特征值/多项式,那么您可以使用 Jordan 分解计算矩阵的对数(其他一切都是微不足道的)以获得矩阵的 Jordan 标准形式 In python it could be done via SymPy, http://docs.sympy.org/0.7.1/modules/matrices.html#sympy.matrices.matrices.Matrix.jordan_form , check Compute Jordan normal form of matrix in Python / NumPy also for details.在 python 中,它可以通过 SymPy 完成, http: //docs.sympy.org/0.7.1/modules/matrices.html#sympy.matrices.matrices.Matrix.jordan_form ,检查Compute Jordan normal form of matrix in Python / NumPy也了解详情。

Then log(M) could be computed from Jordan form using Gantmacher 1959 theorem, check this paper https://www.ams.org/journals/proc/1966-017-05/S0002-9939-1966-0202740-6/S0002-9939-1966-0202740-6.pdf for simplified explanation, especially eqns 3.4-3.8然后可以使用 Gantmacher 1959 定理从 Jordan 形式计算 log(M),查看这篇论文https://www.ams.org/journals/proc/1966-017-05/S0002-9939-1966-0202740-6/S0002 -9939-1966-0202740-6.pdf用于简化解释,尤其是 eqns 3.4-3.8

But I bet you a donut Jordan normal form of your matrix will be complex.但我敢打赌,你的矩阵的甜甜圈 Jordan 标准形式会很复杂。

You can define von Neumann entropy in one of two ways according to Nielsen & Chuang in "Quantum Computation and Quantum Information".根据 Nielsen & Chuang 在“Quantum Computation and Quantum Information”中的说法,您可以通过两种方式之一定义冯诺依曼熵。 It can be defined either in terms of (the negative of) the trace of the matrix times its own (matrix) logarithm...or...it can be defined in terms of the eigenvalues.它可以根据(的负数)矩阵的迹乘以它自己的(矩阵)对数来定义……或者……它可以根据特征值来定义。 The above examples all take logarithms base e, but you need base 2. In order to do this you'll need a base change in your computation.上面的例子都采用以 e 为底的对数,但你需要以 2 为底。为了做到这一点,你需要在计算中改变底数。 Here are two functions in Python which can be used, one for each version of the definition of von Neumann entropy (of a density operator say):以下是 Python 中可以使用的两个函数,一个用于每个版本的冯诺依曼熵定义(例如密度算子):

For the trace version对于跟踪版本

def von_neumann_entropy(rho):
    import numpy as np
    from scipy import linalg as la
    R = rho*(la.logm(rho)/la.logm(np.matrix([[2]])))
    S = -np.matrix.trace(R)
    return(S)

For the eigenvalue version对于特征值版本

def vn_eig_entropy(rho):
    import numpy as np
    from scipy import linalg as la
    import math as m
    EV = la.eigvals(rho)

    # Drop zero eigenvalues so that log2 is defined
    my_list = [x for x in EV.tolist() if x]
    EV = np.array(my_list)

    log2_EV = np.matrix(np.log2(EV))
    EV = np.matrix(EV)
    S = -np.dot(EV, log2_EV.H)
    return(S)

These will return the same value, so it does not matter which you use.这些将返回相同的值,因此您使用哪个并不重要。 Just feed one of these functions a square matrix using something like只需使用类似的东西将这些函数之一提供一个方阵

rho = np.matrix([[5/6, 1/6],
                 [1/6, 1/6]])

Obviously any square matrix will work, not just a 2x2, this is just to give you an example.显然任何方阵都可以工作,而不仅仅是 2x2,这只是给你一个例子。 If your matrix has zero eigenvalues, the convention is to set 0*log(0) terms equal to zero.如果您的矩阵的特征值为零,则惯例是将 0*log(0) 项设置为零。 This is taken care of by the second function vn_eig_entropy .这是由第二个函数vn_eig_entropy All density matrices are "non-negative definite", so this is the only issue with eigenvalues you should run into.所有密度矩阵都是“非负定的”,因此这是您应该遇到的唯一特征值问题。 I know this response is a bit late, but maybe it will help someone else.我知道这个回复有点晚了,但也许它会帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM