testQEDOps.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from scipy.constants import pi
  2. import numpy as np
  3. from qedops import uspinor, vspinor, FermionOperator
  4. from qedstatefuncs import FermionState
  5. from statefuncs import omega
  6. import unittest
  7. class TestSpinorIdentities(unittest.TestCase):
  8. def setUp(self):
  9. self.L = 2*pi
  10. self.m = 1.
  11. self.n = 5
  12. self.k = (2.*pi/self.L)*self.n
  13. self.E = omega(self.n, self.L, self.m)
  14. self.GAMMA0 = np.array([[0,1],[1,0]])
  15. self.GAMMA1 = np.array([[0,-1],[1,0]])
  16. self.myUSpinor = uspinor(self.n, self.L, self.m)
  17. self.myVSpinor = vspinor(self.n, self.L, self.m)
  18. def testUbarU(self):
  19. ubaru = np.vdot(self.myUSpinor, np.dot(self.GAMMA0,self.myUSpinor))
  20. self.assertAlmostEqual(ubaru, 2*self.m)
  21. def testUdaggerU(self):
  22. udaggeru = np.vdot(self.myUSpinor, self.myUSpinor)
  23. self.assertEqual(udaggeru, 2*self.E)
  24. def testVbarV(self):
  25. vbarv = np.vdot(self.myVSpinor, np.dot(self.GAMMA0, self.myVSpinor))
  26. self.assertAlmostEqual(vbarv, -2*self.m)
  27. def testVdaggerV(self):
  28. vdaggerv = np.vdot(self.myVSpinor, self.myVSpinor)
  29. self.assertEqual(vdaggerv, 2*self.E)
  30. class TestOrthogonalSpinors(unittest.TestCase):
  31. def setUp(self):
  32. self.L = 2*pi
  33. self.m = 1.
  34. self.n = 5
  35. self.k = (2.*pi/self.L)*self.n
  36. self.E = omega(self.n, self.L, self.m)
  37. self.GAMMA0 = np.array([[0,1],[1,0]])
  38. self.GAMMA1 = np.array([[0,-1],[1,0]])
  39. self.myUSpinor = uspinor(self.n, self.L, self.m)
  40. self.myVSpinor = vspinor(self.n, self.L, self.m)
  41. # make spinors with -p
  42. self.myUSpinor2 = uspinor(-self.n, self.L, self.m)
  43. self.myVSpinor2 = vspinor(-self.n, self.L, self.m)
  44. def testUVorthogonal(self):
  45. ubarv = np.vdot(self.myUSpinor, np.dot(self.GAMMA0,self.myVSpinor))
  46. self.assertEqual(ubarv, 0)
  47. vbaru = np.vdot(self.myVSpinor, np.dot(self.GAMMA0,self.myUSpinor))
  48. self.assertEqual(vbaru, 0)
  49. # orthogonality with the opposite momentum spinors
  50. udaggerv = np.vdot(self.myUSpinor, self.myVSpinor2)
  51. self.assertEqual(udaggerv, 0)
  52. vdaggeru = np.vdot(self.myVSpinor2, self.myUSpinor)
  53. self.assertEqual(vdaggeru, 0)
  54. class TestMasslessSpinors(unittest.TestCase):
  55. def setUp(self):
  56. self.L = 2*pi
  57. self.m = 0.
  58. self.n = 5
  59. self.k = (2.*pi/self.L)*self.n
  60. self.E = omega(self.n, self.L, self.m)
  61. self.myUSpinor = uspinor(self.n, self.L, self.m)
  62. self.myVSpinor = vspinor(self.n, self.L, self.m)
  63. # make spinors with -p
  64. self.myUSpinor2 = uspinor(-self.n, self.L, self.m)
  65. self.myVSpinor2 = vspinor(-self.n, self.L, self.m)
  66. self.myUSpinorNormed = uspinor(self.n, self.L, self.m, normed=True)
  67. self.myVSpinorNormed = vspinor(self.n, self.L, self.m, normed=True)
  68. # make spinors with -p
  69. self.myUSpinor2Normed = uspinor(-self.n, self.L, self.m, normed=True)
  70. self.myVSpinor2Normed = vspinor(-self.n, self.L, self.m, normed=True)
  71. def testStepFunction(self):
  72. # test the step function behavior in the massless limit
  73. # the massless u-spinor for positive n (right-mover) should be [0,sqrt(2E)]
  74. self.assertEqual(0,self.myUSpinor[0])
  75. self.assertEqual(np.sqrt(2*self.E),self.myUSpinor[1])
  76. # the massless v-spinor for positive n should be [0,-sqrt(2E)]
  77. self.assertEqual(0,self.myVSpinor[0])
  78. self.assertEqual(-np.sqrt(2*self.E),self.myVSpinor[1])
  79. # the massless u-spinor for negative n should be [sqrt(2E),0]
  80. self.assertEqual(np.sqrt(2*self.E),self.myUSpinor2[0])
  81. self.assertEqual(0,self.myUSpinor2[1])
  82. # the massless v-spinor for negative n should be [sqrt(2E),0]
  83. self.assertEqual(np.sqrt(2*self.E),self.myVSpinor2[0])
  84. self.assertEqual(0,self.myVSpinor2[1])
  85. def testStepFunctionNormed(self):
  86. # test the step function behavior in the massless limit after
  87. # normalizing by a factor of sqrt(2E)
  88. self.assertEqual(0.,self.myUSpinorNormed[0])
  89. self.assertEqual(1.,self.myUSpinorNormed[1])
  90. self.assertEqual(0.,self.myVSpinorNormed[0])
  91. self.assertEqual(-1.,self.myVSpinorNormed[1])
  92. self.assertEqual(1.,self.myUSpinor2Normed[0])
  93. self.assertEqual(0.,self.myUSpinor2Normed[1])
  94. self.assertEqual(1.,self.myVSpinor2Normed[0])
  95. self.assertEqual(0.,self.myVSpinor2Normed[1])
  96. class TestFermionOperator(unittest.TestCase):
  97. def setUp(self):
  98. self.L = 2*pi
  99. self.nmax = 1
  100. self.m = 0.
  101. self.Emax = 5.
  102. #self.fermionBasis = FermionBasis(L=2*pi, Emax=self.Emax, m=0.)
  103. self.state = FermionState([1,0,1],[1,0,1],self.nmax,self.L,self.m)
  104. #create an operator that annihilates a particle of momentum 1
  105. self.operator = FermionOperator(clist=[],dlist=[1],anticlist=[],
  106. antidlist=[],L=self.L,m=self.m,
  107. normed=True, extracoeff=1)
  108. def testApplyOperator(self):
  109. n, newState = self.operator._transformState(self.state,
  110. returnCoeff=True)
  111. #print(np.transpose(newState.occs))
  112. self.assertEqual(n,-1)
  113. #test that adding a particle to a filled state annihilates it
  114. for c1 in (-1,1):
  115. operator = FermionOperator([c1],[],[],[],self.L,self.m)
  116. self.assertEqual(operator._transformState(self.state),
  117. (0,None))
  118. for c2 in (-1,1):
  119. operator = FermionOperator([],[],[c2],[],self.L,self.m)
  120. self.assertEqual(operator._transformState(self.state),
  121. (0,None))
  122. #test that removing a particle from an empty state annihilates it
  123. #note: we shouldn't really act on the zero mode at all
  124. #because it is not well-defined for the massless fermion
  125. operator = FermionOperator([],[0],[],[],self.L,self.m,normed=True)
  126. self.assertEqual(operator._transformState(self.state), (0,None))
  127. operator = FermionOperator([],[],[],[0],self.L,self.m,normed=True)
  128. self.assertEqual(operator._transformState(self.state), (0,None))
  129. def testDestructionOperatorSigns(self):
  130. # test that destruction operators correctly anticommute when applied
  131. # to states with our convention
  132. dOperator = FermionOperator(clist=[],dlist=[1],anticlist=[],
  133. antidlist=[],L=self.L,m=self.m,
  134. normed=True, extracoeff=1)
  135. # this state has excitations in the n=+1 and n=-1 modes
  136. # so there is one trivial anticommutation
  137. state1 = FermionState([1,0,1],[0,0,0],self.nmax,self.L,self.m,
  138. checkAtRest=False,checkChargeNeutral=False)
  139. # this state only has an excitation in the +1 mode
  140. state2 = FermionState([0,0,1],[0,0,0],self.nmax,self.L,self.m,
  141. checkAtRest=False,checkChargeNeutral=False)
  142. # this state has a particle and an antiparticle
  143. # so there is one trivial anticommutation with the antiparticle op
  144. state3 = FermionState([0,0,1],[0,0,1],self.nmax,self.L,self.m,
  145. checkAtRest=False,checkChargeNeutral=False)
  146. outState1 = FermionState([1,0,0],[0,0,0],self.nmax,self.L,self.m,
  147. checkAtRest=False,checkChargeNeutral=False)
  148. outState2 = FermionState([0,0,0],[0,0,0],self.nmax,self.L,self.m,
  149. checkAtRest=False,checkChargeNeutral=False)
  150. outState3 = FermionState([0,0,0],[0,0,1],self.nmax,self.L,self.m,
  151. checkAtRest=False,checkChargeNeutral=False)
  152. n, newState = dOperator._transformState(state1, returnCoeff=True)
  153. self.assertEqual(n,-1)
  154. self.assertEqual(newState, outState1)
  155. n, newState = dOperator._transformState(state2, returnCoeff=True)
  156. self.assertEqual(n,1)
  157. self.assertEqual(newState, outState2)
  158. n, newState = dOperator._transformState(state3, returnCoeff=True)
  159. self.assertEqual(n,-1)
  160. self.assertEqual(newState, outState3)
  161. def testCreationOperatorOrdering(self):
  162. # these operators have the creation operators in a different order
  163. # so there should be a relative sign in applying these operators
  164. op1 = FermionOperator([-1,1],[],[],[],self.L,self.m,normed=True)
  165. op2 = FermionOperator([1,-1],[],[],[],self.L,self.m,normed=True)
  166. # do this also for antiparticle creation operators
  167. op3 = FermionOperator([],[],[-1,1],[],self.L,self.m,normed=True)
  168. op4 = FermionOperator([],[],[1,-1],[],self.L,self.m,normed=True)
  169. state = FermionState([0,0,0],[0,0,0],self.nmax,self.L,self.m,
  170. checkAtRest=False,checkChargeNeutral=False)
  171. outState = FermionState([1,0,1],[0,0,0],self.nmax,self.L,self.m,
  172. checkAtRest=False,checkChargeNeutral=False)
  173. outState2 = FermionState([0,0,0],[1,0,1],self.nmax,self.L,self.m,
  174. checkAtRest=False,checkChargeNeutral=False)
  175. n, newState = op1._transformState(state, returnCoeff=True)
  176. self.assertEqual(n,1)
  177. self.assertEqual(newState,outState)
  178. n, newState = op2._transformState(state, returnCoeff=True)
  179. self.assertEqual(n,-1)
  180. self.assertEqual(newState,outState)
  181. n, newState = op3._transformState(state, returnCoeff=True)
  182. self.assertEqual(n,1)
  183. self.assertEqual(newState,outState2)
  184. n, newState = op4._transformState(state, returnCoeff=True)
  185. self.assertEqual(n,-1)
  186. self.assertEqual(newState,outState2)
  187. def testOrderingParticleAntiparticles(self):
  188. state1 = FermionState([0,1,0],[0,0,0],2,self.L,self.m,
  189. checkAtRest=False,checkChargeNeutral=False)
  190. state2 = FermionState([1,1,0],[0,0,0],2,self.L,self.m,
  191. checkAtRest=False,checkChargeNeutral=False)
  192. state3 = FermionState([1,1,0],[1,0,0],2,self.L,self.m,
  193. checkAtRest=False,checkChargeNeutral=False)
  194. state4 = FermionState([1,1,0],[1,0,1],2,self.L,self.m,
  195. checkAtRest=False,checkChargeNeutral=False)
  196. op1 = FermionOperator([2],[],[],[],self.L,self.m,normed=True)
  197. n, newState = op1._transformState(state1, returnCoeff=True)
  198. #one anticommutation to get to the right spot
  199. self.assertEqual(n,-1)
  200. #two anticommutations
  201. n, newState = op1._transformState(state2, returnCoeff=True)
  202. self.assertEqual(n,1)
  203. #three anticommutations
  204. n, newState = op1._transformState(state3, returnCoeff=True)
  205. self.assertEqual(n,-1)
  206. #four anticommutations ah ah ah
  207. n, newState = op1._transformState(state4, returnCoeff=True)
  208. self.assertEqual(n,1)