Skip to content

Commit

Permalink
speedup in pyqpp compilation time (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsoftco authored Aug 15, 2023
1 parent b34cb37 commit e16cb87
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Pre-release
- Significant speedup in pyqpp's compilation time

Version 4.3.4 - 14 August 2023
- Docker update, see the ["docker/"] directory
- Fix in ["types.hpp"] for defaulting types when not using CMake
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_CLASSES_CIRCUITS_CIRCUITS_BIND_HPP_
#define PYQPP_CLASSES_CIRCUITS_CIRCUITS_BIND_HPP_

/* qpp::QCircuit and related free functions */
void init_classes_circuits_circuits(py::module_& m) {
inline void init_classes_circuits_circuits(py::module_& m) {
using namespace qpp;

auto pyQCircuit =
Expand Down Expand Up @@ -488,3 +489,5 @@ void init_classes_circuits_circuits(py::module_& m) {
"Replicates a quantum circuit description", py::arg("qc"),
py::arg("n"));
}

#endif /* PYQPP_CLASSES_CIRCUITS_CIRCUITS_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_CLASSES_CIRCUITS_ENGINES_BIND_HPP_
#define PYQPP_CLASSES_CIRCUITS_ENGINES_BIND_HPP_

/* qpp::QNoisyEngine instantiator */
template <typename NoiseModel, typename... CtorTypeList>
Expand Down Expand Up @@ -73,7 +74,7 @@ void declare_noisy_engine(py::module& m, const std::string& type) {
}

/* qpp::QEngine */
void init_classes_circuits_engines(py::module_& m) {
inline void init_classes_circuits_engines(py::module_& m) {
using namespace qpp;

py::class_<QEngine>(m, "QEngine")
Expand Down Expand Up @@ -146,3 +147,5 @@ void init_classes_circuits_engines(py::module_& m) {
declare_noisy_engine<qpp::QuditDepolarizingNoise, realT, idx>(
m, "QuditDepolarizingNoise");
}

#endif /* PYQPP_CLASSES_CIRCUITS_ENGINES_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_CLASSES_GATES_BIND_HPP_
#define PYQPP_CLASSES_GATES_BIND_HPP_

/* qpp::Gates */
void init_classes_gates(py::module_& m) {
inline void init_classes_gates(py::module_& m) {
using namespace qpp;

auto gates = m.def_submodule("gates");
Expand Down Expand Up @@ -87,3 +88,5 @@ void init_classes_gates(py::module_& m) {
"Zd", [](idx D) { return qpp::gt.Zd(D); },
"Generalized Z gate for qudits", py::arg("D") = 2);
}

#endif /* PYQPP_CLASSES_GATES_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_CLASSES_REVERSIBLE_BIND_HPP_
#define PYQPP_CLASSES_REVERSIBLE_BIND_HPP_

/* qpp::Dynamic_bitset and qpp::Bit_circuit */
void init_classes_reversible(py::module_& m) {
inline void init_classes_reversible(py::module_& m) {
using namespace qpp;

/* qpp::Dynamic_bitset */
Expand Down Expand Up @@ -136,3 +137,5 @@ void init_classes_reversible(py::module_& m) {
return oss.str();
});
}

#endif /* PYQPP_CLASSES_REVERSIBLE_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_CLASSES_STATES_BIND_HPP_
#define PYQPP_CLASSES_STATES_BIND_HPP_

/* qpp::States */
void init_classes_states(py::module_& m) {
inline void init_classes_states(py::module_& m) {
using namespace qpp;

auto states = m.def_submodule("states");
Expand Down Expand Up @@ -78,3 +79,5 @@ void init_classes_states(py::module_& m) {
"zero", [](idx n, idx d) { return qpp::st.zero(n, d); },
"Zero state of n qudits", py::arg("n") = 1, py::arg("d") = 2);
}

#endif /* PYQPP_CLASSES_STATES_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_CONSTANTS_BIND_HPP_
#define PYQPP_CONSTANTS_BIND_HPP_

/* Constants from constants.hpp */
void init_constants(py::module_& m) {
inline void init_constants(py::module_& m) {
using namespace qpp;

m.attr("ee") = qpp::ee;
m.def("omega", &qpp::omega, "D-th root of unity", py::arg("D"));
m.attr("pi") = qpp::pi;
}

#endif /* PYQPP_CONSTANTS_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_FUNCTIONS_BIND_HPP_
#define PYQPP_FUNCTIONS_BIND_HPP_

/* Some free functions (non-exhaustive list) from functions.hpp */
void init_functions(py::module_& m) {
inline void init_functions(py::module_& m) {
using namespace qpp;

/* Template methods must be explicitly instantiated, some examples below */
Expand Down Expand Up @@ -73,3 +74,5 @@ void init_functions(py::module_& m) {
"transpose", [](const cmat& A) { return qpp::transpose(A); },
"Transpose", py::arg("A"));
}

#endif /* PYQPP_FUNCTIONS_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_INSTRUMENTS_BIND_HPP_
#define PYQPP_INSTRUMENTS_BIND_HPP_

/* Some free functions (non-exhaustive list) from instruments.hpp */
void init_instruments(py::module_& m) {
inline void init_instruments(py::module_& m) {
using namespace qpp;

m.def(
Expand Down Expand Up @@ -80,3 +81,5 @@ void init_instruments(py::module_& m) {
py::arg("num_samples"), py::arg("A"), py::arg("target"),
py::arg("d") = 2);
}

#endif /* PYQPP_INSTRUMENTS_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_PYQPP_SPECIFIC_BIND_HPP_
#define PYQPP_PYQPP_SPECIFIC_BIND_HPP_

// Python-specific functions only (not necessarily with an equivalent version in
// qpp)
void init_pyqpp_specific(py::module_& m) {
/* Python-specific functions only (not necessarily with an equivalent version in
* qpp) */
inline void init_pyqpp_specific(py::module_& m) {
using namespace qpp;

m.def(
Expand All @@ -45,3 +46,5 @@ void init_pyqpp_specific(py::module_& m) {
},
"Sets the prng seed to a random value");
}

#endif /* PYQPP_PYQPP_SPECIFIC_BIND_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_QASM_QASM_BIND_HPP_
#define PYQPP_QASM_QASM_BIND_HPP_

/* OpenQASM interfacing */
void init_qasm_qasm(py::module_& m) {
inline void init_qasm_qasm(py::module_& m) {
using namespace qpp;

auto py_qasm = m.def_submodule("qasm");
py_qasm.def("read_from_file", &qpp::qasm::read_from_file,
"Get QCircuit representation of OpenQASM circuit");
}

#endif /* PYQPP_QASM_QASM_BIND_HPP_ */
7 changes: 5 additions & 2 deletions pyqpp/src/random_bind.cpp → pyqpp/include/random_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
* SOFTWARE.
*/

#include "pyqpp_common.h"
#ifndef PYQPP_RANDOM_BIND_HPP_
#define PYQPP_RANDOM_BIND_HPP_

/* Some free functions (non-exhaustive list) from random.hpp */
void init_random(py::module_& m) {
inline void init_random(py::module_& m) {
using namespace qpp;

m.def("randU", &qpp::randU, "Generates a random unitary matrix",
py::arg("D") = 2);
}

#endif /* PYQPP_RANDOM_BIND_HPP_ */
23 changes: 11 additions & 12 deletions pyqpp/qpp_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@

#include "pyqpp_common.h"

void init_constants(py::module_&);
void init_functions(py::module_&);
void init_instruments(py::module_&);
void init_random(py::module_&);
#include "classes/circuits/circuits_bind.hpp"
#include "classes/circuits/engines_bind.hpp"

void init_classes_gates(py::module_&);
void init_classes_reversible(py::module_&);
void init_classes_states(py::module_&);
#include "classes/gates_bind.hpp"
#include "classes/reversible_bind.hpp"
#include "classes/states_bind.hpp"

void init_classes_circuits_circuits(py::module_&);
void init_classes_circuits_engines(py::module_&);
#include "qasm/qasm_bind.hpp"

void init_qasm_qasm(py::module_&);

void init_pyqpp_specific(py::module_&);
#include "constants_bind.hpp"
#include "functions_bind.hpp"
#include "instruments_bind.hpp"
#include "pyqpp_specific_bind.hpp"
#include "random_bind.hpp"

PYBIND11_MODULE(pyqpp, m) {
m.doc() =
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
if eigen_path is None:
raise Exception('Eigen3 not found!')

source_files = [f for f in sorted(glob("pyqpp/**/*.cpp", recursive=True))]
#source_files = [f for f in sorted(glob("pyqpp/**/*.cpp", recursive=True))]
source_files = ["pyqpp/qpp_wrapper.cpp"]
ext_modules = [
Pybind11Extension(
"pyqpp",
Expand Down

0 comments on commit e16cb87

Please sign in to comment.