Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
Evaluate inner product of bra and ket in Sympy Quantum

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?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório

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
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda