+1 vote
asked by (130 points)

Hello, I am working on implementing the expect(psi,"Sz") function on the C++ side. In Julia one can create operators (using Sz as an example) without a SiteSet, by doing something like

OpSz=ITensors.op("Sz",Index(2, "Site,S=1/2"))

In c++ I can see how to do something similar if I have SiteSet available. But is there a way in C++ to get an operator from just the string and and an Index (no SiteSet)?

1 Answer

0 votes
answered by (70.1k points)

Hi Jan,
It's a really good question. At the moment there is no analogue of that feature in the C++ version. It was something we never figured out in C++ but was easy to do in Julia.

To give you some background on how the siteset types work in C++, for each site location (an integer "i") the siteset stores a "Site" object such as a SpinHalfSite or a SpinOneSite. But these are stored in a "type erased box" which in practice means they are stored inside of a type called a SiteHolder (this is the "box" type) which derives from a generic interface type called SiteBase. When class methods like .op are called on a siteset, internally it calls the .op method on some type like SiteHolder but where the virtual mechanism is figuring out or unwrapping the actual type and again the storage is just a bunch of SiteBase objects for all the siteset knows.

That was just the best way I knew how at the time to get the dynamic behavior I wanted. It's been a moderate success but I think sufficiently complicated that users are generally nervous to get their hands on the internals beyond following a few patterns.

So it would be a real "win" if something like the behavior in Julia could be done in C++, perhaps using more modern C++ dialects like C++17.

However, there may just be some inherent limitations since C++ doesn't have true introspection, and since I don't think you can parameterize types with string values, etc. But maybe there's some trick I haven't thought of which comes close to the Julia behavior.


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.