python中计算两个参数的函数时出现维度不匹配错误

2024-09-28 22:27:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我输入一对参数,函数将进行计算。但当我尝试在网格点计算函数时,它失败了,说“ValueError:dimension mismatch”。我将代码粘贴到下面。有人能帮忙解决这个问题吗

import numpy as np
from qutip import*
import pylab as plt
from numpy import eye,pi,kron,random,vdot,absolute,power,sqrt,matmul
from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show

j0= tensor(sigmaz(),identity(2),identity(2))
def otoc(k0,n):
      u0=(-1j*k0/6 *(tensor(sigmaz(),sigmaz(),identity(2)))).expm()
      jn= u0.inv()**n*j0*u0**n
      return ((commutator(jn,j0)* commutator(j0,jn)).tr()/8)

k0 = np.arange(0,4,0.1)
n = np.linspace(0,50,50)
X,Y = meshgrid(k0, n) 
Z = otoc(X, Y)

显示的错误如下所示

Traceback (most recent call last):

  File "/Users/sreerampg/Dropbox/myself/programs/levy's lemma/untitled8.py", line 27, in <module>
    Z = otoc(X, Y)

  File "/Users/sreerampg/Dropbox/myself/programs/levy's lemma/untitled8.py", line 20, in otoc
    u0=(-1j*k0/6 *(tensor(sigmaz(),sigmaz(),identity(2)))).expm()

  File "/Applications/anaconda3/lib/python3.7/site-packages/qutip/qobj.py", line 599, in __rmul__
    return other * self.data

  File "/Applications/anaconda3/lib/python3.7/site-packages/scipy/sparse/base.py", line 550, in __rmul__
    return (self.transpose() * tr).transpose()

  File "/Applications/anaconda3/lib/python3.7/site-packages/cvxpy/interface/scipy_wrapper.py", line 31, in new_method
    return method(self, other)

  File "/Applications/anaconda3/lib/python3.7/site-packages/scipy/sparse/base.py", line 516, in __mul__
    raise ValueError('dimension mismatch')

ValueError: dimension mismatch

Tags: infrompyimportreturnlineidentityfile