Commit 9f9deb70 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

fixed up qcor tests, fixed bug in adapt qcor_hybrid

parent 92e65aeb
Loading
Loading
Loading
Loading
Loading
+6 −9
Original line number Diff line number Diff line
#include "qcor_hybrid.hpp"
#include "qcor_utils.hpp"
#include <iomanip>

// Define the state preparation kernel
@@ -24,15 +25,11 @@ int main() {

  // Create ADAPT-VQE instance
  // Run H2 with the singlet-adapted-uccsd pool
  int nElectrons = 2;
  std::string pool = "singlet-adapted-uccsd";
  std::string subAlgo = "vqe";
  std::string gradStrategy = "central-difference-gradient";
  qcor::ADAPT adapt(initial_state, H, optimizer,
                    {{"sub-algorithm", subAlgo},
                     {"pool", pool},
                     {"n-electrons", nElectrons},
                     {"gradient_strategy", gradStrategy}});
  ADAPT adapt(initial_state, H, optimizer,
                    {{"sub-algorithm", "vqe"},
                     {"pool", "singlet-adapted-uccsd"},
                     {"n-electrons", 2},
                     {"gradient_strategy", "central"}});

  // Execute!
  auto energy = adapt.execute();
+1 −1
Original line number Diff line number Diff line
@@ -466,7 +466,7 @@ public:
    auto accelerator = xacc::internal_compiler::get_qpu();

    options.insert("initial-state", parent_composite);
    options.insert("observable", &observable);
    options.insert("observable", __internal__::qcor_as_shared(&observable));
    options.insert("optimizer", optimizer);
    options.insert("accelerator", accelerator);

+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ install(FILES ${HEADERS} DESTINATION include/qcor)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)

if (QCOR_BUILD_TESTS)
  #add_subdirectory(tests)
  add_subdirectory(tests)
endif()

add_subdirectory(objectives)
+0 −5
Original line number Diff line number Diff line
link_directories(${XACC_ROOT}/lib)
add_executable(VQEObjTester VQETester.cpp)
add_test(NAME qcor_VQEObjTester COMMAND VQEObjTester)
target_include_directories(VQEObjTester PRIVATE ${XACC_ROOT}/include/gtest)
target_link_libraries(VQEObjTester ${XACC_TEST_LIBRARIES} qcor)
+0 −68
Original line number Diff line number Diff line
#include "qcor.hpp"
#include "xacc_internal_compiler.hpp"
#include "xacc_quantum_gate_api.hpp"
#include "xacc_service.hpp"
#include <gtest/gtest.h>

using namespace xacc;
const std::string rucc = R"rucc(__qpu__ void f(qbit q, double t0) {
    X(q[0]);
    X(q[1]);
    Rx(q[0],1.5707);
    H(q[1]);
    H(q[2]);
    H(q[3]);
    CNOT(q[0],q[1]);
    CNOT(q[1],q[2]);
    CNOT(q[2],q[3]);
    Rz(q[3], t0);
    CNOT(q[2],q[3]);
    CNOT(q[1],q[2]);
    CNOT(q[0],q[1]);
    Rx(q[0],-1.5707);
    H(q[1]);
    H(q[2]);
    H(q[3]);
})rucc";

TEST(VQETester, checkSimple) {

  xacc::internal_compiler::compiler_InitializeXACC("qpp");

  auto buffer = qalloc(4);

  auto ruccsd = xacc::getService<xacc::Compiler>("xasm")
                    ->compile(rucc, nullptr)
                    ->getComposite("f");

  auto optimizer = qcor::createOptimizer("nlopt");
  std::shared_ptr<Observable> observable = xacc::quantum::getObservable(
      "pauli",
      std::string(
          "(0.174073,0) Z2 Z3 + (0.1202,0) Z1 Z3 + (0.165607,0) Z1 Z2 + "
          "(0.165607,0) Z0 Z3 + (0.1202,0) Z0 Z2 + (-0.0454063,0) Y0 Y1 X2 X3 "
          "+ "
          "(-0.220041,0) Z3 + (-0.106477,0) + (0.17028,0) Z0 + (-0.220041,0) "
          "Z2 "
          "+ (0.17028,0) Z1 + (-0.0454063,0) X0 X1 Y2 Y3 + (0.0454063,0) X0 Y1 "
          "Y2 X3 + (0.168336,0) Z0 Z1 + (0.0454063,0) Y0 X1 X2 Y3"));

  auto vqe = xacc::getService<qcor::ObjectiveFunction>("vqe");
  vqe->initialize(observable.get(), ruccsd);
  vqe->set_qreg(buffer);

  qcor::OptFunction f(
      [&](const std::vector<double> x, std::vector<double> &grad) {
        return (*vqe)(buffer, x[0]);
      },
      1);
  auto results = optimizer->optimize(f);

  EXPECT_NEAR(-1.13717, results.first, 1e-4);
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  return ret;
}
Loading