用sympy.physics.quantum简化量子表达

use*_*500 13 python sympy states orthogonal

与python同情:

from sympy import sqrt
from sympy.physics.quantum import Bra,Ket,qapply
superpos = (Ket('Dead')+Ket('Alive'))/sqrt(2)
d = qapply(Bra('Dead')*superpos)
Run Code Online (Sandbox Code Playgroud)

它给:

sqrt(2)*<Dead|Alive>/2 + sqrt(2)*<Dead|Dead>/2
Run Code Online (Sandbox Code Playgroud)

如何将'Dead'和'Alive'设置为正交状态,以便d.doit()给出:

sqrt(2)/2
Run Code Online (Sandbox Code Playgroud)

(我只能这样做:

d.subs(Bra('Dead')*Ket('Dead'),1).subs(Bra('Dead')*Ket('Alive'),0)
Run Code Online (Sandbox Code Playgroud)

但我相信有更好的方法)

Pet*_*ain 4

您的问题是InnerProduct不知道如何计算这些值,因此保留了未简化的表达式。查看源代码,我发现它尝试调用_eval_innerproduct()Ket上面写着这一点。

def _eval_innerproduct(self, bra, **hints):
    """Evaluate the inner product betweeen this ket and a bra.

    This is called to compute <bra|ket>, where the ket is ``self``.

    This method will dispatch to sub-methods having the format::

        ``def _eval_innerproduct_BraClass(self, **hints):``

    Subclasses should define these methods (one for each BraClass) to
    teach the ket how to take inner products with bras.
    """
Run Code Online (Sandbox Code Playgroud)

因此,您应该能够通过创建 2 个新Bra类和一个Ket实现 2 个方法的新类来解决您的问题 - 一个用于评估每个内积(使用上面规定的命名约定)。

为了完整起见,您可能还想为正交状态实现另一个Ket,并确保dual_class在每种情况下返回正确的类。