Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert to/from ITensors MPS #247

Closed
starsfordummies opened this issue Nov 12, 2024 · 2 comments · Fixed by #251
Closed

convert to/from ITensors MPS #247

starsfordummies opened this issue Nov 12, 2024 · 2 comments · Fixed by #251
Assignees

Comments

@starsfordummies
Copy link
Contributor

It would be good to be able to convert between the two. An ITensorMPS.MPS is basically a list of ITensors plus a couple integers (llim, rlim) that localize the orthogonality center, but those should be added automatically when you create one.

The simplest way is probably to convert the tensors of Tenet's MPS into ITensors, and feed the list of ITensors into the ITensorMPS.MPS() constructor. The only thing I don't know how to do in Tenet,as always, is how to map the indices into the tensors. Here's a (non-working) attempt

 psi_tensors = [rand(2,10), rand(2,10,12), rand(2,12,4), rand(2,4)]
 psi_tenet = MPS(psi_tensors)

itensors = [] 
# this assumes some ordering of the Tenet tensor indices. one should also treat first and last separately but you get the idea
for (j,t) in enumerate(tensors(psi_tenet))
   it = ITensor(t, Index(dim("physical index of t"),"Site,n=$j"),Index(dim("left link index of t"),"Link,l=$j"),Index(dim("right link index of t"),"Link,l=$(j+1")
push!(itensors, it)
end 
psi_itensors = ITensorMPS.MPS(itensors)

the other way around is probably simpler (for me at least), here's one way to do it

psiten = ITensorMPS.random_mps(siteinds(4, 10), linkdims=7) 

# bring itensors to (o,l,r) and convert them

# first and last always annoying
first_ten = array(psiten[1], siteinds(psiten)[1], linkinds(psiten)[1])
last_ten = array(psiten[end], siteinds(psiten)[end], linkinds(psiten)[end])

ttensors = Array{}[array(psiten[jj], siteinds(psiten)[jj], linkinds(psiten)[jj-1],linkinds(psiten)[jj]) for jj in eachindex(psiten)[2:end-1]]
pushfirst!(ttensors, first_ten)
push!(ttensors,last_ten)

Tenet.MPS(ttensors)
@jofrevalles jofrevalles self-assigned this Nov 13, 2024
@jofrevalles
Copy link
Member

jofrevalles commented Nov 13, 2024

You can convert from Tenet.MPS to ITensorsMPS.MPS like this:

using Tenet
using ITensors
using Tenet: MPS, tensors
using ITensors: Index, ITensor, dim
using ITensorMPS

psi_tensors = [rand(2,10), rand(2,10,12), rand(2,12,4), rand(2,4)]
psi_tenet = MPS(psi_tensors)

itensors = ITensor[]

for (j,t) in enumerate(tensors(psi_tenet))
    it = if j == 1 || j == length(tensors(psi_tenet))
        ITensor(parent(t), Index(size(t, 1), "Site,n=$j"), Index(size(t, 2), "Link,l=$j"))
    else
        ITensor(parent(t), Index(size(t, 1), "Site,n=$j"), Index(size(t, 2), "Link,l=$j"), Index(size(t, 3), "Link,l=$(j+1)"))
    end

    push!(itensors, it)
end
psi_itensors = ITensorMPS.MPS(itensors)

Do you want to have this as an extension in Tenet? We should also store the ortogonality center information

@mofeing
Copy link
Member

mofeing commented Nov 14, 2024

Conversion from/to ITensors.ITensor and Tenet.Tensor is already implemented. You can just do:

# Tensor -> ITensor
convert(ITensor, tensor)

# ITensor -> Tensor
convert(Tensor, itensor)

Furthermore, you can convert a Vector{ITensor} to a Tenet.TensorNetwork and viceversa.

What I'm not sure about is whether Index carries some site information. In that case, we currently lose it and we will need to do sth more for converting ITensorsMPS.MPS to our MPS and the other way around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants