Skip to content

Commit

Permalink
Merge cd1f66d into 95c0a70
Browse files Browse the repository at this point in the history
  • Loading branch information
emstoudenmire committed Apr 13, 2024
2 parents 95c0a70 + cd1f66d commit 4e8f7f2
Show file tree
Hide file tree
Showing 30 changed files with 298 additions and 240 deletions.
4 changes: 2 additions & 2 deletions ITensorGaussianMPS/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensorGaussianMPS"
uuid = "2be41995-7c9f-4653-b682-bfa4e7cebb93"
authors = ["Matthew Fishman <mfishman@flatironinstitute.org> and contributors"]
version = "0.1.4"
version = "0.1.5"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand All @@ -10,6 +10,6 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[compat]
Compat = "3.40.0, 4"
ITensors = "0.3.58"
ITensors = "0.3.58, 0.4"
LinearAlgebra = "1.6"
julia = "1.6"
4 changes: 2 additions & 2 deletions ITensorVisualizationBase/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensorVisualizationBase"
uuid = "cd2553d2-8bef-4d93-8a38-c62f17d5ad23"
authors = ["Matthew Fishman <mfishman@flatironinstitute.org> and contributors"]
version = "0.1.5"
version = "0.1.6"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand All @@ -20,7 +20,7 @@ AbstractTrees = "0.4"
Compat = "3.40.0, 4"
GeometryBasics = "0.4.1"
Graphs = "1.4.1"
ITensors = "0.2.12, 0.3"
ITensors = "0.2.12, 0.3, 0.4"
MetaGraphs = "0.7.1"
NetworkLayout = "0.4.3"
Statistics = "1"
Expand Down
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensors"
uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5"
authors = ["Matthew Fishman <mfishman@flatironinstitute.org>", "Miles Stoudenmire <mstoudenmire@flatironinstitute.org>"]
version = "0.3.67"
version = "0.4.0"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand All @@ -11,7 +11,6 @@ Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
IsApprox = "28f27b66-4bd8-47e7-9110-e2746eb8bed7"
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -34,9 +33,11 @@ Zeros = "bd1ec220-6eb4-527a-9b49-e79c3db6233b"
ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444"

[weakdeps]
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8"

[extensions]
ITensorsHDF5Ext = "HDF5"
ITensorsVectorInterfaceExt = "VectorInterface"

[compat]
Expand Down Expand Up @@ -72,4 +73,5 @@ ZygoteRules = "0.2.2"
julia = "1.6"

[extras]
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8"
3 changes: 2 additions & 1 deletion docs/settings.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Documenter, ITensors
using Documenter
using ITensors

DocMeta.setdocmeta!(ITensors, :DocTestSetup, :(using ITensors); recursive=true)

Expand Down
3 changes: 2 additions & 1 deletion docs/src/ITensorType.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ itensor(::Array{<:Number}, ::ITensors.Indices)
## QN BlockSparse Constructors

```@docs
ITensor(::Type{<:Number}, ::QN, ::ITensors.Indices)
ITensor(::Type{<:Number}, ::QN, ::ITensors.QNIndices)
ITensor(::ITensors.AliasStyle, ::Type{<:Number}, ::Array{<:Number}, ::ITensors.QNIndices; tol=0)
ITensor(::Type{<:Number}, ::UndefInitializer, ::QN, ::ITensors.Indices)
```
Expand Down Expand Up @@ -117,6 +117,7 @@ swapinds(::ITensor, ::Any...)
```@docs
*(::ITensor, ::ITensor)
dag(T::ITensor; kwargs...)
directsum(::Pair{ITensor},::Pair{ITensor},::Pair{ITensor},args...; kws...)
exp(::ITensor, ::Any, ::Any)
nullspace(::ITensor, ::Any...)
```
Expand Down
11 changes: 7 additions & 4 deletions docs/src/examples/ITensor.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ UT = dag(C) * CT

## Write and Read an ITensor to Disk with HDF5



!!! info
Make sure to install the HDF5 package to use this feature. (Run `julia> ] add HDF5` in the Julia REPL console.)

Saving ITensors to disk can be very useful. For example, you
might encounter a bug in your own code, and by reading the
ITensors involved from disk you can shortcut the process of
Expand All @@ -520,7 +525,7 @@ from a calculation. To write it to an HDF5 file named "myfile.h5"
you can use the following pattern:

```julia
using ITensors.HDF5
using HDF5
f = h5open("myfile.h5","w")
write(f,"T",T)
close(f)
Expand All @@ -531,16 +536,14 @@ or "Result Tensor" and doesn't have to have the same name as the reference `T`.
Closing the file `f` is optional and you can also write other objects to the same
file before closing it.

[*Above we did `using ITensors.HDF5` since HDF5 is already included as a dependency with ITensor. You can also do `using HDF5` but must add the HDF5 package beforehand for that to work.*]

**Reading an ITensor from an HDF5 File**

Say you have an HDF5 file "myfile.h5" which contains an ITensor stored as a dataset with the
name "T". (Which would be the situation if you wrote it as in the example above.)
To read this ITensor back from the HDF5 file, use the following pattern:

```julia
using ITensors.HDF5
using HDF5
f = h5open("myfile.h5","r")
T = read(f,"T",ITensor)
close(f)
Expand Down
10 changes: 5 additions & 5 deletions docs/src/examples/MPSandMPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,17 @@ can be brought into orthogonal form by calling `psi = orthogonalize(psi, 1)`.

## Write and Read an MPS or MPO to Disk with HDF5

!!! info
Make sure to install the HDF5 package to use this feature. (Run `julia> ] add HDF5` in the Julia REPL console.)

**Writing an MPS to an HDF5 File**

Let's say you have an MPS `psi` which you have made or obtained
from a calculation. To write it to an HDF5 file named "myfile.h5"
you can use the following pattern:

```julia
using ITensors.HDF5
using HDF5
f = h5open("myfile.h5","w")
write(f,"psi",psi)
close(f)
Expand All @@ -402,16 +405,14 @@ or "Result MPS" and doesn't have to have the same name as the reference `psi`.
Closing the file `f` is optional and you can also write other objects to the same
file before closing it.

[*Above we did `using ITensors.HDF5` since HDF5 is already included as a dependency with ITensor. You can also do `using HDF5` but must add the HDF5 package beforehand for that to work.*]

**Reading an MPS from an HDF5 File**

Say you have an HDF5 file "myfile.h5" which contains an MPS stored as a dataset with the
name "psi". (Which would be the situation if you wrote it as in the example above.)
To read this ITensor back from the HDF5 file, use the following pattern:

```julia
using ITensors.HDF5
using HDF5
f = h5open("myfile.h5","r")
psi = read(f,"psi",MPS)
close(f)
Expand All @@ -436,7 +437,6 @@ H = MPO(os,sites)
energy_psi = inner(psi',H,psi)
```


Note the `MPS` argument to the read function, which tells Julia which read function
to call and how to interpret the data stored in the HDF5 dataset named "psi". In the
future we might lift the requirement of providing the type and have it be detected
Expand Down
28 changes: 28 additions & 0 deletions ext/ITensorsHDF5Ext/ITensorMPS/mpo.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HDF5: HDF5, attributes, create_group, open_group, read, write
using ITensors: ITensor
using ITensors.ITensorMPS: MPO

function HDF5.write(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, M::MPO)
g = create_group(parent, name)
attributes(g)["type"] = "MPO"
attributes(g)["version"] = 1
N = length(M)
write(g, "rlim", M.rlim)
write(g, "llim", M.llim)
write(g, "length", N)
for n in 1:N
write(g, "MPO[$(n)]", M[n])
end
end

function HDF5.read(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, ::Type{MPO})
g = open_group(parent, name)
if read(attributes(g)["type"]) != "MPO"
error("HDF5 group or file does not contain MPO data")
end
N = read(g, "length")
rlim = read(g, "rlim")
llim = read(g, "llim")
v = [read(g, "MPO[$(i)]", ITensor) for i in 1:N]
return MPO(v, llim, rlim)
end
28 changes: 28 additions & 0 deletions ext/ITensorsHDF5Ext/ITensorMPS/mps.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HDF5: HDF5, attributes, create_group, open_group, read, write
using ITensors: ITensor
using ITensors.ITensorMPS: MPS

function HDF5.write(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, M::MPS)
g = create_group(parent, name)
attributes(g)["type"] = "MPS"
attributes(g)["version"] = 1
N = length(M)
write(g, "length", N)
write(g, "rlim", M.rlim)
write(g, "llim", M.llim)
for n in 1:N
write(g, "MPS[$(n)]", M[n])
end
end

function HDF5.read(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, ::Type{MPS})
g = open_group(parent, name)
if read(attributes(g)["type"]) != "MPS"
error("HDF5 group or file does not contain MPS data")
end
N = read(g, "length")
rlim = read(g, "rlim")
llim = read(g, "llim")
v = [read(g, "MPS[$(i)]", ITensor) for i in 1:N]
return MPS(v, llim, rlim)
end
12 changes: 12 additions & 0 deletions ext/ITensorsHDF5Ext/ITensorsHDF5Ext.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ITensorsHDF5Ext

include("index.jl")
include("itensor.jl")
include("qnindex.jl")
include("indexset.jl")
include("qn.jl")
include("tagset.jl")
include("ITensorMPS/mps.jl")
include("ITensorMPS/mpo.jl")

end
43 changes: 43 additions & 0 deletions ext/ITensorsHDF5Ext/index.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using HDF5: HDF5, attributes, create_group, open_group, read, write
using ITensors: Arrow, dim, dir, id, Index, plev, QNBlocks, space, tags, TagSet

function HDF5.write(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, I::Index)
g = create_group(parent, name)
attributes(g)["type"] = "Index"
attributes(g)["version"] = 1
write(g, "id", id(I))
write(g, "dim", dim(I))
write(g, "dir", Int(dir(I)))
write(g, "tags", tags(I))
write(g, "plev", plev(I))
if typeof(space(I)) == Int
attributes(g)["space_type"] = "Int"
elseif typeof(space(I)) == QNBlocks
attributes(g)["space_type"] = "QNBlocks"
write(g, "space", space(I))
else
error("Index space type not recognized")
end
end

function HDF5.read(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, ::Type{Index})
g = open_group(parent, name)
if read(attributes(g)["type"]) != "Index"
error("HDF5 group or file does not contain Index data")
end
id = read(g, "id")
dim = read(g, "dim")
dir = Arrow(read(g, "dir"))
tags = read(g, "tags", TagSet)
plev = read(g, "plev")
space_type = "Int"
if haskey(attributes(g), "space_type")
space_type = read(attributes(g)["space_type"])
end
if space_type == "Int"
space = dim
elseif space_type == "QNBlocks"
space = read(g, "space", QNBlocks)
end
return Index(id, space, dir, tags, plev)
end
24 changes: 24 additions & 0 deletions ext/ITensorsHDF5Ext/indexset.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using HDF5: HDF5, attributes, create_group, open_group, read, write
using ITensors: Index, Indices

function HDF5.write(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, is::Indices)
g = create_group(parent, name)
attributes(g)["type"] = "IndexSet"
attributes(g)["version"] = 1
N = length(is)
write(g, "length", N)
for n in 1:N
write(g, "index_$n", is[n])
end
end

function HDF5.read(
parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, T::Type{<:Indices}
)
g = open_group(parent, name)
if read(attributes(g)["type"]) != "IndexSet"
error("HDF5 group or file does not contain IndexSet data")
end
n = read(g, "length")
return T(Index[read(g, "index_$j", Index) for j in 1:n])
end
36 changes: 36 additions & 0 deletions ext/ITensorsHDF5Ext/itensor.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using HDF5: HDF5, attributes, create_group, open_group, read, write
using ITensors: inds, itensor, ITensor, storage
using NDTensors:
NDTensors, BlockSparse, Combiner, Dense, Diag, DiagBlockSparse, EmptyStorage

function HDF5.write(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, T::ITensor)
g = create_group(parent, name)
attributes(g)["type"] = "ITensor"
attributes(g)["version"] = 1
write(g, "inds", inds(T))
return write(g, "storage", storage(T))
end

function HDF5.read(
parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, ::Type{ITensor}
)
g = open_group(parent, name)
if read(attributes(g)["type"]) != "ITensor"
error("HDF5 group or file does not contain ITensor data")
end
inds = read(g, "inds", Vector{<:Index})

# check input file for key name of ITensor data
# ITensors.jl <= v0.1.x uses `store` as key
# whereas ITensors.jl >= v0.2.x uses `storage` as key
for key in ["storage", "store"]
if haskey(g, key)
stypestr = read(attributes(open_group(g, key))["type"])
stype = eval(Meta.parse(stypestr))
storage = read(g, key, stype)
return itensor(storage, inds)
end
end
return error("HDF5 file: $(g) does not contain correct ITensor data.\nNeither key
`store` nor `storage` could be found.")
end
26 changes: 26 additions & 0 deletions ext/ITensorsHDF5Ext/qn.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using HDF5: HDF5, attributes, create_group, open_group, read, write
using ITensors: maxQNs, modulus, name, QN, QNVal, val

function HDF5.write(parent::Union{HDF5.File,HDF5.Group}, gname::AbstractString, q::QN)
g = create_group(parent, gname)
attributes(g)["type"] = "QN"
attributes(g)["version"] = 1
names = [String(name(q[n])) for n in 1:maxQNs]
vals = [val(q[n]) for n in 1:maxQNs]
mods = [modulus(q[n]) for n in 1:maxQNs]
write(g, "names", names)
write(g, "vals", vals)
return write(g, "mods", mods)
end

function HDF5.read(parent::Union{HDF5.File,HDF5.Group}, name::AbstractString, ::Type{QN})
g = open_group(parent, name)
if read(attributes(g)["type"]) != "QN"
error("HDF5 group or file does not contain QN data")
end
names = read(g, "names")
vals = read(g, "vals")
mods = read(g, "mods")
mqn = ntuple(n -> QNVal(names[n], vals[n], mods[n]), maxQNs)
return QN(mqn)
end
Loading

0 comments on commit 4e8f7f2

Please sign in to comment.