main function

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.

Wz

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:

MPO
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,
Miles

commented by (190 points)
Thank you very much！
Best wishes
Wz