# Analogue of julia ITensors.op() on the C++ side

+1 vote

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)?

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.

Miles