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

Work on runtime circuit optimization



Boilerplate to enable us to collect data about optimization runs.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent e70f27e2
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -100,6 +100,9 @@ using GradientEvaluator =
    std::function<void(std::vector<double> x, std::vector<double> &dx)>;

namespace __internal__ {
// !! TEMP CODE !!
// This will eventually be parsed from the CLI
#define __internal__qcor__compile__opt__level 1

// This class gives us a way to
// run some startup routine before
@@ -111,6 +114,9 @@ public:
  internal_startup() {
#ifdef __internal__qcor__compile__backend
    quantum::initialize(__internal__qcor__compile__backend, "empty");
#endif 
#ifdef __internal__qcor__compile__opt__level
  xacc::internal_compiler::__opt_level = __internal__qcor__compile__opt__level;
#endif
  }
};
+13 −0
Original line number Diff line number Diff line
#include "pass_manager.hpp"

namespace qcor {
namespace internal {
PassManager::PassManager(int level):
    m_level(level) {}

std::vector<PassStat> PassManager::optimize(std::shared_ptr<xacc::CompositeInstruction> program) const {
    // TODO
    return {};
}
}
}
 No newline at end of file
+44 −0
Original line number Diff line number Diff line
#pragma once
#include <unordered_map>
#include <vector>
#include <memory>

namespace xacc {
class CompositeInstruction;
}
namespace qcor {
namespace internal {
// Stats about an optimization pass:
struct PassStat
{
    // Name of the pass
    std::string passName;
    // Count per gate
    std::unordered_map<std::string, int> gateCountBefore;
    std::unordered_map<std::string, int> gateCountAfter;
    // Elapsed-time of this pass.
    double wallTimeMs;
};

class PassManager
{
public:
    PassManager(int level);
    // Optimizes the input program. 
    // Returns the full statistics about all the passes that have been executed.
    std::vector<PassStat> optimize(std::shared_ptr<xacc::CompositeInstruction> program) const;
    // List of passes for level 1:
    // Ordered list of passes to be executed. 
    // Can have duplicated entries (run multiple times).
    static const constexpr char* const LEVEL1_PASSES [] = { 
        "rotation-folding", 
        "circuit-optimizer",
        "swap-shortest-path"
    };
    // TODO: define other levels if neccesary:
    // e.g. could be looping those passes multiple times.
private: 
    int m_level;
};
}
}
 No newline at end of file
+4 −2
Original line number Diff line number Diff line
@@ -7,10 +7,12 @@
#include <Eigen/Dense>
#include <Utils.hpp>

std::vector<int> xacc::internal_compiler::__controlledIdx = {};

namespace xacc {
namespace internal_compiler {
// Extern vars:
int __opt_level = 0;
std::vector<int> __controlledIdx = {};

void simplified_qrt_call_one_qbit(const char *gate_name,
                                  const char *buffer_name,
                                  const std::size_t idx) {
+6 −0
Original line number Diff line number Diff line
@@ -104,6 +104,12 @@ namespace internal_compiler {
// Current runtime controlled bit indices
// (if the current kernel is wrapped in a Controlled block)
extern std::vector<int> __controlledIdx;
// Optimization level: parsed from command line input.
// Convention: 
// 0 : no optimization
// 1 : standard optimization (within reasonable walltime limit)
// 2 : extensive optimization (TBD)
extern int __opt_level;

void simplified_qrt_call_one_qbit(const char *gate_name,
                                  const char *buffer_name,