52
52
// ===----------------------------------------------------------------------===//
53
53
54
54
#include " API/api.h"
55
+ #include " lib_enums.h"
55
56
56
57
#include < pybind11/functional.h>
57
58
#include < pybind11/pybind11.h>
66
67
#include < unordered_map>
67
68
#include < vector>
68
69
70
+ namespace py = pybind11;
71
+
69
72
// / Call into the qss-compiler via an interface to qss-compile's command line
70
73
// / 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) {
74
77
std::string outputStr (" " );
75
78
76
79
#ifndef NDEBUG
@@ -96,10 +99,10 @@ pybind11::tuple py_compile_by_args(const std::vector<std::string> &args,
96
99
std::cerr << " Compile " << (success ? " successful" : " failed" ) << std::endl;
97
100
#endif
98
101
99
- return pybind11 ::make_tuple (success, pybind11 ::bytes (outputStr));
102
+ return py ::make_tuple (success, py ::bytes (outputStr));
100
103
}
101
104
102
- pybind11 ::tuple
105
+ py ::tuple
103
106
py_link_file (const std::string &input, const bool enableInMemoryInput,
104
107
const std::string &outputPath,
105
108
const std::string &target, const std::string &configPath,
@@ -118,63 +121,19 @@ py_link_file(const std::string &input, const bool enableInMemoryInput,
118
121
#ifndef NDEBUG
119
122
std::cerr << " Link " << (success ? " successful" : " failed" ) << std::endl;
120
123
#endif
121
- return pybind11 ::make_tuple (success, pybind11 ::bytes (inMemoryOutput));
124
+ return py ::make_tuple (success, py ::bytes (inMemoryOutput));
122
125
}
123
126
124
127
128
+ // Pybind module
125
129
PYBIND11_MODULE (py_qssc, m) {
126
130
m.doc () = " Python bindings for the QSS Compiler." ;
127
131
128
132
m.def (" _compile_with_args" , &py_compile_by_args,
129
133
" Call compiler via cli qss-compile" );
130
134
m.def (" _link_file" , &py_link_file, " Call the linker tool" );
131
135
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);
180
139
}
0 commit comments