+1 vote
asked by (260 points)

Hi,

This is a rudimentary question and maybe have been answered already but I couldn't find it myself. So here is my problem:

I am trying to store and access the MPS tensors that I get out of a DMRG algorithm. For example, in a chain of N=10 sites, I want to look at the MPS at the middle site and perform numerical operations using it. So, I use the code:

energy,psi = dmrg(H,psi0,sweeps).

I expect the middle site MPS to be of the dimension 2* D* D and I want to look at 2 of the D*D matrices that in contained there. In Matlab, this would correspond to something like:

psi[5] (1, :, :) and psi[5] (2, :, :) ; so my first question is if there is an equivalent way of accessing the tensors in Julia?

If not, how can I print the tensors out in a text/HDF5 file with their indices included so that they can be read off by a Matlab script?

1 Answer

+1 vote
answered by (46.9k points)

Hi, thanks for the question. This is something we need to document even more about the Julia version of ITensor to make it clear it’s available and that you can use it for MPS too.

Here are the steps to write an ITensor to an HDF5 file, and you can use exactly the same steps to write a whole MPS to HDF5, just with T being an object of type MPS instead of type ITensor:

http://itensor.org/docs.cgi?vers=julia&page=formulas/itensor_hdf5

Please let me know if you have any questions about that or if you run into any issues and I’d be happy to discuss.

Miles

commented by (8.5k points)
Also, to answer the question about how to grab the matrices of an MPS tensor, here is a quick script to give you and idea for how to do it:

using ITensors
N = 5
s = siteinds("S=1/2", N)
psi = randomMPS(s, 4)  # MPS with maximum bond dimension 4
A3 = psi[3]  # Grab the 3rd MPS tensor (order 3 ITensor)
A3_1 = A3 * setelt(s[3] => 1)  # Project onto the 1st MPS matrix (order 2 ITensor)
A3_2 = A3 * setelt(s[3] => 2)  # Project onto the 2nd MPS matrix (order 2 ITensor)

Then, you can view the elements of those ITensors. The ITensor A3_1 and A3_2 will have the following indices:

l2 = linkind(psi, 2)
l3 = linkind(psi, 3)
@show hassameinds(A3_1, (l2, l3))
@show hassameinds(A3_2, (l2, l3))

You can print them, index into them with operations like `A3_1[l2 => 1, l3 => 1]`, SVD them with operations like `svd(A3_1, l2)`, etc. Please let us know what kind of numerical operations you are interested in performing on the MPS tensors, and we can help you out.

-Matt
commented by (8.5k points)
To add on, in the future we would like to have slicing operations available for ITensors (similar to the Matlab notation you show in your question), for example with a notation like:

A3_1 = A3[s[3] => 1, l2 => :, l3 => :]

as an alternative to using the contraction `A3_1 = A3 * setelt(s[3] => 1)`. That is not supported yet, but is on our todo list.
commented by (260 points)
Hi Miles,

Thank you so much for taking the time to answer my question. I actually tried  running the following script:
 energy,psi = dmrg(H,psi0,sweeps)
f = h5open("mynewfile.h5","w")
write(f,"T",psi)
close(f)
f = h5open("mynewfile.h5","r")
F = read(f,"T",ITensor)
close(f)

But I get an error message saying the data wasn't stored properly:

HDF5 group or file does not contain ITensor data

Stacktrace:
 [1] error(::String) at .\error.jl:33
 [2] read(::HDF5File, ::String, ::Type{ITensor}) at C:\Users\Personal\.julia\packages\ITensors\qxIT8\src\itensor.jl:1509
 [3] top-level scope at In[3]:35
commented by (46.9k points)
Hi Arnab,
I see - so the error is coming from the fact that you set the type as ITensor when reading the data back. However the type of psi is MPS. Since the example I linked you to was for writing and reading an ITensor, I can see how this would be confusing.

So I just made a new code formula specifically about writing and reading MPS:
http://itensor.org/docs.cgi?vers=julia&page=formulas/mps_mpo_hdf5

In the near future, we hope to remove the requirement of having to put the type when reading in HDF5 data, because I think we can automatically detect the data type but I'll need to work on it a bit to be sure.

Miles
Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.

Categories

...