% LKB photon box: QND measurement of photon
% July 2019
% pierre.rouchon@mines-paristech.fr
% density operator formulation
%
clear all;
%% operator for the quantum harmonic oscillator
i=sqrt(-1); Nmax=7; % truncation to Nmax
Vacuum=zeros(Nmax+1,1);Vacuum(1)=1;
Id=eye(Nmax+1);
N=diag((0:1:Nmax)); % photon  number operator
A=diag( sqrt((1:1:Nmax)),1); % annihilation operator
%%  measurement operators
ph0=pi/7; % dephasing per photon
phR=0; % phase of the second Ramsey pulse
Mg=diag(cos((ph0*(0:1:Nmax)+phR)/2));
Me=diag(sin((ph0*(0:1:Nmax)+phR)/2));
%% initial state
Vaccum=zeros(Nmax+1,1);Vaccum(1)=1; Psi= expm(sqrt(3)*(A-A'))*Vaccum; 
%rho=0*Id; rho(1,1)=1; % 0 photon; 
rho=Id/trace(Id); % fully mixed state (no prior  information) 
% rho=Psi*(Psi'); % coherent state with 3 photons
%% simulation loop
Niter=100;
Pop=zeros(Nmax+1,Niter);y=zeros(1,Niter); % buffer for graphics
for ii=1:Niter
    Pop(:,ii)=diag(rho);
    rhog=Mg*rho*Mg';rhoe=Me*rho*Me';
    if (rand < trace(rhog))
        y(ii)=-1;
        rho=rhog/trace(rhog);
    else
        y(ii)=1;
        rho=rhoe/trace(rhoe);
    end
end
%% graphics
figure(1),clf,
subplot(211);
bar([1:Niter],y);
ylabel('Measurement outcome');
axis([0 Niter+1 -1.02 1.02 ]);
subplot(212);
plot([1:Niter],Pop,'linewidth',4);
legend('0','1','2','3','4','5','6','7');
axis([1 Niter -0.02 1.02 ]);
ylabel('populations');
xlabel('step number');



