Advertisement
Guest User

Partial Trace

a guest
Jul 19th, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.98 KB | None | 0 0
  1. function idx = mrg(i,k,b)
  2. % mixes the bits of i with the bits of k, taking bits from i if b is set to
  3. % 1, and from k otherwise
  4. idx = 0;
  5. ss = size(b);
  6.  
  7. for i=ss:-1:1
  8.     if b(i)
  9.         idx = idx + bitshift( bitand(i,1), i-ss);
  10.         i = bitshift(i,-1);
  11.     else
  12.         idx = idx + bitshift( bitand(k,1), i-ss);
  13.         k = bitshift(k,-1);
  14.     end
  15. end
  16.  
  17. end
  18.  
  19.  
  20. % assumes par is a cvx matrix of size 2^b * 2^b
  21. % b is an array containing which qubits are traced out
  22. function res=prtrace(par, b)
  23.  
  24. % number of bits in the remaining states
  25. cnt = 0;
  26. n = size(b);
  27. for i=1:n
  28.  cnt = cnt + b(i);
  29. end
  30.  
  31. SZ = bitshift(1,n);
  32. sz = bitshift(1,cnt);
  33. rem = bitshift(1,n-cnt);
  34.  
  35.  
  36. res = par;
  37.  
  38. for i = sz+1:SZ
  39.  res(sz+1,:)=[];    
  40. end
  41.  
  42. for i = sz+1:SZ
  43.  res(:,sz+1)=[];    
  44. end
  45.  
  46. for i=1:sz
  47.  for j=1:sz
  48.      res(i,j) = 0;
  49.  end
  50. end
  51.  
  52. for i=1:sz
  53.  for j=1:sz
  54.      for k=1:rem
  55.          res(i,j) = res(i,j) + res(1+mrg(i-1,k-1,b), 1 + mrg(j-1,k-1,b));
  56.      end
  57.  end
  58. end
  59.  
  60. end
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement