-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradientDescentForFullTomoFuncClean.m
159 lines (121 loc) · 3.71 KB
/
GradientDescentForFullTomoFuncClean.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
%Apply quantum state recovery with the gradient descent method
function [rhor Xhi2 puri fid] = GradientDescentForFullTomoFuncClean(...
A,counts,init,actualState,actualPrimary,pData,alpha,iMax,...
gam,method,alpha2,poiss,smolinWay);
s = size(A);
N=s(1);
d = sqrt(s(2));
AA = A'*A;
tau =0.;
%Calulate probabilities and add Poissonian noise to them
P = pData(1:N)*alpha+1E-10;
% Pnoise = P';
Pnoise = reshape(P,[length(P) 1]);
%Prepare initial guess
% init=2;
if init==1
%best solution in the general space of matrices
x = pinv(AA+eye(length(AA))/10*0)*A'*Pnoise;
% x = rand(d^2,1)*counts;
elseif init==2
%random state produces with projector matrix
x = rand(length(AA),1)*max(Pnoise);
end
%%% start with best pure state
% rhox = reshape(x,[sqrt(length(x)) sqrt(length(x))]);
% [primary maxEig] = primaryEigenvector(rhox);
% x = primary*primary'*maxEig;
% eig(x)
% x = reshape(x,[length(AA) 1]);
%start with closest state in the 2-norm
rhor = reshape(x,[d d]);
rhorSmo = smolin(rhor);
x = reshape(rhorSmo,[d^2 1]);
%see what the initial Smolin Fid and pur are on console
% fidSmolin = fidelityRho(actualState,rhorSmo /trace(rhorSmo ))
% purityRe = trace((rhorSmo /trace(rhorSmo ))^2);
% puriSmolin = purityRe
%%%%% try to make the purity higher because the initial guess is always too
%%%%% mixed
% rhor = reshape(x,[d d]);
% [eigvec eigval]= eig(rhor);
% eigval = eigval - sum(sum(eigval))*0.05;
% eigval(eigval<0) = 0;
% eigval = eigval/sum(sum(eigval));
% rhor = eigvec*eigval*eigvec'*counts;
% x = reshape(rhor,[d^2 1]);
x_nplus1 = x;
previousX = x;
gradFromX = x*0;
y = x;
% Xhi2(1)=1E20;
%Gradient descent method
for i=1:iMax
rhor = reshape(x_nplus1,[d d]);
%Force legitimacy of density matrix
if smolinWay==1
rhor = rhor/trace(rhor);
ddd = eig(rhor);
rhor = smolin(rhor);
%
% ddd- eig(rhor)
else
[eigvec eigval]= eig(rhor);
eigval(eigval<tau) = 0;
sumEig(i) = sum(sum(abs(eigval)));
eigval = eigval/sum(sum(eigval));
rhor = eigvec*eigval*eigvec';
rhor = rhor/trace(rhor);
end
fid(i) = fidelityRho(actualState,rhor);
purityRe = trace(rhor^2);
puri(i) = purityRe;
%Change the test state in the opposite side of the gradient
x = reshape(rhor,[d^2 1])*counts;
Ax = A*x;
Axb = (Ax-Pnoise);
if poiss==1
%gradient for poisson noise
Xhi2(i) = real(sum(Ax-Pnoise.*log(Ax)));
gradient = A'*(1-Pnoise./Ax);
elseif poiss==2
%gradient for gaussian noise
Xhi2(i+1) = sum(abs(Axb).^2./abs(Ax))/length(Pnoise);
gradient = A'*((Axb./Ax).*(2-(Axb./Ax)));
else
Xhi2(i+1) = sum(abs(Axb).^2);
gradient = 2*A'*Axb;
end
switch method
case 'gradientDescent'
x_nplus1 = x-gam.*gradient;
case 'momentum'
if i==1 v{1} = gradient*eps;
v2{1} = v{1};
end
v{i+1} = alpha2*v{i} - gam*gradient ;
x_nplus1 = x + v{i+1} ;
end
previousGrad = gradient;
previousX = x;
if Xhi2(i)==min(Xhi2)
bestrho=rhor;
end
end
rhor = bestrho;
% rhor = smolin(rhor);
% fid(i) = fidelityRho(actualState,rhor);
% purityRe = trace(rhor^2);
% puri(i) = purityRe;
%
% [eigvec eigval]= eig(rhor);
% eigval(eigval<tau) = 0;
% sumEig(i) = sum(sum(abs(eigval)));
% eigval = eigval/sum(sum(eigval));
% rhor = eigvec*eigval*eigvec';
rhor = rhor/trace(rhor);
% fid(i) = fidelityRho(actualState,rhor)
% x = reshape(rhor,[d^2 1])*counts;
% Ax = A*x;
% Axb = (Ax-Pnoise);
% Xhi2(i+1) = sum(abs(Axb).^2);