Driving PSI4 using C++

Hello all,

I am interested in using PSI4 as a library for hybrid QM/Monte Carlo where I would like to link against the PSI4 shared library and drive it using C++. I haven’t managed to find any examples of driving the engine using C++ - all the examples are in Python. There are no doubt underlying C++ calls, but I am certain there is also some amount of “book keeping” that goes on that enables the simple syntax in the input scripts. Are there any examples of setting up a basic energy calculating using C++, or if not could I be pointed to the appropriate location(s) in the code for me to try and put something together?


We’ve not tried what you’re proposing. I know of no reason it couldn’t be done (through may requires some tweaks from us), but you’re right that a lot of the clean-looking input and calls would be lost. @ryanmrichard is working on a project that would help you out, but it’s pretty far down the pipeline.

I think the approach for you would be to build psi4.so (https://github.com/psi4/psi4/wiki/8_FAQ_Contents#how-to-build-psi4-as-an-importable-python-module--shared-object-library); it may need a few changes to run as a Linux library rather than a Python import, but I know how to make those. Then with a regular executable psi4, run the simplest input, like below with psi4 -v. That’ll show you the actual python commands that the input syntax is translated into. Then look into src/bin/psi4/python.cc and src/bin/psi4/export_* to see what C++ those python commands are connecting to. Note that a few things are always done in Python-- basis set reading most notably-- but there’s an older C++ version of that, too. The psi4.so library should do all the initialization of the (shrinking, yay!) global variable space that the program uses.

I’m glad to help you find relevant pieces of the code and probably to make any little modifications you may need, but I’m not really a C++ person. Good luck!

molecule {
H 0.0 0.0 0.0
H 1.0 0.0 0.0

set basis cc-pvdz


Sometime ago I actually tried what you are purposing, driving Psi4 from C++, and it did not work too well. Long story short, the way Psi4 is currently setup is that when you call the Psi4 executable, which is in C++, it then calls Python, which then calls C++. Essentially the C++ interface that you may have inferred is there, is written in Python, which is launched from within C++. If you go with psi4.so mentioned above you can remove the top C++ call and instead replace the it with your QMC code, but you will still have to go through the Python interface in the same way the binary does. The other option is to rewrite a lot of infrastructure. The latter option is a sort of duplication of effort because that is essentially what the project mentioned by @loriab does.

One of the key aspects of this project (despite it being now in development for about six months we have yet to agree on a name, hence the sort of mystery) is that the driver will be mirrored both in C++ and Python enabling driving from either language. The former I have found is more natural for high-performance computing. I’m optimistic that a working version of this project is not as far away as @loriab has made it sound (at least in terms of driving Psi4). At the moment, I’m currently using it to write a many-body expansion (break a cluster into monomers, than compute monomer energies, two-body energies, three-body energies, etc.) method, which similar to QMC, is just spinning off a lot of orthogonal electronic structure computations. I am anticipating that it will be in a usable state by the end of the month. If at that time you would be open to collaborating, we would love to work with you to get your QMC code to drive Psi4 through our interface. Depending on how we spin it, there may even be the possibility for some sort of paper detailing the interface, again assuming you are up for it.

Has this topic been broached since? Has anyone tried to run Psi4 from a c++ program instead of a python script?

I’m going to lock this topic up, as it’s so old.