Skip to content

Commit

Permalink
Update for ITensors.ITensorMPS (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Mar 26, 2024
1 parent 3622d19 commit a23e1f6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensorTDVP"
uuid = "25707e16-a4db-4a07-99d9-4d67b7af0342"
authors = ["Matthew Fishman <[email protected]> and contributors"]
version = "0.2.0"
version = "0.2.1"

[deps]
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
Expand All @@ -12,7 +12,7 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"

[compat]
ITensors = "0.3.32"
ITensors = "0.3.58"
KrylovKit = "0.6"
Observers = "0.2"
TimerOutputs = "0.5"
Expand Down
4 changes: 2 additions & 2 deletions src/alternating_update.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ITensors:
using ITensors: permute
using ITensors.ITensorMPS:
AbstractObserver,
MPO,
MPS,
Expand All @@ -9,7 +10,6 @@ using ITensors:
disk,
linkind,
maxlinkdim,
permute,
siteinds

function _compute_nsweeps(t; time_step=default_time_step(t), nsweeps=default_nsweeps())
Expand Down
9 changes: 5 additions & 4 deletions src/projmpo_apply.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ITensors: ITensors, AbstractProjMPO, ITensor, MPO, MPS
using ITensors: ITensor
using ITensors.ITensorMPS: ITensorMPS, AbstractProjMPO, MPO, MPS

"""
A ProjMPOApply represents the application of an
Expand Down Expand Up @@ -35,12 +36,12 @@ function Base.copy(P::ProjMPOApply)
return ProjMPOApply(P.lpos, P.rpos, P.nsite, copy(P.psi0), copy(P.H), copy(P.LR))
end

function ITensors.set_nsite!(P::ProjMPOApply, nsite)
function ITensorMPS.set_nsite!(P::ProjMPOApply, nsite)
P.nsite = nsite
return P
end

function ITensors.makeL!(P::ProjMPOApply, psi::MPS, k::Int)
function ITensorMPS.makeL!(P::ProjMPOApply, psi::MPS, k::Int)
# Save the last `L` that is made to help with caching
# for DiskProjMPO
ll = P.lpos
Expand All @@ -64,7 +65,7 @@ function ITensors.makeL!(P::ProjMPOApply, psi::MPS, k::Int)
return P
end

function ITensors.makeR!(P::ProjMPOApply, psi::MPS, k::Int)
function ITensorMPS.makeR!(P::ProjMPOApply, psi::MPS, k::Int)
# Save the last `R` that is made to help with caching
# for DiskProjMPO
rl = P.rpos
Expand Down
11 changes: 6 additions & 5 deletions src/projmpo_mps2.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ITensors: AbstractProjMPO, ProjMPO, contract, makeL!, makeR!, nsite, set_nsite!
using ITensors: contract
using ITensors.ITensorMPS: AbstractProjMPO, ProjMPO, makeL!, makeR!, nsite, set_nsite!

mutable struct ProjMPO_MPS2 <: AbstractProjMPO
PH::ProjMPO
Expand All @@ -15,25 +16,25 @@ end

Base.copy(P::ProjMPO_MPS2) = ProjMPO_MPS2(copy(P.PH), copy(P.Ms))

ITensors.nsite(P::ProjMPO_MPS2) = nsite(P.PH)
ITensorMPS.nsite(P::ProjMPO_MPS2) = nsite(P.PH)

function ITensors.set_nsite!(P::ProjMPO_MPS2, nsite)
function ITensorMPS.set_nsite!(P::ProjMPO_MPS2, nsite)
set_nsite!(P.PH, nsite)
for m in P.Ms
set_nsite!(m, nsite)
end
return P
end

function ITensors.makeL!(P::ProjMPO_MPS2, psi::MPS, k::Int)
function ITensorMPS.makeL!(P::ProjMPO_MPS2, psi::MPS, k::Int)
makeL!(P.PH, psi, k)
for m in P.Ms
makeL!(m, psi, k)
end
return P
end

function ITensors.makeR!(P::ProjMPO_MPS2, psi::MPS, k::Int)
function ITensorMPS.makeR!(P::ProjMPO_MPS2, psi::MPS, k::Int)
makeR!(P.PH, psi, k)
for m in P.Ms
makeR!(m, psi, k)
Expand Down
12 changes: 6 additions & 6 deletions src/projmps2.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ITensors:
AbstractProjMPO, ITensor, OneITensor, dag, dim, lproj, prime, rproj, site_range
using ITensors: ITensor, dag, dim, prime
using ITensors.ITensorMPS: ITensorMPS, AbstractProjMPO, OneITensor, lproj, rproj, site_range

"""
Holds the following data where psi
Expand Down Expand Up @@ -29,12 +29,12 @@ function Base.copy(P::ProjMPS2)
return ProjMPS2(P.lpos, P.rpos, P.nsite, copy(P.M), copy(P.LR))
end

function ITensors.set_nsite!(P::ProjMPS2, nsite)
function ITensorMPS.set_nsite!(P::ProjMPS2, nsite)
P.nsite = nsite
return P
end

function ITensors.makeL!(P::ProjMPS2, psi::MPS, k::Int)
function ITensorMPS.makeL!(P::ProjMPS2, psi::MPS, k::Int)
# Save the last `L` that is made to help with caching
# for DiskProjMPO
ll = P.lpos
Expand All @@ -58,7 +58,7 @@ function ITensors.makeL!(P::ProjMPS2, psi::MPS, k::Int)
return P
end

function ITensors.makeR!(P::ProjMPS2, psi::MPS, k::Int)
function ITensorMPS.makeR!(P::ProjMPS2, psi::MPS, k::Int)
# Save the last `R` that is made to help with caching
# for DiskProjMPO
rl = P.rpos
Expand All @@ -82,7 +82,7 @@ function ITensors.makeR!(P::ProjMPS2, psi::MPS, k::Int)
return P
end

function ITensors.contract(P::ProjMPS2, v::ITensor)
function ITensorMPS.contract(P::ProjMPS2, v::ITensor)
itensor_map = Union{ITensor,OneITensor}[lproj(P)]
append!(itensor_map, [prime(t, "Link") for t in P.M[site_range(P)]])
push!(itensor_map, rproj(P))
Expand Down
13 changes: 7 additions & 6 deletions src/sweep_update.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ITensors:
MPS, isortho, orthocenter, orthogonalize!, position!, replacebond!, set_nsite!, uniqueinds
using ITensors: uniqueinds
using ITensors.ITensorMPS:
ITensorMPS, MPS, isortho, orthocenter, orthogonalize!, position!, replacebond!, set_nsite!
using LinearAlgebra: norm, normalize!, svd
using Observers: update!
using Printf: @printf
Expand Down Expand Up @@ -270,9 +271,9 @@ function region_update!(
psi[b] = U
phi0 = S * V
if isforward(direction)
ITensors.setleftlim!(psi, b)
ITensorMPS.setleftlim!(psi, b)
elseif isreverse(direction)
ITensors.setrightlim!(psi, b)
ITensorMPS.setrightlim!(psi, b)
end
set_nsite!(PH, nsite - 1)
position!(PH, psi, b1)
Expand All @@ -281,9 +282,9 @@ function region_update!(
normalize && (phi0 ./= norm(phi0))
psi[b + Δ] = phi0 * psi[b + Δ]
if isforward(direction)
ITensors.setrightlim!(psi, b + Δ + 1)
ITensorMPS.setrightlim!(psi, b + Δ + 1)
elseif isreverse(direction)
ITensors.setleftlim!(psi, b + Δ - 1)
ITensorMPS.setleftlim!(psi, b + Δ - 1)
end
set_nsite!(PH, nsite)
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_contract_mpo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using Test: @test, @testset
H = MPO(elt, os, s)
Hpsi = apply(H, psi; alg="fit", nsweeps=2)
@test ITensors.scalartype(Hpsi) == elt
@test inner(psi, Hpsi) inner(psi', H, psi) rtol = eps(real(elt))
@test inner(psi, Hpsi) inner(psi', H, psi) rtol = 10 * eps(real(elt))
# Change "top" indices of MPO to be a different set
t = siteinds("S=1/2", N; conserve_qns)
psit = deepcopy(psi)
Expand All @@ -46,7 +46,7 @@ using Test: @test, @testset
# Test with nsweeps=2
Hpsi = apply(H, psi; alg="fit", nsweeps=2)
@test ITensors.scalartype(Hpsi) == elt
@test inner(psit, Hpsi) inner(psit, H, psi) rtol = eps(real(elt))
@test inner(psit, Hpsi) inner(psit, H, psi) rtol = 10 * eps(real(elt))
# Test with less good initial guess MPS not equal to psi
psi_guess = copy(psi)
truncate!(psi_guess; maxdim=2)
Expand Down

0 comments on commit a23e1f6

Please sign in to comment.