Skip to content

Commit

Permalink
Merge pull request #6 from tensor4all/terasaki/issue-5
Browse files Browse the repository at this point in the history
Issue 5
  • Loading branch information
terasakisatoshi authored Nov 16, 2024
2 parents c1ec1a5 + 8cf4166 commit 3a0ad7c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 32 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ concurrency:
# Cancel intermediate builds: only if it is a pull request build.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}

permissions:
actions: write
contents: read

jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
Expand All @@ -19,7 +24,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.9'
- 'lts'
- '1'
os:
- ubuntu-latest
Expand All @@ -33,7 +38,7 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v1
- uses: julia-actions/cache@v2
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
continue-on-error: ${{ matrix.version == 'nightly' }}
Expand Down
8 changes: 6 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ authors = ["Ritter.Marc <[email protected]> and contributors"]
version = "0.1.4"

[deps]
ITensorMPS = "0d1a4710-d33b-49a5-8f18-73bdf49b47e2"
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
TensorCrossInterpolation = "b261b2ec-6378-4871-b32e-9173bb050604"

[compat]
ITensors = "0.3, 0.4, 0.5, 0.6"
ITensorMPS = "0.3.2"
ITensors = "0.7"
Reexport = "1.2.2"
TensorCrossInterpolation = "0.8, 0.9"
julia = "1.6"
julia = "1.10"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
4 changes: 3 additions & 1 deletion src/TCIITensorConversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module TCIITensorConversion

import TensorCrossInterpolation as TCI
using ITensors
using Reexport: @reexport
import ITensorMPS
@reexport using ITensorMPS: MPS

export MPS
export evaluate_mps

include("ttmpsconversion.jl")
Expand Down
18 changes: 9 additions & 9 deletions src/mpsutil.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
@doc raw"""
function evaluate_mps(
mps::Union{ITensors.MPS,ITensors.MPO},
indexspecs::Vararg{AbstractVector{<:Tuple{ITensors.Index,Int}}}
mps::Union{ITensorMPS.MPS,ITensorMPS.MPO},
indexspecs::Vararg{AbstractVector{<:Tuple{ITensorMPS.Index,Int}}}
)
Evaluates an MPS or MPO for a given set of index values.
- `indexspec` is a list of tuples, where each tuple contains an `Itensors.Index` object specifying an index, and an `Int` corresponding to the value of the specified index.
- `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.
If many evaluations are necessary, it may be advantageous to convert your MPS to a `TensorCrossInterpolation.TTCache` object first.
"""
function evaluate_mps(
mps::Union{ITensors.MPS,ITensors.MPO},
mps::Union{ITensorMPS.MPS,ITensorMPS.MPO},
indexspecs::Vararg{AbstractVector{<:Tuple{ITensors.Index,Int}}}
)
if isempty(indexspecs)
Expand All @@ -22,27 +22,27 @@ function evaluate_mps(

V = ITensor(1.0)
for j in eachindex(indexspecs[1])
states = prod(state(spec[j]...) for spec in indexspecs)
states = prod(ITensorMPS.state(spec[j]...) for spec in indexspecs)
V *= mps[j] * states
end
return scalar(V)
end
@doc raw"""
function evaluate_mps(
mps::Union{ITensors.MPS,ITensors.MPO},
indices::AbstractVector{<:ITensors.Index},
mps::Union{ITensorMPS.MPS,ITensorMPS.MPO},
indices::AbstractVector{<:ITensorMPS.Index},
indexvalues::AbstractVector{Int}
)
Evaluates an MPS or MPO for a given set of index values.
- `indices` is a list of `ITensors.Index` objects that specifies the order in which indices are given.
- `indices` is a list of `ITensorMPS.Index` objects that specifies the order in which indices are given.
- `indexvalues` is a list of integer values in the same order as `indices`.
If many evaluations are necessary, it may be advantageous to convert your MPS to a `TensorCrossInterpolation.TTCache` object first.
"""
function evaluate_mps(
mps::Union{ITensors.MPS,ITensors.MPO},
mps::Union{ITensorMPS.MPS,ITensorMPS.MPO},
indices::AbstractVector{<:ITensors.Index},
indexvalues::AbstractVector{Int}
)
Expand Down
32 changes: 16 additions & 16 deletions src/ttmpsconversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Convert a tensor train to an ITensor MPS
If `siteindices` is left empty, a default set of indices will be used.
"""
function ITensors.MPS(tt::TCI.TensorTrain{T}; sites=nothing)::MPS where {T}
function ITensorMPS.MPS(tt::TCI.TensorTrain{T}; sites=nothing)::MPS where {T}
N = length(tt)
localdims = [size(t, 2) for t in tt]

Expand All @@ -30,11 +30,11 @@ function ITensors.MPS(tt::TCI.TensorTrain{T}; sites=nothing)::MPS where {T}
return MPS(tensors_)
end

function ITensors.MPS(tci::TCI.AbstractTensorTrain{T}; sites=nothing)::MPS where {T}
function ITensorMPS.MPS(tci::TCI.AbstractTensorTrain{T}; sites=nothing)::MPS where {T}
return MPS(TCI.tensortrain(tci), sites=sites)
end

function ITensors.MPO(tt::TCI.TensorTrain{T}; sites=nothing)::MPO where {T}
function ITensorMPS.MPO(tt::TCI.TensorTrain{T}; sites=nothing)::ITensorMPS.MPO where {T}
N = length(tt)
localdims = TCI.sitedims(tt)

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

return MPO(tensors_)
return ITensorMPS.MPO(tensors_)
end

function ITensors.MPO(tci::TCI.AbstractTensorTrain{T}; sites=nothing)::MPO where {T}
return MPO(TCI.tensortrain(tci), sites=sites)
function ITensorMPS.MPO(tci::TCI.AbstractTensorTrain{T}; sites=nothing)::ITensorMPS.MPO where {T}
return ITensorMPS.MPO(TCI.tensortrain(tci), sites=sites)
end


"""
function TCI.TensorTrain(mps::ITensors.MPS)
function TCI.TensorTrain(mps::ITensorMPS.MPS)
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).
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::ITensorMPS.MPO)`](@ref).
"""
function TCI.TensorTrain(mps::ITensors.MPS)
links = linkinds(mps)
sites = siteinds(mps)
function TCI.TensorTrain(mps::ITensorMPS.MPS)
links = ITensorMPS.linkinds(mps)
sites = ITensors.SiteTypes.siteinds(mps)
Tfirst = zeros(ComplexF64, 1, dim(sites[1]), dim(links[1]))
Tfirst[1, :, :] = Array(mps[1], sites[1], links[1])
Tlast = zeros(ComplexF64, dim(links[end]), dim(sites[end]), 1)
Expand All @@ -87,15 +87,15 @@ function TCI.TensorTrain(mps::ITensors.MPS)
end

"""
function TCI.TensorTrain(mps::ITensors.MPO)
function TCI.TensorTrain(mps::ITensorMPS.MPO)
Convertes an ITensor MPO object into a TensorTrain.
"""
function TCI.TensorTrain{V, N}(mpo::ITensors.MPO; sites=nothing) where {N, V}
links = linkinds(mpo)
function TCI.TensorTrain{V, N}(mpo::ITensorMPS.MPO; sites=nothing) where {N, V}
links = ITensorMPS.linkinds(mpo)
if sites === nothing
sites = siteinds(mpo)
elseif !all(issetequal.(siteinds(mpo), sites))
sites = ITensors.SiteTypes.siteinds(mpo)
elseif !all(issetequal.(ITensors.SiteTypes.siteinds(mpo), sites))
error("Site indices do not correspond to the site indices of the MPO.")
end

Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ITensors
using ITensors.SiteTypes: siteinds
using ITensorMPS
import TensorCrossInterpolation as TCI
using TCIITensorConversion
using Test
Expand Down
4 changes: 2 additions & 2 deletions test/test_ttmpsconversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
@testset "TCIITensorConversion.jl" begin
@testset "TT to MPS conversion" begin
tt = TCI.TensorTrain([rand(1, 4, 4), rand(4, 4, 2), rand(2, 4, 7), rand(7, 4, 1)])
mps = ITensors.MPS(tt)
mps = ITensorMPS.MPS(tt)
@test linkdims(mps) == [4, 2, 7]
end

@testset "TT to MPO conversion and back" begin
tt = TCI.TensorTrain([rand(1, 4, 3, 4), rand(4, 2, 4, 2), rand(2, 5, 1, 7), rand(7, 9, 4, 1)])
mpo = ITensors.MPO(tt)
mpo = ITensorMPS.MPO(tt)
@test linkdims(mpo) == [4, 2, 7]
@test dim.(siteinds(mpo)[1]) == [4, 3]
@test dim.(siteinds(mpo)[2]) == [2, 4]
Expand Down

0 comments on commit 3a0ad7c

Please sign in to comment.