Skip to content

Commit

Permalink
Improve siteinds, simplify example
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Jan 31, 2025
1 parent 58a3a0b commit f738f58
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
56 changes: 30 additions & 26 deletions examples/dmrg/1d_heisenberg_conserve_spin.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
using ITensors, ITensorMPS
using LinearAlgebra
using Printf
using Random
using Strided

Random.seed!(1234)
BLAS.set_num_threads(1)
Strided.set_num_threads(1)
ITensors.enable_threaded_blocksparse()
#ITensors.disable_threaded_blocksparse()

let
N = 100

sites = siteinds("S=1", N; conserve_qns=true)
using SymmetrySectors

function heisenberg(N)
os = OpSum()
for j in 1:(N - 1)
os += 0.5, "S+", j, "S-", j + 1
os += 0.5, "S-", j, "S+", j + 1
os += "Sz", j, "Sz", j + 1
end
H = MPO(os, sites)
return os
end

Random.seed!(1234)

state = [isodd(n) ? "Up" : "Dn" for n in 1:N]
psi0 = random_mps(sites, state; linkdims=10)
N = 10

# Plan to do 5 DMRG sweeps:
nsweeps = 5
# Set maximum MPS bond dimensions for each sweep
maxdim = [10, 20, 100, 100, 200]
# Set maximum truncation error allowed when adapting bond dimensions
cutoff = [1E-11]
s = siteinds("S=1/2", N; gradings=("Sz",))

# Run the DMRG algorithm, returning energy and optimized MPS
energy, psi = dmrg(H, psi0; nsweeps, maxdim, cutoff)
@printf("Final energy = %.12f\n", energy)
end
os = heisenberg(N)

# Input operator terms which define a Hamiltonian
# Convert these terms to an MPO tensor network
H = MPO(os, s)

# Create an initial random matrix product state
# psi0 = random_mps(s; linkdims=10)
psi0 = MPS(s, j -> isodd(j) ? "" : "")

# Plan to do 5 DMRG sweeps:
nsweeps = 5
# Set maximum MPS bond dimensions for each sweep
maxdim = [10]
# Set maximum truncation error allowed when adapting bond dimensions
cutoff = [1E-11]

# Run the DMRG algorithm, returning energy and optimized MPS
energy, psi = dmrg(H, psi0; nsweeps, maxdim, cutoff)
@show inner(psi', H, psi)
@show inner(psi, psi)
@printf("Final energy = %.12f\n", energy)
1 change: 1 addition & 0 deletions examples/dmrg/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Strided = "5e0ebb24-38b0-5f93-81fe-25c709ecae67"
SymmetrySectors = "f8a8ad64-adbc-4fce-92f7-ffe2bb36a86e"
7 changes: 3 additions & 4 deletions src/siteinds.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using ITensors: Index, settag
using ITensors: ITensors, Index, settag
using QuantumOperatorDefinitions: SiteType

# TODO: Move to `ITensorSites.jl`?
# TODO: Add support for symmetry sectors.
function siteinds(t::String, n::Int)
return [settag(Index(length(SiteType(t))), "sitetype", t) for _ in 1:n]
function siteinds(t::String, n::Int; kwargs...)
return [settag(Index(SiteType(t; kwargs...)), "n", string(j)) for j in 1:n]
end

0 comments on commit f738f58

Please sign in to comment.