16

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?

m93a
  • 8,866
  • 9
  • 40
  • 58
user4624500
  • 286
  • 1
  • 10

5 Answers5

4

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.

m93a
  • 8,866
  • 9
  • 40
  • 58
Peter Brittain
  • 13,489
  • 3
  • 41
  • 57
  • That's it ! I copy/paste boson.py rename it to OrthogonalBra/OrthogonalKet and change innerproduct to ` def _eval_innerproduct_OrthogonalBra(self, bra, **hints): if self.n == bra.n: return 1 else: return 0` – user4624500 Apr 13 '17 at 05:33
1

As Peter points out in his answer, you need to implement a new Bra and Ket class yourself. This is a nice general implementation for orthogonal states that you can use.

Example usage:

>>> OrthogonalBra(n)*OrthogonalKet(n)
1
>>> OrthogonalBra(n)*OrthogonalKet(n+1)
0
>>> OrthogonalBra(n)*OrthogonalKet(m)
<n|m>

Implementation:

class OrthogonalKet(Ket):

    @classmethod
    def dual_class(self):
        return OrthogonalBra

    def _eval_innerproduct(self, bra, **hints):

        if len(self.args) != len(bra.args):
            raise ValueError('Cannot multiply a ket that has a different number of labels.')

        for i in range(len(self.args)):
            diff = self.args[i] - bra.args[i]
            diff.simplify()

            if diff.is_nonzero:
                return 0

            if not diff.is_zero:
                return None

        return 1


class OrthogonalBra(Bra):

    @classmethod
    def dual_class(self):
        return OrthogonalKet

m93a
  • 8,866
  • 9
  • 40
  • 58
  • Relevant PR on the sympy project [here](https://github.com/sympy/sympy/pull/18182). – m93a Dec 30 '19 at 20:02
  • Thanks, this looks wonderful! But I guess there hasn't been a release which includes it yet. I could get this working by copy pasting the definition into my own code, but I had to include a `.doit()` call to get it to actually evaluate the inner products. Without that I was still getting the notation. – aquirdturtle Apr 25 '20 at 06:50
  • Yes, you can call either `expr.doit()` or `qapply(expr)` on the expression, similar to some other operations in the QM module. – m93a Apr 27 '20 at 19:50
0

This isn't exactly what you're looking for, but you can use Qubit to create orthogonal states.

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

dead = Qubit(0)
alive = Qubit(1)

These create Ket(0) and Ket(1). To make the Bra, you can use the Dagger function.

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

When applied to your problem:

superpos = (dead + alive) / sqrt(2)
d = qapply(Dagger(dead) * superpos)

print(d)
sqrt(2)/2
Mike C
  • 21
  • 1
  • 1
    Thank you for your effort. Unfortunately, I am looking for a more general solution : N-dimension basis as with `qutip` (for instance: `from qutip import *` `ket_i=basis(N,i)` ) – user4624500 Apr 06 '17 at 21:42
0

Maybe expression.xreplace() is what you are looking for? According to this book the xreplace function can take a dictionary where sympy-symbols or expressions are hashable keys. This would still be clunky like this:

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)

(caution: did not yet test it...)

This at least gives you the option to define all the desired substitutions in one place and "reuse" them wherever you like.

Chris
  • 710
  • 7
  • 15
  • This is basically the same kind of substitution as in the question. I want to perform the innerproduct automatically as with: `sympy.Matrix([1,0,0]).dot(sympy.Matrix([0,1,0]))` – user4624500 Apr 11 '17 at 21:54
0

An answer in Fock space:

>>> 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

Is the same thing possible in Hilbert space ?

user4624500
  • 286
  • 1
  • 10