0 votes
asked by (260 points)

Dear all, I'm Meng. I want to know how to read data (for example 2 rows, 2 columns) from a file using itensor function directly? Although it can read the file data with the following C++ code, I guess itensor function maybe even simple and direct.

#include< iostream>
#include< fstream>
#include< typeinfo>
using namespace std;
int main()
{
float data[2][2] = { 0 };
ifstream infile;
infile.open("file.dat");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
infile >> data[i][j];
}
}
infile.close();

cout << data[0][0] << endl;
cout << data[0][1] << endl;
cout << data[1][0] << endl;
cout << data[1][1] << endl;

return 0;
}

commented by (70.1k points)
Hi Meng, could you say more about what kind of data you are trying to read, and specifically what format it is in? ITensor isn't really intended as a library to help read in different files of various types, but only has support for two types of files currently: binary files which hold ITensor Library object data (such as ITensor, MPS, MPO, etc.) and uses a non-standard format, or else HDF5 files which we are starting to support.

If you are asking about HDF5 perhaps, those can be used in other contexts besides ITensor, such as to take the data out of an ITensor and use it in another library or code, or to read in data from another library or code. It is a portable and more organized solution for storing numerical data in files.

Best,
Miles
commented by (260 points)
Thank you, Miles!

The non-standard format data is produced by MATLAB, and I have stored it in HDF5 format.
Those parameters data will be used to construct MPO in boson site. Information about hdf5 file are:

HDF5 cfdata.h5
Group '/'
    Dataset 'g1'
        Size:  10x1
        MaxSize:  10x1
        Datatype:   H5T_IEEE_F64LE (double)
        ChunkSize:  []
        Filters:  none
        FillValue:  0.000000
    Dataset 'g2'
        Size:  10x1
        MaxSize:  10x1
        Datatype:   H5T_IEEE_F64LE (double)
        ChunkSize:  []
        Filters:  none
        FillValue:  0.000000
    Dataset 'w1'
        Size:  10x1
        MaxSize:  10x1
        Datatype:   H5T_IEEE_F64LE (double)
        ChunkSize:  []
        Filters:  none
        FillValue:  0.000000
    Dataset 'w2'
        Size:  10x1
        MaxSize:  10x1
        Datatype:   H5T_IEEE_F64LE (double)
        ChunkSize:  []
        Filters:  none
        FillValue:  0.000000
commented by (70.1k points)
Hi, thanks, so that's good to know the data is coming from an HDF5 file.

So I think I still don't quite know what your goal is, or if I do, how it is connected to the ITensor library.

Is your goal or question one of these?
1. to read in numerical data from a dataset inside an HDF5 file, then construct an ITensor whose components or elements are made from this data?
2. to use the ITensor software to help more generally with reading data from HDF5 files? (I ask this second one because in your code example you don't mention the ITensor type or wanting to use any part of the ITensor system or the types it defines)

Thanks,
Miles
commented by (260 points)
Hi Miles,

I'm sorry to make the question complex and unclear. In actuality, I just want to read a set of parameters (two-dimension but not in ITensor type) from a file and allocate it to a matrix or ITensor (T(i,j)).
 
For example, the set of parameters stored in the myfile.dat file with content :
1 2 3 4  
5 6 7 8
How to read this two-dimension data and allocate it to matrix/Itensor T?

I wish this makes the question clear~~

Thanks,
Meng
commented by (14.1k points)
Hi Meng,

The simplest thing to do would be to take the array you made above, create an empty ITensor of the correct size, and then copy the values over to that ITensor. Does that work for your use case?

If the data is very large and it takes up a lot of time to do that, we could suggest something more efficient.

-Matt

Please log in or register to answer this question.

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

...