Skip to content

Commit

Permalink
Fix the Ket*Op->Op*Ket bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Costor authored Oct 24, 2022
1 parent b1de270 commit 00ed353
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions sympy/physics/quantum/boson.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def _eval_hilbert_space(cls, label):
def _eval_innerproduct_BosonFockBra(self, bra, **hints):
return KroneckerDelta(self.n, bra.n)

def _apply_operator_BosonOp(self, op, **options):
def _apply_from_right_to_BosonOp(self, op, **options):
if op.is_annihilation:
return sqrt(self.n) * BosonFockKet(self.n - 1)
else:
Expand Down Expand Up @@ -223,7 +223,7 @@ def _eval_innerproduct_BosonCoherentBra(self, bra, **hints):
else:
return exp(-(abs(self.alpha)**2 + abs(bra.alpha)**2 - 2 * conjugate(bra.alpha) * self.alpha)/2)

def _apply_operator_BosonOp(self, op, **options):
def _apply_from_right_to_BosonOp(self, op, **options):
if op.is_annihilation:
return self.alpha * self
else:
Expand Down
2 changes: 1 addition & 1 deletion sympy/physics/quantum/fermion.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _eval_hilbert_space(cls, label):
def _eval_innerproduct_FermionFockBra(self, bra, **hints):
return KroneckerDelta(self.n, bra.n)

def _apply_operator_FermionOp(self, op, **options):
def _apply_from_right_to_FermionOp(self, op, **options):
if op.is_annihilation:
if self.n == 1:
return FermionFockKet(0)
Expand Down
10 changes: 5 additions & 5 deletions sympy/physics/quantum/pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,25 +457,25 @@ def _eval_hilbert_space(cls, label):
def _eval_innerproduct_SigmaZBra(self, bra, **hints):
return KroneckerDelta(self.n, bra.n)

def _apply_operator_SigmaZ(self, op, **options):
def _apply_from_right_to_SigmaZ(self, op, **options):
if self.n == 0:
return self
else:
return S.NegativeOne * self

def _apply_operator_SigmaX(self, op, **options):
def _apply_from_right_to_SigmaX(self, op, **options):
return SigmaZKet(1) if self.n == 0 else SigmaZKet(0)

def _apply_operator_SigmaY(self, op, **options):
def _apply_from_right_to_SigmaY(self, op, **options):
return I * SigmaZKet(1) if self.n == 0 else (-I) * SigmaZKet(0)

def _apply_operator_SigmaMinus(self, op, **options):
def _apply_from_right_to_SigmaMinus(self, op, **options):
if self.n == 0:
return SigmaZKet(1)
else:
return S.Zero

def _apply_operator_SigmaPlus(self, op, **options):
def _apply_from_right_to_SigmaPlus(self, op, **options):
if self.n == 0:
return S.Zero
else:
Expand Down
2 changes: 1 addition & 1 deletion sympy/physics/quantum/qapply.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def qapply_Mul(e, **options):
result = lhs._apply_operator(rhs, **options)
except (NotImplementedError, AttributeError):
try:
result = rhs._apply_operator(lhs, **options)
result = rhs._apply_from_right_to(lhs, **options)
except (NotImplementedError, AttributeError):
if isinstance(lhs, BraBase) and isinstance(rhs, KetBase):
result = InnerProduct(lhs, rhs)
Expand Down
12 changes: 6 additions & 6 deletions sympy/physics/quantum/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,26 +254,26 @@ def _eval_innerproduct(self, bra, **hints):
"""
return dispatch_method(self, '_eval_innerproduct', bra, **hints)

def _apply_operator(self, op, **options):
"""Apply an Operator to this Ket.
def _apply_from_right_to(self, op, **options):
"""Apply an Operator to this Ket as Operator*Ket
This method will dispatch to methods having the format::
``def _apply_operator_OperatorName(op, **options):``
``def _apply_from_right_to_OperatorName(op, **options):``
Subclasses should define these methods (one for each OperatorName) to
teach the Ket how operators act on it.
teach the Ket how to implement OperatorName*Ket
Parameters
==========
op : Operator
The Operator that is acting on the Ket.
The Operator that is acting on the Ket as op*Ket
options : dict
A dict of key/value pairs that control how the operator is applied
to the Ket.
"""
return dispatch_method(self, '_apply_operator', op, **options)
return dispatch_method(self, '_apply_from_right_to', op, **options)


class BraBase(StateBase):
Expand Down

0 comments on commit 00ed353

Please sign in to comment.