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

Code clean up



- Removed an unused class declaration.

- Added helper to pick observable evaluator in the workflow execute.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 52d9fbfa
Loading
Loading
Loading
Loading
+23 −7
Original line number Diff line number Diff line
@@ -2,6 +2,26 @@
#include "xacc.hpp"
#include "xacc_service.hpp"

namespace {
using namespace qcor;
std::shared_ptr<qsim::CostFunctionEvaluator>
getEvaluator(Observable *observable, const HeterogeneousMap &params) {
  // If an evaluator was provided explicitly:
  if (params.pointerLikeExists<qsim::CostFunctionEvaluator>("evaluator")) {
    return xacc::as_shared_ptr(
        params.getPointerLike<qsim::CostFunctionEvaluator>("evaluator"));
  }

  // Cost Evaluator was provided by name:
  if (params.stringExists("evaluator")) {
    return qsim::getObjEvaluator(observable, params.getString("evaluator"));
  }

  // No specific evaluator/evaluation method was requested,
  // use the default one (partial tomography based).
  return qsim::getObjEvaluator(observable);
}
} // namespace
namespace qcor {
namespace qsim {
Ansatz TrotterEvolution::create_ansatz(Observable *obs,
@@ -37,13 +57,14 @@ bool TimeDependentWorkflow::initialize(const HeterogeneousMap &params) {
  }

  t_final = nbSteps * dt;
  config_params = params;
  return true;
}

QuantumSimulationResult
TimeDependentWorkflow::execute(const QuantumSimulationModel &model) {
  QuantumSimulationResult result;
  evaluator = getObjEvaluator(model.observable);
  evaluator = getEvaluator(model.observable, config_params);
  auto ham_func = model.hamiltonian;
  // A TD workflow: stepping through Trotter steps,
  // compute expectations at each step.
@@ -100,12 +121,7 @@ VqeWorkflow::execute(const QuantumSimulationModel &model) {
  // If the model includes a concrete variational ansatz:
  if (model.user_defined_ansatz) {
    auto nParams = model.user_defined_ansatz->nParams();
    if (config_params.pointerLikeExists<CostFunctionEvaluator>("evaluator")) {
      evaluator = xacc::as_shared_ptr(
          config_params.getPointerLike<CostFunctionEvaluator>("evaluator"));
    } else {
      evaluator = getObjEvaluator(model.observable);
    }
    evaluator = getEvaluator(model.observable, config_params);

    OptFunction f(
        [&](const std::vector<double> &x, std::vector<double> &dx) {
+1 −16
Original line number Diff line number Diff line
@@ -25,22 +25,6 @@ public:
  virtual const std::string description() const override { return ""; }
};

// other methods.

// Estimate the cost function based on bitstring distribution,
// e.g. actual quantum hardware.
// Note: we can sub-class CostFunctionEvaluator to add post-processing or
// analysis of the result.
class BitCountExpectationEstimator : public CostFunctionEvaluator {
public:
  // Evaluate the cost
  virtual double
  evaluate(std::shared_ptr<CompositeInstruction> state_prep) override;

private:
  size_t nb_samples;
};

// VQE-type workflow which involves an optimization loop, i.e. an Optimizer.
class VqeWorkflow : public QuantumSimulationWorkflow {
public:
@@ -67,6 +51,7 @@ public:
  virtual const std::string description() const override { return ""; }

private:
  HeterogeneousMap config_params;
  double t_0;
  double t_final;
  double dt;