-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pybind #55
Pybind #55
Conversation
I would recommend using a git submodule for pybind, rather than copying the code directly. Other than that, this looks good - the fact that it lets us keep C++17 behavior is enough reason to use it instead of Cython. |
It is almost luck that pybind11 accepts some c++17 like std::optional. Otherwise I would have implemented it. I added pybind11 as submodule. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should decide on a directory structure for this at some point (e.g. C++ files in qflex/src, Python files in qflex/py) but that can be saved for a future PR.
pybind_test.py
Outdated
@@ -0,0 +1,23 @@ | |||
import qflex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an equivalent of clang-format for Python that we can use to enforce Google style in this repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR has grown to cover much more than just pybind. Could you split off the commits into separate conceptual stages and send a PR for each? (At least one for 'pybind' and 'QflexInput'; others as you see the need for them.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for taking so long on reviewing this - and thanks again for taking it on!
pybind_main.cpp
Outdated
input.grid_width = grid_width; | ||
input.super_cycles = super_cycles; | ||
|
||
//deprecated? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is deprecated and will be removed in #56. It can be removed here as well.
pybind_main.cpp
Outdated
input.grid_data = &grid_stream; | ||
|
||
/* | ||
TODO: for the moment, the following two are not sent as parameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this TODO be completed for this PR? It should only require passing initial_state and final_state_A as strings to simulate() (unless there's some python-to-C++ complexity I'm missing).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In place of the argc > current_arg
checks, !{input_value}.empty()
is fine.
pybind_main.cpp
Outdated
for (int c = 0; c < amplitudes.size(); ++c) { | ||
const auto &state = amplitudes[c].first; | ||
const auto &litude = amplitudes[c].second; | ||
std::cout << "XXXX" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be safe to use input.initial_state
here - EvaluateCircuit() will default-initialize it if it wasn't provided as input.
pybind_test.py
Outdated
@@ -0,0 +1,23 @@ | |||
import qflex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pybind_test.py
Outdated
@@ -0,0 +1,28 @@ | |||
import qflex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File-level comment: Could you add a comment explaining the purpose of this file, and leave a TODO to convert it to a unit test (e.g. using pytest)?
If I may, I would suggest to avoid pybind and use callable C libraries to call from within python. It's much cleaner and callable libraries can be called from other languages like Julia. |
pybind is headers only and super clean compared to cython. The result is a callable c++ lib (see Makefile). Are you suggesting to use ctypes? I can look into that too, as the interface between cirq and qflex is a single function, thus lightweight. However, I preferred at this stage pybind because it seemed faster to prototype, check and more robust against compilation errors. |
I took a closer look to your code and I agree it's simpler than I thought (I never used pybind before), so I suggest to keep using pybind. Do you know if languages like Julia would be able to use the c++ lib? |
I do not know if Julia is able to use the lib. It is an interesting question. I will look into that, after I made sure that the current Cirq integration is working. |
Let me say that having qFlex working with Julia is not a priority for our first release. So I would just focus on Python. Thanks! |
Made it work with pybind11. This PR is to show that pybind11 worked almost out of the box.
include
folder)make pybind
)python3 pybind_test
to test the simulator with the default parameters like the example from main.cppRequirements for pybind11 are python3-dev (e.g. sudo apt-get install python3-dev)