0 votes
asked by (120 points)
edited by

Hi all,
I am starting to use ITensor for a work on a dynamical simulation of boson systems, and my model has site operators of the type @@(a_i + a_i^+)^n@@, where @@n@@ can be any positive integer. I know I can easily expand them but I want to reuse part of a code already written and I would prefer not to rewrite everything. Even if I exapnd them I still need operators @@a^n@@ and @@(a^+)^n@@.

Is there a simple way to write a new site operator of this type? I know how to compute the operator itself but I don't understand if it is possible to subclass the BosonSite class or if I have to write a completely new class.

Thanks
Raffaele

commented by (120 points)
After digging a bit into the code  it seems that the class BosonSite (and similar) cannot be subclassed. I mean I cannot find a simple way to extend the method Op()  that returns the operators using their name as a string. The only thing I could do is to add my code directly into the boson.h file. Of course I can  copy the boson.h file and make a brad new class.

I understand that this is due to the fact the BasicSiteSet inherits the method Op() from the SiteSet class however I still don't understand how autoMPO goes through the Op() methods belonging to the BasicSiteSet class and the BosonSite class, I will dig into the code a bit more but I am quite satisfied with my code. Any suggestion on the topic?  

Thanks.

1 Answer

0 votes
answered by (37k points)

Hi, thanks for the question about how the site classes and site sets work.

The shortest answer to your question is that to add new operators, it’s best to create your own new site type, using one of the provided ones as an example. You can copy, say, the boson.h file and just change the name of the class from BosonSite to CustomBosonSite say. Then add new operators and define your own new site set type at the top of the file.

The longer answer, regarding subclassing and reusing existing code, is that you technically can subclass these site classes, but that’s not how they’re intended to be used or extended. (In general C++ doesn’t have a great mechanism for extending things. Each one, such as inheritance, which seems promising at first ends up causing a ton of headaches when you really start using it more seriously.)

But a good approach for what you want to do would be to make a new class CustomBosonSite which just holds a BosonSite inside it (composition). Calling CustomBosonSite’s index() method is then defined as just calling .index() on the BosonSite object held inside of it. For the op(...) method, you would check if the name is one of the new ones you want to define and construct and return the desired operator. Otherwise, you would just call .op(...) on the BosonSite object internally as a fallback method. This way you could save yourself from having to copy-paste all of BosonSites internals into your class.

For some more background on how these site objects work, when they go into a site set object (such as BasicSiteSet) the site set holds a set of opaque “box” types (this is called type erasure) which can hold any one of the site types. This is so that different site types can be mixed and matched within a site set (e.g. S=1 sites throughout the bulk with S=1/2 sites at the edges).

When you call op(sites,”Name”,j) on some site set object “sites”, what it does is call the .op(“Name”,j) method on the site object held in “box” number j. This site object then runs its own op method which creates and returns an ITensor. The site set then returns this ITensor.

Hope that gives you the information you need. Please post another question below if you have a follow up.

Finally, in the new Julia version of ITensor we are almost done with, we are able to take great advantage of the dynamic and language reflection capabilities to make this all much more flexible and extensible with a minimum of code. So please look forward to that!

Best regards,
Miles

commented by (120 points)
Hi,
thanks for you very detailed and useful answer. I have written the new class just adding the required new functionalities to the boson class.

Raffaele
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

...