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

Added gate merging to the list of passes



Tested by: printing out runtime optimization data.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent af67d2ed
Loading
Loading
Loading
Loading
+19 −9
Original line number Diff line number Diff line
@@ -45,21 +45,31 @@ PassManager::PassManager(int level) : m_level(level) {}

std::vector<PassStat> PassManager::optimize(
    std::shared_ptr<xacc::CompositeInstruction> program) const {

  // We only support level 1 atm.
  if (m_level != 1) {
    return {};
  std::vector<PassStat> passData;
  // Selects the list of passes based on the optimization level.
  const auto passesToRun = [&]() {
    if (m_level == 1) {
      return std::vector<std::string>(std::begin(LEVEL1_PASSES),
                                      std::end(LEVEL1_PASSES));
    } else if (m_level == 2) {
      return std::vector<std::string>(std::begin(LEVEL2_PASSES),
                                      std::end(LEVEL2_PASSES));
    }
    return std::vector<std::string>();
  }();

  std::vector<PassStat> passData;
  for (const auto &passName : LEVEL1_PASSES) {
  for (const auto &passName : passesToRun) {
    PassStat stat;
    stat.passName = passName;
    // Counts gate before:
    stat.gateCountBefore = PassStat::countGates(program);
    xacc::ScopeTimer timer(passName, false);
    auto xaccOptTransform = xacc::getService<xacc::IRTransformation>(passName);
    auto xaccOptTransform =
        xacc::getService<xacc::IRTransformation>(passName, false);
    // Graciously ignores passes which cannot be located.
    if (xaccOptTransform) {
      xaccOptTransform->apply(program, nullptr);
    }
    // Stores the elapsed time.
    stat.wallTimeMs = timer.getDurationMs();
    // Counts gate after:
+22 −4
Original line number Diff line number Diff line
@@ -34,10 +34,28 @@ public:
  // 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"};
  // TODO: define other levels if neccesary:
  // e.g. could be looping those passes multiple times.
  static const constexpr char *const LEVEL1_PASSES[] = {
    "rotation-folding",
    // Merge single-qubit gates before running the circuit-optimizer
    // so that there are potentially more patterns emerged.
    "single-qubit-gate-merging",
    "circuit-optimizer",
  };

  // Level 2 is experimental, brute-force optimization
  // which could result in long runtime.
  static const constexpr char *const LEVEL2_PASSES[] = {
    "rotation-folding",
    "single-qubit-gate-merging",
    "circuit-optimizer",
    // Try to look for any two-qubit blocks
    // which can be simplified.
    "two-qubit-block-merging",
    // Re-run those simpler optimizers to 
    // make sure all simplification paterns are captured.
    "single-qubit-gate-merging",
    "circuit-optimizer",
  };
private:
  int m_level;
};