LambdaVisitorTester.in.cpp 11.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <gtest/gtest.h>
#include <llvm-9/llvm/Support/raw_ostream.h>

#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"

#include "clang/Lex/PreprocessorOptions.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"

#include "FuzzyParsingExternalSemaSource.hpp"
#include "LambdaVisitor.hpp"
#include "QCORASTConsumer.hpp"
#include "QCORPluginAction.hpp"

22
#include "CNOT.hpp"
23
24
25
#include "CountGatesOfTypeVisitor.hpp"
#include "Hadamard.hpp"
#include "Measure.hpp"
26
27
#include "XACC.hpp"
#include "xacc_service.hpp"
28

29
#include "IRGenerator.hpp"
30
31
32
33
34
35
36
37
38
39
40
#include "qcor.hpp"
#include "clang/Parse/ParseAST.h"

#include <fstream>
using namespace llvm;
using namespace clang;
using namespace qcor;

class TestQCORFrontendAction : public clang::ASTFrontendAction {

public:
41
  TestQCORFrontendAction(Rewriter &rw) : rewriter(rw) {}
42
43
44
45
46
47
48
49
50
51
52
53

protected:
  Rewriter &rewriter;
  std::unique_ptr<clang::ASTConsumer>
  CreateASTConsumer(clang::CompilerInstance &Compiler,
                    llvm::StringRef /* dummy */) override {
    return llvm::make_unique<compiler::QCORASTConsumer>(Compiler, rewriter);
  }

  void ExecuteAction() override {
    CompilerInstance &CI = getCompilerInstance();
    CI.createSema(getTranslationUnitKind(), nullptr);
54
55
56
    compiler::FuzzyParsingExternalSemaSource fuzzyParser(CI.getASTContext());
    CI.getSema().addExternalSource(&fuzzyParser);

57
58
59
60
61
62
    rewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());

    ParseAST(CI.getSema());
    CI.getDiagnosticClient().EndSourceFile();

    std::error_code error_code;
63
64
65
66
    llvm::raw_fd_ostream outFile(".output.cpp", error_code,
                                 llvm::sys::fs::F_None);
    rewriter.getEditBuffer(CI.getSourceManager().getMainFileID())
        .write(outFile);
67
68
69
70
71
  }
};

const std::string bell = R"bell(
int main() {
72
73
74
75
76
    auto l = []() {
        H(0);
        CX(0,1);
        Measure(0);
    };
77
78
79
80
    return 0;
})bell";
const std::string param0 = R"param0(
int main() {
81
82
83
84
85
    auto l = [&](double t) {
        X(0);
        Ry(t, 1);
        CX(1,0);
    };
86
87
    return 0;
})param0";
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const std::string unary0 = R"unary0(
int main() {
    auto l = [&](double t) {
        X(0);
        Ry(t, 1);
        CX(1,0);
        Rx(-1.57,1);
    };
    return 0;
})unary0";

const std::string rtimeCapture = R"rtimeCapture(
int main() {
    double pi = 3.1415;
    auto l = [&](double t) {
        X(0);
        Ry(t, 1);
        CX(1,0);
        Rx(pi,1);
        Rx(-pi,1);
    };
    return 0;
})rtimeCapture";
111
112
113
114

const std::string hwe0 = R"hwe0(#include <vector>
int main(int argc, char** argv){
    auto l = [&](std::vector<double> x) {
115
        hwe(x, {{"n-qubits",2},{"layers",1}});
116
117
118
119
120
121
    };
    return 0;
})hwe0";

const std::string hwe1 = R"hwe1(#include <vector>
int main(int argc, char** argv){
122
    int nq = 2;
123
    auto l = [&](std::vector<double> x) {
124
        hwe(x, {{"n-qubits", nq},{"layers",1}});
125
126
127
128
    };
    return 0;
})hwe1";

129
130
131
132
133
134
135
136
const std::string hwe2 = R"hwe2(#include <vector>
int main(int argc, char** argv){
    int nq = argc;
    auto l = [&](std::vector<double> x) {
        hwe(x, {{"n-qubits", nq},{"layers",1}});
    };
    return 0;
})hwe2";
137
138
139
140
141
142
143
144
145
const std::string hwe3 = R"hwe3(#include <vector>
int main(int argc, char** argv){
    int nq = argc;
    std::vector<std::pair<int,int>> c{{1,0}};
    auto l = [&](std::vector<double> x) {
        hwe(x, {{"n-qubits", nq},{"layers",1},{"coupling", c} });
    };
    return 0;
})hwe3";
146
147
148
149
150
151
152
153
154
155
156
157
158
159

const std::string hwe4 = R"hwe4(#include <vector>
int main(int argc, char** argv){
    int nq = argc;
    auto l = [&](std::vector<double> x) {
        hwe(x, {
            {"n-qubits", nq},
            {"layers",1},
            {"coupling", {{1,0}, {0,1}} },
            {"testVector", {1,2,3,4,5,6} },
            });
    };
    return 0;
})hwe4";
160
161
162
163
164
TEST(LambdaVisitorTester, checkSimple) {
  Rewriter rewriter1, rewriter2;
  auto action1 = new TestQCORFrontendAction(rewriter1);
  auto action2 = new TestQCORFrontendAction(rewriter2);

165
  xacc::setOption("qcor-compiled-filename", "lambda_visitor_tester");
166

167
  std::vector<std::string> args{"-std=c++11"};
168
169
170
171
172

  EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action1, bell, args));

  const std::string expectedSrc = R"expectedSrc(
int main() {
173
    auto l = [&](){return "lambda_visitor_tester";};
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    return 0;
})expectedSrc";
  std::ifstream t(".output.cpp");
  std::string src((std::istreambuf_iterator<char>(t)),
                  std::istreambuf_iterator<char>());
  std::remove(".output.cpp");

  EXPECT_EQ(expectedSrc, src);

  auto function = qcor::loadCompiledCircuit("lambda_visitor_tester");
  xacc::quantum::CountGatesOfTypeVisitor<xacc::quantum::Hadamard> h(function);
  xacc::quantum::CountGatesOfTypeVisitor<xacc::quantum::CNOT> cx(function);
  xacc::quantum::CountGatesOfTypeVisitor<xacc::quantum::Measure> m(function);

  EXPECT_EQ(1, h.countGates());
  EXPECT_EQ(1, cx.countGates());
  EXPECT_EQ(1, m.countGates());

192
  EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action2, param0, args));
193
194
195

  std::ifstream t2(".output.cpp");
  std::string src2((std::istreambuf_iterator<char>(t2)),
196
                   std::istreambuf_iterator<char>());
197
198
199
200
201
202
  std::remove(".output.cpp");

  EXPECT_EQ(expectedSrc, src2);
}

TEST(LambdaVisitorTester, checkGenerator) {
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    Rewriter rewriter1, rewriter2;
    auto action1 = new TestQCORFrontendAction(rewriter1);
    auto action2 = new TestQCORFrontendAction(rewriter2);

    xacc::setOption("qcor-compiled-filename", "lambda_visitor_tester");

    std::vector<std::string> args{"-std=c++11"};

    std::cout << "Source Code:\n" << hwe0 << "\n";
    // first case, I know compile time values, so ahead-of-time compilation
    EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action1, hwe0, args));

    const std::string expectedSrc = R"expectedSrc(#include <vector>
int main(int argc, char** argv){
    auto l = [&](){return "lambda_visitor_tester";};
    return 0;
})expectedSrc";

    std::ifstream t(".output.cpp");
    std::string src((std::istreambuf_iterator<char>(t)),
                    std::istreambuf_iterator<char>());
    std::remove(".output.cpp");

    std::cout << "SOURCE:\n" << src << "\n";
    EXPECT_EQ(expectedSrc, src);

    EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action2, hwe1, args));

    std::ifstream t1(".output.cpp");
    std::string src2((std::istreambuf_iterator<char>(t1)),
                     std::istreambuf_iterator<char>());
    std::remove(".output.cpp");

    const std::string expectedSrc2 = R"expectedSrc2(#include <vector>
int main(int argc, char** argv){
    int nq = 2;
    auto l = [&](){return "lambda_visitor_tester";};
    return 0;
})expectedSrc2";
    EXPECT_EQ(expectedSrc2, src2);
243
244
}

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

TEST(LambdaVisitorTester, checkRuntimeGenerator) {
    Rewriter rewriter1, rewriter2;
    auto action1 = new TestQCORFrontendAction(rewriter1);
    auto action2 = new TestQCORFrontendAction(rewriter2);

    xacc::setOption("qcor-compiled-filename", "lambda_visitor_tester");

    std::vector<std::string> args{"-std=c++11"};

    std::cout << "Source Code:\n" << hwe2 << "\n";
    // first case, I know compile time values, so ahead-of-time compilation
    EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action1, hwe2, args));

    std::ifstream t1(".output.cpp");
    std::string src2((std::istreambuf_iterator<char>(t1)),
                     std::istreambuf_iterator<char>());
    std::remove(".output.cpp");

    std::cout << "HELLO:\n" << src2 <<"\n";
    const std::string exp1 = R"exp1(#include <vector>
int main(int argc, char** argv){
    int nq = argc;
    auto l = [&]() {
qcor::storeRuntimeVariable("n-qubits", nq);
return "lambda_visitor_tester";
};
    return 0;
})exp1";

    EXPECT_EQ(exp1,src2);
}
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295


TEST(LambdaVisitorTester, checkRuntimeGeneratorWithVectorPair) {
    Rewriter rewriter1, rewriter2;
    auto action1 = new TestQCORFrontendAction(rewriter1);

    xacc::setOption("qcor-compiled-filename", "lambda_visitor_tester");

    std::vector<std::string> args{"-std=c++11"};

    std::cout << "Source Code:\n" << hwe3 << "\n";
    // first case, I know compile time values, so ahead-of-time compilation
    EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action1, hwe3, args));

    std::ifstream t1(".output.cpp");
    std::string src2((std::istreambuf_iterator<char>(t1)),
                     std::istreambuf_iterator<char>());
    std::remove(".output.cpp");

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
    std::cout << "HELLO:\n" << src2 <<"\n";
    const std::string exp1 = R"exp1(#include <vector>
int main(int argc, char** argv){
    int nq = argc;
    std::vector<std::pair<int,int>> c{{1,0}};
    auto l = [&]() {
qcor::storeRuntimeVariable("coupling", c);
qcor::storeRuntimeVariable("n-qubits", nq);
return "lambda_visitor_tester";
};
    return 0;
})exp1";

    EXPECT_EQ(exp1,src2);
}

TEST(LambdaVisitorTester, checkRuntimeGeneratorWithVectorPairAndVector) {
    Rewriter rewriter1, rewriter2;
    auto action1 = new TestQCORFrontendAction(rewriter1);

    xacc::setOption("qcor-compiled-filename", "lambda_visitor_tester");

    std::vector<std::string> args{"-std=c++11"};

    std::cout << "Source Code:\n" << hwe4 << "\n";
    // first case, I know compile time values, so ahead-of-time compilation
    EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action1, hwe4, args));

    std::ifstream t1(".output.cpp");
    std::string src2((std::istreambuf_iterator<char>(t1)),
                     std::istreambuf_iterator<char>());
    std::remove(".output.cpp");

329
330
331
332
333
334
335
336
337
338
339
    std::cout << "HELLO:\n" << src2 <<"\n";
    const std::string exp1 = R"exp1(#include <vector>
int main(int argc, char** argv){
    int nq = argc;
    auto l = [&]() {
qcor::storeRuntimeVariable("n-qubits", nq);
return "lambda_visitor_tester";
};
    return 0;
})exp1";

340
    EXPECT_EQ(exp1,src2);
341
}
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

TEST(LambdaVisitorTester, checkUnary) {
    Rewriter rewriter1, rewriter2;
    auto action1 = new TestQCORFrontendAction(rewriter1);

    xacc::setOption("qcor-compiled-filename", "lambda_visitor_tester");

    std::vector<std::string> args{"-std=c++11"};

    std::cout << "Source Code:\n" << unary0 << "\n";
    // first case, I know compile time values, so ahead-of-time compilation
    EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action1, unary0, args));

    std::ifstream t1(".output.cpp");
    std::string src2((std::istreambuf_iterator<char>(t1)),
                     std::istreambuf_iterator<char>());
    std::remove(".output.cpp");

    std::cout << "HELLO:\n" << src2 <<"\n";
    const std::string expectedSrc = R"expectedSrc(
int main() {
    auto l = [&](){return "lambda_visitor_tester";};
    return 0;
})expectedSrc";

  EXPECT_EQ(expectedSrc, src2);
}

TEST(LambdaVisitorTester, checkRuntimeCapture) {
    Rewriter rewriter1, rewriter2;
    auto action1 = new TestQCORFrontendAction(rewriter1);

    xacc::setOption("qcor-compiled-filename", "lambda_visitor_tester");

    std::vector<std::string> args{"-std=c++11"};

    std::cout << "Source Code:\n" << rtimeCapture << "\n";
    // first case, I know compile time values, so ahead-of-time compilation
    EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action1, rtimeCapture, args));

    std::ifstream t1(".output.cpp");
    std::string src2((std::istreambuf_iterator<char>(t1)),
                     std::istreambuf_iterator<char>());
    std::remove(".output.cpp");

    std::cout << "HELLO:\n" << src2 <<"\n";
    const std::string expectedSrc = R"expectedSrc(
int main() {
    double pi = 3.1415;
    auto l = [&](){return "lambda_visitor_tester";};
    return 0;
})expectedSrc";

  EXPECT_EQ(expectedSrc, src2);
}
397
398
399
400
401
402
int main(int argc, char **argv) {
  qcor::Initialize(argc, argv);
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  return ret;
}