Commit d6b7f65f authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Work on Python bindings



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 48982d39
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -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
+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;
@@ -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.");
  }
}
+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 &params) override {
    PYBIND11_OVERLOAD_PURE(bool, CostFunctionEvaluator, initialize);
  }
};
} // namespace qsim
} // namespace qcor