Skip to content

Commit

Permalink
[ITensorMPS] Update examples to use ITensorMPS.jl (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored May 9, 2024
1 parent 36da003 commit 3d36e74
Show file tree
Hide file tree
Showing 60 changed files with 227 additions and 187 deletions.
1 change: 1 addition & 0 deletions .github/workflows/Downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
os: [ubuntu-latest]
package:
- {user: ITensor, repo: ITensorGaussianMPS.jl}
- {user: ITensor, repo: ITensorMPS.jl}
- {user: ITensor, repo: ITensorTDVP.jl}
- {user: ITensor, repo: ITensorUnicodePlots.jl}
- {user: ITensor, repo: ITensorVisualizationBase.jl}
Expand Down
2 changes: 1 addition & 1 deletion 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 <[email protected]>", "Miles Stoudenmire <[email protected]>"]
version = "0.6.0"
version = "0.6.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
2 changes: 1 addition & 1 deletion docs/settings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ settings = Dict(
"Observer.md",
"DMRGObserver.md",
],
"OpSum (AutoMPO)" => "OpSum.md",
"OpSum" => "OpSum.md",
],
"Frequently Asked Questions" => [
"Programming Language (Julia, C++, ...) FAQs" => "faq/JuliaAndCpp.md",
Expand Down
28 changes: 5 additions & 23 deletions docs/src/AdvancedUsageGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ NDTensors.Dense{Float64,Array{Float64,1}}
A common place you might accidentally come across this is when
you are creating a Hamiltonian with `OpSum`:
```julia
julia> using ITensors, ITensorMPS

julia> N = 4;

julia> sites = siteinds("S=1/2",N);
Expand Down Expand Up @@ -180,7 +182,7 @@ Julia provides many tools for searching for documentation interactively at the R
julia> using ITensors

julia> ?ITensor
search: ITensor ITensors itensor emptyITensor randomITensor
search: ITensor ITensors itensor randomITensor

An ITensor is a tensor whose interface is independent of its
memory layout. Therefore it is not necessary to know the ordering
Expand Down Expand Up @@ -220,27 +222,7 @@ julia> fieldnames(ITensor)
```
which shows the fields of a type. Note that in general the specific names of the fields and structures of types may change (we consider those to be internal details), however we often make functions to access the fields of a type that have the same name as the field, so it is a good place to get started. For example, you can access the storage and indices of an ITensor `A` with the functions `store(A)` and `inds(A)`.

Another helpful function is `apropos`, which search through all documentation for a string (ignoring the case) and prints a list of all types and methods with documentation that contain the string. For example:
```julia
julia> apropos("IndexSet")
ITensors.IndexSet
ITensors.push
ITensors.insertat
ITensors.getfirst
ITensors.commoninds
ITensors.pushfirst
NDTensors.mindim
[...]
```
This can often return too much information. A helpful way to narrow down the search is with regular expressions, for example:
```julia
julia> apropos(r"ITensor.*IndexSet")
ITensors.block
ITensors.hasinds
ITensors.ITensor
NDTensors.inds
```
where the notation `r"..."` is Julia notation for making a string that will be interpreted as a [regular expression](https://docs.julialang.org/en/v1/manual/strings/#Regular-Expressions). Here, we are searching for any documentation that contains the string "ITensor" followed at some point by "IndexSet". The notation `.*` is regular expression notation for matching any number of any type of character.
Another helpful function is `apropos`, which search through all documentation for a string (ignoring the case) and prints a list of all types and methods with documentation that contain the string.

Based on the `apropos` function, we can make some helper functions that may be useful. For example:
```julia
Expand Down Expand Up @@ -949,7 +931,7 @@ This allows us to have code like:
julia> i = Index(2, "i")
(dim=2|id=811|"i")

julia> A = emptyITensor(i', i);
julia> A = ITensor(i', i);

julia> @show A;
A = ITensor ord=2
Expand Down
20 changes: 10 additions & 10 deletions docs/src/ContractionSequenceOptimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ h₃ = Index(k, "h₃")
s₁ = Index(d, "s₁")
s₂ = Index(d, "s₂")
H₁ = emptyITensor(dag(s₁), s₁', dag(h₁), h₂)
H₂ = emptyITensor(dag(s₂), s₂', dag(h₂), h₃)
L = emptyITensor(dag(l), l', h₁)
R = emptyITensor(dag(r), r', h₃)
ψ = emptyITensor(l, s₁, s₂, r)
H₁ = ITensor(dag(s₁), s₁', dag(h₁), h₂)
H₂ = ITensor(dag(s₂), s₂', dag(h₂), h₃)
L = ITensor(dag(l), l', h₁)
R = ITensor(dag(r), r', h₃)
ψ = ITensor(l, s₁, s₂, r)
TN = [ψ, L, H₁, H₂, R]
sequence1 = Any[2, Any[3, Any[4, Any[1, 5]]]]
Expand Down Expand Up @@ -80,11 +80,11 @@ function tensor_network(; m, k, d)
s₁ = Index(d, "s₁")
s₂ = Index(d, "s₂")

ψ = emptyITensor(l, s₁, s₂, r)
L = emptyITensor(dag(l), l', h₁)
H₁ = emptyITensor(dag(s₁), s₁', dag(h₁), h₂)
H₂ = emptyITensor(dag(s₂), s₂', dag(h₂), h₃)
R = emptyITensor(dag(r), r', h₃)
ψ = ITensor(l, s₁, s₂, r)
L = ITensor(dag(l), l', h₁)
H₁ = ITensor(dag(s₁), s₁', dag(h₁), h₂)
H₂ = ITensor(dag(s₂), s₂', dag(h₂), h₃)
R = ITensor(dag(r), r', h₃)
return [ψ, L, H₁, H₂, R]
end

Expand Down
17 changes: 9 additions & 8 deletions docs/src/Multithreading.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ julia> using LinearAlgebra
julia> BLAS.vendor() # Check which BLAS you are using
:mkl

julia> using ITensors

julia> BLAS.get_num_threads()
6

Expand All @@ -60,17 +58,19 @@ $ julia -t 4
julia> Threads.nthreads()
4

julia> ITensors.Strided.get_num_threads()
julia> using Strided

julia> Strided.get_num_threads()
4
```
We find that this threading competes with BLAS threading as well as ITensors.jl's own block sparse
multithreading, so if you are using Julia with multiple threads you may want to disable
Strided.jl's threading with:
```julia
julia> ITensors.Strided.disable_threads()
julia> Strided.disable_threads()
1

julia> ITensors.Strided.get_num_threads()
julia> Strided.get_num_threads()
1
```
in favor of either BLAS threading or ITensors.jl's block sparse threading.
Expand All @@ -88,12 +88,13 @@ Here is a simple example of using block sparse multithreading to speed up a spar
tensor contraction:
```julia
using BenchmarkTools
using ITensors
using ITensors, ITensorMPS
using LinearAlgebra
using Strided

function main(; d = 20, order = 4)
BLAS.set_num_threads(1)
ITensors.Strided.set_num_threads(1)
Strided.set_num_threads(1)

println("#################################################")
println("# order = ", order)
Expand Down Expand Up @@ -139,7 +140,7 @@ julia> main(d = 20, order = 4)
Threads.nthreads() = 5
Sys.CPU_THREADS = 6
BLAS.get_num_threads() = 1
ITensors.Strided.get_num_threads() = 1
Strided.get_num_threads() = 1
Serial contract:
21.558 ms (131 allocations: 7.34 MiB)
Expand Down
4 changes: 3 additions & 1 deletion docs/src/Observer.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ For example, let's make a type called `DemoObserver`
as:

```julia
using ITensors, ITensorMPS

mutable struct DemoObserver <: AbstractObserver
energy_tol::Float64
last_energy::Float64
Expand Down Expand Up @@ -137,7 +139,7 @@ energy, psi = dmrg(H,psi0,sweeps; observer=obs, outputlevel=1)
## Complete Sample Code

```julia
using ITensors
using ITensors, ITensorMPS

mutable struct DemoObserver <: AbstractObserver
energy_tol::Float64
Expand Down
17 changes: 11 additions & 6 deletions docs/src/examples/DMRG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ or a wavefunction we need to construct a "site set" which will hold the site ind
the physical Hilbert space:

```julia
using ITensors, ITensorMPS
N = 100
sites = siteinds("S=1",N)
```
Expand Down Expand Up @@ -66,7 +67,7 @@ approximation to the ground state as the variable `psi`.
Below you can find a complete working code that includes all of these steps:

```julia
using ITensors
using ITensors, ITensorMPS

let
N = 100
Expand Down Expand Up @@ -118,7 +119,7 @@ These tags tell the OpSum system which local operators to use for these
sites when building the Hamiltonian MPO.

```julia
using ITensors
using ITensors, ITensorMPS

let
N = 100
Expand Down Expand Up @@ -208,7 +209,7 @@ the geometry a cylinder.
**Full example code:**

```julia
using ITensors
using ITensors, ITensorMPS

let
Ny = 6
Expand Down Expand Up @@ -285,7 +286,7 @@ within the same quantum number sector.
**Full Example code:**

```julia
using ITensors
using ITensors, ITensorMPS

let
N = 20
Expand Down Expand Up @@ -377,6 +378,8 @@ First we define our custom observer type, `EntanglementObserver`, and overload t
for it:

```julia
using ITensors, ITensorMPS

mutable struct EntanglementObserver <: AbstractObserver
end

Expand All @@ -398,7 +401,7 @@ has just finished optimizing.
Here is a complete sample code including constructing the observer and passing it to DMRG:

```julia
using ITensors
using ITensors, ITensorMPS

mutable struct EntanglementObserver <: AbstractObserver
end
Expand Down Expand Up @@ -473,6 +476,8 @@ First we define our custom observer type, `SizeObserver`, and overload the `meas
for it:

```julia
using ITensors, ITensorMPS

mutable struct SizeObserver <: AbstractObserver
end

Expand All @@ -493,7 +498,7 @@ When it runs, it calls `Base.summarysize` on the wavefunction `psi` object and t
Here is a complete sample code including constructing the observer and passing it to DMRG:

```julia
using ITensors
using ITensors, ITensorMPS

mutable struct SizeObserver <: AbstractObserver
end
Expand Down
13 changes: 9 additions & 4 deletions docs/src/examples/MPSandMPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ In the example code below we will obtain the element ``T^{1,2,1,1,2,1,2,2,2,1}``
which is (implicitly) defined by the MPS psi:

```@example mps_element
using ITensors # hide
using ITensors, ITensorMPS
let # hide
N = 10
s = siteinds(2,N)
Expand Down Expand Up @@ -139,7 +139,7 @@ following example we will use a random MPS of bond dimension ``\chi=4`` but the
MPS could be obtained other ways such as through a DMRG calculation.

```@example expect
using ITensors # hide
using ITensors, ITensorMPS
N = 10
chi = 4
sites = siteinds("S=1/2",N)
Expand Down Expand Up @@ -306,6 +306,8 @@ as well as limits on the maximum bond dimension (`maxdim` keyword argument).
**Complete code example**

```julia
using ITensors, ITensorMPS

psi = orthogonalize(psi, 3)

wf = (psi[3] * psi[4]) * G
Expand Down Expand Up @@ -355,7 +357,8 @@ the algorithm for sampling MPS is a `perfect' sampling algorithm with no autocor
In more detail, say we have a set of `N` site indices `s` and define a random MPS
with these sites:
```@example sample_mps; continued=true
using ITensors # hide
using ITensors, ITensorMPS
N = 10 # number of sites
d = 3 # dimension of each site
chi = 16 # bond dimension of the MPS
Expand Down Expand Up @@ -384,7 +387,7 @@ can be brought into orthogonal form by calling `psi = orthogonalize(psi, 1)`.

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

!!! info
!!! 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**
Expand Down Expand Up @@ -427,6 +430,8 @@ So for example, to create an MPO from an OpSum which has the same site indices
as your MPS `psi`, do the following:

```julia
using ITensors, ITensorMPS

os = OpSum()
# Then put operators into os...

Expand Down
Loading

2 comments on commit 3d36e74

@mtfishman
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/106505

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.1 -m "<description of version>" 3d36e74c2196d833d84e461a2b578a86cb3f0bfa
git push origin v0.6.1

Please sign in to comment.