0 votes
asked by (640 points)
reopened by

Hello!

If I write

  for(int i = L-1; i >= 1; --i)
  {
        auto hterm =  sites.op("Adag",i)*sites.op("A",i+1);
        hterm +=  sites.op("A",i)*sites.op("Adag",i+1);
         auto g = Gate(sites,i,i+1,Gate::tReal,tstep/2.,hterm);
    gates.push_back(g);
  }

it is OK, but if I write

for(int i = L-1; i >= 1; --i)
{
    if (i % 2 == 0)
    {
        auto hterm = sites.op("Adag",i)*sites.op("A",i+1);
        hterm += sites.op("A",i)*sites.op("Adag",i+1);
    }

    else
    {
        auto hterm = 2.0*sites.op("Adag",i)*sites.op("A",i+1);
        hterm += 2.0*sites.op("A",i)*sites.op("Adag",i+1);
    }

    auto g = Gate(sites,i,i+1,Gate::tReal,tstep/2.,hterm);
    gates.push_back(g);
}

It says
error: no declaration of the variable «hterm»
auto g = Gate(sites,i,i+1,Gate::tReal,tstep/2.,hterm);

But the following snippet gives "ITensorT::operator+=: different index structure
"

  for(int i = L-1; i >= 1; --i)
{
    if (i % 2 == 0)
    {
        auto hterm =  sites.op("Adag",i)*sites.op("A",i+1);
        hterm +=  sites.op("A",1)*sites.op("Adag",i+1);
        auto g = Gate(sites,i,i+1,Gate::tReal,tstep/2.,hterm);
        gates.push_back(g);
    }

    else
    {
        auto hterm = 2.0*sites.op("Adag",i)*sites.op("A",i+1);
        hterm += 2.0*sites.op("A",i)*sites.op("Adag",i+1);
        auto g = Gate(sites,i,i+1,Gate::tReal,tstep/2.,hterm);
        gates.push_back(g);
    }

How one can curcumvent this issue? I need different hamiltonians for even and odd i

1 Answer

+1 vote
answered by (14.1k points)
selected by
 
Best answer

Hello,

In your first code, you just have to initialize hterm before the if statement:

for(int i = L-1; i >= 1; --i)
  {
  IQTensor hterm;
  if (i % 2 == 0)
    {
    hterm = sites.op("Adag",i)*sites.op("A",i+1);
    hterm += sites.op("A",i)*sites.op("Adag",i+1);
    }
  else
    {
    hterm = 2.0*sites.op("Adag",i)*sites.op("A",i+1);
    hterm += 2.0*sites.op("A",i)*sites.op("Adag",i+1);
    }
  auto g = Gate(sites,i,i+1,Gate::tReal,tstep/2.,hterm);
  gates.push_back(g);
  }

(assuming you are using QNs, otherwise you need to initialize with ITensor hterm).

In your second code, it looks like there is just a small mistake in the code. Instead of:

 auto hterm =  sites.op("Adag",i)*sites.op("A",i+1);
 hterm +=  sites.op("A",1)*sites.op("Adag",i+1);

it should be:

 auto hterm =  sites.op("Adag",i)*sites.op("A",i+1);
 hterm +=  sites.op("A",i)*sites.op("Adag",i+1);

Cheers,
Matt

commented by (640 points)
Thank you very much, Matt!  
I have aready though about "IQTensor hterm;" but I wrote it down without semicolon.
I am used to Python, where there is no semicolon at the end of a sentence.  I am new to C++.
commented by (14.1k points)
Yes, C++ can be a bit tricky to get used to at first! I definitely still forget to put semicolons :).
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

...