test_qexpr.py 1.8 KB

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