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
Conversation
✅ Hi, I am the SymPy bot (v160). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.7. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
Codecov Report
@@ Coverage Diff @@
## master #19783 +/- ##
=============================================
- Coverage 75.703% 72.173% -3.531%
=============================================
Files 662 663 +1
Lines 172162 172247 +85
Branches 40604 40618 +14
=============================================
- Hits 130332 124316 -6016
- Misses 36138 42066 +5928
- Partials 5692 5865 +173 |
if isinstance(other, IdentityOperator): | ||
return self | ||
|
||
return Mul(self, other) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add more tests for this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this be fine
Dagger(A) * Identity * A == Dagger(A)*A
Identity * A * Dagger(A) == A*Dagger(A)
You can apply this diff: diff --git a/sympy/physics/quantum/tests/test_dagger.py b/sympy/physics/quantum/tests/test_dagger.py
index bcc8a28f58..54298c7ba2 100644
--- a/sympy/physics/quantum/tests/test_dagger.py
+++ b/sympy/physics/quantum/tests/test_dagger.py
@@ -1,6 +1,7 @@
-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.physics.quantum.operator import Operator, IdentityOperator
from sympy.external import import_module
from sympy.testing.pytest import skip
@@ -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): |
sympy/physics/quantum/dagger.py
Outdated
@@ -85,5 +83,13 @@ def __new__(cls, arg): | |||
return obj | |||
return Expr.__new__(cls, arg) | |||
|
|||
def __mul__(self, other): | |||
from sympy.physics.quantum import IdentityOperator | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a Release Notes entry for this PR. |
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
andDagger
that would make more sense here? ShouldDagger
be a subclass ofOperator
?
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
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
LGTM! |
Looks good. Merging |
References to other Issues or PRs
Fixes #19747
Brief description of what is fixed or changed
now gives
Other comments
Release Notes