-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_7.f90
278 lines (241 loc) · 6.69 KB
/
chapter_7.f90
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
! Source codes for Numerical Recipes in Quantum Information Theory and Quantum Computing:
!An Adventure in FORTRAN 90 -by M. S. Ramkarthik and Payal D. Solanki
!This file contains all the source codes for chapter 7.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CHAPTER 7!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.1.1
subroutine RANDDISTGAUSSR(d1,d2,mu,sigma,state)
implicit none
integer::d1,d2,i,j
real*8::state(0:d1-1,0:d2-1),rn1,rn2,pi,mu,sigma
pi=4.0d0*atan(1.0d0)
do i=0,d1-1
do j=0,d2-1
call random_number(rn1)
call random_number(rn2)
state(i,j)=mu+sigma*sqrt(-2*log(rn1))*cos(2*pi*rn2)
end do
end do
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.1.2
subroutine RANDDISTGAUSSC(d1,d2,mu,sigma,state)
implicit none
integer::d1,d2,i,j
complex*16::state(0:d1-1,0:d2-1)
real*8::rn1,rn2,pi,n1,n2,rn3,rn4,mu,sigma
pi=4.0d0*atan(1.0d0)
do i=0,d1-1
do j=0,d2-1
call random_number(rn1)
call random_number(rn2)
call random_number(rn3)
call random_number(rn4)
n1=mu+sigma*sqrt(-2*log(rn1))*cos(2*pi*rn2)
n2=mu+sigma*sqrt(-2*log(rn3))*cos(2*pi*rn4)
state(i,j)=dcmplx(n1,n2)
end do
end do
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.2.1
subroutine RANDDISTUNIR(a,b,d1,d2,state)
implicit none
integer::d1,d2
real*8::state(0:d1-1,0:d2-1),a,b
call random_number(state)
state=a+(b-a)*state
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.2.2
subroutine RANDDISTUNIC(a,b,d1,d2,state)
implicit none
integer::d1,d2,i,j
real*8::state1(0:d1-1,0:d2-1),state2(0:d1-1,0:d2-1),a,b
complex*16::state(0:d1-1,0:d2-1)
call random_number(state1)
call random_number(state2)
state1=a+(b-a)*state1
state2=a+(b-a)*state2
do i=0,d1-1
do j=0,d2-1
state(i,j)=dcmplx(state1(i,j),state2(i,j))
end do
end do
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.3
subroutine RANDSR(d,dist,a,b,mu,sigma,state)
implicit none
integer::i,d
real*8::state(0:d-1,0:d-1),a,b,mu,sigma
character*3::dist
if (dist=='GAU') then
call RANDDISTGAUSSR(d,d,mu,sigma,state)
end if
if (dist=='UNI') then
call RANDDISTUNIR(a,b,d,d,state)
end if
state=(state+transpose(state))/2.d0
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.4
subroutine RANDHC(d,dist,a,b,mu,sigma,state)
implicit none
integer::i,d
complex*16::state(0:d-1,0:d-1)
real*8::a,b,mu,sigma
character*3::dist
if (dist=='GAU') then
call RANDDISTGAUSSC(d,d,mu,sigma,state)
end if
if (dist=='UNI') then
call RANDDISTUNIC(a,b,d,d,state)
end if
state=(state+transpose(conjg(state)))/2.d0
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.5.1
subroutine RANDORTHOGONAL(d1,d2,dist,a,b,mu,sigma,state)
implicit none
integer::i,d1,d2
real*8::state(0:d1-1,0:d2-1),a,b,mu,sigma,R(0:d1-1,0:d2-1)
character*3::dist
if (dist=='GAU') then
call RANDDISTGAUSSR(d1,d2,mu,sigma,state)
end if
if (dist=='UNI') then
call RANDDISTUNIR(a,b,d1,d2,state)
end if
call QRMR(d1,d2,state,R)
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.5.2
subroutine RANDUNITARY(d1,d2,dist,a,b,mu,sigma,state)
implicit none
integer::i,d1,d2
complex*16::state(0:d1-1,0:d2-1),R(0:d1-1,0:d2-1)
real*8::a,b,mu,sigma
character*3::dist
if (dist=='GAU') then
call RANDDISTGAUSSC(d1,d2,mu,sigma,state)
end if
if (dist=='UNI') then
call RANDDISTUNIC(a,b,d1,d2,state)
end if
call QRMC(d1,d2,state,R)
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.6
subroutine GINIBRER(d,state)
implicit none
integer::d
real*8::state(0:d-1,0:d-1),mu,sigma
mu=0.0d0
sigma=1.0d0
call RANDDISTGAUSSR(d,d,mu,sigma,state)
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.7
subroutine GINIBREC(d,state)
implicit none
integer::d
complex*16::state(0:d-1,0:d-1)
real*8::mu,sigma
mu=0.0d0
sigma=1.0d0/sqrt(2.0d0)
call RANDDISTGAUSSC(d,d,mu,sigma,state)
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.8.1
subroutine WISHARTSR(d,state)
implicit none
integer::d
real*8::state(0:d-1,0:d-1)
call GINIBRER(d,state)
state=matmul(state,transpose(state))
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.8.2
subroutine WISHARTHC(d,state)
implicit none
integer::d
complex*16::state(0:d-1,0:d-1)
call GINIBREC(d,state)
state=matmul(state,transpose(conjg(state)))
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.9
subroutine RANDPVEC(d,state)
implicit none
integer::d,i
real*8::state(0:d-1),norm
character*3::dist
call random_number(state)
norm=0.0d0
do i=0,d-1
norm=norm+state(i)
end do
state=state/norm
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.10.1
subroutine RANDVR(d,dist,a,b,mu,sigma,state)
implicit none
integer::i,d
real*8::state1(0:d-1,0:0),a,b,state(0:d-1),norm,mu,sigma
character*3::dist
if (dist=='GAU') then
call RANDDISTGAUSSR(d,1,mu,sigma,state1)
end if
if (dist=='UNI') then
call RANDDISTUNIR(a,b,d,1,state1)
end if
norm=0.0d0
do i=0,d-1,1
state(i)=state1(i,0)
end do
call NORMALIZEVR(state,d)
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.10.2
subroutine RANDVC(d,dist,a,b,mu,sigma,state)
implicit none
integer::i,d
complex*16::state1(0:d-1,0:0),state(0:d-1)
real*8::a,b,mu,sigma
character*3::dist
if (dist=='GAU') then
call RANDDISTGAUSSC(d,1,mu,sigma,state1)
end if
if (dist=='UNI') then
call RANDDISTUNIC(a,b,d,1,state1)
end if
do i=0,d-1,1
state(i)=state1(i,0)
end do
call NORMALIZEVC(state,d)
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.11.1
subroutine RANDDMR(d,state)
integer::d
real*8::state(0:d-1,0:d-1),mu,sigma,trace
call WISHARTSR(d,state)
trace=0.0d0
do i=0,d-1,1
trace=trace+state(i,i)
end do
state=state/trace
end subroutine
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!7.11.2
subroutine RANDDMC(d,state)
integer::d
complex*16::state(0:d-1,0:d-1),trace
call WISHARTHC(d,state)
trace=0.0d0
do i=0,d-1,1
trace=trace+state(i,i)
end do
state=state/trace
end subroutine