WIP: Python Bindings
Created by: gcarleo
This is a work-in-progress PR for Python Bindings, using pybind11, and addresses issue #8 (closed)
It is possible to compile NetKet and install the python library just doing
pip3 install netket/
This will automatically call CMake, and also register the python library.
At the moment, this PR provides bindings for Graph
, Hilbert
, Hamiltonian
, SparseMatrixWrapper
classes. Eventually, all major classes will have Python bindings.
In Tutorials/PyNetKet
there is an example usage. For example one can do things like
import pynetket as nk
from mpi4py import MPI
g=nk.Graph("Hypercube",L=10,Dimension=1)
print(g.Distances(1))
print(g.IsBipartite())
print(g.IsConnected())
The beauty of pybind11 is that all methods are exported using this piece of code
PYBIND11_MODULE(pynetket, m) {
py::class_<Graph>(m, "Graph")
.def(py::init<std::string, py::kwargs>())
.def("Nsites", &Graph::Nsites)
.def("AdjacencyList", &Graph::AdjacencyList)
.def("SymmetryTable", &Graph::SymmetryTable)
.def("EdgeColors", &Graph::EdgeColors)
.def("IsBipartite", &Graph::IsBipartite)
.def("IsConnected", &Graph::IsConnected)
.def("Distances", &Graph::Distances)
.def("AllDistances", &Graph::AllDistances);
}
The rest is just some changes in the constructors of the c++ classes to take **kwargs
in addition to Json objects.
Still to do (help/suggestions welcome):
-
Pybind11 is added as a git subtree, instead of using the current strategy of using
ExternalProject_Add
in the CMake. I wasn't able to make the latter work, and decided to use a much easier solution based on subtrees. However, if there is a way to make it work as an external project that would be nice. -
Unit tests are missing for the python part. What's a good strategy?
-
Provide bindings for Hamiltonian, Machine, Sampler...etc
-
What to do with random number generators? Can we bind them?
Important remarks:
- This PR changes the APIs for
Hilbert
andHamiltonian
. In particular, it is not possible to deduce theHilbert
fromHamiltonian
anymore, andHilbert
must be specified in the input file. This change is necessary to avoid unnecessary complications in the maintenance. It also makes sense to have to specify explicitly the Hilbert space. - All glue classes now use
std::unique_ptr
, removing the previous use ofstd::share_ptr
, see also #42