In sympy I have defined a two kets and a corresponding bra, when I apply the bra to the kets...
from sympy import sqrt
from sympy.physics.quantum import Bra,Ket,qapply
superpos = (Ket('Dead')+Ket('Alive'))/sqrt(2)
d = qapply(Bra('Dead')*superpos)
...I get this result:
sqrt(2)*<Dead|Alive>/2 + sqrt(2)*<Dead|Dead>/2
How do I set 'Dead' and 'Alive' as orthogonal states, so that d.doit()
gives sqrt(2)/2
?
So far I've only been able to substitute the brakets by hand:
d.subs(Bra('Dead')*Ket('Dead'),1).subs(Bra('Dead')*Ket('Alive'),0)
But how do I make the brakets evaluate automatically? Why doesn't the InnerProduct
result to 1 for identical brakets and 0 for brakets with different labels?
Your problem is that InnerProduct doesn't know how to evaluate these values and so leaves the unsimplified expression instead. Looking at the source, I see that it tries to call _eval_innerproduct()
on the Ket
, which says this.
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.
"""
You should therefore be able to solve your problem by creating 2 new Bra
classes and a new Ket
class that implements 2 methods - one to evaluate each of the inner products (using the naming convention mandated above).
For completeness you probably also want to implement the other Ket
for your orthogonal state and to make sure that dual_class
returns the right class in each case.