Commit b3e504c8 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding createOperator api calls to c++ and python

parent d110cb18
Loading
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -369,6 +369,39 @@ PYBIND11_MODULE(_pyqcor, m) {
      },
      "");

  m.def(
      "createOperator",
      [](const std::string &repr) { return qcor::createOperator(repr); }, "");
  m.def(
      "createOperator",
      [](const std::string &type, const std::string &repr) {
        return qcor::createOperator(type, repr);
      },
      "");
  m.def(
      "createOperator",
      [](const std::string &type, PyHeterogeneousMap &options) {
        auto nativeHetMap = heterogeneousMapConvert(options);
        return qcor::createOperator(type, nativeHetMap);
      },
      "");
  m.def(
      "createObservable",
      [](const std::string &repr) { return qcor::createOperator(repr); }, "");
  m.def(
      "createObservable",
      [](const std::string &type, const std::string &repr) {
        return qcor::createOperator(type, repr);
      },
      "");
  m.def(
      "createObservable",
      [](const std::string &type, PyHeterogeneousMap &options) {
        auto nativeHetMap = heterogeneousMapConvert(options);
        return qcor::createOperator(type, nativeHetMap);
      },
      "");

  m.def(
      "internal_observe",
      [](std::shared_ptr<CompositeInstruction> kernel,
+5 −1
Original line number Diff line number Diff line
import unittest
from qcor import *

class TestVQEObjectiveFunction(unittest.TestCase):
class TestQCORSpecAPI(unittest.TestCase):
    def test_simple_deuteron(self):

        @qjit
@@ -72,6 +72,10 @@ class TestVQEObjectiveFunction(unittest.TestCase):
        self.assertAlmostEqual(opt_val, 0.0, places=1)
        self.assertAlmostEqual(opt_params[0], .5, places=1)
    
    def test_operator(self):
        H = createOperator('-2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1 + 5.907')
        print(H)


if __name__ == '__main__':
    unittest.main()
+41 −7
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ PauliOperator operator-(PauliOperator &op, double coeff) {
  return -1.0 * coeff + op;
}


PauliOperator X(int idx) { return PauliOperator({{idx, "X"}}); }

PauliOperator Y(int idx) { return PauliOperator({{idx, "Y"}}); }
@@ -48,12 +47,47 @@ PauliOperator SM(int idx) {
std::shared_ptr<xacc::Observable> createObservable(const std::string &repr) {
  if (!xacc::isInitialized())
    xacc::internal_compiler::compiler_InitializeXACC();
  return xacc::quantum::getObservable("pauli", std::string(repr));
  return xacc::quantum::getObservable("pauli", repr);
}

std::shared_ptr<Observable> createObservable(const std::string &name,
                                             const std::string &repr) {
  if (!xacc::isInitialized())
    xacc::internal_compiler::compiler_InitializeXACC();
  return xacc::quantum::getObservable(name, repr);
}
std::shared_ptr<Observable> createObservable(const std::string &name,
                                             HeterogeneousMap &&options) {
  if (!xacc::isInitialized())
    xacc::internal_compiler::compiler_InitializeXACC();
  return xacc::quantum::getObservable(name, options);
}
std::shared_ptr<Observable> createObservable(const std::string &name,
                                             HeterogeneousMap &options) {
  if (!xacc::isInitialized())
    xacc::internal_compiler::compiler_InitializeXACC();
  return xacc::quantum::getObservable(name, options);
}

std::shared_ptr<Observable> createOperator(const std::string &repr) {
  return createObservable(repr);
}
std::shared_ptr<Observable> createOperator(const std::string &name,
                                           const std::string &repr) {
  return createObservable(name, repr);
}
std::shared_ptr<Observable> createOperator(const std::string &name,
                                           HeterogeneousMap &&options) {
  return createObservable(name, options);
}
std::shared_ptr<Observable> createOperator(const std::string &name,
                                           HeterogeneousMap &options) {
  return createObservable(name, options);
}

namespace __internal__ {
std::vector<std::shared_ptr<xacc::CompositeInstruction>>
observe(std::shared_ptr<xacc::Observable> obs,
std::vector<std::shared_ptr<xacc::CompositeInstruction>> observe(
    std::shared_ptr<xacc::Observable> obs,
    std::shared_ptr<CompositeInstruction> program) {
  return obs->observe(program);
}
+10 −0
Original line number Diff line number Diff line
@@ -98,4 +98,14 @@ observe(std::shared_ptr<Observable> obs,

// Create an observable from a string representation
std::shared_ptr<Observable> createObservable(const std::string &repr);
std::shared_ptr<Observable> createObservable(const std::string& name, const std::string &repr);
std::shared_ptr<Observable> createObservable(const std::string &name, HeterogeneousMap&& options);
std::shared_ptr<Observable> createObservable(const std::string &name, HeterogeneousMap& options);

std::shared_ptr<Observable> createOperator(const std::string &repr);
std::shared_ptr<Observable> createOperator(const std::string& name, const std::string &repr);
std::shared_ptr<Observable> createOperator(const std::string &name, HeterogeneousMap&& options);
std::shared_ptr<Observable> createOperator(const std::string &name, HeterogeneousMap& options);


} // namespace qcor
 No newline at end of file