Navigation

Source code for sympy.physics.quantum.operator

"""Quantum mechanical operators.TODO:* Fix early 0 in apply_operators.* Debug and test apply_operators.* Get cse working with classes in this file.* Doctests and documentation of special methods for InnerProduct, Commutator, AntiCommutator, represent, apply_operators."""from__future__importprint_function,divisionfromsympyimportDerivative,Expr,Integer,oo,Mulfromsympy.printing.pretty.stringpictimportprettyFormfromsympy.physics.quantum.daggerimportDaggerfromsympy.physics.quantum.qexprimportQExpr,dispatch_methodfromsympy.matricesimporteye__all__=['Operator','HermitianOperator','UnitaryOperator','IdentityOperator','OuterProduct','DifferentialOperator']#-----------------------------------------------------------------------------# Operators and outer products#-----------------------------------------------------------------------------

[docs]classIdentityOperator(Operator):"""An identity operator I that satisfies op * I == I * op == op for any operator op. Parameters ========== N : Integer Optional parameter that specifies the dimension of the Hilbert space of operator. This is used when generating a matrix representation. Examples ======== >>> from sympy.physics.quantum import IdentityOperator >>> IdentityOperator() I """@propertydefdimension(self):returnself.N@classmethoddefdefault_args(self):return(oo,)def__init__(self,*args,**hints):ifnotlen(args)in[0,1]:raiseValueError('0 or 1 parameters expected, got %s'%args)self.N=args[0]if(len(args)==1andargs[0])elseoodef_eval_commutator(self,other,**hints):returnInteger(0)def_eval_anticommutator(self,other,**hints):return2*otherdef_eval_inverse(self):returnselfdef_eval_adjoint(self):returnselfdef_apply_operator(self,ket,**options):returnketdef_eval_power(self,exp):returnselfdef_print_contents(self,printer,*args):return'I'def_print_contents_pretty(self,printer,*args):returnprettyForm('I')def_print_contents_latex(self,printer,*args):returnr'{\mathcal{I}}'def__mul__(self,other):ifisinstance(other,Operator):returnotherreturnMul(self,other)def__rmul__(self,other):ifisinstance(other,Operator):returnotherreturnMul(other,self)def_represent_default_basis(self,**options):ifnotself.Norself.N==oo:raiseNotImplementedError('Cannot represent infinite dimensional'+' identity operator as a matrix')format=options.get('format','sympy')ifformat!='sympy':raiseNotImplementedError('Representation in format '+'%s not implemented.'%format)returneye(self.N)

[docs]classOuterProduct(Operator):"""An unevaluated outer product between a ket and bra. This constructs an outer product between any subclass of ``KetBase`` and ``BraBase`` as ``|a><b|``. An ``OuterProduct`` inherits from Operator as they act as operators in quantum expressions. For reference see [1]_. Parameters ========== ket : KetBase The ket on the left side of the outer product. bar : BraBase The bra on the right side of the outer product. Examples ======== Create a simple outer product by hand and take its dagger:: >>> from sympy.physics.quantum import Ket, Bra, OuterProduct, Dagger >>> from sympy.physics.quantum import Operator >>> k = Ket('k') >>> b = Bra('b') >>> op = OuterProduct(k, b) >>> op |k><b| >>> op.hilbert_space H >>> op.ket |k> >>> op.bra <b| >>> Dagger(op) |b><k| In simple products of kets and bras outer products will be automatically identified and created:: >>> k*b |k><b| But in more complex expressions, outer products are not automatically created:: >>> A = Operator('A') >>> A*k*b A*|k>*<b| A user can force the creation of an outer product in a complex expression by using parentheses to group the ket and bra:: >>> A*(k*b) A*|k><b| References ========== .. [1] http://en.wikipedia.org/wiki/Outer_product """is_commutative=Falsedef__new__(cls,*args,**old_assumptions):fromsympy.physics.quantum.stateimportKetBase,BraBaseket=args[0]bra=args[1]ifnotisinstance(ket,KetBase):raiseTypeError('KetBase subclass expected, got: %r'%ket)ifnotisinstance(bra,BraBase):raiseTypeError('BraBase subclass expected, got: %r'%ket)ifnotket.dual_class()==bra.__class__:raiseTypeError('ket and bra are not dual classes: %r, %r'%(ket.__class__,bra.__class__))# TODO: make sure the hilbert spaces of the bra and ket are compatibleobj=Expr.__new__(cls,*args,**old_assumptions)obj.hilbert_space=ket.hilbert_spacereturnobj@property

[docs]defket(self):"""Return the ket on the left side of the outer product."""returnself.args[0]

@property

[docs]defbra(self):"""Return the bra on the right side of the outer product."""returnself.args[1]

def_eval_adjoint(self):returnOuterProduct(Dagger(self.bra),Dagger(self.ket))def_sympystr(self,printer,*args):returnstr(self.ket)+str(self.bra)def_sympyrepr(self,printer,*args):return'%s(%s,%s)'%(self.__class__.__name__,printer._print(self.ket,*args),printer._print(self.bra,*args))def_pretty(self,printer,*args):pform=self.ket._pretty(printer,*args)returnprettyForm(*pform.right(self.bra._pretty(printer,*args)))def_latex(self,printer,*args):k=printer._print(self.ket,*args)b=printer._print(self.bra,*args)returnk+bdef_represent(self,**options):k=self.ket._represent(**options)b=self.bra._represent(**options)returnk*bdef_eval_trace(self,**kwargs):# TODO if operands are tensorproducts this may be will be handled# differently.returnself.ket._eval_trace(self.bra,**kwargs)

[docs]classDifferentialOperator(Operator):"""An operator for representing the differential operator, i.e. d/dx It is initialized by passing two arguments. The first is an arbitrary expression that involves a function, such as ``Derivative(f(x), x)``. The second is the function (e.g. ``f(x)``) which we are to replace with the ``Wavefunction`` that this ``DifferentialOperator`` is applied to. Parameters ========== expr : Expr The arbitrary expression which the appropriate Wavefunction is to be substituted into func : Expr A function (e.g. f(x)) which is to be replaced with the appropriate Wavefunction when this DifferentialOperator is applied Examples ======== You can define a completely arbitrary expression and specify where the Wavefunction is to be substituted >>> from sympy import Derivative, Function, Symbol >>> from sympy.physics.quantum.operator import DifferentialOperator >>> from sympy.physics.quantum.state import Wavefunction >>> from sympy.physics.quantum.qapply import qapply >>> f = Function('f') >>> x = Symbol('x') >>> d = DifferentialOperator(1/x*Derivative(f(x), x), f(x)) >>> w = Wavefunction(x**2, x) >>> d.function f(x) >>> d.variables (x,) >>> qapply(d*w) Wavefunction(2, x) """@property