Matplotlib-在绘制具有相同尺寸的2个值时出错

时间:2018-10-13 12:38:36

标签: python-3.x matplotlib matrix physics

def Hamiltonian(alpha,h):

    Sx = np.array([[0,1],[1,0]])
    Sy = np.array([[0,-1j],[1j,0]])
    Sz = np.array([[1,0],[0,-1]])
    I  = np.array([[1,0],[0,1]])

    H = -1*((alpha*np.kron(np.kron(Sx,Sx),I))
       + (alpha*np.kron(np.kron(Sy,Sy),I))
       + (alpha*np.kron(np.kron(I,Sx),Sx))
       + (alpha*np.kron(np.kron(I,Sy),Sy))
       + (h*np.kron(np.kron(I,Sz),I)))

    return H
np.set_printoptions(linewidth=100)
Hamiltonian(1,0.5).real

哪个返回以下矩阵(为清楚起见,只需输入此矩阵即可) enter image description here

定义哈密顿量后,我想根据h参数来寻找熵。对于这类问题,代码背后的物理原理并不重要。

# von Neumann entropy as a function of h and beta - Complete
# Definition of a mixed state: [Thermal Density Matrix used]
h = np.arange(0,2.5,0.1)
beta = 2
for i in range(h.size):
    H = Hamiltonian(1.0, h[i] )
    rho_thermal = expm(-1.0 * beta * H)
    tr = np.trace(rho_thermal)
    rho_thermal = rho_thermal / tr
    np.set_printoptions(linewidth=100)
    eigvals_rho_thermal, eigvecs_rho_thermal = LA.eigh(rho_thermal)
    # Entropy
    s = 0.0
    for i in range(eigvals_rho_thermal.size):
        s += -1.0 * (eigvals_rho_thermal[i] * np.log(eigvals_rho_thermal[i]))
    print(s)
plt.plot(h,s)

我的问题是为什么会出现以下错误: enter image description here

我的代码为s返回25个值,为h返回25个值,为什么不绘制它们?

1 个答案:

答案 0 :(得分:1)

您需要将在每个循环步骤中获得的chdir($dirpath); $filelist = glob("*.html"); 的值存储在某个位置,以便以后可以为每个s绘制每个s。通常,您可以稍微简化代码,并使用函数代替for循环。 这就是我的做法。

h

enter image description here