Skip to content

Commit

Permalink
Merge pull request #19783 from dhruvmendiratta6/Issue_19747
Browse files Browse the repository at this point in the history
Dagger() * IdentityOperator() now simplifies by default
  • Loading branch information
oscarbenjamin committed Jul 17, 2020
2 parents 195640c + d91e3fe commit d5dd533
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
11 changes: 8 additions & 3 deletions sympy/physics/quantum/dagger.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Hermitian conjugation."""

from __future__ import print_function, division

from sympy.core import Expr
from sympy.core import Expr, Mul
from sympy.functions.elementary.complexes import adjoint

__all__ = [
Expand Down Expand Up @@ -85,5 +83,12 @@ def __new__(cls, arg):
return obj
return Expr.__new__(cls, arg)

def __mul__(self, other):
from sympy.physics.quantum import IdentityOperator
if isinstance(other, IdentityOperator):
return self

return Mul(self, other)

adjoint.__name__ = "Dagger"
adjoint._sympyrepr = lambda a, b: "Dagger(%s)" % b._print(a.args[0])
2 changes: 1 addition & 1 deletion sympy/physics/quantum/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def _print_contents_latex(self, printer, *args):

def __mul__(self, other):

if isinstance(other, Operator):
if isinstance(other, (Operator, Dagger)):
return other

return Mul(self, other)
Expand Down
12 changes: 11 additions & 1 deletion sympy/physics/quantum/tests/test_dagger.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sympy import I, Matrix, symbols, conjugate, Expr, Integer
from sympy import I, Matrix, symbols, conjugate, Expr, Integer, Mul

from sympy.physics.quantum.dagger import adjoint, Dagger
from sympy.external import import_module
from sympy.testing.pytest import skip
from sympy.physics.quantum.operator import Operator, IdentityOperator


def test_scalars():
Expand All @@ -29,6 +30,15 @@ def test_matrix():
assert Dagger(m) == m.H


def test_dagger_mul():
O = Operator('O')
I = IdentityOperator()
assert Dagger(O)*O == Dagger(O)*O
assert Dagger(O)*O*I == Mul(Dagger(O), O)*I
assert Dagger(O)*Dagger(O) == Dagger(O)**2
assert Dagger(O)*Dagger(I) == Dagger(O)


class Foo(Expr):

def _eval_adjoint(self):
Expand Down
2 changes: 2 additions & 0 deletions sympy/physics/quantum/tests/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def test_identity():

assert I * O == O
assert O * I == O
assert I * Dagger(O) == Dagger(O)
assert Dagger(O) * I == Dagger(O)
assert isinstance(I * I, IdentityOperator)
assert isinstance(3 * I, Mul)
assert isinstance(I * x, Mul)
Expand Down

0 comments on commit d5dd533

Please sign in to comment.