Skip to content

Commit

Permalink
[Docs] Fix entanglement entropy example (#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Mar 28, 2024
1 parent 9fb3595 commit 7f8d21b
Show file tree
Hide file tree
Showing 23 changed files with 192 additions and 226 deletions.
24 changes: 11 additions & 13 deletions ITensorGPU/examples/dmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@ using ITensors
using ITensorGPU

# Set to identity to run on CPU
gpu = cu
device = cu

N = 50
sites = siteinds("S=1", N)

ampo = AutoMPO()
opsum = OpSum()
for j in 1:(N - 1)
ampo .+= 0.5, "S+", j, "S-", j + 1
ampo .+= 0.5, "S-", j, "S+", j + 1
ampo .+= "Sz", j, "Sz", j + 1
opsum .+= 0.5, "S+", j, "S-", j + 1
opsum .+= 0.5, "S-", j, "S+", j + 1
opsum .+= "Sz", j, "Sz", j + 1
end
H = gpu(MPO(ampo, sites))
H = device(MPO(opsum, sites))

ψ₀ = gpu(randomMPS(sites))
ψ₀ = device(randomMPS(sites))

sweeps = Sweeps(6)
maxdim!(sweeps, 10, 20, 40, 100)
mindim!(sweeps, 1, 10)
cutoff!(sweeps, 1e-11)
noise!(sweeps, 1e-10)
energy, ψ = @time dmrg(H, ψ₀, sweeps)
dmrg_kwargs = (;
nsweeps=6, maxdim=[10, 20, 40, 100], mindim=[1, 10], cutoff=1e-11, noise=1e-10
)
energy, ψ = @time dmrg(H, ψ₀; dmrg_kwargs...)
@show energy
64 changes: 32 additions & 32 deletions ITensorGPU/test/test_dmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ end
N = 10
sites = siteinds("S=1",N; conserve_qns=true)
ampo = AutoMPO()
opsum = OpSum()
for j=1:N-1
add!(ampo,"Sz",j,"Sz",j+1)
add!(ampo,0.5,"S+",j,"S-",j+1)
add!(ampo,0.5,"S-",j,"S+",j+1)
add!(opsum,"Sz",j,"Sz",j+1)
add!(opsum,0.5,"S+",j,"S-",j+1)
add!(opsum,0.5,"S-",j,"S+",j+1)
end
H = cuMPO(MPO(ampo,sites))
H = cuMPO(MPO(opsum,sites))
state = [isodd(n) ? "Up" : "Dn" for n in 1:N]
psi = randomCuMPS(sites,state,4)
Expand All @@ -63,12 +63,12 @@ end
Random.seed!(432)
psi0 = randomCuMPS(sites)

ampo = AutoMPO()
opsum = OpSum()
for j in 1:N
j < N && add!(ampo, -1.0, "Sz", j, "Sz", j + 1)
add!(ampo, -0.5, "Sx", j)
j < N && add!(opsum, -1.0, "Sz", j, "Sz", j + 1)
add!(opsum, -0.5, "Sx", j)
end
H = cuMPO(MPO(ampo, sites))
H = cuMPO(MPO(opsum, sites))

sweeps = Sweeps(5)
maxdim!(sweeps, 10, 20)
Expand Down Expand Up @@ -104,14 +104,14 @@ end
Random.seed!(42)
psi0 = randomCuMPS(sites)
ampo = AutoMPO()
opsum = OpSum()
for j = 1:N-1
add!(ampo,-1.0,"Sz",j,"Sz",j+1)
add!(opsum,-1.0,"Sz",j,"Sz",j+1)
end
for j = 1:N
add!(ampo,-0.2,"Sx",j)
add!(opsum,-0.2,"Sx",j)
end
H = cuMPO(MPO(ampo,sites))
H = cuMPO(MPO(opsum,sites))
sweeps = Sweeps(3)
maxdim!(sweeps,10)
Expand All @@ -138,18 +138,18 @@ end
N = 10
sites = siteinds("S=1",N)
ampoZ = AutoMPO()
ampoZ = OpSum()
for j=1:N-1
add!(ampoZ,"Sz",j,"Sz",j+1)
add!(opsumZ,"Sz",j,"Sz",j+1)
end
HZ = MPO(ampoZ,sites)
HZ = MPO(opsumZ,sites)
ampoXY = AutoMPO()
ampoXY = OpSum()
for j=1:N-1
add!(ampoXY,0.5,"S+",j,"S-",j+1)
add!(ampoXY,0.5,"S-",j,"S+",j+1)
add!(opsumXY,0.5,"S+",j,"S-",j+1)
add!(opsumXY,0.5,"S-",j,"S+",j+1)
end
HXY = MPO(ampoXY,sites)
HXY = MPO(opsumXY,sites)
psi = randomMPS(sites)
Expand All @@ -170,13 +170,13 @@ end
sites[1] = Index(2,"S=1/2,n=1,Site")
sites[N] = Index(2,"S=1/2,n=$N,Site")
ampo = AutoMPO()
opsum = OpSum()
for j=1:N-1
add!(ampo,"Sz",j,"Sz",j+1)
add!(ampo,0.5,"S+",j,"S-",j+1)
add!(ampo,0.5,"S-",j,"S+",j+1)
add!(opsum,"Sz",j,"Sz",j+1)
add!(opsum,0.5,"S+",j,"S-",j+1)
add!(opsum,0.5,"S-",j,"S+",j+1)
end
H = cuMPO(MPO(ampo,sites))
H = cuMPO(MPO(opsum,sites))
psi0i = randomCuMPS(sites,10)
Expand Down Expand Up @@ -211,17 +211,17 @@ end
state[7] = 2
psi0 = productMPS(s,state)
ampo = AutoMPO()
opsum = OpSum()
for j=1:N-1
ampo += (-t1, "Cdag", j, "C", j+1)
ampo += (-t1, "Cdag", j+1, "C", j)
ampo += ( V, "N", j, "N", j+1)
opsum += (-t1, "Cdag", j, "C", j+1)
opsum += (-t1, "Cdag", j+1, "C", j)
opsum += ( V, "N", j, "N", j+1)
end
for j=1:N-2
ampo += (-t2, "Cdag", j, "C", j+2)
ampo += (-t2, "Cdag", j+2, "C", j)
opsum += (-t2, "Cdag", j, "C", j+2)
opsum += (-t2, "Cdag", j+2, "C", j)
end
H = MPO(ampo, s)
H = MPO(opsum, s)
sweeps = Sweeps(5)
maxdim!(sweeps, 10, 20, 100, 100, 200)
Expand Down
10 changes: 2 additions & 8 deletions ITensorGaussianMPS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,10 @@ println("\nRandom state starting energy")
@show inner(ψr, H, ψr)

println("\nRun dmrg with random starting state")
sweeps = Sweeps(10)
setmaxdim!(sweeps,10,20,40,60)
setcutoff!(sweeps,1E-12)
@time dmrg(H, ψr, sweeps)
@time dmrg(H, ψr; nsweeps=10, maxdim=[10, 20, 40, 60], cutoff=1e-12)

println("\nRun dmrg with free fermion starting state")
sweeps = Sweeps(4)
setmaxdim!(sweeps,60)
setcutoff!(sweeps,1E-12)
@time dmrg(H, ψ0, sweeps)
@time dmrg(H, ψ0; nsweeps=4, maxdim=60, cutoff=1e-12)
```
This will output something like:
```julia
Expand Down
3 changes: 3 additions & 0 deletions ITensorGaussianMPS/examples/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[deps]
ITensorGaussianMPS = "2be41995-7c9f-4653-b682-bfa4e7cebb93"
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,16 @@ H = MPO(os, s)

println("Random starting state energy")
@show flux(ψr)
@show inner(ψr, H, ψr)
@show inner(ψr', H, ψr)
println()
println("Free fermion starting state energy")
@show flux(ψ0)
@show inner(ψ0, H, ψ0)
@show inner(ψ0', H, ψ0)

println("\nStart from product state")
sweeps = Sweeps(10)
setmaxdim!(sweeps, 10, 20, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
@time dmrg(H, ψr, sweeps)
@time dmrg(H, ψr; nsweeps=10, maxdim=[10, 20, _maxlinkdim], cutoff=_cutoff)

println("\nStart from free fermion state")
sweeps = Sweeps(5)
setmaxdim!(sweeps, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
@time dmrg(H, ψ0, sweeps)
@time dmrg(H, ψ0; nsweeps=5, maxdim=_maxlinkdim, cutoff=_cutoff)

nothing
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ println("\nFree fermion MPS starting state energy")
@show inner(ψ0, H, ψ0)

println("\nStart from random product state")
sweeps = Sweeps(10)
setmaxdim!(sweeps, 10, 20, 100, 200, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
setnoise!(sweeps, 1e-7, 1e-8, 1e-10, 0.0)
@time dmrg(H, ψr, sweeps)
dmrg_kwargs = (;
nsweeps=10,
maxdim=[10, 20, 100, 200, _maxlinkdim],
cutoff=_cutoff,
noise=[1e-7, 1e-8, 1e-10, 0.0],
)
@time dmrg(H, ψr; dmrg_kwargs...)

println("\nStart from free fermion state")
sweeps = Sweeps(10)
setmaxdim!(sweeps, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
@time dmrg(H, ψ0, sweeps)
@time dmrg(H, ψ0; nsweeps=10, maxdim=_maxlinkdim, cutoff=_cutoff)

nothing
16 changes: 5 additions & 11 deletions ITensorGaussianMPS/examples/hubbard_1d_spin_conservation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ for n in 1:(N - 1)
end

H_noninteracting = MPO(os_noninteracting, s)
@show inner(ψ0, H_noninteracting, ψ0)
@show inner(ψ0', H_noninteracting, ψ0)
@show sum(diag(Φ_up' * h_up * Φ_up)) + sum(diag(Φ_dn' * h_dn * Φ_dn))

# The total interacting Hamiltonian
Expand All @@ -90,25 +90,19 @@ H = MPO(os_interacting, s)

println("Random starting state energy")
@show flux(ψr)
@show inner(ψr, H, ψr)
@show inner(ψr', H, ψr)
println()
println("Free fermion starting state energy")
@show flux(ψ0)
@show inner(ψ0, H, ψ0)
@show inner(ψ0', H, ψ0)

println("\nStart from random product state")
sweeps = Sweeps(10)
setmaxdim!(sweeps, 10, 20, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
er, ψ̃r = @time dmrg(H, ψr, sweeps)
er, ψ̃r = @time dmrg(H, ψr; nsweeps=10, maxdim=[10, 20, _maxlinkdim], cutoff=_cutoff)
@show er
@show flux(ψ̃r)

println("\nStart from free fermion state")
sweeps = Sweeps(5)
setmaxdim!(sweeps, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
e0, ψ̃0 = @time dmrg(H, ψ0, sweeps)
e0, ψ̃0 = @time dmrg(H, ψ0; nsweeps=5, maxdim=_maxlinkdim, cutoff=_cutoff)
@show e0
@show flux(ψ̃0)

Expand Down
21 changes: 10 additions & 11 deletions ITensorGaussianMPS/examples/hubbard_2d_spin_conservation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,22 @@ H = MPO(os, s)

println("\nRandom starting state energy")
@show flux(ψr)
@show inner(ψr, H, ψr)
@show inner(ψr', H, ψr)

println("\nFree fermion MPS starting state energy")
@show flux(ψ0)
@show inner(ψ0, H, ψ0)
@show inner(ψ0', H, ψ0)

println("\nStart from random product state")
sweeps = Sweeps(10)
setmaxdim!(sweeps, 10, 20, 100, 200, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
setnoise!(sweeps, 1e-7, 1e-8, 1e-10, 0.0)
@time dmrg(H, ψr, sweeps)
dmrg_kwargs = (;
nsweeps=10,
maxdim=[10, 20, 100, 200, _maxlinkdim],
cutoff=_cutoff,
noise=[1e-7, 1e-8, 1e-10, 0.0],
)
@time dmrg(H, ψr; dmrg_kwargs...)

println("\nStart from free fermion state")
sweeps = Sweeps(10)
setmaxdim!(sweeps, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
@time dmrg(H, ψ0, sweeps)
@time dmrg(H, ψ0; nsweeps=10, maxdim=_maxlinkdim, cutoff=_cutoff)

nothing
9 changes: 3 additions & 6 deletions ITensorGaussianMPS/examples/mps_to_determinants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,10 @@ H = MPO(os, s)

println("Free fermion starting state energy")
@show flux(ψ0)
@show inner(ψ0, H, ψ0)
@show inner(ψ0', H, ψ0)

println("\nStart from free fermion state")
sweeps = Sweeps(5)
setmaxdim!(sweeps, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
e, ψ = @time dmrg(H, ψ0, sweeps)
e, ψ = @time dmrg(H, ψ0; nsweeps=5, maxdim=_maxlinkdim, cutoff=_cutoff)
@show e
@show flux(ψ)

Expand All @@ -81,4 +78,4 @@ using ITensorGaussianMPS: correlation_matrix_to_gmps, correlation_matrix_to_mps,
Λ_dn = correlation_matrix(ψ, "Cdagdn", "Cdn")
ψ̃0 = correlation_matrix_to_mps(s, Λ_up, Λ_dn; eigval_cutoff=1e-2, maxblocksize=4)
@show inner(ψ̃0, ψ)
@show inner(ψ̃0, H, ψ̃0)
@show inner(ψ̃0', H, ψ̃0)
14 changes: 4 additions & 10 deletions ITensorGaussianMPS/examples/spinless_fermion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,16 @@ H = MPO(os, s)

println("\nRandom state starting energy")
@show flux(ψr)
@show inner(ψr, H, ψr)
@show inner(ψr', H, ψr)

println("\nFree fermion starting energy")
@show flux(ψ0)
@show inner(ψ0, H, ψ0)
@show inner(ψ0', H, ψ0)

println("\nRun dmrg with random starting state")
sweeps = Sweeps(20)
setmaxdim!(sweeps, 10, 20, 40, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
@time dmrg(H, ψr, sweeps)
@time dmrg(H, ψr; nsweeps=20, maxdim=[10, 20, 40, _maxlinkdim], cutoff=_cutoff)

println("\nRun dmrg with free fermion starting state")
sweeps = Sweeps(4)
setmaxdim!(sweeps, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
@time dmrg(H, ψ0, sweeps)
@time dmrg(H, ψ0; nsweeps=4, maxdim=_maxlinkdim, cutoff=_cutoff)

nothing
9 changes: 3 additions & 6 deletions ITensorGaussianMPS/examples/spinless_fermion_pairing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,16 @@ let

println("\nFree fermion starting energy")
@show flux(psi)
@show inner(psi, H, psi)
@show inner(psi', H, psi)
println("\nRun dmrg with GMPS starting state")
sweeps = Sweeps(12)
setmaxdim!(sweeps, 10, 20, 40, _maxlinkdim)
setcutoff!(sweeps, _cutoff)
_, psidmrg = dmrg(H, psi, sweeps)
_, psidmrg = dmrg(H, psi; nsweeps=12, maxdim=[10, 20, 40, _maxlinkdim], cutoff=_cutoff)
cdagc_dmrg = correlation_matrix(psidmrg, "C", "Cdag")
cc_dmrg = correlation_matrix(psidmrg, "C", "C")

@show norm(cdagc_dmrg - cdagc)
@show norm(cc_dmrg - cc)

@show inner(psidmrg, H, psidmrg)
@show inner(psidmrg', H, psidmrg)
@show(abs(inner(psidmrg, psi)))

#return
Expand Down
2 changes: 1 addition & 1 deletion ITensorUnicodePlots/examples/ex_qn_mps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using ITensorUnicodePlots

s = siteinds("S=1/2", 5; conserve_qns=true)
ψ = randomMPS(s, n -> isodd(n) ? "" : ""; linkdims=2)
orthogonalize!(ψ, 2)
ψ = orthogonalize(ψ, 2)
ψdag = prime(linkinds, dag(ψ))
tn =..., ψdag...]

Expand Down
Loading

0 comments on commit 7f8d21b

Please sign in to comment.