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

Port adapt to QSim



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent c6875495
Loading
Loading
Loading
Loading
+40 −2
Original line number Diff line number Diff line
#include "qsim_utils.hpp"
#include "adapt.hpp"
#include "qsim_utils.hpp"
#include "xacc.hpp"

namespace qcor {
namespace qsim {
@@ -13,12 +14,49 @@ bool AdaptVqeWorkflow::initialize(const HeterogeneousMap &params) {
    optimizer = createOptimizer(DEFAULT_OPTIMIZER);
  }
  config_params = params;

  if (!params.stringExists("gradient_strategy")) {
    config_params.insert("gradient_strategy", "autodiff");
  }
  
  return (optimizer != nullptr);
}

QuantumSimulationResult
AdaptVqeWorkflow::execute(const QuantumSimulationModel &model) {
  return {};
  // Note: to make sure compatibility with XACC, we use the same
  // parameters for Adapt as XACC, just forward it to the XACC impl.
  
  // Some extra adapt params comming from the model.
  auto accelerator = xacc::internal_compiler::get_qpu();
  xacc::HeterogeneousMap extra_params{{"observable", xacc::as_shared_ptr(model.observable)},
                                      {"sub-algorithm", "vqe"},
                                      {"accelerator", accelerator},
                                      {"optimizer", optimizer}};

  // If the model contains an ansatz:
  if (model.user_defined_ansatz) {
    std::shared_ptr<xacc::CompositeInstruction> state_prep_circ =
        model.user_defined_ansatz->evaluate_kernel({});
    extra_params.insert("initial-state", state_prep_circ);
  }

  auto adapt_params = config_params;
  adapt_params.merge(extra_params);
  auto adapt = xacc::getAlgorithm("adapt", adapt_params);
  auto buffer = xacc::qalloc(model.observable->nBits());
  adapt->execute(buffer);
  auto opt_val = (*buffer)["opt-val"].as<double>();
  auto opt_params = (*buffer)["opt-params"].as<std::vector<double>>();
  auto opt_ansatz = (*buffer)["opt-ansatz"].as<std::vector<int>>();

  return {
      {"opt-val", opt_val},
      // Alias opt-val to energy as well
      {"energy", opt_val},
      {"opt-params", opt_params},
      {"opt-ansatz", opt_ansatz},
  };
}
} // namespace qsim
} // namespace qcor
 No newline at end of file
+42 −1
Original line number Diff line number Diff line
@@ -3,7 +3,48 @@
#include "xacc.hpp"
#include <gtest/gtest.h>

TEST(AdaptVqeWorkflowTester, checkSimple) {}
TEST(AdaptVqeWorkflowTester, checkSimple) {
  using namespace qcor;
  const int nElectrons = 2;
  const auto pool_vqe = "qubit-pool";
  const auto str = std::string(
      "(-0.165606823582,-0)  1^ 2^ 1 2 + (0.120200490713,0)  1^ 0^ 0 1 + "
      "(-0.0454063328691,-0)  0^ 3^ 1 2 + (0.168335986252,0)  2^ 0^ 0 2 + "
      "(0.0454063328691,0)  1^ 2^ 3 0 + (0.168335986252,0)  0^ 2^ 2 0 + "
      "(0.165606823582,0)  0^ 3^ 3 0 + (-0.0454063328691,-0)  3^ 0^ 2 1 + "
      "(-0.0454063328691,-0)  1^ 3^ 0 2 + (-0.0454063328691,-0)  3^ 1^ 2 0 + "
      "(0.165606823582,0)  1^ 2^ 2 1 + (-0.165606823582,-0)  0^ 3^ 0 3 + "
      "(-0.479677813134,-0)  3^ 3 + (-0.0454063328691,-0)  1^ 2^ 0 3 + "
      "(-0.174072892497,-0)  1^ 3^ 1 3 + (-0.0454063328691,-0)  0^ 2^ 1 3 + "
      "(0.120200490713,0)  0^ 1^ 1 0 + (0.0454063328691,0)  0^ 2^ 3 1 + "
      "(0.174072892497,0)  1^ 3^ 3 1 + (0.165606823582,0)  2^ 1^ 1 2 + "
      "(-0.0454063328691,-0)  2^ 1^ 3 0 + (-0.120200490713,-0)  2^ 3^ 2 3 + "
      "(0.120200490713,0)  2^ 3^ 3 2 + (-0.168335986252,-0)  0^ 2^ 0 2 + "
      "(0.120200490713,0)  3^ 2^ 2 3 + (-0.120200490713,-0)  3^ 2^ 3 2 + "
      "(0.0454063328691,0)  1^ 3^ 2 0 + (-1.2488468038,-0)  0^ 0 + "
      "(0.0454063328691,0)  3^ 1^ 0 2 + (-0.168335986252,-0)  2^ 0^ 2 0 + "
      "(0.165606823582,0)  3^ 0^ 0 3 + (-0.0454063328691,-0)  2^ 0^ 3 1 + "
      "(0.0454063328691,0)  2^ 0^ 1 3 + (-1.2488468038,-0)  2^ 2 + "
      "(0.0454063328691,0)  2^ 1^ 0 3 + (0.174072892497,0)  3^ 1^ 1 3 + "
      "(-0.479677813134,-0)  1^ 1 + (-0.174072892497,-0)  3^ 1^ 3 1 + "
      "(0.0454063328691,0)  3^ 0^ 1 2 + (-0.165606823582,-0)  3^ 0^ 3 0 + "
      "(0.0454063328691,0)  0^ 3^ 2 1 + (-0.165606823582,-0)  2^ 1^ 2 1 + "
      "(-0.120200490713,-0)  0^ 1^ 0 1 + (-0.120200490713,-0)  1^ 0^ 1 0 + "
      "(0.7080240981,0)");

  FermionOperator H_vqe;
  H_vqe.fromString(str);
  std::cout << H_vqe.toString() << "\n";
  xacc::internal_compiler::qpu = xacc::getAccelerator("qsim");
  auto problemModel = qsim::ModelBuilder::createModel(&H_vqe);
  auto optimizer = createOptimizer("nlopt", {{"nlopt-optimizer", "l-bfgs"}, {"stopval", -1.137}});
  auto workflow = qsim::getWorkflow("adapt", {{"optimizer", optimizer},
                                              {"pool", pool_vqe},
                                              {"n-electrons", nElectrons}});
  set_verbose(true);
  auto result = workflow->execute(problemModel);
  std::cout << "Final energy: " << result.get<double>("energy") << "\n";
}

int main(int argc, char **argv) {
  xacc::Initialize();