+1 vote
asked by (130 points)

Hello Everyone,
I tried to output the data into a file and learned the writeToFile,
but I do not how to show them.
For example

include "itensor/all.h"

using namespace itensor;

int N = 100; //number of sites
SpinHalf sites(N); //SpinHalf is a subclass of SiteSet
MPS psi(sites);

When vi psi_file in the folder, I get :


I would like to list my own data in three columns : i , j , result
Thanks for any help

1 Answer

0 votes
answered by (25k points)

Hi, so the writeToFile function is only for the purpose of writing ITensor objects as binary data. So the odd output you're seeing is raw binary reinterpreted by your text editor as ascii data (even though it's not ascii data).

If you want to write data in ascii to a file, I'd recommend writing a loop over the data you want to save and inside the loop, use the printfln function. Here is an example:

auto i = Index("i",3);
auto j = Index("j",3);
auto k = Index("k",3);

auto T = ITensor(i,j,k);


std::ofstream of("of.dat");
for(auto ni : range1(i.m()))
for(auto nj : range1(j.m()))
for(auto nk : range1(k.m()))
    printfln(of,"%d %d %d %.12f",ni,nj,nk,T.real(i(ni),j(nj),k(nk)));
commented by (130 points)
Hello Miles
Thank you very much for your nice example of showing how to output the data.
I ran the code and it worked very well.
Thank you again for solving the problem.
commented by (25k points)
Great. If it's too slow (such as for a very large tensor) then let me know and I can suggest some code that would be faster.

Another simpler way to see the contents of a tensor if you don't mind some additional output showing the tensor indices etc is to do this:
std::ofstream of("of.dat");
printfln(of,"T = %f",T);

The %f formatting option prints both the tensor information and all of the non-zero entries. It uses a faster and more generic algorithm that works for a tensor of any rank.
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.