Hi,

The website is  http://code.google.com/p/openket/ although is
outdated. I am attaching the most recent version together with a
couple of examples.

I guess that we can get rid of most of the code by taking advantage of
sympy. I also believe we can improve the API and have a more general
implementation of operators.

I started working on the sympy version (attached as t.py) and created
a special kind of multiplication to handle inner product and operator
evaluation. I'm not sure if it's the best way to do it and also I had
to chage a line in mul.py to get it to work. Namely, in the
_expandsums method I had to change

terms = [Mul(a,b) for a in left for b in right]
to
terms = [a*b for a in left for b in right].

I would also like to have the eval function called automatically and
get rid of the apply_operators function.

¿Is this a good approach?

I've been looking at secondquant.py but still need to do more reading.

Greetings,
Asaf


On Tue, Mar 16, 2010 at 11:11 AM, Brian Granger <ellisonbg....@gmail.com> wrote:
> Asaf,
>
>> Hi Asaf!
>>
>> On Mon, Mar 15, 2010 at 6:46 PM, Asaf Paris Mandoki <asa...@gmail.com> wrote:
>> > A friend of mine developed a python module for doing Dirac notation
>> > algebra for quantum computing calculations. I figured this would be a
>> > nice addition to sympy and am interested in integrating it. Is this
>> > something that would fit with the sympy project?
>
> I am definitely interested in this and could possible have a student
> help with this.
> Ondrej and I are also working on PDE based numerical methods of
> solving the Schrodinger
> equation.  As part of this work, I will be creating Python classes for
> Operators, States, etc.  While these will be numerically focused, I
> would like them to integrate with Sympy for handling things
> symbolically.
>
>> Yes, that's definitely something that many people would be interested
>> in. Let me know if you need any help with integrating it with sympy.
>> You might also be interested in sympy/physics/secondquant.py. If you
>> could post here some example how it works, it'd be cool, then we can
>> think how to best integrate it.
>
> Definitely have a look at this.  In their we have the basics of states
> and operators.  It would be great to have a base level quantum module
> that defines symbolic States, Operators, time evolution, delta
> function, etc in a generic manner.  Then the quantum computing and
> second quantization package can subclass accordingly.
>
> Is there a link to the package your friend developed?
>
> Cheers,
>
> Brian
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sympy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Attachment: openket.tar.gz
Description: GNU Zip compressed data

from sympy.core.basic import Basic
from  sympy.core.numbers import One
from sympy import Add, Symbol, Mul

"""
example usage:
>>> apply_operators(Symbol('x')+Bra(0,0)*Bra(0,'A')*Ket(0,0)*Ket(0,'A'))
"""

#TODO Each kind of Dirac object should take care of defining its own adjoint

class DiracObject(Basic):
    is_commutative = False
        
    def __mul__(self, other):
        if isinstance(other, DiracObject):
            return DiracMult(self, other)
        else:
            #return super(DiracObject, self).__mul__(self, other)
            return Mul(self, other)
    def __rmul__(self, other):
        if isinstance(other, DiracObject):
            return DiracMult(other, self)
        else:
            #return super(DiracObject, self).__rmul__(self, other)
            return Mul(other, self)
            
    
class DiracVector(DiracObject):
    def __init__(self, eig, op='default'):
        if isinstance(eig, str):
            eig = Symbol(eig)
        self.eig = eig
        self.op = op
        
class DiracOperator(DiracObject):
    pass

class Bra(DiracVector):
    def __repr__(self):
        if self.op == 'default':
            return "<"+str(self.eig)+"|"
        else:
            return "<" + str(self.eig) + "_" + str(self.op) + "|"

class Ket(DiracVector):
    def __repr__(self):
        if self.op == 'default':
            return "|"+str(self.eig)+">"
        else:
            return "|" + str(self.eig) + "_" + str(self.op) + ">"

class DiracMult(DiracObject):
    factors = None

    def __init__(self, *factors):
        self.factors = list(factors)
        self._flatten()

    def append_factors(self, *factors):
        self.factors = self.factors + list(factors)
        self._flatten()

    def _flatten(self):
        for factor in self.factors:
            if isinstance(factor, DiracMult):
                n = self.factors.index(factor)
                self.factors[n:n+1] = factor.factors
                
    def eval(self):
        for i in range(len(self.factors)):
            if isinstance(self.factors[i], Bra): 
                #Look forward for a matching Ket and 
                #apply inner product
                for j in range(i, len(self.factors)):
                    if isinstance(self.factors[j], Ket):
                        if self.factors[i].op == self.factors[j].op:
                            if self.factors[i].eig == self.factors[j].eig:
                                self.factors[i] = One()
                                self.factors[j] = One()
                            else:
                                self.factors[i] = 0
                            break
            if isinstance(self.factors[i], DiracOperator):
                pass #TODO Look forward for a matching Ket and apply operator
        new_factors = One()
        for factor in self.factors:
            new_factors *= factor
        return new_factors

def apply_operators(e):
    self = e.expand()
    muls = e.atoms(DiracMult)
    subs_list = [(m,m.eval()) for m in iter(muls)]
    return e.subs(subs_list)

Reply via email to