Hi:
I have some code to frequently generate haar random matrices and I want it to run fast. I use the suggestion in https://discourse.julialang.org/t/code-speed-decreases-when-loop-number-increases/78920 that I can use StaticArrays
to speed my code up. It works. But recently I realize that the usual Float64
variable can’t give me enough accuracy so I try to use the package DoubleFloats
which can provide high precision variables. But the problem is that when I use these two packages together, the code becomes extremely slow. So I think I must do something wrong.
using LinearAlgebra
using StatsBase
using Statistics
using Random
using StaticArrays
using Future
using DoubleFloats
using Dates
function rand_haar2(::Val{n}) where n
M = SMatrix{n,n,ComplexDF64}(randn(ComplexDF64,n,n))
q = qr(M).Q
L = cispi.(2 .* @SVector(rand(Double64,n)))
return q*diagm(L)
end
function main(; ps=2, theta = 1.05, Nk = 800, Nr = 10000)
S_curve = zeros(Nk)
S_var = zeros(Nk)
Npool = Int(floor(10^ps))
pool = zeros(Double64,Npool)
pool_sample = zeros(Double64,Npool)
spool = zeros(Double64,Npool)
Kup= @SMatrix[cos(theta) 0; 0 sin(theta)]
Kdown = @SMatrix[sin(theta) 0; 0 cos(theta)]
P2up = kron(@SMatrix[1. 0.;0. 1.], @SMatrix[1 0; 0 0])
P2down = kron(@SMatrix[1 0;0 1], @SMatrix[0 0;0 1])
for i in 1:Nr
pool .= 0.5
pool_sample .= 0
for k in 1:Nk
poolcount = 0
while poolcount < Npool
z1 = pool[rand(1:Npool)]
rho1 = diagm(@SVector[z1,1-z1])
z2 = pool[rand(1:Npool)]
rho2 = diagm(@SVector[z2,1-z2])
u1 = rand_haar2(Val{2}())
u2 = rand_haar2(Val{2}())
K1up = u1*Kup*u1'
K1down = u1*Kdown*u1'
K2up = u2*Kup*u2'
K2down = u2*Kdown*u2'
rho1p = K1up*rho1*K1up'
rho2p = K2up*rho2*K2up'
p1 = real(tr(rho1p+rho1p'))/2
p2 = real(tr(rho2p+rho2p'))/2
if rand()<p1
rho1p = (rho1p+rho1p')/(2*p1)
else
rho1p = K1down*rho1*K1down'/((1-p1))
end
if rand()<p2
rho2p = (rho2p+rho2p')/(2*p2)
else
rho2p = K2down*rho2*K2down'/((1-p2))
end
rho = kron(rho1p,rho2p)
U = rand_haar2(Val{4}())
rho_p = P2up*U*rho*U'*P2up'
p = real(tr(rho_p+rho_p'))/2
if rand()<p
temp =(rho_p+rho_p')/2
rho_f = @SMatrix[temp[1,1]+temp[2,2] temp[1,3]+temp[2,4]; temp[3,1]+temp[4,2] temp[3,3]+temp[4,4]]/(p)
else
temp = P2down*U*rho*U'*P2down'
rho_f = @SMatrix[temp[1,1]+temp[2,2] temp[1,3]+temp[2,4]; temp[3,1]+temp[4,2] temp[3,3]+temp[4,4]]/(1-p)
end
rho_f = (rho_f+rho_f')/2
t = abs(tr(rho_f*rho_f))
z = (1-t)/(1+abs(sqrt(2*t-1)))
if !iszero(abs(z))
poolcount = poolcount+1
pool_sample[poolcount] = abs(z)
end
end
pool .= pool_sample
pool_sample .= 0
spool .=-pool.*log.(pool).-(1.0 .- pool).*log.(1.0 .-pool)
S_curve[k] += mean(spool)
S_var[k] += (std(spool)/sqrt(Npool))^2
end
end
return S_curve/Nr, sqrt.(S_var)/Nr
end
@time s,t = main(ps = 6,theta=1.04,Nr = 1)
When I use DoubleFloats
without StaticArrays
, the speed is slow but not too bad. But when I put these two together, the speed suddenly becomes really slow.
Anyone can help?
Thanks