Commit ce9c1b2d authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

fix raw pointer that wasn't deleted in quasimo

parent 7bc50bf3
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4,4 +4,4 @@ run git clone --recursive https://github.com/eclipse/xacc && cd xacc && mkdir bu
    && make -j$(nproc) install 
run cd ../../ && git clone -b master https://github.com/ornl-qci/qcor && cd qcor && mkdir build && cd build \
    && cmake .. -DXACC_DIR=~/.xacc -DLLVM_ROOT=/usr/local/aideqc/llvm -DMLIR_DIR=/usr/local/aideqc/llvm/lib/cmake/mlir -DQCOR_BUILD_TESTS=TRUE \
    && make -j$(nproc) install && ctest --output-on-failure
 No newline at end of file
    && make -j$(nproc) install && ctest -j4 --output-on-failure
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
@@ -91,6 +91,9 @@ ModelFactory::createModel(ModelType type, const HeterogeneousMap &params) {
      }

      model_observable = observable;

      // FIXME workaround to handle above raw pointer.
      model.owns_observable = true;
    }

    assert(model_observable);
+74 −47
Original line number Diff line number Diff line
#pragma once
#include <memory>
#include <qalloc>
#include <xacc_internal_compiler.hpp>

#include "Accelerator.hpp"
#include "AcceleratorBuffer.hpp"
#include "Circuit.hpp"
@@ -9,9 +13,6 @@
#include "qcor_utils.hpp"
#include "qrt.hpp"
#include "quantum_kernel.hpp"
#include <memory>
#include <qalloc>
#include <xacc_internal_compiler.hpp>

using CompositeInstruction = xacc::CompositeInstruction;
using Accelerator = xacc::Accelerator;
@@ -49,7 +50,8 @@ public:
  // Evaluate the cost
  virtual double evaluate(std::shared_ptr<CompositeInstruction> state_prep) = 0;
  // Batching evaluation: observing multiple kernels in batches.
  // E.g. for non-vqe cases (Trotter), we have all kernels ready for observable evaluation
  // E.g. for non-vqe cases (Trotter), we have all kernels ready for observable
  // evaluation
  virtual std::vector<double> evaluate(
      std::vector<std::shared_ptr<CompositeInstruction>> state_prep_circuits) {
    // Default is one-by-one, subclass to provide batching if supported.
@@ -78,6 +80,18 @@ using TdObservable = std::function<Operator(double)>;

struct QuantumSimulationModel {
  QuantumSimulationModel() : observable(nullptr) {}
  // Copy Constructor
  QuantumSimulationModel(QuantumSimulationModel &other)
      : name(other.name),
        observable(other.observable),
        hamiltonian(other.hamiltonian),
        user_defined_ansatz(other.user_defined_ansatz),
        owns_observable(other.owns_observable) {
    if (other.owns_observable) {
      // Transfer ownership.
      other.owns_observable = false;
    }
  }
  // Model name.
  std::string name;

@@ -90,6 +104,20 @@ struct QuantumSimulationModel {

  // QuantumSimulationModel also support a user-defined (fixed) ansatz.
  std::shared_ptr<KernelFunctor> user_defined_ansatz;

  // clients can create raw operator pointers, and
  // provided them to this Model and indicate that
  // the Model owns the observable, and will delete it
  // upon destruction
  bool owns_observable = false;

  // Destructor, delete observable if we own it
  ~QuantumSimulationModel() {
    if (owns_observable) {
      printf("Deleting the raw ptr\n");
      delete observable;
    }
  }
};

// Generic model builder (factory)
@@ -119,8 +147,10 @@ public:
    void fromDict(const HeterogeneousMap &params);

    bool validateModel() const {
      const bool ext_dir_valid = (ext_dir == "X" || ext_dir == "Y" || ext_dir == "Z");
      const bool initial_spins_valid = (initial_spins.empty() || (initial_spins.size() == num_spins));
      const bool ext_dir_valid =
          (ext_dir == "X" || ext_dir == "Y" || ext_dir == "Z");
      const bool initial_spins_valid =
          (initial_spins.empty() || (initial_spins.size() == num_spins));
      return ext_dir_valid && initial_spins_valid;
    }
  };
@@ -129,10 +159,10 @@ public:
  // Strongly-typed parameters/argument.
  // Build a simple Hamiltonian-based model: static Hamiltonian which is also
  // the observable of interest.
  static QuantumSimulationModel createModel(Operator *obs,
                                           const HeterogeneousMap &params = {});
  static QuantumSimulationModel
  createModel(Operator &obs, const HeterogeneousMap &params = {}) {
  static QuantumSimulationModel createModel(
      Operator *obs, const HeterogeneousMap &params = {});
  static QuantumSimulationModel createModel(
      Operator &obs, const HeterogeneousMap &params = {}) {
    return createModel(&obs, params);
  }

@@ -140,12 +170,11 @@ public:
  //  -  obs: observable operator to measure.
  //  -  td_ham: time-dependent Hamiltonian to evolve the system.
  //     e.g. a function to map from time to Hamiltonian operator.
  static QuantumSimulationModel createModel(Operator *obs, TdObservable td_ham,
                                           const HeterogeneousMap &params = {});
  static QuantumSimulationModel createModel(
      Operator *obs, TdObservable td_ham, const HeterogeneousMap &params = {});
  // Pauli operator overload:
  static QuantumSimulationModel
  createModel(Operator &obs, TdObservable td_ham,
              const HeterogeneousMap &params = {}) {
  static QuantumSimulationModel createModel(
      Operator &obs, TdObservable td_ham, const HeterogeneousMap &params = {}) {
    return createModel(&obs, td_ham, params);
  }

@@ -158,15 +187,15 @@ public:
  //  - params: extra parameters to pass to the parser/generator,
  //            e.g. any transformations required in order to generate the
  //            Observable.
  static QuantumSimulationModel createModel(const std::string &format,
                                           const std::string &data,
  static QuantumSimulationModel createModel(
      const std::string &format, const std::string &data,
      const HeterogeneousMap &params = {});
  // Predefined model type that we support intrinsically.
  enum class ModelType { Heisenberg };
  static QuantumSimulationModel createModel(ModelType type,
                                            const HeterogeneousMap &params);
  // ========== QuantumSimulationModel with a fixed (pre-defined) ansatz ========
  // The ansatz is provided as a QCOR kernel.
  // ========== QuantumSimulationModel with a fixed (pre-defined) ansatz
  // ======== The ansatz is provided as a QCOR kernel.
  template <typename... Args>
  static inline QuantumSimulationModel createModel(
      void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
@@ -190,18 +219,16 @@ public:
  }

  // Passing the state-preparation ansatz as a CompositeInstruction
  static inline QuantumSimulationModel
  createModel(std::shared_ptr<CompositeInstruction> composite,
              Operator *obs) {
  static inline QuantumSimulationModel createModel(
      std::shared_ptr<CompositeInstruction> composite, Operator *obs) {
    QuantumSimulationModel model;
    model.observable = obs;
    model.user_defined_ansatz = createKernelFunctor(composite);
    return model;
  }

  static inline QuantumSimulationModel
  createModel(std::shared_ptr<CompositeInstruction> composite,
              Operator &obs) {
  static inline QuantumSimulationModel createModel(
      std::shared_ptr<CompositeInstruction> composite, Operator &obs) {
    return createModel(composite, &obs);
  }
};
@@ -215,23 +242,23 @@ using QuantumSimulationResult = HeterogeneousMap;
class QuantumSimulationWorkflow : public Identifiable {
 public:
  virtual bool initialize(const HeterogeneousMap &params) = 0;
  virtual QuantumSimulationResult
  execute(const QuantumSimulationModel &model) = 0;
  virtual QuantumSimulationResult execute(
      const QuantumSimulationModel &model) = 0;

 protected:
  std::shared_ptr<CostFunctionEvaluator> evaluator;
};

// Get workflow by name:
std::shared_ptr<QuantumSimulationWorkflow>
getWorkflow(const std::string &name, const HeterogeneousMap &init_params);
std::shared_ptr<QuantumSimulationWorkflow> getWorkflow(
    const std::string &name, const HeterogeneousMap &init_params);

// Get the Obj (cost) function evaluator:
std::shared_ptr<CostFunctionEvaluator>
getObjEvaluator(Operator *observable, const std::string &name = "default",
std::shared_ptr<CostFunctionEvaluator> getObjEvaluator(
    Operator *observable, const std::string &name = "default",
    const HeterogeneousMap &init_params = {});
inline std::shared_ptr<CostFunctionEvaluator>
getObjEvaluator(Operator &obs, const std::string &name = "default",
inline std::shared_ptr<CostFunctionEvaluator> getObjEvaluator(
    Operator &obs, const std::string &name = "default",
    const HeterogeneousMap &init_params = {}) {
  return getObjEvaluator(&obs, name, init_params);
}
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
#include "xacc.hpp"
#include "xacc_service.hpp"
#include <gtest/gtest.h>
#include <fstream>

// These tests take quite some time, hence just run one at a time...
// Default for CI: only run the simple test