Skip to content
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

Closed
wants to merge 21 commits into from
Closed

Pybind #55

wants to merge 21 commits into from

Conversation

alexandrupaler
Copy link
Contributor

Made it work with pybind11. This PR is to show that pybind11 worked almost out of the box.

  • pybind comes in the form of headers (these were copied in the include folder)
  • made some edits in Makefile (pybind compilation is make pybind)
  • the pybind_main.cpp includes a simple simulate method that is exposed to Python
  • python3 pybind_test to test the simulator with the default parameters like the example from main.cpp

Requirements for pybind11 are python3-dev (e.g. sudo apt-get install python3-dev)

@95-martin-orion
Copy link
Contributor

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.

@alexandrupaler
Copy link
Contributor Author

It is almost luck that pybind11 accepts some c++17 like std::optional. Otherwise I would have implemented it.

I added pybind11 as submodule.

Copy link
Contributor

@95-martin-orion 95-martin-orion left a 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_main.cpp Outdated Show resolved Hide resolved
pybind_test.py Outdated
@@ -0,0 +1,23 @@
import qflex
Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like YAPF is the recommended solution here - let's handle this in a separate PR. (Opened #65 to track this - mark as resolved after reading.)

Copy link
Contributor

@95-martin-orion 95-martin-orion left a 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.)

Copy link
Contributor

@95-martin-orion 95-martin-orion left a 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 Show resolved Hide resolved
pybind_main.cpp Outdated Show resolved Hide resolved
pybind_main.cpp Outdated
input.grid_width = grid_width;
input.super_cycles = super_cycles;

//deprecated?
Copy link
Contributor

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 Show resolved Hide resolved
pybind_main.cpp Outdated
input.grid_data = &grid_stream;

/*
TODO: for the moment, the following two are not sent as parameters
Copy link
Contributor

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

Copy link
Contributor

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 &amplitude = amplitudes[c].second;
std::cout << "XXXX"
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like YAPF is the recommended solution here - let's handle this in a separate PR. (Opened #65 to track this - mark as resolved after reading.)

pybind_test.py Outdated
@@ -0,0 +1,28 @@
import qflex
Copy link
Contributor

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

@s-mandra
Copy link
Member

s-mandra commented Oct 6, 2019

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.

@alexandrupaler
Copy link
Contributor Author

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.

@s-mandra
Copy link
Member

s-mandra commented Oct 7, 2019

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?

@alexandrupaler
Copy link
Contributor Author

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.

@s-mandra
Copy link
Member

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!

@s-mandra s-mandra added the enhancement New feature or request label Oct 10, 2019
@s-mandra s-mandra added this to the qFlex initial release milestone Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants