使用python sympy:
from sympy import sqrt
from sympy.physics.quantum import Bra,Ket,qapply
superpos = (Ket('Dead')+Ket('Alive'))/sqrt(2)
d = qapply(Bra('Dead')*superpos)
它给出:
sqrt(2)*<Dead|Alive>/2 + sqrt(2)*<Dead|Dead>/2
如何将“dead”和“alive”设置为正交状态,以便d.doit()给出:
sqrt(2)/2
(我只能这样做:
d.subs(Bra('Dead')*Ket('Dead'),1).subs(Bra('Dead')*Ket('Alive'),0)
但我相信有更好的方法)
最佳答案
您的问题是,InnerProduct不知道如何计算这些值,因此留下了不简单的表达式。查看source,我看到它试图调用_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.
"""
因此,您应该能够通过创建两个新的
Bra
类和一个实现两种方法的新的Ket
类来解决您的问题-一个方法用于评估每个内部产品(使用上面指定的命名约定)。为了完整性,您可能还希望为正交状态实现另一个
Ket
,并确保在每种情况下dual_class
都返回正确的类。关于python - 用sympy.physics.quantum简化量子表达,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42423148/