-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest10.m
140 lines (133 loc) · 5.24 KB
/
test10.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
% Classical simulation for 10 photons and 10 modes using Glynn's algorithm
% and Gurvits's algorithm.
%
% Both the initial state and final state are
% standard state, i.e. [1,1,1,1,1,1,1,1,1,1]. Transition amplitudes are
% calculated.
%
% Total Glynn's algorithm takes 27.7 days, so only 1/10^4 fraction of it is
% executed. If number fo samples is chosen to be 5*10^6, total Gurvits's
% algorithm takes 17 minutes.
%
% ©2018 Jin-Long Huang All Right Reserved
% -------------------------------------------------------------------------
file1 = fopen('test10.txt', 'w');
randU = RandomUnitary(10)
% Glynn's Algorithm (deterministic algorithm)
i0 = 1;
i1 = 1;
i2 = 1;
i3 = 1;
i4 = 1;
i5 = 1;
i6 = 1;
i7 = 1;
i8 = 1;
i9 = 1;
sum = 0+0*1i;
mGenGly = 0+0*1i;
Z = [1 + 0*1i, exp(1i*2*pi/10), exp(1i*4*pi/10), exp(1i*6*pi/10)...
, exp(1i*8*pi/10), exp(1i*10*pi/10), exp(1i*12*pi/10), ...
exp(1i*14*pi/10),exp(1i*16*pi/10),exp(1i*18*pi/10),];
tic0 = tic;
while i0 <= 10
while i1 <= 10
while i2 <= 10
while i3 <= 10
while i4 <= 10
tic1 = tic;
while i5 <= 10
while i6 <= 10
while i7 <= 10
while i8 <= 10
while i9 <= 10
ZVec = [Z(i0);Z(i1);Z(i2);...
Z(i3);Z(i4);Z(i5);...
Z(i6);Z(i7);Z(i8);Z(i9)];
mGenGly = Z(i0)'*Z(i1)'*Z(i2)' ...
*Z(i3)'*Z(i4)'*Z(i5)'...
*Z(i6)'*Z(i7)'*Z(i8)'...
*Z(i9)' ...
*dot(randU(1,:),ZVec)...
*dot(randU(2,:),ZVec) ...
*dot(randU(3,:),ZVec) ...
*dot(randU(4,:),ZVec) ...
*dot(randU(5,:),ZVec) ...
*dot(randU(6,:),ZVec) ...
*dot(randU(7,:),ZVec) ...
*dot(randU(8,:),ZVec) ...
*dot(randU(9,:),ZVec) ...
*dot(randU(10,:),ZVec);
sum = sum + mGenGly;
i9 = i9 + 1;
end
i9 = 1;
i8 = i8 + 1;
end
i8 = 1;
i7 = i7 + 1;
end
i7 = 1;
i6 = i6 + 1;
end
i6 = 1;
i5 = i5 + 1;
end
toc1 = toc(tic1);
sum1 = abs(sum)/(10^5*i4);
fprintf(file1, 'Transition amplitude from %d*10^5 samples is %14.8e\n', i4, sum1);
if(i4==1)
fprintf(file1, '1/10^5 time for Glynn is %14.8e \n\n', toc1);
end
i5 = 1;
i4 = i4 + 1;
end
break;
i4 = 1;
i3 = i3 + 1;
end
break;
i3 = 1;
i2 = i2 + 1;
end
break;
i2 = 1;
i1 = i1 + 1;
end
break;
i1 = 1;
i0 = i0 + 1;
end
transAmp = abs(sum)/(10^6);
fprintf(file1, 'Transition amplitude from 10^6 samples is %14.8e\n', transAmp);
% Gurvits' algorithm (Sampling algorithm)
tic2 = tic;
T = 5*10^6;
count = 1;
sum = 0 + 0*1i;
while count <= T
r = randi([1,10],10,1);
Z = [1 + 0*1i, exp(1i*2*pi/10), exp(1i*4*pi/10), exp(1i*6*pi/10) ...
, exp(1i*8*pi/10), exp(1i*10*pi/10), exp(1i*12*pi/10), ...
exp(1i*14*pi/10),exp(1i*16*pi/10),exp(1i*18*pi/10),];
ZVec = [Z(r(1));Z(r(2));Z(r(3));Z(r(4));Z(r(5));Z(r(6));Z(r(7));Z(r(8));Z(r(9));Z(r(10))];
mGenGly = Z(r(1))'*Z(r(2))'*Z(r(3))'*Z(r(4))'*Z(r(5))'*Z(r(6))'...
*Z(r(7))'*Z(r(8))'*Z(r(9))'*Z(r(10))' ...
*dot(randU(1,:),ZVec)...
*dot(randU(2,:),ZVec) ...
*dot(randU(3,:),ZVec) ...
*dot(randU(4,:),ZVec) ...
*dot(randU(5,:),ZVec) ...
*dot(randU(6,:),ZVec) ...
*dot(randU(7,:),ZVec) ...
*dot(randU(8,:),ZVec) ...
*dot(randU(9,:),ZVec) ...
*dot(randU(10,:),ZVec);
sum = sum + mGenGly;
count = count + 1;
end
transAmp1 = sum/T;
fprintf(file1, 'Transition amplitude for Gurvits is %14.8e\n', abs(transAmp1));
toc2 = toc(tic2);
fprintf(file1, 'Total time for Gurvits is %14.8e\n\n', toc2);
fclose(file1);