xacc.hpp 9.12 KB
Newer Older
1
2
3
4
5
6
/*******************************************************************************
 * Copyright (c) 2017 UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompanies this
 * distribution. The Eclipse Public License is available at
7
8
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
 *License is available at https://eclipse.org/org/documents/edl-v10.php
9
10
 *
 * Contributors:
11
12
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
13
14
15
#ifndef XACC_XACC_HPP_
#define XACC_XACC_HPP_

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"
Mccaskey, Alex's avatar
Mccaskey, Alex committed
23

24
25
#include "heterogeneous.hpp"

26
#include <memory>
27
28
29
30
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>

31
32
namespace xacc {

33
34
35
36
namespace constants {
static constexpr double pi = 3.141592653589793238;
}

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
48
49
50
extern std::string rootPathString;

extern std::map<std::string, std::shared_ptr<CompositeInstruction>>
    compilation_database;
51
52
extern std::map<std::string, std::shared_ptr<AcceleratorBuffer>>
    allocated_buffers;
53

54
55
56
57
// The qbit type is critical to qcor
// We want it to be a shared_ptr, but we
// need it to have an operator[]() exposed
// 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
63
  qbit(const int n)
      : AcceleratorBufferPtr(std::make_shared<xacc::AcceleratorBuffer>(n)) {}
64
  qbit(const AcceleratorBufferPtr &ab) : AcceleratorBufferPtr(ab) {}
65

66
67
68
  int operator[](const int &i) { return 0; }
  ExtraInfo operator[](const std::string &key) {
    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
78
79

void Initialize(std::vector<std::string> argv);
void Initialize();
bool isInitialized();
80
void PyInitialize(const std::string rootPath);
81
int getArgc();
82
char **getArgv();
83

84
85
void set_verbose(bool verbose);

86
const std::string getRootDirectory();
87
88
std::vector<std::string> getIncludePaths();

89
90
void setIsPyApi();

91
92
93
94
void addCommandLineOption(const std::string &optionName,
                          const std::string &optionDescription = "");
void addCommandLineOptions(const std::string &category,
                           const std::map<std::string, std::string> &options);
95
void addCommandLineOptions(const std::map<std::string, std::string> &options);
96

Mccaskey, Alex's avatar
Mccaskey, Alex committed
97
void setGlobalLoggerPredicate(MessagePredicate predicate);
98
99
void logToFile(bool enable);
void setLoggingLevel(int level);
100
int getLoggingLevel();
101
102
void subscribeLoggingLevel(LoggingLevelNotification callback);

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
void info(const std::string &msg,
          MessagePredicate predicate = std::function<bool(void)>([]() {
            return true;
          }));
void warning(const std::string &msg,
             MessagePredicate predicate = std::function<bool(void)>([]() {
               return true;
             }));
void debug(const std::string &msg,
           MessagePredicate predicate = std::function<bool(void)>([]() {
             return true;
           }));
void error(const std::string &msg,
           MessagePredicate predicate = std::function<bool(void)>([]() {
             return true;
           }));
119

120
void clearOptions();
121
122
123
124
bool optionExists(const std::string &optionKey);
const std::string getOption(const std::string &optionKey);
void setOption(const std::string &optionKey, const std::string &value);
void unsetOption(const std::string &optionKey);
Mccaskey, Alex's avatar
Mccaskey, Alex committed
125

126
std::shared_ptr<IRProvider> getIRProvider(const std::string &name);
127

128
void storeBuffer(std::shared_ptr<AcceleratorBuffer> buffer);
129
130
void storeBuffer(const std::string name,
                 std::shared_ptr<AcceleratorBuffer> buffer);
131
std::shared_ptr<AcceleratorBuffer> getBuffer(const std::string &name);
132
bool hasBuffer(const std::string &name);
Mccaskey, Alex's avatar
Mccaskey, Alex committed
133

134
void setAccelerator(const std::string &acceleratorName);
135
136
137
138
139
std::shared_ptr<Accelerator>
getAccelerator(const std::string &name, const HeterogeneousMap &params = {});
std::shared_ptr<Accelerator>
getAccelerator(const std::string &name, std::shared_ptr<Client> client,
               const HeterogeneousMap &params = {});
140
std::shared_ptr<Accelerator> getAccelerator();
141
142
143
144
std::shared_ptr<Accelerator>
getAcceleratorDecorator(const std::string &decorator,
                        std::shared_ptr<Accelerator> acc,
                        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
161
162
163
std::shared_ptr<Optimizer> getOptimizer(const std::string name,
                                        const HeterogeneousMap &opts);
std::shared_ptr<Optimizer> getOptimizer(const std::string name,
                                        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
172
173
template <typename T>
void appendCache(const std::string fileName, const std::string key, T &&param,
                 const std::string subdirectory = "") {
174
  auto rootPathStr = xacc::getRootDirectory();
175
176
177
178
179
180
181
182
183
184
  if (!subdirectory.empty()) {
    rootPathStr += "/" + subdirectory;
    if (!xacc::directoryExists(rootPathStr)) {
      auto status =
          mkdir(rootPathStr.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    }
  }
  // Check if file exists
  if (xacc::fileExists(rootPathStr + "/" + fileName)) {
    auto existingCache = getCache(fileName, subdirectory);
185
    existingCache.insert(key, param); 
186

187
    appendCache(fileName, existingCache, subdirectory);
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  } else {
    std::ofstream out(rootPathStr + "/" + fileName);
    std::stringstream s;
    AcceleratorBuffer b;
    b.useAsCache();

    HeterogeneousMap params(std::make_pair(key, param));
    std::map<std::string, ExtraInfo> einfo;
    HeterogenousMap2ExtraInfo h2ei(einfo);
    params.visit(h2ei);

    b.addExtraInfo(key, einfo[key]);

    b.print(s);

    out << s.str();
    out.close();
205
206
  }
}
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

template <typename T>
void appendCache(const std::string fileName, const std::string key, T &param,
                 const std::string subdirectory = "") {
  auto rootPathStr = xacc::getRootDirectory();
  if (!subdirectory.empty()) {
    rootPathStr += "/" + subdirectory;
    if (!xacc::directoryExists(rootPathStr)) {
      auto status =
          mkdir(rootPathStr.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    }
  }
  // Check if file exists
  if (xacc::fileExists(rootPathStr + "/" + fileName)) {
    auto existingCache = getCache(fileName, subdirectory);
222
    existingCache.insert(key, param);
223

224
    appendCache(fileName, existingCache, subdirectory);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  } else {
    std::ofstream out(rootPathStr + "/" + fileName);
    std::stringstream s;
    AcceleratorBuffer b;
    b.useAsCache();

    HeterogeneousMap params(std::make_pair(key, param));
    std::map<std::string, ExtraInfo> einfo;
    HeterogenousMap2ExtraInfo h2ei(einfo);
    params.visit(h2ei);

    b.addExtraInfo(key, einfo[key]);

    b.print(s);

    out << s.str();
    out.close();
  }
}
244
245

std::shared_ptr<IRTransformation> getIRTransformation(const std::string &name);
246

247
248
249
const std::string
translate(std::shared_ptr<CompositeInstruction> CompositeInstruction,
          const std::string toLanguage);
250

251
252
void appendCompiled(std::shared_ptr<CompositeInstruction> composite,
                    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 {
258
259
260
261
262
263
264
265
class ExternalLanguagePluginLoader : public Identifiable {
public:
  virtual bool load() = 0;
  virtual bool unload() = 0;
};
void load_external_language_plugins();
void unload_external_language_plugins();
} // namespace external
266

267
namespace ir {
268
269
270
271
272
std::shared_ptr<CompositeInstruction>
asComposite(std::shared_ptr<Instruction> inst);
std::shared_ptr<Instruction>
asInstruction(std::shared_ptr<CompositeInstruction> comp);
} // namespace ir
273
274
void Finalize();

275
} // namespace xacc
276

277
#endif