XACC
xacc.hpp
1 /*******************************************************************************
2  * Copyright (c) 2017 UT-Battelle, LLC.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and Eclipse Distribution License v1.0 which accompanies this
6  * distribution. The Eclipse Public License is available at
7  * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
8  *License is available at https://eclipse.org/org/documents/edl-v10.php
9  *
10  * Contributors:
11  * Alexander J. McCaskey - initial API and implementation
12  *******************************************************************************/
13 #ifndef XACC_XACC_HPP_
14 #define XACC_XACC_HPP_
15 
16 #include "Compiler.hpp"
17 #include "RemoteAccelerator.hpp"
18 #include "IRProvider.hpp"
19 
20 #include "Algorithm.hpp"
21 #include "Optimizer.hpp"
22 #include "IRTransformation.hpp"
23 
24 #include "heterogeneous.hpp"
25 
26 #include <memory>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fstream>
30 
31 namespace xacc {
32 
33 namespace constants {
34 static constexpr double pi = 3.141592653589793238;
35 }
36 
37 extern bool xaccFrameworkInitialized;
38 extern bool isPyApi;
39 extern bool verbose;
40 
41 class CLIParser;
42 extern std::shared_ptr<CLIParser> xaccCLParser;
43 
44 extern int argc;
45 extern char **argv;
46 
47 extern std::string rootPathString;
48 
49 extern std::map<std::string, std::shared_ptr<CompositeInstruction>>
50  compilation_database;
51 extern std::map<std::string, std::shared_ptr<AcceleratorBuffer>>
52  allocated_buffers;
53 
54 // The qbit type is critical to qcor
55 // We want it to be a shared_ptr, but we
56 // need it to have an operator[]() exposed
57 // so we can do things like H(q[0])
58 using AcceleratorBufferPtr = std::shared_ptr<xacc::AcceleratorBuffer>;
59 class qbit : public AcceleratorBufferPtr {
60 public:
61  qbit() : AcceleratorBufferPtr(std::make_shared<xacc::AcceleratorBuffer>()) {}
62  qbit(const int n)
63  : AcceleratorBufferPtr(std::make_shared<xacc::AcceleratorBuffer>(n)) {}
64  qbit(const AcceleratorBufferPtr &ab) : AcceleratorBufferPtr(ab) {}
65 
66  int operator[](const int &i) { return 0; }
67  ExtraInfo operator[](const std::string &key) {
68  return AcceleratorBufferPtr::get()->getInformation(key);
69  }
70  qbit &operator=(qbit &q) { return q; }
71 };
72 qbit qalloc(const int n);
73 qbit qalloc();
74 
75 void Initialize(int argc, char **argv);
76 
77 void Initialize(std::vector<std::string> argv);
78 void Initialize();
79 bool isInitialized();
80 void PyInitialize(const std::string rootPath);
81 int getArgc();
82 char **getArgv();
83 
84 void set_verbose(bool verbose);
85 
86 const std::string getRootDirectory();
87 std::vector<std::string> getIncludePaths();
88 
89 void setIsPyApi();
90 
91 void addCommandLineOption(const std::string &optionName,
92  const std::string &optionDescription = "");
93 void addCommandLineOptions(const std::string &category,
94  const std::map<std::string, std::string> &options);
95 void addCommandLineOptions(const std::map<std::string, std::string> &options);
96 
97 void setGlobalLoggerPredicate(MessagePredicate predicate);
98 void logToFile(bool enable);
99 void setLoggingLevel(int level);
100 int getLoggingLevel();
101 void subscribeLoggingLevel(LoggingLevelNotification callback);
102 
103 void info(const std::string &msg,
104  MessagePredicate predicate = std::function<bool(void)>([]() {
105  return true;
106  }));
107 void warning(const std::string &msg,
108  MessagePredicate predicate = std::function<bool(void)>([]() {
109  return true;
110  }));
111 void debug(const std::string &msg,
112  MessagePredicate predicate = std::function<bool(void)>([]() {
113  return true;
114  }));
115 void error(const std::string &msg,
116  MessagePredicate predicate = std::function<bool(void)>([]() {
117  return true;
118  }));
119 
120 void clearOptions();
121 bool optionExists(const std::string &optionKey);
122 const std::string getOption(const std::string &optionKey);
123 void setOption(const std::string &optionKey, const std::string &value);
124 void unsetOption(const std::string &optionKey);
125 
126 std::shared_ptr<IRProvider> getIRProvider(const std::string &name);
127 
128 void storeBuffer(std::shared_ptr<AcceleratorBuffer> buffer);
129 void storeBuffer(const std::string name,
130  std::shared_ptr<AcceleratorBuffer> buffer);
131 std::shared_ptr<AcceleratorBuffer> getBuffer(const std::string &name);
132 bool hasBuffer(const std::string &name);
133 
134 void setAccelerator(const std::string &acceleratorName);
135 std::shared_ptr<Accelerator>
136 getAccelerator(const std::string &name, const HeterogeneousMap &params = {});
137 std::shared_ptr<Accelerator>
138 getAccelerator(const std::string &name, std::shared_ptr<Client> client,
139  const HeterogeneousMap &params = {});
140 std::shared_ptr<Accelerator> getAccelerator();
141 std::shared_ptr<Accelerator>
142 getAcceleratorDecorator(const std::string &decorator,
143  std::shared_ptr<Accelerator> acc,
144  const HeterogeneousMap &params = {});
145 
146 bool hasAccelerator(const std::string &name);
147 
148 void setCompiler(const std::string &compilerName);
149 std::shared_ptr<Compiler> getCompiler(const std::string &name);
150 std::shared_ptr<Compiler> getCompiler();
151 bool hasCompiler(const std::string &name);
152 
153 std::shared_ptr<Algorithm> getAlgorithm(const std::string name,
154  const xacc::HeterogeneousMap &params);
155 std::shared_ptr<Algorithm> getAlgorithm(const std::string name,
156  const xacc::HeterogeneousMap &&params);
157 std::shared_ptr<Algorithm> getAlgorithm(const std::string name);
158 
159 std::shared_ptr<Optimizer> getOptimizer(const std::string name);
160 std::shared_ptr<Optimizer> getOptimizer(const std::string name,
161  const HeterogeneousMap &opts);
162 std::shared_ptr<Optimizer> getOptimizer(const std::string name,
163  const HeterogeneousMap &&opts);
164 
165 bool hasCache(const std::string fileName, const std::string subdirectory = "");
166 
167 HeterogeneousMap getCache(const std::string fileName,
168  const std::string subdirectory = "");
169 void appendCache(const std::string fileName, HeterogeneousMap &params,
170  const std::string subdirectory = "");
171 template <typename T>
172 void appendCache(const std::string fileName, const std::string key, T &&param,
173  const std::string subdirectory = "") {
174  auto rootPathStr = xacc::getRootDirectory();
175  if (!subdirectory.empty()) {
176  rootPathStr += "/" + subdirectory;
177  if (!xacc::directoryExists(rootPathStr)) {
178  auto status =
179  mkdir(rootPathStr.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
180  }
181  }
182  // Check if file exists
183  if (xacc::fileExists(rootPathStr + "/" + fileName)) {
184  auto existingCache = getCache(fileName, subdirectory);
185  existingCache.insert(key, param);
186 
187  appendCache(fileName, existingCache, subdirectory);
188  } else {
189  std::ofstream out(rootPathStr + "/" + fileName);
190  std::stringstream s;
192  b.useAsCache();
193 
194  HeterogeneousMap params(std::make_pair(key, param));
195  std::map<std::string, ExtraInfo> einfo;
196  HeterogenousMap2ExtraInfo h2ei(einfo);
197  params.visit(h2ei);
198 
199  b.addExtraInfo(key, einfo[key]);
200 
201  b.print(s);
202 
203  out << s.str();
204  out.close();
205  }
206 }
207 
208 template <typename T>
209 void appendCache(const std::string fileName, const std::string key, T &param,
210  const std::string subdirectory = "") {
211  auto rootPathStr = xacc::getRootDirectory();
212  if (!subdirectory.empty()) {
213  rootPathStr += "/" + subdirectory;
214  if (!xacc::directoryExists(rootPathStr)) {
215  auto status =
216  mkdir(rootPathStr.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
217  }
218  }
219  // Check if file exists
220  if (xacc::fileExists(rootPathStr + "/" + fileName)) {
221  auto existingCache = getCache(fileName, subdirectory);
222  existingCache.insert(key, param);
223 
224  appendCache(fileName, existingCache, subdirectory);
225  } else {
226  std::ofstream out(rootPathStr + "/" + fileName);
227  std::stringstream s;
229  b.useAsCache();
230 
231  HeterogeneousMap params(std::make_pair(key, param));
232  std::map<std::string, ExtraInfo> einfo;
233  HeterogenousMap2ExtraInfo h2ei(einfo);
234  params.visit(h2ei);
235 
236  b.addExtraInfo(key, einfo[key]);
237 
238  b.print(s);
239 
240  out << s.str();
241  out.close();
242  }
243 }
244 
245 std::shared_ptr<IRTransformation> getIRTransformation(const std::string &name);
246 
247 const std::string
248 translate(std::shared_ptr<CompositeInstruction> CompositeInstruction,
249  const std::string toLanguage);
250 
251 void appendCompiled(std::shared_ptr<CompositeInstruction> composite,
252  bool _override = true);
253 std::shared_ptr<CompositeInstruction> getCompiled(const std::string name);
254 bool hasCompiled(const std::string name);
255 
256 void qasm(const std::string &qasmString);
257 namespace external {
259 public:
260  virtual bool load() = 0;
261  virtual bool unload() = 0;
262 };
263 void load_external_language_plugins();
264 void unload_external_language_plugins();
265 } // namespace external
266 
267 namespace ir {
268 std::shared_ptr<CompositeInstruction>
269 asComposite(std::shared_ptr<Instruction> inst);
270 std::shared_ptr<Instruction>
271 asInstruction(std::shared_ptr<CompositeInstruction> comp);
272 } // namespace ir
273 void Finalize();
274 
275 } // namespace xacc
276 
277 #endif
Definition: Accelerator.hpp:25
Definition: AcceleratorBuffer.hpp:87
Definition: heterogeneous.hpp:45
Definition: Identifiable.hpp:25
Definition: heterogeneous.hpp:240
Definition: CompositeInstruction.hpp:72
Definition: AcceleratorBuffer.hpp:117
virtual void print()
Definition: AcceleratorBuffer.cpp:432
Definition: xacc.hpp:59