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

Clean up the qsim examples



- Renamed those example files to be more specific.

- Added qcor-qsim linkage to the qcor driver script.

- Added unit tests to compile those two example with qcor.

- Remove debug logging in the impl.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 7830b996
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -39,6 +39,6 @@ endif()
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
install(FILES ${HEADERS} DESTINATION include/qcor)

# if (QCOR_BUILD_TESTS)
# 	add_subdirectory(tests)
# endif()
 No newline at end of file
if (QCOR_BUILD_TESTS)
	add_subdirectory(tests)
endif()
 No newline at end of file
+4 −3
Original line number Diff line number Diff line
@@ -57,6 +57,8 @@ TimeDependentWorkflow::execute(const QuatumSimulationModel &model) {
    // Evaluate the time-dependent Hamiltonian:
    auto ham_t = ham_func(currentTime);
    auto stepAnsatz = method.create_ansatz(&ham_t, {{"dt", dt}});
    // std::cout << "t = " << currentTime << "\n";
    // std::cout << stepAnsatz.circuit->toString() << "\n";
    // First step:
    if (!totalCirc) {
      totalCirc = stepAnsatz.circuit;
@@ -64,10 +66,9 @@ TimeDependentWorkflow::execute(const QuatumSimulationModel &model) {
      // Append Trotter steps
      totalCirc->addInstructions(stepAnsatz.circuit->getInstructions());
    }

    // std::cout << totalCirc->toString() << "\n";
    // Evaluate the expectation after these Trotter steps:
    const double ham_expect = evaluator->evaluate(totalCirc);
    std::cout << "<Ham> = " << ham_expect << "\n";
    resultExpectationValues.emplace_back(ham_expect);

    currentTime += dt;
@@ -110,7 +111,7 @@ VqeWorkflow::execute(const QuatumSimulationModel &model) {
        nParams);

    auto result = optimizer->optimize(f);
    std::cout << "Min energy = " << result.first << "\n";
    // std::cout << "Min energy = " << result.first << "\n";
    return {{"energy", result.first}, {"opt-params", result.second}};
  }

+2 −5
Original line number Diff line number Diff line
file(GLOB SRC *.cpp)

add_executable(test-qsim ${SRC})
target_include_directories(test-qsim PUBLIC ../..)
target_link_libraries(test-qsim PUBLIC qcor qcor-qsim)
add_test(NAME qsim_vqe COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/VqeWithAnsatzCircuit.cpp)
add_test(NAME qsim_trotter COMMAND ${CMAKE_BINARY_DIR}/qcor ${CMAKE_CURRENT_SOURCE_DIR}/TrotterTdWorkflow.cpp)
+43 −0
Original line number Diff line number Diff line
#include "qcor_qsim.hpp"

// High-level usage of Model Builder
// High-level usage of Model Builder for time-dependent quantum simulation
// problem using Trotter method.

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

int main(int argc, char **argv) {
  // Define the Hamiltonian using the QCOR API
  // Note: we have support for high-level integration w/ Psi4, OpenFermion,
  // PySCF, etc.
  // Time-dependent Hamiltonian
  // Define the time-dependent Hamiltonian and observable operator using the
  // QCOR API Time-dependent Hamiltonian
  TdObservable H = [](double t) {
    // See Eq. (1): these are dummy params atm
    const double Jz = 1.0;
    const double epsilon = 1.0;
    const double omega = 1.0;
    // Parameters:
    const double Jz = 2 * M_PI * 2.86265 * 1e-3;
    const double epsilon = Jz; // Values: 0.2Jz, 0.5Jz, Jz, 5Jz
    const double omega = 4.8 * 2 * M_PI * 1e-3;
    return -Jz * Z(0) * Z(1) - Jz * Z(1) * Z(2) -
           epsilon * std::cos(omega * t) * (X(0) + X(1) + X(2));
  };
@@ -21,11 +25,19 @@ int main(int argc, char **argv) {
  // Example: build model and TD workflow for Fig. 2 of
  // https://journals.aps.org/prb/pdf/10.1103/PhysRevB.101.184305
  auto problemModel = ModelBuilder::createModel(&observable, H);
  // Trotter step = 3fs, number of steps = 100 -> end time = 300fs
  auto workflow = qcor::getWorkflow(
      "td-evolution", {{"method", "trotter"}, {"dt", 3.0}, {"steps", 100}});

  // Result should contain the observable expectation value along Trotter steps.
  auto result = workflow->execute(problemModel);
  // Get the observable values (average magnetization)
  const auto obsVals = result.get<std::vector<double>>("exp-vals");

  // Print out for debugging:
  for (const auto &val : obsVals) {
    std::cout << "<Magnetization> = " << val << "\n";
  }

  return 0;
}
+10 −1
Original line number Diff line number Diff line
#include "qcor_qsim.hpp"

// High-level usage of Model Builder for typical VQE problem.

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

// Define a fixed ansatz as a QCOR kernel
__qpu__ void ansatz(qreg q, double theta) {
  X(q[0]);
  auto exponent_op = X(0) * Y(1) - Y(0) * X(1);
@@ -16,11 +23,13 @@ int main(int argc, char **argv) {

  auto problemModel = ModelBuilder::createModel(ansatz, H.get(), q.size(), 1);
  auto optimizer = createOptimizer("nlopt");

  // Instantiate a VQE workflow with the nlopt optimizer
  auto workflow = qcor::getWorkflow("vqe", {{"optimizer", optimizer}});

  // Result should contain the observable expectation value along Trotter steps.
  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
Loading