Commit 7b988df7 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Merge branch 'master' of https://github.com/ornl-qci/qcor

parents 070bb988 d1652729
Loading
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
#include "qcor_qsim.hpp"

// Demonstrate qsim's QAOA workflow 

// Compile and run with:
/// $ qcor -qpu qpp QaoaWorkflow.cpp
/// $ ./a.out

int main(int argc, char **argv) {
  // Create the Deuteron Hamiltonian
  auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.143 * Y(0) * Y(1) + 0.21829 * Z(0) -
           6.125 * Z(1);
  auto problemModel = qsim::ModelBuilder::createModel(H);
  auto optimizer = createOptimizer("nlopt");
  // Instantiate a QAOA workflow with the nlopt optimizer
  // "steps" = the (p) param in QAOA algorithm.
  auto workflow = qsim::getWorkflow("qaoa", {{"optimizer", optimizer}, {"steps", 8}});

  // Result should contain the ground-state energy along with the optimal
  // parameters.
  auto result = workflow->execute(problemModel);

  const auto energy = result.get<double>("energy");
  std::cout << "Ground-state energy = " << energy << "\n";
  return 0;
}
 No newline at end of file
+27 −0
Original line number Diff line number Diff line
#include "qcor_qsim.hpp"

// Demonstrate qsim's QITE workflow
// with a simple 1-qubit Hamiltonian from
// https://www.nature.com/articles/s41567-019-0704-4 Compile and run with:
/// $ qcor -qpu qpp QiteWorkflow.cpp
/// $ ./a.out

int main(int argc, char **argv) {
  auto ham = 0.7071067811865475 * X(0) + 0.7071067811865475 * Z(0);
  // Number of QITE time steps and step size
  const int nbSteps = 25;
  const double stepSize = 0.1;
  auto problemModel = qsim::ModelBuilder::createModel(ham);
  auto workflow =
      qsim::getWorkflow("qite", {{"steps", nbSteps}, {"step-size", stepSize}});
  auto result = workflow->execute(problemModel);
  const auto energy = result.get<double>("energy");
  const auto energyAtStep = result.get<std::vector<double>>("exp-vals");
  std::cout << "QITE energy: [ ";
  for (const auto &val : energyAtStep) {
    std::cout << val << " ";
  }
  std::cout << "]\n";
  std::cout << "Ground state energy: " << energy << "\n";
  return 0;
}
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
@@ -91,6 +91,11 @@ public:
  // the observable of interest.
  static QuantumSimulationModel createModel(Observable *obs,
                                           const HeterogeneousMap &params = {});
  static QuantumSimulationModel
  createModel(PauliOperator &obs, const HeterogeneousMap &params = {}) {
    return createModel(&obs, params);
  }
  
  // Build a time-dependent problem model:
  //  -  obs: observable operator to measure.
  //  -  td_ham: time-dependent Hamiltonian to evolve the system.
+4 −5
Original line number Diff line number Diff line
set(LIBRARY_NAME qcor-quantum-simulation)
file(GLOB SRC *.cpp modules/*.cpp)
file(GLOB HEADERS *.hpp)
file(GLOB SRC *.cpp ansatz_generator/*.cpp cost_evaluator/*.cpp utils/*.cpp workflow/*.cpp)

usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)
@@ -9,7 +8,7 @@ set(_bundle_name qcor_qsim)
add_library(${LIBRARY_NAME} SHARED ${SRC})

target_link_libraries(${LIBRARY_NAME} PUBLIC qcor qcor-qsim)
target_include_directories(${LIBRARY_NAME} PUBLIC . ../base ${XACC_ROOT}/include/eigen)
target_include_directories(${LIBRARY_NAME} PUBLIC . ./utils ../base ${XACC_ROOT}/include/eigen)
xacc_configure_library_rpath(${LIBRARY_NAME})

set_target_properties(${LIBRARY_NAME}
@@ -37,8 +36,8 @@ else()
endif()

install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
install(FILES ${HEADERS} DESTINATION include/qcor)

if (QCOR_BUILD_TESTS)
  add_subdirectory(modules/tests)
  add_subdirectory(cost_evaluator/tests)
  add_subdirectory(workflow/tests)
endif()
 No newline at end of file
+26 −0
Original line number Diff line number Diff line
#include "trotter.hpp"
#include "xacc_service.hpp"

namespace qcor {
namespace qsim {
Ansatz TrotterEvolution::create_ansatz(Observable *obs,
                                       const HeterogeneousMap &params) {
  Ansatz result;
  // This ansatz generator requires an observable.
  assert(obs != nullptr);
  double dt = 1.0;
  if (params.keyExists<double>("dt")) {
    dt = params.get<double>("dt");
  }
  // Just use exp_i_theta for now
  // TODO: formalize a standard library kernel for this.
  auto expCirc = std::dynamic_pointer_cast<xacc::quantum::Circuit>(
      xacc::getService<xacc::Instruction>("exp_i_theta"));
  expCirc->expand({{"pauli", obs->toString()}});
  result.circuit = expCirc->operator()({dt});
  result.nb_qubits = expCirc->nRequiredBits();

  return result;
}
} // namespace qsim
} // namespace qcor
 No newline at end of file
Loading