+1 vote
asked by (190 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
main()
{
int N = 100; //number of sites
SpinHalf sites(N); //SpinHalf is a subclass of SiteSet
MPS psi(sites);
writeToFile("sitesfile",sites);
writeToFile("psi
file",psi);
}

When vi psi_file in the folder, I get :

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ø^?^A^@^@^@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@Site^@^@^@H4<9c>ó^B^@^@^@S=1/2

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

1 Answer

0 votes
answered by (70.1k 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);
randomize(T);

PrintData(T);

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)));
    }
of.close();
commented by (190 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.
Wz
commented by (70.1k 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);
of.close();

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.
commented by (230 points)
I'm not sure if the case for larger tensors has been addressed yet in another post, but I'd be interested to see what that is. I would just like to take a MPS of a wavefunction that I've contracted to an ITensor object just by multiplying the psi.A(i)'s together (i.e. convert the MPS into a 1D vector of coefficients) and write this to a text file.
commented by (70.1k points)
Hi, so the code in my comment above would generalize to a tensor with any number of indices, which could be an MPS that you turn into a single wavefunction tensor by just multiplying the "A" tensors together as you mention. However, this does suggest the need for a function in ITensor which more efficiently writes an ITensor's components out to a text file. But a simple intermediate solution could be to do the following:
1. Obtain or make an ITensor "T" you want to write to a text file.
2. Call the T.order(i,j,k,l,...) method to ensure the indices are in the order you want (where i,j,k,l are the indices of your ITensor. This step is optional if you are sure of the order or also write this out as separate information.
3. Use one of the approaches on the following page to extract the storage of the ITensor (assuming it's a dense, non-QN conserving ITensor): http://itensor.org/docs.cgi?page=formulas/extractdense
4. Since the extracted storage is (or contains) a regular std::vector class, just loop over this std::vector and write all of the values to a text file.

Best regards,
Miles
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

...