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

Added cpp and python examples for qsim qaoa and qite workflows



Added an overload for PauliOp-only model.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 3b5d482b
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 −0
Original line number Diff line number Diff line
@@ -14,6 +14,10 @@ Employing the QCOR just-in-time (qjit) compilation features, we can wrap QCOR in

`qsim_vqe.py` qsim example of VQE routine for Deuteron Hamiltonian.

`qsim_deuteron_qaoa.py` qsim example of QAOA routine for Deuteron Hamiltonian.

`qsim_qite_simple.py` qsim example of QITE (Quantum Imaginary Time Evolution) routine for a simple Hamiltonian.

`vqe_qcor_spec.py` example of VQE using `taskInitiate` for asynchronous execution of quantum-classical hybrid computations. 

`pyscf_qubit_tapering.py` example of using the QCOR `OperatorTransform`, specifically running [Qubit Tapering](https://arxiv.org/abs/1701.08213) followed by VQE using the QSim library.
+20 −0
Original line number Diff line number Diff line
from qcor import * 

# Solving deuteron problem using qsim QAOA

# Define the deuteron hamiltonian 
H = -2.1433 * X(0) * X(1) - 2.1433 * \
    Y(0) * Y(1) + .21829 * Z(0) - 6.125 * Z(1) + 5.907

problemModel = qsim.ModelBuilder.createModel(H)
      
# Create the NLOpt derivative free optimizer
optimizer = createOptimizer('nlopt')

# Create the QAOA workflow with p = 8 steps
workflow = qsim.getWorkflow('qaoa', {'optimizer': optimizer, 'steps': 8})

# Execute and print the result
result = workflow.execute(problemModel)
energy = result['energy']
print(energy)
 No newline at end of file
Loading