Skip to content

Commit 66f1e50

Browse files
committed
Formatting and package updates.
1 parent ee03ce5 commit 66f1e50

14 files changed

Lines changed: 334 additions & 203 deletions

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
1515

1616
[compat]
1717
ITensorMPS = "0.1, 0.2, 0.3"
18-
ITensors = "0.3, 0.4, 0.5, 0.6, 0.7"
18+
ITensors = "0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9"
1919
LinearAlgebra = "1.9, 1.10"
2020
SparseArrays = "1.9, 1.10"
2121
TimerOutputs = "0.5"

examples/electronic-structure.jl

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using ITensorMPS
33
using ITensors
44
using PyCall
55

6-
function get_coefficients(N::Int)::Tuple{Array{Float64, 2}, Array{Float64, 8}}
6+
function get_coefficients(N::Int)::Tuple{Array{Float64,2},Array{Float64,8}}
77
py"""
88
import numpy as np
99
@@ -19,11 +19,10 @@ end
1919

2020
function electronic_structure(
2121
N::Int,
22-
one_electron_coeffs::Array{Float64, 2},
23-
two_electron_coeffs::Array{Float64, 8};
24-
useITensorsAlg::Bool=false
22+
one_electron_coeffs::Array{Float64,2},
23+
two_electron_coeffs::Array{Float64,8};
24+
useITensorsAlg::Bool=false,
2525
)::MPO
26-
2726
os = OpSum{Float64}()
2827
@time "\tConstructing OpSum" let
2928
for a in 1:N
@@ -52,7 +51,16 @@ function electronic_structure(
5251
s_m = s_l
5352
(s_m, m) <= (s_k, k) && continue
5453

55-
value = two_electron_coeffs[j, (s_j == "up") + 1, l, (s_l == "up") + 1, m, (s_m == "up") + 1, k, (s_k == "up") + 1]
54+
value = two_electron_coeffs[
55+
j,
56+
(s_j == "up") + 1,
57+
l,
58+
(s_l == "up") + 1,
59+
m,
60+
(s_m == "up") + 1,
61+
k,
62+
(s_k == "up") + 1,
63+
]
5664
os .+= value, "Cdag$s_j", j, "Cdag$s_l", l, "C$s_m", m, "C$s_k", k
5765
os .+= conj(value), "Cdag$s_k", k, "Cdag$s_m", m, "C$s_l", l, "C$s_j", j
5866
end
@@ -69,7 +77,39 @@ function electronic_structure(
6977
if useITensorsAlg
7078
return @time "\tConstrucing MPO" MPO(os, sites)
7179
else
72-
operatorNames = [[
80+
operatorNames = [
81+
[
82+
"I",
83+
"Cdn",
84+
"Cup",
85+
"Cdagdn",
86+
"Cdagup",
87+
"Ndn",
88+
"Nup",
89+
"Cup * Cdn",
90+
"Cup * Cdagdn",
91+
"Cup * Ndn",
92+
"Cdagup * Cdn",
93+
"Cdagup * Cdagdn",
94+
"Cdagup * Ndn",
95+
"Nup * Cdn",
96+
"Nup * Cdagdn",
97+
"Nup * Ndn",
98+
] for _ in 1:N
99+
]
100+
101+
op_cache_vec = to_OpCacheVec(sites, operatorNames)
102+
return @time "\tConstrucing MPO" MPO_new(os, sites; basis_op_cache_vec=op_cache_vec)
103+
end
104+
end
105+
106+
function electronic_structure_OpIDSum(
107+
N::Int, one_electron_coeffs::Array{Float64,2}, two_electron_coeffs::Array{Float64,8}
108+
)::MPO
109+
sites = siteinds("Electron", N; conserve_qns=true)
110+
111+
operatorNames = [
112+
[
73113
"I",
74114
"Cdn",
75115
"Cup",
@@ -86,38 +126,8 @@ function electronic_structure(
86126
"Nup * Cdn",
87127
"Nup * Cdagdn",
88128
"Nup * Ndn",
89-
] for _ in 1:N]
90-
91-
op_cache_vec = to_OpCacheVec(sites, operatorNames)
92-
return @time "\tConstrucing MPO" MPO_new(os, sites; basis_op_cache_vec=op_cache_vec)
93-
end
94-
end
95-
96-
function electronic_structure_OpIDSum(
97-
N::Int,
98-
one_electron_coeffs::Array{Float64, 2},
99-
two_electron_coeffs::Array{Float64, 8}
100-
)::MPO
101-
sites = siteinds("Electron", N; conserve_qns=true)
102-
103-
operatorNames = [[
104-
"I",
105-
"Cdn",
106-
"Cup",
107-
"Cdagdn",
108-
"Cdagup",
109-
"Ndn",
110-
"Nup",
111-
"Cup * Cdn",
112-
"Cup * Cdagdn",
113-
"Cup * Ndn",
114-
"Cdagup * Cdn",
115-
"Cdagup * Cdagdn",
116-
"Cdagup * Ndn",
117-
"Nup * Cdn",
118-
"Nup * Cdagdn",
119-
"Nup * Ndn",
120-
] for _ in 1:N]
129+
] for _ in 1:N
130+
]
121131

122132
op_cache_vec = to_OpCacheVec(sites, operatorNames)
123133

@@ -128,7 +138,7 @@ function electronic_structure_OpIDSum(
128138
opCdag(k::Int, spin::Bool) = OpID{UInt8}(4 + spin, k)
129139
opN(k::Int, spin::Bool) = OpID{UInt8}(6 + spin, k)
130140

131-
os = OpIDSum{4, Float64, UInt8}(2 * N^4, op_cache_vec)
141+
os = OpIDSum{4,Float64,UInt8}(2 * N^4, op_cache_vec)
132142
@time "\tConstructing OpIDSum" let
133143
for a in 1:N
134144
for b in a:N
@@ -169,16 +179,16 @@ function electronic_structure_OpIDSum(
169179
end
170180
end
171181

172-
return @time "\tConstructing MPO" MPO_new(
173-
os, sites; basis_op_cache_vec=op_cache_vec
174-
)
182+
return @time "\tConstructing MPO" MPO_new(os, sites; basis_op_cache_vec=op_cache_vec)
175183
end
176184

177185
let
178186
for N in [10]
179187
println("Constructing the electronic structure MPO for $N sites using ITensorMPS")
180188
one_electron_coeffs, two_electron_coeffs = get_coefficients(N)
181-
@time "Total construction time" mpo = electronic_structure(N, one_electron_coeffs, two_electron_coeffs; useITensorsAlg=true)
189+
@time "Total construction time" mpo = electronic_structure(
190+
N, one_electron_coeffs, two_electron_coeffs; useITensorsAlg=true
191+
)
182192
println("The maximum bond dimension is $(maxlinkdim(mpo))")
183193
println("The sparsity is $(ITensorMPOConstruction.sparsity(mpo))")
184194
println()
@@ -187,9 +197,13 @@ end
187197

188198
let
189199
for N in [10]
190-
println("Constructing the electronic structure MPO for $N sites using ITensorMPOConstruction")
200+
println(
201+
"Constructing the electronic structure MPO for $N sites using ITensorMPOConstruction"
202+
)
191203
one_electron_coeffs, two_electron_coeffs = get_coefficients(N)
192-
@time "Total construction time" mpo = electronic_structure_OpIDSum(N, one_electron_coeffs, two_electron_coeffs)
204+
@time "Total construction time" mpo = electronic_structure_OpIDSum(
205+
N, one_electron_coeffs, two_electron_coeffs
206+
)
193207
println("The maximum bond dimension is $(maxlinkdim(mpo))")
194208
println("The sparsity is $(ITensorMPOConstruction.sparsity(mpo))")
195209
# @time "splitblocks" mpo = ITensors.splitblocks(linkinds, mpo)

examples/fermi-hubbard.jl

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ function Fermi_Hubbard_momentum_space(
7979
end
8080

8181
function transcorrelated_Fermi_Hubbard_momentum_space_OpIDSum(
82-
N::Int, t::Real=1.0, U::Real=4.0, J::Real=0.0; output_level::Int=0)::MPO
82+
N::Int, t::Real=1.0, U::Real=4.0, J::Real=0.0; output_level::Int=0
83+
)::MPO
8384
sites = siteinds("Electron", N; conserve_qns=true)
8485

8586
operatorNames = [electron_basis_ops for _ in 1:N]
@@ -95,7 +96,7 @@ function transcorrelated_Fermi_Hubbard_momentum_space_OpIDSum(
9596

9697
total_terms = N^3 + 2 * N
9798
J != 0 && (total_terms += 2 * N^5 + 2 * N^3)
98-
os = OpIDSum{6, Float64, UInt16}(total_terms, op_cache_vec)
99+
os = OpIDSum{6,Float64,UInt16}(total_terms, op_cache_vec)
99100

100101
@time "\tConstructing OpIDSum" let
101102
epsilon(k) = -2 * t * cospi(2 * k / N)
@@ -133,8 +134,26 @@ function transcorrelated_Fermi_Hubbard_momentum_space_OpIDSum(
133134
s == q && continue
134135

135136
gamma = 2 * t * (cosh(J) - 1) / (N^2) * epsilon(p - k + K)
136-
add!(os, gamma, opCdag(p - k, ), opCdag(q + K, ), opCdag(s + k - K, ), opC(s, ), opC(q, ), opC(p, ))
137-
add!(os, gamma, opCdag(p - k, ), opCdag(q + K, ), opCdag(s + k - K, ), opC(s, ), opC(q, ), opC(p, ))
137+
add!(
138+
os,
139+
gamma,
140+
opCdag(p - k, ),
141+
opCdag(q + K, ),
142+
opCdag(s + k - K, ),
143+
opC(s, ),
144+
opC(q, ),
145+
opC(p, ),
146+
)
147+
add!(
148+
os,
149+
gamma,
150+
opCdag(p - k, ),
151+
opCdag(q + K, ),
152+
opCdag(s + k - K, ),
153+
opC(s, ),
154+
opC(q, ),
155+
opC(p, ),
156+
)
138157
end
139158
end
140159
end
@@ -148,14 +167,15 @@ function transcorrelated_Fermi_Hubbard_momentum_space_OpIDSum(
148167
)
149168
end
150169

151-
152170
let
153171
for useITensorsAlg in [true, false]
154172
for N in [10]
155173
alg = useITensorsAlg ? "ITensorMPS" : "ITensorMPOConstruction"
156174

157175
println("Constructing the Fermi-Hubbard real space MPO for $N sites using $alg")
158-
@time "Total construction time" mpo = Fermi_Hubbard_real_space(N; useITensorsAlg=useITensorsAlg)
176+
@time "Total construction time" mpo = Fermi_Hubbard_real_space(
177+
N; useITensorsAlg=useITensorsAlg
178+
)
159179
println("The maximum bond dimension is $(maxlinkdim(mpo))")
160180
println("The sparsity is $(ITensorMPOConstruction.sparsity(mpo))")
161181
println()
@@ -165,8 +185,12 @@ end
165185

166186
let
167187
for N in [10]
168-
println("Constructing the Fermi-Hubbard momentum space MPO for $N sites using ITensorMPS")
169-
@time "Total construction time" mpo = Fermi_Hubbard_momentum_space(N; useITensorsAlg=true)
188+
println(
189+
"Constructing the Fermi-Hubbard momentum space MPO for $N sites using ITensorMPS"
190+
)
191+
@time "Total construction time" mpo = Fermi_Hubbard_momentum_space(
192+
N; useITensorsAlg=true
193+
)
170194
println("The maximum bond dimension is $(maxlinkdim(mpo))")
171195
println("The sparsity is $(ITensorMPOConstruction.sparsity(mpo))")
172196
println()
@@ -175,8 +199,12 @@ end
175199

176200
let
177201
for N in [10]
178-
println("Constructing the Fermi-Hubbard momentum space MPO for $N sites using ITensorMPOConstruction")
179-
@time "Total construction time" mpo = transcorrelated_Fermi_Hubbard_momentum_space_OpIDSum(N)
202+
println(
203+
"Constructing the Fermi-Hubbard momentum space MPO for $N sites using ITensorMPOConstruction",
204+
)
205+
@time "Total construction time" mpo = transcorrelated_Fermi_Hubbard_momentum_space_OpIDSum(
206+
N
207+
)
180208
println("The maximum bond dimension is $(maxlinkdim(mpo))")
181209
println("The sparsity is $(ITensorMPOConstruction.sparsity(mpo))")
182210
println()

examples/haldane-shastry.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ using ITensorMPS
33
using ITensors
44
using Test
55

6-
function haldane_shastry_mpo(N::Int, J::Real, tol::Real, absolute_tol::Bool; useITensorsAlg::Bool=false)::MPO
6+
function haldane_shastry_mpo(
7+
N::Int, J::Real, tol::Real, absolute_tol::Bool; useITensorsAlg::Bool=false
8+
)::MPO
79
os = OpSum()
810
@time "Constructing OpSum" for n in 1:N
911
for m in (n + 1):N
@@ -28,13 +30,15 @@ function ground_state_energy(N::Int, J::Real, twoSz::Int)
2830
return J * π^2 * ((N - 1 / N) / 24 + M * ((M^2 - 1) / (3 * N^2) - 1 / 4))
2931
end
3032

31-
function test_dmrg(H::MPO, twoSz::Int, noise::Vector{Float64}; maxdim::Int=0, compute_variance::Bool=false)::Nothing
33+
function test_dmrg(
34+
H::MPO, twoSz::Int, noise::Vector{Float64}; maxdim::Int=0, compute_variance::Bool=false
35+
)::Nothing
3236
N = length(H)
3337
sites = noprime(siteinds(first, H))
3438

3539
@assert abs(twoSz) <= N
3640
@assert mod(twoSz, 2) == mod(N, 2)
37-
41+
3842
maxdim == 0 && (maxdim = 2^(N ÷ 2))
3943
nUp = (twoSz + N) ÷ 2
4044
psi = random_mps(sites, [i <= nUp ? "Up" : "Dn" for i in 1:N]; linkdims=maxdim)
@@ -64,7 +68,7 @@ end
6468

6569
noise = [1e-5, 1e-6, 1e-7, 0, 0, 0, 0, 0, 0, 0]
6670

67-
for twoSz in -N:2:N
71+
for twoSz in (-N):2:N
6872
test_dmrg(H, twoSz, noise)
6973
println()
7074
end

0 commit comments

Comments
 (0)