Skip to content

Commit

Permalink
Adjust bindings to new mqt.core.
Browse files Browse the repository at this point in the history
  • Loading branch information
pehamTom committed Oct 31, 2023
1 parent 9781f7c commit b9c6eac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/mqt/qcec/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 55 additions & 33 deletions src/python/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <exception>
#include <memory>
Expand All @@ -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<py::str>(circ)) {
const auto file = circ.cast<std::string>();
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<py::str>(circ)) {
// const auto file = circ.cast<std::string>();
// 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<EquivalenceCheckingManager>
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<qc::QuantumComputation>(circ)) {
return circ.cast<qc::QuantumComputation>();
}

if (py::isinstance<py::str>(circ)) {
const auto file = circ.cast<std::string>();
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<qc::QuantumComputation>();
} 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<EquivalenceCheckingManager>
createManagerFromConfiguration(const py::object& circ1, const py::object& circ2,
const Configuration& configuration = {}) {
auto qc1 = qcFromPy(circ1);
auto qc2 = qcFromPy(circ2);

return std::make_unique<EquivalenceCheckingManager>(qc1, qc2, configuration);
}
Expand Down Expand Up @@ -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<EquivalenceCheckingManager>(
// qiskit_to_mqt(circ1).cast<qc::QuantumComputation>(),
// qiskit_to_mqt(circ2).cast<qc::QuantumComputation>(),
// 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
Expand Down

0 comments on commit b9c6eac

Please sign in to comment.