QCOR
pass_manager.hpp
1 #pragma once
2 #include <memory>
3 #include <unordered_map>
4 #include <vector>
5 #include <string>
6 #include "qcor_ir.hpp"
7 
8 // namespace xacc {
9 // class CompositeInstruction;
10 // }
11 namespace qcor {
12 namespace internal {
13 // Stats about an optimization pass:
14 struct PassStat {
15  // Name of the pass
16  std::string passName;
17  std::string kernelName;
18  // Count per gate
19  std::unordered_map<std::string, int> gateCountBefore;
20  std::unordered_map<std::string, int> gateCountAfter;
21  // Elapsed-time of this pass.
22  double wallTimeMs;
23  // Helper to collect stats.
24  static std::unordered_map<std::string, int>
25  countGates(const std::shared_ptr<CompositeInstruction> &program);
26  // Pretty printer.
27  std::string toString(bool shortForm = true) const;
28 };
29 
30 class PassManager {
31 public:
32  PassManager(int level, const std::vector<int> &qubitMap = {}, const std::string &placementName = "");
33  // Static helper to run an optimization pass
34  static PassStat runPass(const std::string &passName, std::shared_ptr<CompositeInstruction> program);
35  // Default placement strategy
36  static constexpr const char *DEFAULT_PLACEMENT = "swap-shortest-path";
37  // Apply placement
38  void applyPlacement(std::shared_ptr<CompositeInstruction> program) const;
39 
40  // Optimizes the input program.
41  // Returns the full statistics about all the passes that have been executed.
42  std::vector<PassStat>
43  optimize(std::shared_ptr<CompositeInstruction> program) const;
44  // List of passes for level 1:
45  // Ordered list of passes to be executed.
46  // Can have duplicated entries (run multiple times).
47  static const constexpr char *const LEVEL1_PASSES[] = {
48  "rotation-folding",
49  // Merge single-qubit gates before running the circuit-optimizer
50  // so that there are potentially more patterns emerged.
51  "single-qubit-gate-merging",
52  "circuit-optimizer",
53  };
54 
55  // Level 2 is experimental, brute-force optimization
56  // which could result in long runtime.
57  static const constexpr char *const LEVEL2_PASSES[] = {
58  "rotation-folding",
59  "single-qubit-gate-merging",
60  "circuit-optimizer",
61  // Try to look for any two-qubit blocks
62  // which can be simplified.
63  "two-qubit-block-merging",
64  // Re-run those simpler optimizers to
65  // make sure all simplification paterns are captured.
66  "single-qubit-gate-merging",
67  "circuit-optimizer",
68  };
69 private:
70  // Circuit optimization level
71  int m_level;
72  // Placement config.
73  std::vector<int> m_qubitMap;
74  std::string m_placement;
75 };
76 } // namespace internal
77 } // namespace qcor
qcor::internal::PassStat
Definition: pass_manager.hpp:14
qcor::internal::PassManager
Definition: pass_manager.hpp:30
qcor
Definition: qcor_syntax_handler.cpp:15