Skip to content

Commit

Permalink
Better handling of commutative parts in qapply.
Browse files Browse the repository at this point in the history
Fixes issue 3044.
  • Loading branch information
ellisonbg committed Feb 14, 2012
1 parent 18c5ef4 commit c7fcad6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion sympy/physics/quantum/qapply.py
Expand Up @@ -86,7 +86,13 @@ def qapply(e, **options):


# We have a Mul where there might be actual operators to apply to kets. # We have a Mul where there might be actual operators to apply to kets.
elif isinstance(e, Mul): elif isinstance(e, Mul):
result = qapply_Mul(e, **options) c_part, nc_part = e.args_cnc()
c_mul = Mul(*c_part)
nc_mul = Mul(*nc_part)
if isinstance(nc_mul, Mul):
result = c_mul*qapply_Mul(nc_mul, **options)
else:
result = c_mul*qapply(nc_mul, **options)
if result == e and dagger: if result == e and dagger:
return Dagger(qapply_Mul(Dagger(e), **options)) return Dagger(qapply_Mul(Dagger(e), **options))
else: else:
Expand Down
10 changes: 9 additions & 1 deletion sympy/physics/quantum/tests/test_qapply.py
@@ -1,4 +1,4 @@
from sympy import I, Integer, sqrt, symbols from sympy import I, Integer, sqrt, symbols, S, Mul


from sympy.physics.quantum.anticommutator import AntiCommutator from sympy.physics.quantum.anticommutator import AntiCommutator
from sympy.physics.quantum.commutator import Commutator from sympy.physics.quantum.commutator import Commutator
Expand All @@ -9,6 +9,7 @@
from sympy.physics.quantum.qapply import qapply from sympy.physics.quantum.qapply import qapply
from sympy.physics.quantum.qubit import Qubit from sympy.physics.quantum.qubit import Qubit
from sympy.physics.quantum.spin import Jx, Jy, Jz, Jplus, Jminus, J2, JzKet from sympy.physics.quantum.spin import Jx, Jy, Jz, Jplus, Jminus, J2, JzKet
from sympy.physics.quantum.tensorproduct import TensorProduct
from sympy.physics.quantum.state import Ket from sympy.physics.quantum.state import Ket




Expand Down Expand Up @@ -90,3 +91,10 @@ def test_issue2974():
B = Operator('B') B = Operator('B')
assert qapply(A) == A assert qapply(A) == A
assert qapply(A.dual*B) == A.dual*B assert qapply(A.dual*B) == A.dual*B


def test_issue3044():
expr1 = TensorProduct(Jz*JzKet(S(2),S(-1))/sqrt(2), Jz*JzKet(S(1)/2,S(1)/2))
result = Mul(S(-1), S(1)/4, (2**(S(1)/2)), hbar**2)
result *= TensorProduct(JzKet(2,-1), JzKet(S(1)/2,S(1)/2))
assert qapply(expr1) == result

0 comments on commit c7fcad6

Please sign in to comment.