It is interesting. I can only say that the problem is with the np.matrix subclass. For example, the following works just fine:
h = np.array(hamiltonian) unitary = [linalg.expm(-(1j)*t*h) for t in t_list]
Digging a little deeper into the trace, the exception is expressed in _fragment_2_1 in scipy.sparse.linalg.matfuncs.py , specifically these lines :
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]
Error message
X[k, k] = exp_diag[k] TypeError: only length-1 arrays can be converted to Python scalars
offers me that exp_diag[k] should be a scalar, but instead returns a vector (and you cannot assign a vector X[k, k] , which is a scalar).
Setting a breakpoint and examining the forms of these variables confirms this:
ipdb> l 751
The main problem is that exp_diag is considered either 1D or a column, but the diagonal of the np.matrix object is a row vector. This emphasizes the more general point that np.matrix is generally less well supported than np.ndarray , so in most cases it is better to use the latter.
One possible solution would be to use np.ravel() to smooth diag_T into 1D np.ndarray :
diag_T = np.ravel(T.diagonal().copy())
This seems to np.matrix issue you are facing, although there may be other issues related to np.matrix that I haven't noticed yet.
I opened the transfer request here .
ali_m
source share