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

Added a simple opt loop to the example



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 549364a8
Loading
Loading
Loading
Loading
+18 −10
Original line number Diff line number Diff line
@@ -15,15 +15,23 @@ int main(int argc, char **argv) {
  // Hamiltonian
  auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) -
           6.125 * Z(1);

  const auto angles = xacc::linspace(-xacc::constants::pi, xacc::constants::pi, 20);
  for (const auto& angle: angles)
  {
    // Ansatz at a specific angle
  // TODO:
  // We can hook this up to the QCOR's Objective function
  // The amount of code to manually instantiate the optimization loop here is
  // also very minimal.
  auto optimizer = createOptimizer("nlopt");
  xacc::OptFunction f(
      [&](const std::vector<double> &x, std::vector<double> &dx) {
        const double angle = x[0];
        auto statePrep =
            std::function<void(qreg)>{[angle](qreg q) { ansatz(q, angle); }};
        double energy = 0.0;
        EstimateEnergy(q, statePrep, H, 1024, energy);
        std::cout << "Energy(" << angle << ") = " << energy << "\n";
  }
        return energy;
      },
      1);

  auto result = optimizer->optimize(f);
  std::cout << "Min Energy = " << result.first << "\n";
}