-
Notifications
You must be signed in to change notification settings - Fork 9
/
timeevolve.jl
261 lines (247 loc) · 8.5 KB
/
timeevolve.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
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
function bit_rep(element::Integer, L::Integer)
bit_rep = falses(L)
for site in 1:L
bit_rep[site] = (element >> (site - 1)) & 1
end
return bit_rep
end
function int_rep(element::BitVector, L::Integer)
int = 1
for site in 1:L
int += (element[site] << (site - 1))
end
return int
end
function generate_basis(L::Integer)
basis = fill(falses(L), 2^L)
for elem in 1:2^L
basis[elem] = bit_rep(elem-1, L)
end
return basis
end
function magnetization(state::Vector, basis)::Float64
M = 0.
for (index, element) in enumerate(basis)
element_M = 0.
for spin in element
element_M += (state[index]^2 * (spin ? 1 : -1))/length(element)
end
@assert abs(element_M) <= 1
M += abs(element_M)
end
return M
end
abstract type AbstractHamiltonian{T, S<:AbstractMatrix} <: AbstractArray{T,2} end
# Hamiltonians may be real or complex
type TransverseFieldIsing{Tv, S<:AbstractMatrix} <: AbstractHamiltonian{Tv, S}
L::Int
basis::Vector # may be a vector of BitVectors/BitMatrices/Vectors/Matrices
Mat::S #this may be sparse, or Diagonal, or something else
h::Real
function TransverseFieldIsing{Tv, S}(L::Integer, h::Real=0.) where Tv where S <: Matrix
basis = generate_basis(L)
H = zeros(2^L, 2^L)
for (index, element) in enumerate(basis)
# the diagonal part is easy
diag_term = 0.
for site in 1:L-1
diag_term -= !xor(element[site], element[site+1])
end
H[index, index] = diag_term
# off diagonal part
for site in 1:L-1
mask = falses(L)
mask[site] = true
new_element = xor.(element, mask)
new_index = int_rep(new_element, L)
H[index, new_index] = -h
end
end
new(L, basis, Hermitian(H), h)
end
function TransverseFieldIsing{Tv, S}(L::Integer, h::Real=0.) where Tv where S <: SparseMatrixCSC
basis = generate_basis(L)
H_Is = Integer[]
H_Js = Integer[]
H_Vs = Tv[]
for (index, element) in enumerate(basis)
# the diagonal part is easy
diag_term = 0.
for site in 1:L-1
diag_term -= !xor(element[site], element[site+1])
end
push!(H_Is, index)
push!(H_Js, index)
push!(H_Vs, diag_term)
# off diagonal part
for site in 1:L-1
mask = falses(L)
mask[site] = true
new_element = xor.(element, mask)
new_index = int_rep(new_element, L)
push!(H_Is, index)
push!(H_Js, new_index)
push!(H_Vs, -h)
end
end
new(L, basis, Hermitian(sparse(H_Is, H_Js, H_Vs, 2^L, 2^L)), h)
end
end
σˣ = [0 1; 1 0]
σʸ = [0 -im; im 0]
σᶻ = [1 0; 0 -1]
σ⁺ = (σˣ + im*σʸ)/2
σ⁻ = (σˣ - im*σʸ)/2
function σᶻσᶻ(ψ::BitVector, site_i::Int, site_j::Int)
return 2.0*xor(ψ[site_i], ψ[site_j]) - 1.0
end
function σ⁺σ⁻(ψ::BitVector, basis, site_i::Int, site_j::Int)
i_bit = ψ[site_i]
j_bit = ψ[site_j]
if xor(i_bit, j_bit)
flipped_ψ = copy(ψ)
flipped_ψ[site_i] ⊻= true
flipped_ψ[site_j] ⊻= true
return findfirst(basis, flipped_ψ)
else
return 0
end
end
type Heisenberg{Tv, S} <: AbstractHamiltonian{Tv, S}
L::Int
basis::Vector
Mat::S
function Heisenberg{Tv, S}(L::Integer) where Tv where S <: Matrix
basis = generate_basis(L)
H = zeros(2^L, 2^L)
for (index, element) in enumerate(basis)
# the diagonal part is easy
diag_term = 0.
for site in 1:L-1
diag_term += σᶻσᶻ(element, site, site+1)
end
H[index, index] = diag_term
# off diagonal part
for site in 1:L-1
new_index = σ⁺σ⁻(element, basis, site, site+1)
if new_index > 0
H[index, new_index] = -4.0
end
end
end
new(L, basis, Hermitian(H))
end
function Heisenberg{Tv, S}(L::Integer) where Tv where S <: SparseMatrixCSC
basis = generate_basis(L)
H_Is = Integer[]
H_Js = Integer[]
H_Vs = Tv[]
for (index, element) in enumerate(basis)
# the diagonal part is easy
diag_term = 0.
for site in 1:L-1
diag_term += σᶻσᶻ(element, site, site+1)
end
push!(H_Is, index)
push!(H_Js, index)
push!(H_Vs, diag_term)
# off diagonal part
for site in 1:L-1
new_index = σ⁺σ⁻(element, basis, site, site+1)
if new_index > 0
push!(H_Is, index)
push!(H_Js, new_index)
push!(H_Vs, -4.0)
end
end
end
new(L, basis, Hermitian(sparse(H_Is, H_Js, H_Vs, 2^L, 2^L)))
end
end
type XXZ{Tv, S<:AbstractMatrix} <: AbstractHamiltonian{Tv, S}
L::Int
basis::Vector
Mat::S
Δ::Tv
function XXZ{Tv, S}(L::Integer, Δ::Tv) where Tv where S <: Matrix
basis = generate_basis(L)
H = zeros(2^L, 2^L)
for (index, element) in enumerate(basis)
# the diagonal part is easy
diag_term = 0.
for site in 1:L-1
diag_term += Δ*σᶻσᶻ(element, site, site+1)
end
H[index, index] = diag_term
# off diagonal part
for site in 1:L-1
new_index = σ⁺σ⁻(element, basis, site, site+1)
if new_index > 0
H[index, new_index] = -4.0
end
end
end
new(L, basis, Hermitian(H), Δ)
end
function XXZ{Tv, S}(L::Integer, Δ::Tv) where Tv where S <: SparseMatrixCSC
basis = generate_basis(L)
H_Is = Integer[]
H_Js = Integer[]
H_Vs = Tv[]
for (index, element) in enumerate(basis)
# the diagonal part is easy
diag_term = 0.
for site in 1:L-1
diag_term += Δ*σᶻσᶻ(element, site, site+1)
end
push!(H_Is, index)
push!(H_Js, index)
push!(H_Vs, diag_term)
# off diagonal part
for site in 1:L-1
new_index = σ⁺σ⁻(element, basis, site, site+1)
if new_index > 0
push!(H_Is, index)
push!(H_Js, new_index)
push!(H_Vs, -4.0)
end
end
end
new(L, basis, Hermitian(sparse(H_Is, H_Js, H_Vs, 2^L, 2^L)), Δ)
end
end
# A little more fun with types
Base.eigfact(A::TransverseFieldIsing; kwargs...) = eigfact(A.Mat; kwargs...)
Base.eigvals(A::TransverseFieldIsing; kwargs...) = eigvals(A.Mat; kwargs...)
Base.eigvecs(A::TransverseFieldIsing; kwargs...) = eigvecs(A.Mat; kwargs...)
Base.eig(A::TransverseFieldIsing; kwargs...) = eig(A.Mat; kwargs...)
Base.ishermitian(A::TransverseFieldIsing) = true
Base.issparse(A::TransverseFieldIsing) = issparse(A.Mat)
Base.size(A::TransverseFieldIsing) = size(A.Mat)
Base.size(A::TransverseFieldIsing, dim::Int) = size(A.Mat, dim)
Base.ndims(A::TransverseFieldIsing) = 2
Base.eigfact(A::XXZ; kwargs...) = eigfact(A.Mat; kwargs...)
Base.eigvals(A::XXZ; kwargs...) = eigvals(A.Mat; kwargs...)
Base.eigvecs(A::XXZ; kwargs...) = eigvecs(A.Mat; kwargs...)
Base.eig(A::XXZ; kwargs...) = eig(A.Mat; kwargs...)
Base.ishermitian(A::XXZ) = true
Base.issparse(A::XXZ) = issparse(A.Mat)
Base.size(A::XXZ) = size(A.Mat)
Base.size(A::XXZ, dim::Int) = size(A.Mat, dim)
Base.ndims(A::XXZ) = 2
Base.eigfact(A::Heisenberg; kwargs...) = eigfact(A.Mat; kwargs...)
Base.eigvals(A::Heisenberg; kwargs...) = eigvals(A.Mat; kwargs...)
Base.eigvecs(A::Heisenberg; kwargs...) = eigvecs(A.Mat; kwargs...)
Base.eig(A::Heisenberg; kwargs...) = eig(A.Mat; kwargs...)
Base.ishermitian(A::Heisenberg) = true
Base.issparse(A::Heisenberg) = issparse(A.Mat)
Base.size(A::Heisenberg) = size(A.Mat)
Base.size(A::Heisenberg, dim::Int) = size(A.Mat, dim)
Base.ndims(A::Heisenberg) = 2
XXZHam = XXZ{Float64, Matrix}(10, 1.0)
HeisHam = Heisenberg{Float64, Matrix}(10)
@assert eigvals(XXZHam) ≈ eigvals(HeisHam)
function get_groundstate(L::Integer, h::Float64=0.)
H = TransverseFieldIsing{Float64, Matrix}(L, h)
return eigvecs(H)[:,1], H
end