keep.m

来自「该算法是用于量子运算的matlab程序」· M 代码 · 共 61 行

M
61
字号
% keep   Reduced density matrix keeping the given qubits.%    keep(rho,list) removes the qubits not given in the list.%    qubits are numbered between 1 and N for an N qubit state.%    The expression keep(kron(m2,m1),2) gives m2, while%    keep(kron(m2,m1),1) gives m1. %    (m's are density matrices of pure states.)%    Thus mkron(m4,m3,m2,m1) would indicate how matrices%    are numbered for keep.%    If rho is a state vector, it is converted into%    a normalized density matrix.%    If rho is an unnormalized matrix then it is%    normalized to have trace 1.% Copyright (C) 2005  Geza Toth    E.mail: toth@alumni.nd.edu%% This program is free software; you can redistribute it and/or% modify it under the terms of the GNU General Public License% as published by the Free Software Foundation; see gpl.txt% of this subroutine package.%% This program is distributed in the hope that it will be useful,% but WITHOUT ANY WARRANTY; without even the implied warranty of% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the% GNU General Public License for more details.% % You should have received a copy of the GNU General Public License% along with this program; if not, write to the Free Software% Foundation, Inc., 51 Franklin Street, Fifth Floor, % Boston, MA  02110-1301, USA.function r=keep(rho_in,listneg)rho_in=ketbra2(rho_in);[sx,sy]=size(rho_in);N=log2(sx);list=setdiff(N:-1:1,listneg);%listneg=-sort(-listneg);Nr=length(listneg);rho=reorder(rho_in,[listneg list]);rho_red=zeros(Nr,Nr);for k=0:2^Nr-1   for l=0:2^Nr-1      rr=0;      i1=1+k*2^(N-Nr);      i2=1+l*2^(N-Nr);      for n=0:2^(N-Nr)-1         rr=rr+rho(i1+n,i2+n);         end %for      rho_red(k+1,l+1)=rr;   end %forend %forr=rho_red;        

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?