test_fermion.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from pytest import raises
  2. import sympy
  3. from sympy.physics.quantum import Dagger, AntiCommutator, qapply
  4. from sympy.physics.quantum.fermion import FermionOp
  5. from sympy.physics.quantum.fermion import FermionFockKet, FermionFockBra
  6. from sympy import Symbol
  7. def test_fermionoperator():
  8. c = FermionOp('c')
  9. d = FermionOp('d')
  10. assert isinstance(c, FermionOp)
  11. assert isinstance(Dagger(c), FermionOp)
  12. assert c.is_annihilation
  13. assert not Dagger(c).is_annihilation
  14. assert FermionOp("c") == FermionOp("c", True)
  15. assert FermionOp("c") != FermionOp("d")
  16. assert FermionOp("c", True) != FermionOp("c", False)
  17. assert AntiCommutator(c, Dagger(c)).doit() == 1
  18. assert AntiCommutator(c, Dagger(d)).doit() == c * Dagger(d) + Dagger(d) * c
  19. def test_fermion_states():
  20. c = FermionOp("c")
  21. # Fock states
  22. assert (FermionFockBra(0) * FermionFockKet(1)).doit() == 0
  23. assert (FermionFockBra(1) * FermionFockKet(1)).doit() == 1
  24. assert qapply(c * FermionFockKet(1)) == FermionFockKet(0)
  25. assert qapply(c * FermionFockKet(0)) == 0
  26. assert qapply(Dagger(c) * FermionFockKet(0)) == FermionFockKet(1)
  27. assert qapply(Dagger(c) * FermionFockKet(1)) == 0
  28. def test_power():
  29. c = FermionOp("c")
  30. assert c**0 == 1
  31. assert c**1 == c
  32. assert c**2 == 0
  33. assert c**3 == 0
  34. assert Dagger(c)**1 == Dagger(c)
  35. assert Dagger(c)**2 == 0
  36. assert (c**Symbol('a')).func == sympy.core.power.Pow
  37. assert (c**Symbol('a')).args == (c, Symbol('a'))
  38. with raises(ValueError):
  39. c**-1
  40. with raises(ValueError):
  41. c**3.2
  42. with raises(TypeError):
  43. c**1j