test_qexpr.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from sympy.core.numbers import Integer
  2. from sympy.core.symbol import Symbol
  3. from sympy.physics.quantum.qexpr import QExpr, _qsympify_sequence
  4. from sympy.physics.quantum.hilbert import HilbertSpace
  5. from sympy.core.containers import Tuple
  6. x = Symbol('x')
  7. y = Symbol('y')
  8. def test_qexpr_new():
  9. q = QExpr(0)
  10. assert q.label == (0,)
  11. assert q.hilbert_space == HilbertSpace()
  12. assert q.is_commutative is False
  13. q = QExpr(0, 1)
  14. assert q.label == (Integer(0), Integer(1))
  15. q = QExpr._new_rawargs(HilbertSpace(), Integer(0), Integer(1))
  16. assert q.label == (Integer(0), Integer(1))
  17. assert q.hilbert_space == HilbertSpace()
  18. def test_qexpr_commutative():
  19. q1 = QExpr(x)
  20. q2 = QExpr(y)
  21. assert q1.is_commutative is False
  22. assert q2.is_commutative is False
  23. assert q1*q2 != q2*q1
  24. q = QExpr._new_rawargs(Integer(0), Integer(1), HilbertSpace())
  25. assert q.is_commutative is False
  26. def test_qexpr_commutative_free_symbols():
  27. q1 = QExpr(x)
  28. assert q1.free_symbols.pop().is_commutative is False
  29. q2 = QExpr('q2')
  30. assert q2.free_symbols.pop().is_commutative is False
  31. def test_qexpr_subs():
  32. q1 = QExpr(x, y)
  33. assert q1.subs(x, y) == QExpr(y, y)
  34. assert q1.subs({x: 1, y: 2}) == QExpr(1, 2)
  35. def test_qsympify():
  36. assert _qsympify_sequence([[1, 2], [1, 3]]) == (Tuple(1, 2), Tuple(1, 3))
  37. assert _qsympify_sequence(([1, 2, [3, 4, [2, ]], 1], 3)) == \
  38. (Tuple(1, 2, Tuple(3, 4, Tuple(2,)), 1), 3)
  39. assert _qsympify_sequence((1,)) == (1,)