Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Symphy Quantum中评估bra和ket的内积_Python_State_Sympy_Orthogonal - Fatal编程技术网

Python 在Symphy Quantum中评估bra和ket的内积

Python 在Symphy Quantum中评估bra和ket的内积,python,state,sympy,orthogonal,Python,State,Sympy,Orthogonal,在sympy中,我定义了两个KET和相应的胸罩,当我将胸罩应用于KET时 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|De

在sympy中,我定义了两个KET和相应的胸罩,当我将胸罩应用于KET时

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

但是如何让制动器自动评估?为什么
InnerProduct
对于相同的brakets不为1,对于标签不同的brakets不为0?

这并不是您想要的,但是您可以使用
量子位来创建正交状态

from sympy import sqrt
from sympy.physics.quantum import Dagger, qapply
from sympy.physics.quantum.qubit import Qubit

dead = Qubit(0)
alive = Qubit(1)
它们创建
Ket(0)
Ket(1)
。要制作胸罩,您可以使用
匕首
功能

print(Dagger(dead) * dead)
<0|0>

也许您正在寻找的是
expression.xreplace()
?根据
xreplace
函数可以获取一个字典,其中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)

mySubs = {Bra('Dead')*Ket('Dead'): 1,  Bra('Dead')*Ket('Alive'): 0} ##plus other bindings
d.xreplace(mySubs)
(注意:尚未对其进行测试…)


这至少为您提供了一个选项,可以在一个地方定义所有所需的替换,并在您喜欢的任何地方“重用”它们

Fock空间中的答案:

>>> from sympy import sqrt
>>> from sympy.physics.quantum import Dagger,qapply
>>> from sympy.physics.quantum.boson import BosonFockKet
>>> ket_Dead = BosonFockKet(0)
>>> ket_Alive = BosonFockKet(1)
>>> superpos = (ket_Dead+ket_Alive)/sqrt(2)
>>> bra_Dead = Dagger(ket_Dead)
>>> qapply(bra_Dead*superpos).doit()
sqrt(2)/2
在希尔伯特空间中也可能发生同样的事情吗?

您的问题是,它不知道如何计算这些值,因此保留了未简化的表达式。查看,我看到它试图在
Ket
上调用
\u eval\u innerproduct()

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.
    """
def_eval_内部产品(self、bra、**提示):
“评估这款胸罩和胸罩之间的内在产品。
这被称为compute,其中ket是“self”。
此方法将分派给具有以下格式的子方法:
``def_eval_innerproduct_BraClass(自,**提示):``
子类应该定义这些方法(每个括号类一个)来
教ket如何用胸罩取内衣。
"""
因此,您应该能够通过创建两个新的
Bra
类和一个新的
Ket
类来解决您的问题,该类实现了两个方法—一个用于评估每个内部产品(使用上面强制规定的命名约定)


为了完整性,您可能还希望为正交状态实现另一个
Ket
,并确保
dual\u类在每种情况下都返回正确的类。

正如Peter在回答中指出的,您需要自己实现一个新的Bra和Ket类。这是一个很好的正交状态的通用实现,您可以使用它

用法示例:

>>正交胸罩(n)*正交胸罩(n)
1.
>>>正交胸罩(n)*正交胸罩(n+1)
0
>>>正交B(n)*正交K(m)
实施:

类正交Ket(Ket):
@类方法
def双_级(自):
返回正交
def _eval_内衣(自我、文胸、**提示):
如果len(self.args)!=len(bra.args):
raise VALUERROR('不能将具有不同标签数的ket相乘')
对于范围内的i(len(self.args)):
diff=self.args[i]-bra.args[i]
diff.simplify()
如果差值不为零:
返回0
如果差异不为零:
一无所获
返回1
类正交文胸(文胸):
@类方法
def双_级(自):
返回正交曲线

感谢您的努力。不幸的是,我正在寻找一个更普遍的解决方案:与
qutip
(例如:
来自qutip import*
ket_I=basis(N,I)
)一样的N维基。这基本上与问题中的替换类型相同。我想自动执行内积,就像:
sympy.Matrix([1,0,0]).dot(sympy.Matrix([0,1,0]))
就是这样!我复制/粘贴boson.py将其重命名为OrthogonalBra/OrthogonalKet,并将innerproduct更改为'def\u eval\u innerproduct\u OrthogonalBra(self,bra,**提示):if self.n==bra.n:return 1 else:return 0` Symphy项目的相关PR。谢谢,这看起来很棒!但我想还没有一个版本包含它。我可以通过将定义复制粘贴到我自己的代码中来实现这一点,但我必须包含一个
.doit()
调用,以使其实际评估内部产品。没有这些,我仍然得到了表示法。是的,您可以调用表达式上的
expr.doit()
qapply(expr)
,类似于QM模块中的一些其他操作。
>>> from sympy import sqrt
>>> from sympy.physics.quantum import Dagger,qapply
>>> from sympy.physics.quantum.boson import BosonFockKet
>>> ket_Dead = BosonFockKet(0)
>>> ket_Alive = BosonFockKet(1)
>>> superpos = (ket_Dead+ket_Alive)/sqrt(2)
>>> bra_Dead = Dagger(ket_Dead)
>>> qapply(bra_Dead*superpos).doit()
sqrt(2)/2
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.
    """