0 votes
asked by (190 points)

I would like to put construction of ampo in a separate sub function
to make the main function clear and simple.

One may take the following lines as an example

for(int j = 1; j < N; ++j)
ampo += 0.5,"S+",j,"S-",j+1;
ampo += 0.5,"S-",j,"S+",j+1;
ampo += "Sz",j,"Sz",j+1;

The other thing is measuring quantities such as magnetization

Real Szj = (psi.A(j)* sites.op("Sz",j) * dag(prime(psi.A(j),Site))).real();

I measured a lof of quantities but I would like to put them in a separate
function either.

Thanks you very much.


1 Answer

0 votes
answered by (70.1k points)

Hi, so if you want to put ITensor code into a function (which is a good idea), you can do this in the same way as putting any other C++ code into a function.

  1. identify which variables you would like to be changeable or adjustable each time the function is run. (For the example of AutoMPO, let's say this is the Sz*Sz coupling called Jz.) Also you will need to pass variables to the function which are required for it to run, such as your site set needed to create your AutoMPO.

  2. create a function which takes the needed variables (here it is your site set and Jz).

  3. put the code into the function and then return the result of that code, in this case the MPO you create from the AutoMPO.

Here is an example:

makeH(SiteSet const& sites, 
               Real Jz)
auto N = sites.N();
auto ampo = AutoMPO(sites);
for(int j = 1; j < N; ++j)
    ampo += 0.5,"S+",j,"S-",j+1;
    ampo += 0.5,"S-",j,"S+",j+1;
    ampo += Jz,"Sz",j,"Sz",j+1;
return MPO(ampo);

(Advanced C++ user note: If you put the function into a header file, don't forget to put the "inline" keyword at the front of the function definition.)

Best regards,

commented by (190 points)
Thank you very much!
Best wishes
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.