Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. import numpy as np
  2. from scipy.linalg import sqrtm
  3.  
  4. #Computation basis definition
  5. LO = np.array([[1.0],[0.0]],dtype=complex)
  6. HI = np.array([[0.0],[1.0]],dtype=complex)
  7.  
  8. #hermite conjugation
  9. dagger = lambda x: x.transpose().conjugate()
  10. #inner product of two kets (kets->number)
  11. braket = lambda x, y: np.dot(dagger(x),y).item(0,0)
  12. #outer product of two kets (kets->matrix)
  13. ketbra = lambda x, y: np.dot(x,dagger(y))
  14. #computation base vector (tuple of 0,1 -> ket)
  15. binket = lambda x: reduce(np.kron, map(lambda y: ((y==1)*HI + (y==0)*LO), x))
  16.  
  17. #Bell states ket vectors
  18. Bell1 = binket((0,1))+binket((1,0))
  19. Bell2 = binket((0,1))-binket((1,0))
  20. Bell3 = binket((0,0))+binket((1,1))
  21. Bell4 = binket((0,0))-binket((1,1))
  22.  
  23. #density matrices of Bell states
  24. rBells = map(lambda x: ketbra(x,x)*0.5, [Bell1,Bell2,Bell3,Bell4])
  25.  
  26. print("*"*10)
  27. print("SciPy square roots of Matrix")
  28. print("*"*10+"\n")
  29.  
  30. for Bell in rBells:
  31.     print("Bell state:")
  32.     print(Bell)
  33.     print("and it square root >>>")
  34.     print(sqrtm(Bell))    
  35.     print("*"*5)
  36.  
  37. def sqrtm2(M):
  38.     #Eigen value decomposition based matrix square root computation
  39.     Di, Rot = np.linalg.eig(M)
  40.     Di = np.diag(Di)
  41.     Di = np.sqrt(Di)        
  42.     try:
  43.         N = np.dot(np.dot(Rot,Di),np.linalg.inv(Rot))
  44.     except:
  45.         raise
  46.     return N
  47.  
  48. print("\n"*2+"*"*10)
  49. print("Eigenvalue-based square roots of Matrix")
  50. print("*"*10+"\n")
  51. for Bell in rBells:
  52.     print("Bell state:")
  53.     print(Bell)
  54.     print("and it square root >>>")
  55.     print(sqrtm2(Bell))    
  56.     print("*"*5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement