From b9c6eac83987086a26d1f9736b504a5316e32385 Mon Sep 17 00:00:00 2001 From: Tom Peham Date: Tue, 31 Oct 2023 10:17:16 +0100 Subject: [PATCH] Adjust bindings to new mqt.core. --- extern/mqt-core | 2 +- src/mqt/qcec/verify.py | 1 + src/python/bindings.cpp | 88 +++++++++++++++++++++++++---------------- 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/extern/mqt-core b/extern/mqt-core index 4d79b166..769d1dad 160000 --- a/extern/mqt-core +++ b/extern/mqt-core @@ -1 +1 @@ -Subproject commit 4d79b166f3a8777295820eee8291de53b5a5a9d3 +Subproject commit 769d1dad5e0fe20cde6a7f4b8ee05717d35bb84f diff --git a/src/mqt/qcec/verify.py b/src/mqt/qcec/verify.py index 85af329a..a384fd76 100644 --- a/src/mqt/qcec/verify.py +++ b/src/mqt/qcec/verify.py @@ -10,6 +10,7 @@ from .configuration import ConfigurationOptions + from . import Configuration, EquivalenceCheckingManager from .configuration import augment_config_from_kwargs from .parameterized import __is_parameterized, check_parameterized diff --git a/src/python/bindings.cpp b/src/python/bindings.cpp index c1909bd2..696e8aad 100644 --- a/src/python/bindings.cpp +++ b/src/python/bindings.cpp @@ -4,11 +4,10 @@ // #include "EquivalenceCheckingManager.hpp" +#include "QuantumComputation.hpp" #include "pybind11/pybind11.h" #include "pybind11/stl.h" #include "pybind11_json/pybind11_json.hpp" -#include "python/qiskit/QasmQobjExperiment.hpp" -#include "python/qiskit/QuantumCircuit.hpp" #include #include @@ -17,47 +16,55 @@ namespace py = pybind11; using namespace pybind11::literals; namespace ec { -static qc::QuantumComputation importCircuit(const py::object& circ) { - const py::object quantumCircuit = - py::module::import("qiskit").attr("QuantumCircuit"); - const py::object pyQasmQobjExperiment = - py::module::import("qiskit.qobj").attr("QasmQobjExperiment"); +// static qc::QuantumComputation importCircuit(const py::object& circ) { +// const py::object quantumCircuit = +// py::module::import("qiskit").attr("QuantumCircuit"); +// const py::object pyQasmQobjExperiment = +// py::module::import("qiskit.qobj").attr("QasmQobjExperiment"); - auto qc = qc::QuantumComputation(); +// auto qc = qc::QuantumComputation(); - if (py::isinstance(circ)) { - const auto file = circ.cast(); - qc.import(file); - } else if (py::isinstance(circ, quantumCircuit)) { - qc::qiskit::QuantumCircuit::import(qc, circ); - } else if (py::isinstance(circ, pyQasmQobjExperiment)) { - qc::qiskit::QasmQobjExperiment::import(qc, circ); - } else { - throw std::runtime_error( - "PyObject is neither py::str, QuantumCircuit, nor QasmQobjExperiment"); - } +// if (py::isinstance(circ)) { +// const auto file = circ.cast(); +// qc.import(file); +// } else if (py::isinstance(circ, quantumCircuit)) { +// qc::qiskit::QuantumCircuit::import(qc, circ); +// } else if (py::isinstance(circ, pyQasmQobjExperiment)) { +// qc::qiskit::QasmQobjExperiment::import(qc, circ); +// } else { +// throw std::runtime_error( +// "PyObject is neither py::str, QuantumCircuit, nor +// QasmQobjExperiment"); +// } - return qc; -} +// return qc; +// } -static std::unique_ptr -createManagerFromConfiguration(const py::object& circ1, const py::object& circ2, - const Configuration& configuration = {}) { - qc::QuantumComputation qc1; - try { - qc1 = importCircuit(circ1); - } catch (const std::exception& ex) { - throw std::runtime_error("Could not import first circuit: " + - std::string(ex.what())); +static qc::QuantumComputation qcFromPy(const py::object& circ) { + if (py::isinstance(circ)) { + return circ.cast(); + } + + if (py::isinstance(circ)) { + const auto file = circ.cast(); + return qc::QuantumComputation(file); } - qc::QuantumComputation qc2; + py::object qiskit = py::module_::import("mqt.core.plugins.qiskit"); + py::object qiskit_to_mqt = qiskit.attr("qiskit_to_mqt"); try { - qc2 = importCircuit(circ2); + return qiskit_to_mqt(circ).cast(); } catch (const std::exception& ex) { - throw std::runtime_error("Could not import second circuit: " + + throw std::runtime_error("Could not import circuit: " + std::string(ex.what())); } + return qc::QuantumComputation(); +} +static std::unique_ptr +createManagerFromConfiguration(const py::object& circ1, const py::object& circ2, + const Configuration& configuration = {}) { + auto qc1 = qcFromPy(circ1); + auto qc2 = qcFromPy(circ2); return std::make_unique(qc1, qc2, configuration); } @@ -184,6 +191,21 @@ PYBIND11_MODULE(pyqcec, m) { "config"_a = Configuration(), "Create an equivalence checking manager for two circuits and " "configure it with a :class:`Configuration` object.") + // .def(py::init([](const py::object& circ1, + // const py::object& + // circ2, const + // Configuration& config) + // { + // py::object qiskit = + // py::module_::import("mqt.core.plugins.qiskit"); py::object + // qiskit_to_mqt = qiskit.attr("qiskit_to_mqt"); return + // std::make_unique( + // qiskit_to_mqt(circ1).cast(), + // qiskit_to_mqt(circ2).cast(), + // config);}), + // "circ1"_a, "circ2"_a, "config"_a = Configuration(), + // "Create an equivalence checking manager for two circuits and " + // "configure it with a :class:`Configuration` object.") .def("get_configuration", &EquivalenceCheckingManager::getConfiguration) // Convenience functions // Execution