# Quick Question: Storing an ITensor to a file

+1 vote

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 vote

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

writeToFile(string filename, T const& obj)

or

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:

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;

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 by (24.7k points)
I just updated the website to include documentation on these functions: