# Write/Read tensors in HDF5 format between C++v3 and Julia version

+1 vote

Hi,

Recently I am trying to transfer from the C++v3 version of ITensor to Julia version. And I don't want to waste all the calculations I have done before using C++. So, I would like to know if there is a way to convert the states optimized by C++v3 and stored in binary format to the HDF5 format that can be loaded by ITensors.HDF5 in Julia. And in my case, I am using IQTensors.

There is a similar question quite a while ago. But I met with some problems following the instructions there. And I guess there might be some changes from the previous versions.

Thank you very much!

Best,
Yi

selected by

Hi Yi,
As Matt indicated, this is a feature we have been planning but was not quite yet finished. But now I have finished adding support for all ITensor types and objects with the exception of complex-valued ITensors. As long as your data is real, you should be able to write it to HDF5 (including MPS and MPO objects) and read it again in the Julia version of ITensor.

To obtain this functionality, please do a "git pull" on the "v3" branch of C++ ITensor to get the latest code there.

As an example of how to write objects to HDF5 files, please see the code in unittest/hdf5_test.cc and in general the pattern is like this:

//Make an MPS
auto N = 4;
auto s = SpinHalf(N,{"ConserveQNs=",false});
auto M = randomMPS(s,4);

//Write the MPS to an HDF5 file named "test.h5" to a group (folder) named "mps_M"
auto fo = h5_open("test.h5",'w');
h5_write(fo,"mps_M",M);
close(fo);


Please let us know if you have any questions or run into any trouble using this feature.

Best,
Miles

commented by (400 points)
Hi Miles,

Thank you very much! Now I can save ITensors in HDF5 format which can be successfully read by the Julia version of ITensors. And this can help me smoothly transfer to the Julia version which seems to have better perfomance, at least for my case.

Best,
Yi
commented by (70.1k points)
Glad to hear it! Please let me know if you encounter any bugs or issues with this feature.

Best,
Miles

You should be able to save you MPS/ITensors directly as HDF5 files in C++ (instead of to binary), which can then be read by the Julia version.

You can see some examples to give you an idea for the syntax here:

https://github.com/ITensor/ITensor/blob/2181eacaf100e42d6f98831e7a9a89de72636e9d/unittest/hdf5_test.cc

commented by (14.1k points)
Sorry I may be mistaken, in fact I don't see support for saving ITensor/MPS in C++ to HDF5. It may not be supported right now.
commented by (400 points)
Yeah, when I tried to save the tensors in HDF5 format, it cannot be loaded by Julia. So I guess I only changed the suffix and didn't really save the data in HDF5 format.
commented by (14.1k points)
Miles says that he has a prototype of saving ITensors to HDF5 in C++, maybe he can make that available to you.
commented by (70.1k points)
Hi Yi, unfortunately that feature is not ready yet (writing C++ ITensors to HDF5) though when it is, a main goal is that the files will be readable into the Julia version.

We will work on this and let you know when it's ready. I'll prioritize it more now that I know you have a use case for it.

Miles