3

I have a function that outputs a matrix in a new basis. However, depending on the size of the matrix the number of basis matrix differ. So in simplified "Matlab pseudo code":

if matrixsize==1
   for a1=1:4
      out(a1)=Matrix*basis(a1)
   end

elseif matrixsize==2
   for a1=1:4
      for a2=a1:4
         out(a1,a2)=Matrix*basis(a1)*basis(a2)
      end
   end

elseif matrixsize==3
   for a1=1:4
      for a2=a1:4
          for a3=a2:4
             out(a1,a2,a3)=Matrix*basis(a1)*basis(a2)*basis(a3)
          end
      end
   end

elseif ...

and so on

Is it possible to write this code, for any value of matrix size? In other words: Is it possible to create a loop that automatically creates the loops above? If this does not work in Matlab, is there maybe a solution in Python?

(Background: This question comes from quantum physics, where I want to write a quantum state in the Pauli basis)

Here is a working Matlab code that shows the problem:

function T=newbasis(n)

%create a random matrix
m=2^n;
M=randn(m);

%Pauli matrices
s{1}=sparse([1,0;0,1]);
s{2}=sparse([0,1;1,0]);
s{3}=sparse([0,-1i;1i,0]);
s{4}=sparse([1,0;0,-1]);

if n==1
    for a1=1:4
        T(a1)=trace(M*betterkron(s{a1}));
    end

elseif n==2
    for a1=1:4
        for a2=a1:4
            T(a1,a2)=trace(M*betterkron(s{a1},s{a2}));
        end
    end

elseif n==3
    for a1=1:4
        for a2=a1:4
            for a3=a2:4    
                T(a1,a2,a3)=trace(M*betterkron(s{a1},s{a2},s{a3}));
            end
        end
    end    


else
    T=[]
end

%Not very clever but just to keep it simple
function krn=betterkron(A,varargin)
    krn = A;
    for j = 2:nargin;
        krn = kron(krn,varargin{j-1});
    end   
end

end
2
  • does s has to be sparse? Pauli matrices are rather small ... Commented Jun 18, 2018 at 18:36
  • No, this is not important. However, i made them sparse, because if you calculate the tensor (or Kronecker) product between Pauli matrices again and again, you always put half the entries to zero. So a 10-fold tensorproduct of Pauli matrices will be sparse. Commented Jun 18, 2018 at 19:52

1 Answer 1

3

Although it is possible in principle to do multiple loops like this with a recursive function, it will be complicated. Luckily using multiple loops isn't the best way to do it. MATLAB lets you convert back and forth between N-dimensional subscripts and 1-dimensional linear indices. So you can do a single loop over the linear indices, then convert back to N-dimensional subscripts. So something like this:

for i=1:numel(Matrix)  % loop over linear index 
    inds = ind2sub(size(Matrix), i);  % convert linear index to subscript

    % Each index should be greater than or equal to the previous
    % e.g. a2=a1:4, a2 starts at a1 so cannot be less than a1
    if any(diff(inds) < 0)
        continue
    end

    % Do the calculation
    % s{inds} is equivalent to s{i1}, s{i2}, ...
    T(i) = trace(M*betterkron(s{inds}));
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.