随着时间的数值使用numpy,非连续的eingeNVector

发布于 2025-01-31 22:02:28 字数 3436 浏览 3 评论 0 原文

我正在以数值和象征性的方式解决时间依赖的特征值问题,但它们没有提供相同的解决方案。特征向量的时间依赖性不同。

import sympy as sp
import numpy as np
from sympy.physics.matrices import msigma
from sympy.physics.quantum import TensorProduct as ts
from sympy.physics.quantum.dagger import Dagger
import matplotlib.pyplot as plt

首先使用数值解决方案:

#---- writing the matriz----------------
σx = np.matrix([[0,1],[1,0]])
σy = np.matrix([[0,-1j],[1j,0]])
σz = np.matrix([[1,0],[0,-1]])
I = np.eye(2)


Hi = np.kron(np.kron(σx,σx),I) + np.kron(np.kron(σy,σy),I)
Hm = np.kron(I, np.kron(σx,σx)) + np.kron(I,np.kron(σy,σy))
Hf = np.kron(σz, np.kron(I,σz)) + np.kron(I,np.kron(σz,σz))



s = np.linspace(0.00001,.9999,1000)
ω = s.shape[0]
e_dic = {f"e{i}": [] for i in range(8)}
ev_dic = {f"ev{i}": [] for i in range(8)}
for t in s:
    Had = (1-t)*Hi + t*(1-t)*Hm + t*Hf
    vals,vecs = np.linalg.eigh(Had)
    for e,v in zip(e_dic,vals):
        e_dic[e].append(np.real_if_close(v,tol=10))
    for ev, i in zip(ev_dic, range(8)):
        w = np.real_if_close(vecs[:,i],tol=10)
        ev_dic[ev].append(w)

作为 np.linalg.eigh 的输出,eigenValues及时连续:

for i in e_dic:
    y = e_dic[i]
    plt.plot(s,y,'-',lw=1,label=i)
plt.show()

”

但是eigenvector并不连续(and)使用 np.linalg.eig scipy.linalg.eig 结果甚至最差):

for e in ev_dic:
    a = ev_dic[e]
    for i in range(8):
        y = []
        for j in range(len(a)):
            y.append(float(a[j][i]))
        plt.plot(s,np.real(y),'-',lw=1)
plt.show()

”

使用Synspy解决问题,我可以看到我可以看到特征向量是连续的

I, x, y, z = sp.eye(2), msigma(1), msigma(2), msigma(3)

t = sp.symbols(r't', real=True, positive=True)
f = sp.Function('f')(t)
g = sp.Function('g')(t)

f = t
g = t

Hi = (ts(x,x,I)+ts(y,y,I))
Hm = (ts(I,x,x)+ts(I,y,y))
Hf = (ts(z,I,z) + ts(I,z,z))

Had = (1-f)*Hi + f*(1-f)*Hm + g*Hf

e_mult_ev = Had.eigenvects()

e = []
ev = []
for i in range(len(e_mult_ev)):
    val, mult, [v_i, v_j] = e_mult_ev[i]
    e.append(val)
    e.append(val)
    ev.append(v_i)
    ev.append(v_j)

evN = [vec/sp.sqrt((Dagger(vec)*vec)[0]) for vec in ev] 

这就是我绘制Sympy特征向量的方式:

for i in zip(evN):
    for j in range(8):
        a = i[0][j]
        x = a.evalf()
        if x != 0:
            if x == 1:
                x_vals = np.linspace(0.01, .999, 1000, dtype=complex)
                y_vals = [ 1 for x in x_vals]
                plt.plot(np.real(x_vals),np.real(y_vals),lw=2)
            else:
                lam_x = sp.lambdify(t, x, modules=['mpmath'])

                x_vals = np.linspace(0.01, .999, 1000, dtype=complex)

                y_vals = []
                for _x in x_vals:
                    y_vals.append(complex(lam_x(_x)))
                plt.plot(np.real(x_vals),np.real(y_vals),lw=2)
plt.show()

由于某些原因,特征向量分量正在混合,我不确定如何正确解决此问题

I'm solving a time dependent eigenvalue problem numerically and symbolic but they are not giving the same solution. The time dependency of the eigenvectors are different.

import sympy as sp
import numpy as np
from sympy.physics.matrices import msigma
from sympy.physics.quantum import TensorProduct as ts
from sympy.physics.quantum.dagger import Dagger
import matplotlib.pyplot as plt

First using the numerical solution I have:

#---- writing the matriz----------------
σx = np.matrix([[0,1],[1,0]])
σy = np.matrix([[0,-1j],[1j,0]])
σz = np.matrix([[1,0],[0,-1]])
I = np.eye(2)


Hi = np.kron(np.kron(σx,σx),I) + np.kron(np.kron(σy,σy),I)
Hm = np.kron(I, np.kron(σx,σx)) + np.kron(I,np.kron(σy,σy))
Hf = np.kron(σz, np.kron(I,σz)) + np.kron(I,np.kron(σz,σz))



s = np.linspace(0.00001,.9999,1000)
ω = s.shape[0]
e_dic = {f"e{i}": [] for i in range(8)}
ev_dic = {f"ev{i}": [] for i in range(8)}
for t in s:
    Had = (1-t)*Hi + t*(1-t)*Hm + t*Hf
    vals,vecs = np.linalg.eigh(Had)
    for e,v in zip(e_dic,vals):
        e_dic[e].append(np.real_if_close(v,tol=10))
    for ev, i in zip(ev_dic, range(8)):
        w = np.real_if_close(vecs[:,i],tol=10)
        ev_dic[ev].append(w)

As the output of np.linalg.eigh are ordered the eigenvalues are continuous in time:

for i in e_dic:
    y = e_dic[i]
    plt.plot(s,y,'-',lw=1,label=i)
plt.show()

enter image description here

But the eigenvector aren't continuous (and using np.linalg.eig or scipy.linalg.eig the results are even worsts):

for e in ev_dic:
    a = ev_dic[e]
    for i in range(8):
        y = []
        for j in range(len(a)):
            y.append(float(a[j][i]))
        plt.plot(s,np.real(y),'-',lw=1)
plt.show()

enter image description here

Solving the problem using Sympy I can see that the eigenvectors are continuous

enter image description here

I, x, y, z = sp.eye(2), msigma(1), msigma(2), msigma(3)

t = sp.symbols(r't', real=True, positive=True)
f = sp.Function('f')(t)
g = sp.Function('g')(t)

f = t
g = t

Hi = (ts(x,x,I)+ts(y,y,I))
Hm = (ts(I,x,x)+ts(I,y,y))
Hf = (ts(z,I,z) + ts(I,z,z))

Had = (1-f)*Hi + f*(1-f)*Hm + g*Hf

e_mult_ev = Had.eigenvects()

e = []
ev = []
for i in range(len(e_mult_ev)):
    val, mult, [v_i, v_j] = e_mult_ev[i]
    e.append(val)
    e.append(val)
    ev.append(v_i)
    ev.append(v_j)

evN = [vec/sp.sqrt((Dagger(vec)*vec)[0]) for vec in ev] 

This is how I've plotted the Sympy eigenvectors:

for i in zip(evN):
    for j in range(8):
        a = i[0][j]
        x = a.evalf()
        if x != 0:
            if x == 1:
                x_vals = np.linspace(0.01, .999, 1000, dtype=complex)
                y_vals = [ 1 for x in x_vals]
                plt.plot(np.real(x_vals),np.real(y_vals),lw=2)
            else:
                lam_x = sp.lambdify(t, x, modules=['mpmath'])

                x_vals = np.linspace(0.01, .999, 1000, dtype=complex)

                y_vals = []
                for _x in x_vals:
                    y_vals.append(complex(lam_x(_x)))
                plt.plot(np.real(x_vals),np.real(y_vals),lw=2)
plt.show()

For some reason the eigenvectors components are mixing I'm not sure how to proper solve this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜你已久 2025-02-07 22:02:28

您可以从sympy看到每个特征值都具有多重性:

In [56]: Had.shape
Out[56]: (8, 8)

In [57]: len(Had.eigenvals())
Out[57]: 4

In [58]: Had.charpoly().as_expr().factor()
Out[58]: 
                                                                                         2
          2 ⎛   3      2          4        3        2                    3       2      ⎞ 
(-λ + 2⋅t) ⋅⎝- λ  - 2⋅λ ⋅t + 4⋅λ⋅t  - 8⋅λ⋅t  + 8⋅λ⋅t  - 8⋅λ⋅t + 4⋅λ + 8⋅t  - 16⋅t  + 8⋅t⎠ 

这意味着对于每个特征值,都有可能的特征向量的2D空间。 np.linalg.eigh(hast)函数必须以某种方式选择两个特定的向量,它们跨越了特征空间,但并未唯一确定哪个向量应该是。不连续性仅在于如何做出选择。特征空间在 t 中是连续的,但是即使归一化,此处的特征向量的概念也不是唯一的。

You can see from SymPy that every eigenvalue has multiplicity two:

In [56]: Had.shape
Out[56]: (8, 8)

In [57]: len(Had.eigenvals())
Out[57]: 4

In [58]: Had.charpoly().as_expr().factor()
Out[58]: 
                                                                                         2
          2 ⎛   3      2          4        3        2                    3       2      ⎞ 
(-λ + 2⋅t) ⋅⎝- λ  - 2⋅λ ⋅t + 4⋅λ⋅t  - 8⋅λ⋅t  + 8⋅λ⋅t  - 8⋅λ⋅t + 4⋅λ + 8⋅t  - 16⋅t  + 8⋅t⎠ 

What this means is that for each eigenvalue there is a 2D space of possible eigenvectors. The np.linalg.eigh(Had) function has to somehow choose two particular vectors that span that eigenspace but it is not uniquely determined which vectors those should be. The discontinuities are just in how that choice is made. The eigenspaces are continuous in t but the concept of an eigenvector here is not uniquely defined even if normalised.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文