+1 vote
asked by (320 points)

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 by (70.1k 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 by (70.1k points)
I just updated the website to include documentation on these functions:
http://itensor.org/docs.cgi?page=classes/readwrite
commented by (320 points)
Great! Thats exactly what I was looking for! And thanks for the update!
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

...