Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dagger() * IdentityOperator() now simplifies by default #19783

Merged
merged 3 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion sympy/physics/quantum/dagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import print_function, division
dhruvmendiratta6 marked this conversation as resolved.
Show resolved Hide resolved

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 +85,13 @@ def __new__(cls, arg):
return obj
return Expr.__new__(cls, arg)

def __mul__(self, other):
from sympy.physics.quantum import IdentityOperator
dhruvmendiratta6 marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

if isinstance(other, IdentityOperator):
return self

return Mul(self,other)
dhruvmendiratta6 marked this conversation as resolved.
Show resolved Hide resolved

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) or isinstance(other, Dagger):
dhruvmendiratta6 marked this conversation as resolved.
Show resolved Hide resolved
return other
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a superclass of Operator and Dagger that would make more sense here? Should Dagger be a subclass of Operator?

I don't know the quantum code so well but special casing types like this suggests that things can be better organised somehow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree things can be better organized. Also: not every functionality is tested properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a superclass of Operator and Dagger that would make more sense here? Should Dagger be a subclass of Operator?

This is exactly what I was thinking and suggested on the mailing list. But while working on it, it turns out if such a change is made print doesn't work properly. print(Dagger(A)) gives A. I think this happens due to calling of _print_contents of Operator. And due to unfamiliarity with the printing module I found it difficult to further look into it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if Dagger is a subclass of Operator then if you add a _print_Dagger method it will take precedence over _print_Operator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for not being able to reply earlier, I tried overriding _print_contents() and made a separate _print() function for dagger but still it didn't work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried overriding _print_contents() and made a separate _print() function for dagger but still it didn't work

Can you show us the diff? It would be easier to comment after that. I think it should work if you add a new _print_Dagger() method.


return Mul(self, other)
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