From e2d83e742c607322e2e99205db785767eb82c8e9 Mon Sep 17 00:00:00 2001 From: Lukas Burgholzer Date: Mon, 19 Oct 2020 16:02:31 +0200 Subject: [PATCH] Improve python bindings * fixed namespace package * if csv option is present, the resulting python object now contains an entry with the resulting csv string Signed-off-by: Lukas Burgholzer --- extern/pybind11 | 2 +- include/EquivalenceCheckingResults.hpp | 23 ++++++++++++++--------- jkq/qcec/CMakeLists.txt | 5 +++-- jkq/qcec/bindings.cpp | 6 +++++- setup.py | 4 ++-- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/extern/pybind11 b/extern/pybind11 index c4a8b5bb..0b9acc40 160000 --- a/extern/pybind11 +++ b/extern/pybind11 @@ -1 +1 @@ -Subproject commit c4a8b5bb9140cdc8c214044d41e2593986f875d0 +Subproject commit 0b9acc4009afdda427b5f151a67d500216707dc1 diff --git a/include/EquivalenceCheckingResults.hpp b/include/EquivalenceCheckingResults.hpp index a01573d4..59e9eb00 100644 --- a/include/EquivalenceCheckingResults.hpp +++ b/include/EquivalenceCheckingResults.hpp @@ -232,21 +232,26 @@ namespace ec { virtual std::ostream& printCSVEntry(std::ostream& out) { if (error()) return out; - out << name1 << ";" << nqubits1 << ";" << ngates1 << ";" << name2 << ";" << nqubits2 << ";" << ngates2 << ";" << toString(expected) << ";" << toString(equivalence) << ";" << toString(method) << ";"; + out << produceCSVEntry() << std::endl; + return out; + } + virtual std::string produceCSVEntry() { + if (error()) + return ""; + std::stringstream ss{}; + ss << name1 << ";" << nqubits1 << ";" << ngates1 << ";" << name2 << ";" << nqubits2 << ";" << ngates2 << ";" << toString(expected) << ";" << toString(equivalence) << ";" << toString(method) << ";"; if (timeout) { - out << "TO"; + ss << "TO"; } else { - out << time ; + ss << time ; } - out << ";" << maxActive; + ss << ";" << maxActive; if (nsims > 0) { - out << ";" << nsims; + ss << ";" << nsims; } else { - out << ";-"; + ss << ";-"; } - - out << std::endl; - return out; + return ss.str(); } }; } diff --git a/jkq/qcec/CMakeLists.txt b/jkq/qcec/CMakeLists.txt index 990a1cff..8ce054c3 100644 --- a/jkq/qcec/CMakeLists.txt +++ b/jkq/qcec/CMakeLists.txt @@ -1,6 +1,7 @@ -add_subdirectory("../../extern/pybind11" "extern/pybind11") +add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11" "extern/pybind11") add_library(pybind11 ALIAS pybind11::pybind11) -add_subdirectory("../../extern/pybind11_json" "extern/pybind11_json") + +add_subdirectory("${PROJECT_SOURCE_DIR}/extern/pybind11_json" "extern/pybind11_json") pybind11_add_module(py${PROJECT_NAME} bindings.cpp EXCLUDE_FROM_ALL) target_link_libraries(py${PROJECT_NAME} PUBLIC pybind11_json ${PROJECT_NAME}) diff --git a/jkq/qcec/bindings.cpp b/jkq/qcec/bindings.cpp index d425642e..302bd5e7 100644 --- a/jkq/qcec/bindings.cpp +++ b/jkq/qcec/bindings.cpp @@ -93,7 +93,11 @@ nl::json verify(const py::object& instance) { return {"error", ss.str()}; } - return ec->results.produceJSON(config.printStatistics); + auto result = ec->results.produceJSON(config.printStatistics); + if (config.printCSV) + result["csv"] = ec->results.produceCSVEntry(); + + return result; } PYBIND11_MODULE(pyqcec, m) { diff --git a/setup.py b/setup.py index d82e8804..bae66c49 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ import platform import subprocess -from setuptools import setup, Extension, find_packages +from setuptools import setup, Extension, find_namespace_packages from setuptools.command.build_ext import build_ext @@ -74,7 +74,7 @@ def build_extension(self, ext): ext_modules=[CMakeExtension('pyqcec')], cmdclass=dict(build_ext=CMakeBuild), zip_safe=False, - packages=find_packages(), + packages=find_namespace_packages(), classifiers=[ 'Development Status :: 4 - Beta', "Programming Language :: Python :: 3",