Loading python/examples/qsim_example.py +6 −8 Original line number Diff line number Diff line Loading @@ -3,17 +3,15 @@ from pathlib import Path sys.path.insert(1, str(Path.home()) + '/.xacc') from qcor import * import numpy as np # Time-dependent Hamiltonian: # Returns the Pauli operators at a time point. def td_hamiltonian(t): omega = 1.0 print("HOWDY: Python callback") return X(0) + X(1) + X(2) Jz = 2 * np.pi * 2.86265 * 1e-3 epsilon = Jz omega = 4.8 * 2 * np.pi * 1e-3 return -Jz * Z(0) * Z(1) - Jz * Z(1) * Z(2) + (-epsilon * np.cos(omega * t)) * (X(0) + X(1) + X(2)) # This is for testing-purposes only observable = X(0)*X(1) + Z(0) + Z(1) observable = (1.0 / 3.0) * (Z(0) + Z(1) + Z(2)) print("observable = ", observable.toString()) optimizer = createOptimizer('nlopt') model = qsim.ModelBuilder.createModel(observable, td_hamiltonian) No newline at end of file python/py-qcor.cpp +30 −0 Original line number Diff line number Diff line #include "base/qcor_qsim.hpp" #include "py_costFunctionEvaluator.hpp" #include <pybind11/functional.h> #include <pybind11/pybind11.h> namespace py = pybind11; Loading Loading @@ -46,5 +47,34 @@ PYBIND11_MODULE(_pyqcor, m) { return qcor::qsim::ModelBuilder::createModel(obs, ham_func); }, "Return the Model for a time-dependent problem."); // CostFunctionEvaluator bindings py::class_<qcor::qsim::CostFunctionEvaluator, std::shared_ptr<qcor::qsim::CostFunctionEvaluator>, qcor::qsim::PyCostFunctionEvaluator>( qsim, "CostFunctionEvaluator", "The CostFunctionEvaluator interface provides methods to " "evaluate the observable operator expectation value on quantum " "backends.") .def(py::init<>()) .def( "initialize", [](qcor::qsim::CostFunctionEvaluator &self, qcor::PauliOperator &obs) { return self.initialize(&obs); }, "Initialize the evaluator") .def( "evaluate", [](qcor::qsim::CostFunctionEvaluator &self, std::shared_ptr<CompositeInstruction> state_prep) -> double { return self.evaluate(state_prep); }, "Initialize the evaluator"); qsim.def( "getObjEvaluator", [](qcor::PauliOperator &obs, const std::string &name = "default", py::dict p = {}) { return qcor::qsim::getObjEvaluator(obs, name); }, py::arg("obs"), py::arg("name") = "default", py::arg("p") = py::dict(), py::return_value_policy::reference, "Return the CostFunctionEvaluator."); } } python/py_costFunctionEvaluator.hpp 0 → 100644 +32 −0 Original line number Diff line number Diff line #include "base/qcor_qsim.hpp" #include <memory> #include <pybind11/complex.h> #include <pybind11/eigen.h> #include <pybind11/functional.h> #include <pybind11/iostream.h> #include <pybind11/numpy.h> #include <pybind11/operators.h> #include <pybind11/stl.h> #include <pybind11/stl_bind.h> namespace py = pybind11; namespace qcor { namespace qsim { class PyCostFunctionEvaluator : public CostFunctionEvaluator { const std::string name() const override { PYBIND11_OVERLOAD_PURE(const std::string, CostFunctionEvaluator, name); } const std::string description() const override { PYBIND11_OVERLOAD_PURE(const std::string, CostFunctionEvaluator, description); } double evaluate(std::shared_ptr<CompositeInstruction> state_prep) override { PYBIND11_OVERLOAD_PURE(double, CostFunctionEvaluator, evaluate); } bool initialize(Observable *observable, const HeterogeneousMap ¶ms) override { PYBIND11_OVERLOAD_PURE(bool, CostFunctionEvaluator, initialize); } }; } // namespace qsim } // namespace qcor Loading
python/examples/qsim_example.py +6 −8 Original line number Diff line number Diff line Loading @@ -3,17 +3,15 @@ from pathlib import Path sys.path.insert(1, str(Path.home()) + '/.xacc') from qcor import * import numpy as np # Time-dependent Hamiltonian: # Returns the Pauli operators at a time point. def td_hamiltonian(t): omega = 1.0 print("HOWDY: Python callback") return X(0) + X(1) + X(2) Jz = 2 * np.pi * 2.86265 * 1e-3 epsilon = Jz omega = 4.8 * 2 * np.pi * 1e-3 return -Jz * Z(0) * Z(1) - Jz * Z(1) * Z(2) + (-epsilon * np.cos(omega * t)) * (X(0) + X(1) + X(2)) # This is for testing-purposes only observable = X(0)*X(1) + Z(0) + Z(1) observable = (1.0 / 3.0) * (Z(0) + Z(1) + Z(2)) print("observable = ", observable.toString()) optimizer = createOptimizer('nlopt') model = qsim.ModelBuilder.createModel(observable, td_hamiltonian) No newline at end of file
python/py-qcor.cpp +30 −0 Original line number Diff line number Diff line #include "base/qcor_qsim.hpp" #include "py_costFunctionEvaluator.hpp" #include <pybind11/functional.h> #include <pybind11/pybind11.h> namespace py = pybind11; Loading Loading @@ -46,5 +47,34 @@ PYBIND11_MODULE(_pyqcor, m) { return qcor::qsim::ModelBuilder::createModel(obs, ham_func); }, "Return the Model for a time-dependent problem."); // CostFunctionEvaluator bindings py::class_<qcor::qsim::CostFunctionEvaluator, std::shared_ptr<qcor::qsim::CostFunctionEvaluator>, qcor::qsim::PyCostFunctionEvaluator>( qsim, "CostFunctionEvaluator", "The CostFunctionEvaluator interface provides methods to " "evaluate the observable operator expectation value on quantum " "backends.") .def(py::init<>()) .def( "initialize", [](qcor::qsim::CostFunctionEvaluator &self, qcor::PauliOperator &obs) { return self.initialize(&obs); }, "Initialize the evaluator") .def( "evaluate", [](qcor::qsim::CostFunctionEvaluator &self, std::shared_ptr<CompositeInstruction> state_prep) -> double { return self.evaluate(state_prep); }, "Initialize the evaluator"); qsim.def( "getObjEvaluator", [](qcor::PauliOperator &obs, const std::string &name = "default", py::dict p = {}) { return qcor::qsim::getObjEvaluator(obs, name); }, py::arg("obs"), py::arg("name") = "default", py::arg("p") = py::dict(), py::return_value_policy::reference, "Return the CostFunctionEvaluator."); } }
python/py_costFunctionEvaluator.hpp 0 → 100644 +32 −0 Original line number Diff line number Diff line #include "base/qcor_qsim.hpp" #include <memory> #include <pybind11/complex.h> #include <pybind11/eigen.h> #include <pybind11/functional.h> #include <pybind11/iostream.h> #include <pybind11/numpy.h> #include <pybind11/operators.h> #include <pybind11/stl.h> #include <pybind11/stl_bind.h> namespace py = pybind11; namespace qcor { namespace qsim { class PyCostFunctionEvaluator : public CostFunctionEvaluator { const std::string name() const override { PYBIND11_OVERLOAD_PURE(const std::string, CostFunctionEvaluator, name); } const std::string description() const override { PYBIND11_OVERLOAD_PURE(const std::string, CostFunctionEvaluator, description); } double evaluate(std::shared_ptr<CompositeInstruction> state_prep) override { PYBIND11_OVERLOAD_PURE(double, CostFunctionEvaluator, evaluate); } bool initialize(Observable *observable, const HeterogeneousMap ¶ms) override { PYBIND11_OVERLOAD_PURE(bool, CostFunctionEvaluator, initialize); } }; } // namespace qsim } // namespace qcor