Skip to content

Commit 932af64

Browse files
authored
Merge pull request #68 from tensor4all/67-make-tciitensorconversion-an-extension-to-tcijl
TCIITensorConversion extension
2 parents 14ffeb2 + bbbc2fe commit 932af64

File tree

13 files changed

+243
-152
lines changed

13 files changed

+243
-152
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
fail-fast: false
2424
matrix:
2525
version:
26+
- '1.9'
2627
- 'lts'
2728
- '1'
2829
os:

Project.toml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
1111
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1212

13+
[weakdeps]
14+
ITensorMPS = "0d1a4710-d33b-49a5-8f18-73bdf49b47e2"
15+
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
16+
17+
[extensions]
18+
TCIITensorConversion = ["ITensors", "ITensorMPS"]
19+
1320
[compat]
1421
BitIntegers = "0.3.5"
1522
EllipsisNotation = "1"
23+
ITensorMPS = "0.2, 0.3"
24+
ITensors = "0.6, 0.7, 0.8, 0.9"
1625
QuadGK = "2.9"
17-
Random = "1.10.0"
18-
julia = "1.6"
26+
Random = "1.9.0"
27+
julia = "1.9"
1928

2029
[extras]
2130
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
@@ -28,4 +37,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2837
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
2938

3039
[targets]
31-
test = ["Aqua", "Test", "Random", "ITensors", "Zygote", "Optim", "QuanticsGrids", "JET"]
40+
test = ["Aqua", "Test", "Random", "ITensors", "ITensorMPS", "Zygote", "Optim", "QuanticsGrids", "JET"]

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
ITensorMPS = "0d1a4710-d33b-49a5-8f18-73bdf49b47e2"
4+
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
35
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
46
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
57
TensorCrossInterpolation = "b261b2ec-6378-4871-b32e-9173bb050604"

docs/make.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
using TensorCrossInterpolation
2+
using ITensors
3+
using ITensorMPS
24
using Documenter
35

4-
DocMeta.setdocmeta!(TensorCrossInterpolation, :DocTestSetup, :(using TensorCrossInterpolation); recursive=true)
6+
preamble = quote
7+
using TensorCrossInterpolation
8+
using ITensors
9+
using ITensorMPS
10+
end
11+
12+
DocMeta.setdocmeta!(TensorCrossInterpolation, :DocTestSetup, preamble; recursive=true)
513

614
makedocs(;
7-
modules=[TensorCrossInterpolation],
15+
modules=[
16+
TensorCrossInterpolation,
17+
Base.get_extension(TensorCrossInterpolation, :TCIITensorConversion)
18+
],
819
authors="Ritter.Marc <[email protected]> and contributors",
920
sitename="TensorCrossInterpolation.jl",
1021
format=Documenter.HTML(;
@@ -14,7 +25,7 @@ makedocs(;
1425
pages=[
1526
"Home" => "index.md",
1627
"Documentation" => "documentation.md",
17-
"Implementation details" => "implementation.md"
28+
"Extensions" => "extensions.md",
1829
]
1930
)
2031

docs/src/extensions.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Extensions
2+
3+
This page documents extensions to [TensorCrossInterpolation](https://github.com/tensor4all/TensorCrossInterpolation.jl).
4+
5+
Currently, there is only one extension, TCIITensorConversion.
6+
7+
## TCIITensorConversion
8+
9+
Dependencies:
10+
- [ITensors.jl](https://github.com/ITensor/ITensors.jl)
11+
- [ITensorMPS.jl](https://github.com/ITensor/ITensorMPS.jl)
12+
13+
This module is automatically loaded when ITensors and ITensorMPS are present in addition to TensorCrossInterpolation. It offers conversion constructors between ITensorMPS types and TensorCrossInterpolation types, and an analogue of the [`evaluate`](@ref) function for ITensor types.
14+
15+
```@autodocs
16+
Modules = [Base.get_extension(TensorCrossInterpolation, :TCIITensorConversion)]
17+
```

ext_disabled/TCIITensorConversion.jl renamed to ext/TCIITensorConversion/TCIITensorConversion.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
module TCIITensorConversion
22

33
import TensorCrossInterpolation as TCI
4+
import TensorCrossInterpolation: evaluate
5+
46
using ITensors
7+
import ITensorMPS
8+
import ITensorMPS: MPS, MPO
59

6-
export MPS
7-
export evaluate_mps
10+
export MPS, MPO
11+
export evaluate
812

913
include("ttmpsconversion.jl")
1014
include("mpsutil.jl")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@doc raw"""
2+
function evaluate(
3+
mps::Union{ITensorMPS.MPS,ITensorMPS.MPO},
4+
indexspecs::Vararg{AbstractVector{<:Tuple{ITensorMPS.Index,Int}}}
5+
)
6+
7+
Evaluates an MPS or MPO for a given set of index values.
8+
9+
- `indexspec` is a list of tuples, where each tuple contains an `ITensorMPS.Index` object specifying an index, and an `Int` corresponding to the value of the specified index.
10+
11+
If many evaluations are necessary, it may be advantageous to convert your MPS to a [`TensorCrossInterpolation.TTCache`](@ref) object first.
12+
"""
13+
function evaluate(
14+
mps::Union{ITensorMPS.MPS,ITensorMPS.MPO},
15+
indexspecs::Vararg{AbstractVector{<:Tuple{ITensors.Index,Int}}}
16+
)
17+
if isempty(indexspecs)
18+
error("Please specify at which indices you wish to evaluate the MPS.")
19+
elseif any(length.(indexspecs) .!= length(mps))
20+
error("Need one index per MPS leg")
21+
end
22+
23+
V = ITensor(1.0)
24+
for j in eachindex(indexspecs[1])
25+
states = prod(ITensorMPS.state(spec[j]...) for spec in indexspecs)
26+
V *= mps[j] * states
27+
end
28+
return scalar(V)
29+
end
30+
@doc raw"""
31+
function evaluate(
32+
mps::Union{ITensorMPS.MPS,ITensorMPS.MPO},
33+
indices::AbstractVector{<:ITensors.Index},
34+
indexvalues::AbstractVector{Int}
35+
)
36+
37+
Evaluates an MPS or MPO for a given set of index values.
38+
39+
- `indices` is a list of `ITensors.Index` objects that specifies the order in which indices are given.
40+
- `indexvalues` is a list of integer values in the same order as `indices`.
41+
42+
If many evaluations are necessary, it may be advantageous to convert your MPS to a [`TensorCrossInterpolation.TTCache`](@ref) object first.
43+
"""
44+
function evaluate(
45+
mps::Union{ITensorMPS.MPS,ITensorMPS.MPO},
46+
indices::AbstractVector{<:ITensors.Index},
47+
indexvalues::AbstractVector{Int}
48+
)
49+
return evaluate(mps, collect(zip(indices, indexvalues)))
50+
end

ext_disabled/ttmpsconversion.jl renamed to ext/TCIITensorConversion/ttmpsconversion.jl

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
2-
"""
3-
MPS(tt, siteindices...)
4-
5-
Convert a tensor train to an ITensor MPS
6-
7-
* `tt` Tensor train
8-
* `siteindices` Arrays of ITensor Index objects.
9-
10-
If `siteindices` is left empty, a default set of indices will be used.
11-
"""
12-
function ITensors.MPS(tt::TCI.TensorTrain{T}; sites=nothing)::MPS where {T}
1+
function ITensorMPS.MPS(tt::TCI.TensorTrain{T}; sites=nothing)::MPS where {T}
132
N = length(tt)
143
localdims = [size(t, 2) for t in tt]
154

@@ -30,11 +19,21 @@ function ITensors.MPS(tt::TCI.TensorTrain{T}; sites=nothing)::MPS where {T}
3019
return MPS(tensors_)
3120
end
3221

33-
function ITensors.MPS(tci::TCI.AbstractTensorTrain{T}; sites=nothing)::MPS where {T}
34-
return MPS(TCI.tensortrain(tci), sites=sites)
22+
"""
23+
MPS(tt::TCI.AbstractTensorTrain{T}; sites=nothing)
24+
25+
Convert a tensor train to an `ITensorMPS.MPS`.
26+
Arguments:
27+
- `tt`: The tensor train to be converted.
28+
- `siteindices`: An array of ITensor Index objects, where `sites[n]` corresponds to the index for the nth site.
29+
30+
If `siteindices` is left empty, a default set of indices will be generated.
31+
"""
32+
function ITensorMPS.MPS(tt::TCI.AbstractTensorTrain{T}; sites=nothing)::MPS where {T}
33+
return MPS(TCI.tensortrain(tt), sites=sites)
3534
end
3635

37-
function ITensors.MPO(tt::TCI.TensorTrain{T}; sites=nothing)::MPO where {T}
36+
function ITensorMPS.MPO(tt::TCI.TensorTrain{T}; sites=nothing)::ITensorMPS.MPO where {T}
3837
N = length(tt)
3938
localdims = TCI.sitedims(tt)
4039

@@ -57,22 +56,32 @@ function ITensors.MPO(tt::TCI.TensorTrain{T}; sites=nothing)::MPO where {T}
5756
tensors_[1] *= onehot(links[1] => 1)
5857
tensors_[end] *= onehot(links[end] => 1)
5958

60-
return MPO(tensors_)
59+
return ITensorMPS.MPO(tensors_)
6160
end
6261

63-
function ITensors.MPO(tci::TCI.AbstractTensorTrain{T}; sites=nothing)::MPO where {T}
64-
return MPO(TCI.tensortrain(tci), sites=sites)
62+
"""
63+
MPO(tt::TCI.AbstractTensorTrain{T}; sites=nothing)
64+
65+
Convert a tensor train to an `ITensorMPS.MPO`.
66+
Arguments:
67+
- `tt`: The tensor train to be converted.
68+
- `sites`: An array of arrays of ITensor Index objects, where `sites[n][m]` corresponds to the mth index attached to the nth site.
69+
70+
If `siteindices` is left empty, a default set of indices will be generated.
71+
"""
72+
function ITensorMPS.MPO(tci::TCI.AbstractTensorTrain{T}; sites=nothing)::ITensorMPS.MPO where {T}
73+
return ITensorMPS.MPO(TCI.tensortrain(tci), sites=sites)
6574
end
6675

6776

6877
"""
69-
function TCI.TensorTrain(mps::ITensors.MPS)
78+
function TensorTrain(mps::ITensorMPS.MPS)
7079
71-
Converts an ITensor MPS object into a TensorTrain. Note that this only works if the MPS has a single leg per site! Otherwise, use [`TCI.TensorTrain(mps::ITensors.MPO)`](@ref).
80+
Converts an `ITensorMPS.MPS` object into a TensorTrain. Note that this only works if the MPS has a single leg per site. Otherwise, use [`TensorCrossInterpolation.TensorTrain(mps::ITensorMPS.MPO)`](@ref).
7281
"""
73-
function TCI.TensorTrain(mps::ITensors.MPS)
74-
links = linkinds(mps)
75-
sites = siteinds(mps)
82+
function TCI.TensorTrain(mps::ITensorMPS.MPS)
83+
links = ITensorMPS.linkinds(mps)
84+
sites = ITensors.SiteTypes.siteinds(mps)
7685
Tfirst = zeros(ComplexF64, 1, dim(sites[1]), dim(links[1]))
7786
Tfirst[1, :, :] = Array(mps[1], sites[1], links[1])
7887
Tlast = zeros(ComplexF64, dim(links[end]), dim(sites[end]), 1)
@@ -87,15 +96,15 @@ function TCI.TensorTrain(mps::ITensors.MPS)
8796
end
8897

8998
"""
90-
function TCI.TensorTrain(mps::ITensors.MPO)
99+
function TensorTrain(mps::ITensorMPS.MPO)
91100
92-
Convertes an ITensor MPO object into a TensorTrain.
101+
Converts an `ITensorMPS.MPO` object into a [`TensorCrossInterpolation.TensorTrain`](@ref).
93102
"""
94-
function TCI.TensorTrain{V, N}(mpo::ITensors.MPO; sites=nothing) where {N, V}
95-
links = linkinds(mpo)
103+
function TCI.TensorTrain{V, N}(mpo::ITensorMPS.MPO; sites=nothing) where {N, V}
104+
links = ITensorMPS.linkinds(mpo)
96105
if sites === nothing
97-
sites = siteinds(mpo)
98-
elseif !all(issetequal.(siteinds(mpo), sites))
106+
sites = ITensors.SiteTypes.siteinds(mpo)
107+
elseif !all(issetequal.(ITensors.SiteTypes.siteinds(mpo), sites))
99108
error("Site indices do not correspond to the site indices of the MPO.")
100109
end
101110

ext_disabled/mpsutil.jl

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ include("test_conversion.jl")
2121
include("test_contraction.jl")
2222
include("test_integration.jl")
2323
include("test_globalsearch.jl")
24+
25+
# TCIITensorConversion extension
26+
include("test_TCIITensorConversion.jl")

0 commit comments

Comments
 (0)