test_transforms.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """Tests of transforms of quantum expressions for Mul and Pow."""
  2. from sympy.core.symbol import symbols
  3. from sympy.testing.pytest import raises
  4. from sympy.physics.quantum.operator import (
  5. Operator, OuterProduct
  6. )
  7. from sympy.physics.quantum.state import Ket, Bra
  8. from sympy.physics.quantum.innerproduct import InnerProduct
  9. from sympy.physics.quantum.tensorproduct import TensorProduct
  10. k1 = Ket('k1')
  11. k2 = Ket('k2')
  12. k3 = Ket('k3')
  13. b1 = Bra('b1')
  14. b2 = Bra('b2')
  15. b3 = Bra('b3')
  16. A = Operator('A')
  17. B = Operator('B')
  18. C = Operator('C')
  19. x, y, z = symbols('x y z')
  20. def test_bra_ket():
  21. assert b1*k1 == InnerProduct(b1, k1)
  22. assert k1*b1 == OuterProduct(k1, b1)
  23. # Test priority of inner product
  24. assert OuterProduct(k1, b1)*k2 == InnerProduct(b1, k2)*k1
  25. assert b1*OuterProduct(k1, b2) == InnerProduct(b1, k1)*b2
  26. def test_tensor_product():
  27. # We are attempting to be rigourous and raise TypeError when a user tries
  28. # to combine bras, kets, and operators in a manner that doesn't make sense.
  29. # In particular, we are not trying to interpret regular ``*`` multiplication
  30. # as a tensor product.
  31. with raises(TypeError):
  32. k1*k1
  33. with raises(TypeError):
  34. b1*b1
  35. with raises(TypeError):
  36. k1*TensorProduct(k2, k3)
  37. with raises(TypeError):
  38. b1*TensorProduct(b2, b3)
  39. with raises(TypeError):
  40. TensorProduct(k2, k3)*k1
  41. with raises(TypeError):
  42. TensorProduct(b2, b3)*b1
  43. assert TensorProduct(A, B, C)*TensorProduct(k1, k2, k3) == \
  44. TensorProduct(A*k1, B*k2, C*k3)
  45. assert TensorProduct(b1, b2, b3)*TensorProduct(A, B, C) == \
  46. TensorProduct(b1*A, b2*B, b3*C)
  47. assert TensorProduct(b1, b2, b3)*TensorProduct(k1, k2, k3) == \
  48. InnerProduct(b1, k1)*InnerProduct(b2, k2)*InnerProduct(b3, k3)
  49. assert TensorProduct(b1, b2, b3)*TensorProduct(A, B, C)*TensorProduct(k1, k2, k3) == \
  50. TensorProduct(b1*A*k1, b2*B*k2, b3*C*k3)
  51. def test_outer_product():
  52. assert OuterProduct(k1, b1)*OuterProduct(k2, b2) == \
  53. InnerProduct(b1, k2)*OuterProduct(k1, b2)
  54. def test_compound():
  55. e1 = b1*A*B*k1*b2*k2*b3
  56. assert e1 == InnerProduct(b2, k2)*b1*A*B*OuterProduct(k1, b3)
  57. e2 = TensorProduct(k1, k2)*TensorProduct(b1, b2)
  58. assert e2 == TensorProduct(
  59. OuterProduct(k1, b1),
  60. OuterProduct(k2, b2)
  61. )