LambdaVisitorTester.in.cpp 13.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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"

21
#include "DigitalGates.hpp"
22
#include "CountGatesOfTypeVisitor.hpp"
23
24
#include "XACC.hpp"
#include "xacc_service.hpp"
25

26
#include "IRGenerator.hpp"
27
28
29
30
31
32
33
34
35
36
37
#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:
38
  TestQCORFrontendAction(Rewriter &rw) : rewriter(rw) {}
39
40
41
42
43
44
45
46
47
48
49
50

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);
51
    compiler::FuzzyParsingExternalSemaSource fuzzyParser;
52
    fuzzyParser.initialize();
53
    fuzzyParser.setASTContext(&CI.getASTContext());
54
55
    CI.getSema().addExternalSource(&fuzzyParser);

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

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

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

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

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

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

128
129
130
131
132
133
134
135
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";
136
137
138
139
140
141
142
143
144
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";
145
146
147
148
149
150
151
152
153
154
155
156
157
158

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";
159
160
161
162
163
164
165
166
167
168
169

const std::string dw = R"dw(
int main() {
    auto l = [&]() {
        qmi(0,0,2.2);
        qmi(1,1,3.3);
        qmi(0,1,3.4);
        anneal(20,0,0,0);
    };
    return 0;
})dw";
170
171
172
173
174
TEST(LambdaVisitorTester, checkSimple) {
  Rewriter rewriter1, rewriter2;
  auto action1 = new TestQCORFrontendAction(rewriter1);
  auto action2 = new TestQCORFrontendAction(rewriter2);

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

177
  std::vector<std::string> args{"-std=c++11"};
178
179
180
181
182

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

  const std::string expectedSrc = R"expectedSrc(
int main() {
183
    auto l = [&](){return "lambda_visitor_tester";};
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    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());

202
  EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action2, param0, args));
203
204
205

  std::ifstream t2(".output.cpp");
  std::string src2((std::istreambuf_iterator<char>(t2)),
206
                   std::istreambuf_iterator<char>());
207
208
209
210
211
212
  std::remove(".output.cpp");

  EXPECT_EQ(expectedSrc, src2);
}

TEST(LambdaVisitorTester, checkGenerator) {
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
243
244
245
246
247
248
249
250
251
252
    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);
253
254
}

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

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);
}
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305


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");

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
    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");

339
340
341
342
343
344
345
346
347
348
349
    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";

350
    EXPECT_EQ(exp1,src2);
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
397
398
399
400
401
402
403
404
405
406

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);
}
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443

TEST(LambdaVisitorTester, checkSimpleDW) {
  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"};

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

  const std::string expectedSrc = R"expectedSrc(
int main() {
    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);

  auto function = qcor::loadCompiledCircuit("lambda_visitor_tester");
  EXPECT_EQ(4, function->nInstructions());
  EXPECT_EQ(0, function->getInstruction(0)->bits()[0]);
  EXPECT_EQ(0, function->getInstruction(0)->bits()[1]);
  EXPECT_EQ(1, function->getInstruction(1)->bits()[0]);
  EXPECT_EQ(1, function->getInstruction(1)->bits()[1]);
  EXPECT_EQ(0, function->getInstruction(2)->bits()[0]);
  EXPECT_EQ(1, function->getInstruction(2)->bits()[1]);

  std::cout << "HOWDY:\n" << function->getInstruction(3)->toString() <<"\n";
}

444
445
446
447
448
449
int main(int argc, char **argv) {
  qcor::Initialize(argc, argv);
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  return ret;
}