-
Notifications
You must be signed in to change notification settings - Fork 0
/
QMagic.jl
171 lines (99 loc) · 2.72 KB
/
QMagic.jl
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
using LinearAlgebra, ProgressMeter
function σs(i,j)
#=
inputs:
i: index of the Pauli. i = 0,1,2,3 => Id, X, Y, Z
j: index of the spin. j = +-1
returns: tuple with coefficient and resulting spin flip
=#
if i==0 #Id
coef = 1
spin_flip = j
elseif i==3 #Z
coef = (+1)*(j==1) + (-1)*(j==-1)
spin_flip = j
elseif i==1 #X
coef = 1
spin_flip = -1*j
elseif i==2 #Y
coef = 1im * j
spin_flip = -1*j
end
return coef,spin_flip
end
function Σs(P,s)
#=
inputs:
P: Pauli string
s: spin config
returns: tuple with coefficient and resulting spin configuration
=#
N = length(s);
s_ = copy(s)
coef = 1;
for i=1:N
new_coef, spinflip = σs(P[i],s[i])
coef *= new_coef
s_[i] = spinflip
end
return (coef,s_)
end
function config_to_index(s)
N = length(s)
s_n = (s .+ 1)/2
s_to_basis = Int(dot(s_n,(2 .^ (0:N-1)))) + 1
return s_to_basis
end
function Psi_s(s,vec)
N = length(s)
s_n = (s .+ 1)/2
s_to_basis = Int(dot(s_n,(2 .^ (0:N-1)))) + 1
return vec[s_to_basis]
end
function Mn(n,psi)
@assert n > 1 "Not implemented for n = 1."
expectation_sum = 0
N = Int(log2(length(psi)))
PsiF(s) = Psi_s(s, psi)
@showprogress for i=0:4^N-1
P = digits(i,base=4,pad=N)
spin_sum = 0
for j=0:2^N - 1
s = 2 .* digits(j,base=2,pad=N) .- 1
alpha, Ps = Σs(P,s)
spin_sum += conj(PsiF(Ps)) * alpha * psi[j+1]
end
expectation_sum += spin_sum^(2*n)
end
out = (1-n)^(-1) * log(expectation_sum/2^N)
if abs(imag(out)) > 10^(-12)
@warn "Warning imaginary part is non-zero."
return out
else
return real.(out)
end
end
#n=2 Renyi entropy for mixed states (subtracting log purity)
function M2_mixed(rho)
expectation_numerator = 0
expectation_denominator = 0
N = Int(log2(size(rho)[1]))
@showprogress for i=0:4^N-1
P = digits(i,base=4,pad=N)
spin_sum = 0
for j=0:2^N - 1
s = 2 .* digits(j,base=2,pad=N) .- 1
alpha, Ps = Σs(P,s)
spin_sum += rho[j+1,config_to_index(Ps)] * alpha
end
expectation_numerator += spin_sum^4
expectation_denominator += spin_sum^2
end
out = -1*log(expectation_numerator/expectation_denominator)
if abs(imag(out)) > 10^(-12)
@warn "Warning imaginary part is non-zero."
return out
else
return real(out)
end
end