# Quick Question: Storing an ITensor to a file

+1 vote
asked Aug 1, 2017

Hi all,

as I didn't find a solution yet and am sure not to be the only one who has yet looked for that:

is there a canonical (or how you want to call it) way of storing the data of an ITensor to a file in order to later read the ITeonsor into the RAM again except for reading the data in through a file input/output stream?
I.e. are there functions like
itensor::storeToFile(itensor::ITensor, file)
itensor::ITensor tensor = itensor::readFromFile(file) ?

Best,
Alex

## 1 Answer

+1 vote
answered Aug 1, 2017 by (20,240 points)

Hi Alex,
Yes there are the following two functions provided in ITensor:

writeToFile(string filename, T const& obj)

readFromFile(string filename)

or

readFromFile(string filename, T & obj)

The writeToFile function is a template function that automatically deduces the type of the second argument (the object you want to write to the file).

The readFromFile functions come in two versions. The first one above requires you to specify the type of the object you expect to be stored in the file, so like:
auto T = readFromFile("mytensor");

The second one is able to deduce the type, so no need for the template stuff, but can be more awkward to use sometimes since it can require more lines of code:
ITensor T;
readFromFile("mytensor",T);

By the way, the ITensor read and write system has the ability to read and write certain common container types, such as std::vector. So you can do things like write an entire std::vector to a file like so:

auto v = vector(5);
for(auto& t : v) t = randomTensor(i,j,k,l); //makes random ITensors
writeToFile("vector_itensors",v);

Miles

commented Aug 1, 2017 by (20,240 points)
I just updated the website to include documentation on these functions:
http://itensor.org/docs.cgi?page=classes/readwrite
commented Aug 1, 2017 by (310 points)
Great! Thats exactly what I was looking for! And thanks for the update!