function P=FtoP(F,p,toler)
%INPUT:  F    cell with r matrices of size m by n
%        p    probability distribution size r
%      toler  stopping criteria used for error bound below
%OUTPUT: P    corresponding Choi matrix P 
r=length(F);
[m n]=size(F{1});
mn=m*n;
P=zeros(mn);
for jj=1:n,               % by column
	for ii=1:jj,       % down each column
		temp=0;
		for rr=1:r,
			temp=temp+p(rr)*(F{rr}(:,ii))*(F{rr}(:,jj))';
		end
		if ii==jj,
			temp=(temp+temp')/2;
		        P((ii-1)*m+1:ii*m,(jj-1)*m+1:jj*m)=temp;
		else
		        P((ii-1)*m+1:ii*m,(jj-1)*m+1:jj*m)=temp;
		        P((jj-1)*m+1:jj*m,(ii-1)*m+1:ii*m)=temp';
		end
	end
end
if norm(eye(n)-partialtrace(m,n,P))>((mn)*toler),
   fprintf('In FtoP: error for partial trace ||parttr(P)-I_n||,  is  %g \n',...
		   norm(partialtrace(m,n,P)-eye(n)));
		   keyboard
end
