简体   繁体   English

Python中的矩阵求幂

[英]Matrix exponentiation in Python

I'm trying to exponentiate a complex matrix in Python and am running into some trouble. 我正在尝试对Python中的复杂矩阵求幂,并且遇到了一些麻烦。 I'm using the scipy.linalg.expm function, and am having a rather strange error message when I try the following code: 我正在使用scipy.linalg.expm函数,并且在尝试以下代码时出现了一个非常奇怪的错误消息:

import numpy as np
from scipy import linalg

hamiltonian = np.mat('[1,0,0,0;0,-1,0,0;0,0,-1,0;0,0,0,1]')

# This works
t_list = np.linspace(0,1,10)
unitary = [linalg.expm(-(1j)*t*hamiltonian) for t in t_list]

# This doesn't
t_list = np.linspace(0,10,100)
unitary = [linalg.expm(-(1j)*t*hamiltonian) for t in t_list]

The error when the second experiment is run is: 运行第二个实验时的错误是:

This works!
Traceback (most recent call last):
  File "matrix_exp.py", line 11, in <module>
    unitary_t = [linalg.expm(-1*t*(1j)*hamiltonian) for t in t_list]
  File "/usr/lib/python2.7/dist-packages/scipy/linalg/matfuncs.py",     line 105, in expm
    return scipy.sparse.linalg.expm(A)
  File "/usr/lib/python2.7/dist- packages/scipy/sparse/linalg/matfuncs.py", line 344, in expm
    X = _fragment_2_1(X, A, s)
  File "/usr/lib/python2.7/dist-  packages/scipy/sparse/linalg/matfuncs.py", line 462, in _fragment_2_1
    X[k, k] = exp_diag[k]
TypeError: only length-1 arrays can be converted to Python scalars

This seems really strange since all I changed was the range of t I was using. 这似乎真的很奇怪,因为我所更改的只是我使用的范围t Is it because the Hamiltonian is diagonal? 是因为哈密顿量是对角线吗? In general, the Hamiltonians won't be, but I also want it to work for diagonal ones. 通常,汉密尔顿主义者不会,但我也希望它适用于对角线。 I don't really know the mechanics of expm , so any help would be greatly appreciated. 我真的不太了解expm ,因此不胜感激。

That is interesting. 这太有趣了。 One thing I can say is that the problem is specific to the np.matrix subclass. 我可以说的一件事是,该问题特定于np.matrix子类。 For example, the following works fine: 例如,以下工作正常:

h = np.array(hamiltonian)
unitary = [linalg.expm(-(1j)*t*h) for t in t_list]

Digging a little deeper into the traceback, the exception is being raised in _fragment_2_1 in scipy.sparse.linalg.matfuncs.py , specifically these lines : 深入研究追溯, _fragment_2_1中引发了异常,特别是以下 scipy.sparse.linalg.matfuncs.py

n = X.shape[0]
diag_T = T.diagonal().copy()

# Replace diag(X) by exp(2^-s diag(T)).
scale = 2 ** -s
exp_diag = np.exp(scale * diag_T)
for k in range(n):
    X[k, k] = exp_diag[k]

The error message 错误讯息

    X[k, k] = exp_diag[k]
TypeError: only length-1 arrays can be converted to Python scalars

suggests to me that exp_diag[k] ought to be a scalar, but is instead returning a vector (and you can't assign a vector to X[k, k] , which is a scalar). 给我建议exp_diag[k]应该是一个标量,但是返回一个向量(并且您不能将向量分配给标量X[k, k] )。

Setting a breakpoint and examining the shapes of these variables confirms this: 设置断点并检查这些变量的形状可以确认这一点:

ipdb> l
    751     # Replace diag(X) by exp(2^-s diag(T)).
    752     scale = 2 ** -s
    753     exp_diag = np.exp(scale * diag_T)
    754     for k in range(n):
    755         import ipdb; ipdb.set_trace()  # breakpoint e86ebbd4 //
--> 756         X[k, k] = exp_diag[k]
    757 
    758     for i in range(s-1, -1, -1):
    759         X = X.dot(X)
    760 
    761         # Replace diag(X) by exp(2^-i diag(T)).

ipdb> exp_diag.shape
(1, 4)
ipdb> exp_diag[k].shape
(1, 4)
ipdb> X[k, k].shape
()

The underlying problem is that exp_diag is assumed to be either 1D or a column vector, but the diagonal of an np.matrix object is a row vector. 潜在的问题是, exp_diag被假定为1D或列向量,但是np.matrix对象的对角线是行向量。 This highlights a more general point that np.matrix is generally less well-supported than np.ndarray , so in most cases it's better to use the latter. 这突出了一个更普遍的观点,即np.matrix通常不如np.ndarray更好的支持,因此在大多数情况下,最好使用后者。

One possible solution would be to use np.ravel() to flatten diag_T into a 1D np.ndarray : 一种可能的解决方案是使用np.ravel()diag_T展平为一维np.ndarray

diag_T = np.ravel(T.diagonal().copy())

This seems to fix the problem you're encountering, although there may be other issues relating to np.matrix that I haven't spotted yet. 尽管似乎还没有发现与np.matrix有关的其他问题,但这似乎可以解决您遇到的问题。


I've opened a pull request here . 我在这里打开了请求请求。

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

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