Skip to content

Commit b21a2df

Browse files
authored
Separate out the python interface functions to different module (#175)
## Summary This PR addresses the comment #124 (comment), to separate out the list of error categories `.value()` enum as an include or macro so that this builder function doesn't continue to grow in [lib.cpp](https://github.com/Qiskit/qss-compiler/blob/main/python_lib/qss_compiler/lib.cpp) as we continue to add more exceptions. The only way to get this done was to move out the python interface functions to a different file and to keep the enum in the same file.
1 parent 05d9d05 commit b21a2df

File tree

4 files changed

+83
-54
lines changed

4 files changed

+83
-54
lines changed

python_lib/qss_compiler/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
set(SOURCES
1414
${CMAKE_CURRENT_SOURCE_DIR}/lib.cpp
15+
${CMAKE_CURRENT_SOURCE_DIR}/lib_enums.cpp
1516
)
1617

1718
pybind11_add_module(py_qssc SHARED ${SOURCES})

python_lib/qss_compiler/lib.cpp

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
//===----------------------------------------------------------------------===//
5353

5454
#include "API/api.h"
55+
#include "lib_enums.h"
5556

5657
#include <pybind11/functional.h>
5758
#include <pybind11/pybind11.h>
@@ -66,11 +67,13 @@
6667
#include <unordered_map>
6768
#include <vector>
6869

70+
namespace py = pybind11;
71+
6972
/// Call into the qss-compiler via an interface to qss-compile's command line
7073
/// argument.
71-
pybind11::tuple py_compile_by_args(const std::vector<std::string> &args,
72-
bool outputAsStr,
73-
qssc::DiagnosticCallback onDiagnostic) {
74+
py::tuple py_compile_by_args(const std::vector<std::string> &args,
75+
bool outputAsStr,
76+
qssc::DiagnosticCallback onDiagnostic) {
7477
std::string outputStr("");
7578

7679
#ifndef NDEBUG
@@ -96,10 +99,10 @@ pybind11::tuple py_compile_by_args(const std::vector<std::string> &args,
9699
std::cerr << "Compile " << (success ? "successful" : "failed") << std::endl;
97100
#endif
98101

99-
return pybind11::make_tuple(success, pybind11::bytes(outputStr));
102+
return py::make_tuple(success, py::bytes(outputStr));
100103
}
101104

102-
pybind11::tuple
105+
py::tuple
103106
py_link_file(const std::string &input, const bool enableInMemoryInput,
104107
const std::string &outputPath,
105108
const std::string &target, const std::string &configPath,
@@ -118,63 +121,19 @@ py_link_file(const std::string &input, const bool enableInMemoryInput,
118121
#ifndef NDEBUG
119122
std::cerr << "Link " << (success ? "successful" : "failed") << std::endl;
120123
#endif
121-
return pybind11::make_tuple(success, pybind11::bytes(inMemoryOutput));
124+
return py::make_tuple(success, py::bytes(inMemoryOutput));
122125
}
123126

124127

128+
// Pybind module
125129
PYBIND11_MODULE(py_qssc, m) {
126130
m.doc() = "Python bindings for the QSS Compiler.";
127131

128132
m.def("_compile_with_args", &py_compile_by_args,
129133
"Call compiler via cli qss-compile");
130134
m.def("_link_file", &py_link_file, "Call the linker tool");
131135

132-
pybind11::enum_<qssc::ErrorCategory>(m, "ErrorCategory",
133-
pybind11::arithmetic())
134-
.value("OpenQASM3ParseFailure",
135-
qssc::ErrorCategory::OpenQASM3ParseFailure)
136-
.value("QSSCompilerError", qssc::ErrorCategory::QSSCompilerError)
137-
.value("QSSCompilerNoInputError", qssc::ErrorCategory::QSSCompilerNoInputError)
138-
.value("QSSCompilerCommunicationFailure", qssc::ErrorCategory::QSSCompilerCommunicationFailure)
139-
.value("QSSCompilerEOFFailure", qssc::ErrorCategory::QSSCompilerEOFFailure)
140-
.value("QSSCompilerNonZeroStatus", qssc::ErrorCategory::QSSCompilerNonZeroStatus)
141-
.value("QSSCompilationFailure", qssc::ErrorCategory::QSSCompilationFailure)
142-
.value("QSSLinkerNotImplemented", qssc::ErrorCategory::QSSLinkerNotImplemented)
143-
.value("QSSLinkSignatureWarning", qssc::ErrorCategory::QSSLinkSignatureWarning)
144-
.value("QSSLinkSignatureError", qssc::ErrorCategory::QSSLinkSignatureError)
145-
.value("QSSLinkAddressError", qssc::ErrorCategory::QSSLinkAddressError)
146-
.value("QSSLinkSignatureNotFound", qssc::ErrorCategory::QSSLinkSignatureNotFound)
147-
.value("QSSLinkArgumentNotFoundWarning", qssc::ErrorCategory::QSSLinkArgumentNotFoundWarning)
148-
.value("QSSLinkInvalidPatchTypeError", qssc::ErrorCategory::QSSLinkInvalidPatchTypeError)
149-
.value("UncategorizedError", qssc::ErrorCategory::UncategorizedError)
150-
.export_values();
151-
152-
pybind11::enum_<qssc::Severity>(m, "Severity")
153-
.value("Info", qssc::Severity::Info)
154-
.value("Warning", qssc::Severity::Warning)
155-
.value("Error", qssc::Severity::Error)
156-
.value("Fatal", qssc::Severity::Fatal)
157-
.export_values();
158-
159-
pybind11::class_<qssc::Diagnostic>(m, "Diagnostic")
160-
.def_readonly("severity", &qssc::Diagnostic::severity)
161-
.def_readonly("category", &qssc::Diagnostic::category)
162-
.def_readonly("message", &qssc::Diagnostic::message)
163-
.def("__str__", &qssc::Diagnostic::toString)
164-
.def(pybind11::pickle(
165-
[](const qssc::Diagnostic &d) {
166-
// __getstate__ serializes the C++ object into a tuple
167-
return pybind11::make_tuple(d.severity, d.category, d.message);
168-
},
169-
[](pybind11::tuple const &t) {
170-
// __setstate__ restores the C++ object from a tuple
171-
if (t.size() != 3)
172-
throw std::runtime_error("invalid state for unpickling");
173-
174-
auto severity = t[0].cast<qssc::Severity>();
175-
auto category = t[1].cast<qssc::ErrorCategory>();
176-
auto message = t[2].cast<std::string>();
177-
178-
return qssc::Diagnostic(severity, category, std::move(message));
179-
}));
136+
addErrorCategory(m);
137+
addSeverity(m);
138+
addDiagnostic(m);
180139
}

python_lib/qss_compiler/lib_enums.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "API/api.h"
2+
#include "lib_enums.h"
3+
4+
namespace py = pybind11;
5+
6+
void addErrorCategory(py::module &m) {
7+
py::enum_<qssc::ErrorCategory>(m, "ErrorCategory", py::arithmetic())
8+
.value("OpenQASM3ParseFailure",
9+
qssc::ErrorCategory::OpenQASM3ParseFailure)
10+
.value("QSSCompilerError", qssc::ErrorCategory::QSSCompilerError)
11+
.value("QSSCompilerNoInputError", qssc::ErrorCategory::QSSCompilerNoInputError)
12+
.value("QSSCompilerCommunicationFailure", qssc::ErrorCategory::QSSCompilerCommunicationFailure)
13+
.value("QSSCompilerEOFFailure", qssc::ErrorCategory::QSSCompilerEOFFailure)
14+
.value("QSSCompilerNonZeroStatus", qssc::ErrorCategory::QSSCompilerNonZeroStatus)
15+
.value("QSSCompilationFailure", qssc::ErrorCategory::QSSCompilationFailure)
16+
.value("QSSLinkerNotImplemented", qssc::ErrorCategory::QSSLinkerNotImplemented)
17+
.value("QSSLinkSignatureWarning", qssc::ErrorCategory::QSSLinkSignatureWarning)
18+
.value("QSSLinkSignatureError", qssc::ErrorCategory::QSSLinkSignatureError)
19+
.value("QSSLinkAddressError", qssc::ErrorCategory::QSSLinkAddressError)
20+
.value("QSSLinkSignatureNotFound", qssc::ErrorCategory::QSSLinkSignatureNotFound)
21+
.value("QSSLinkArgumentNotFoundWarning", qssc::ErrorCategory::QSSLinkArgumentNotFoundWarning)
22+
.value("QSSLinkInvalidPatchTypeError", qssc::ErrorCategory::QSSLinkInvalidPatchTypeError)
23+
.value("UncategorizedError", qssc::ErrorCategory::UncategorizedError)
24+
.export_values();
25+
}
26+
27+
void addSeverity(py::module &m) {
28+
py::enum_<qssc::Severity>(m, "Severity")
29+
.value("Info", qssc::Severity::Info)
30+
.value("Warning", qssc::Severity::Warning)
31+
.value("Error", qssc::Severity::Error)
32+
.value("Fatal", qssc::Severity::Fatal)
33+
.export_values();
34+
}
35+
36+
void addDiagnostic(py::module &m) {
37+
py::class_<qssc::Diagnostic>(m, "Diagnostic")
38+
.def_readonly("severity", &qssc::Diagnostic::severity)
39+
.def_readonly("category", &qssc::Diagnostic::category)
40+
.def_readonly("message", &qssc::Diagnostic::message)
41+
.def("__str__", &qssc::Diagnostic::toString)
42+
.def(py::pickle(
43+
[](const qssc::Diagnostic &d) {
44+
// __getstate__ serializes the C++ object into a tuple
45+
return py::make_tuple(d.severity, d.category, d.message);
46+
},
47+
[](py::tuple const &t) {
48+
// __setstate__ restores the C++ object from a tuple
49+
if (t.size() != 3)
50+
throw std::runtime_error("invalid state for unpickling");
51+
52+
auto severity = t[0].cast<qssc::Severity>();
53+
auto category = t[1].cast<qssc::ErrorCategory>();
54+
auto message = t[2].cast<std::string>();
55+
56+
return qssc::Diagnostic(severity, category, std::move(message));
57+
}));
58+
}

python_lib/qss_compiler/lib_enums.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <pybind11/functional.h>
2+
#include <pybind11/pybind11.h>
3+
#include <pybind11/stl.h>
4+
5+
namespace py = pybind11;
6+
7+
void addErrorCategory(py::module &m);
8+
9+
void addSeverity(py::module &m);
10+
11+
void addDiagnostic(py::module &m);

0 commit comments

Comments
 (0)