test_hilbert.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from sympy.physics.quantum.hilbert import (
  2. HilbertSpace, ComplexSpace, L2, FockSpace, TensorProductHilbertSpace,
  3. DirectSumHilbertSpace, TensorPowerHilbertSpace
  4. )
  5. from sympy.core.numbers import oo
  6. from sympy.core.symbol import Symbol
  7. from sympy.printing.repr import srepr
  8. from sympy.printing.str import sstr
  9. from sympy.sets.sets import Interval
  10. def test_hilbert_space():
  11. hs = HilbertSpace()
  12. assert isinstance(hs, HilbertSpace)
  13. assert sstr(hs) == 'H'
  14. assert srepr(hs) == 'HilbertSpace()'
  15. def test_complex_space():
  16. c1 = ComplexSpace(2)
  17. assert isinstance(c1, ComplexSpace)
  18. assert c1.dimension == 2
  19. assert sstr(c1) == 'C(2)'
  20. assert srepr(c1) == 'ComplexSpace(Integer(2))'
  21. n = Symbol('n')
  22. c2 = ComplexSpace(n)
  23. assert isinstance(c2, ComplexSpace)
  24. assert c2.dimension == n
  25. assert sstr(c2) == 'C(n)'
  26. assert srepr(c2) == "ComplexSpace(Symbol('n'))"
  27. assert c2.subs(n, 2) == ComplexSpace(2)
  28. def test_L2():
  29. b1 = L2(Interval(-oo, 1))
  30. assert isinstance(b1, L2)
  31. assert b1.dimension is oo
  32. assert b1.interval == Interval(-oo, 1)
  33. x = Symbol('x', real=True)
  34. y = Symbol('y', real=True)
  35. b2 = L2(Interval(x, y))
  36. assert b2.dimension is oo
  37. assert b2.interval == Interval(x, y)
  38. assert b2.subs(x, -1) == L2(Interval(-1, y))
  39. def test_fock_space():
  40. f1 = FockSpace()
  41. f2 = FockSpace()
  42. assert isinstance(f1, FockSpace)
  43. assert f1.dimension is oo
  44. assert f1 == f2
  45. def test_tensor_product():
  46. n = Symbol('n')
  47. hs1 = ComplexSpace(2)
  48. hs2 = ComplexSpace(n)
  49. h = hs1*hs2
  50. assert isinstance(h, TensorProductHilbertSpace)
  51. assert h.dimension == 2*n
  52. assert h.spaces == (hs1, hs2)
  53. h = hs2*hs2
  54. assert isinstance(h, TensorPowerHilbertSpace)
  55. assert h.base == hs2
  56. assert h.exp == 2
  57. assert h.dimension == n**2
  58. f = FockSpace()
  59. h = hs1*hs2*f
  60. assert h.dimension is oo
  61. def test_tensor_power():
  62. n = Symbol('n')
  63. hs1 = ComplexSpace(2)
  64. hs2 = ComplexSpace(n)
  65. h = hs1**2
  66. assert isinstance(h, TensorPowerHilbertSpace)
  67. assert h.base == hs1
  68. assert h.exp == 2
  69. assert h.dimension == 4
  70. h = hs2**3
  71. assert isinstance(h, TensorPowerHilbertSpace)
  72. assert h.base == hs2
  73. assert h.exp == 3
  74. assert h.dimension == n**3
  75. def test_direct_sum():
  76. n = Symbol('n')
  77. hs1 = ComplexSpace(2)
  78. hs2 = ComplexSpace(n)
  79. h = hs1 + hs2
  80. assert isinstance(h, DirectSumHilbertSpace)
  81. assert h.dimension == 2 + n
  82. assert h.spaces == (hs1, hs2)
  83. f = FockSpace()
  84. h = hs1 + f + hs2
  85. assert h.dimension is oo
  86. assert h.spaces == (hs1, f, hs2)