LambdaVisitorTester.in.cpp 8.09 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
88
89
90
91
    return 0;
})param0";

const std::string hwe0 = R"hwe0(#include <vector>
int main(int argc, char** argv){
    auto l = [&](std::vector<double> x) {
92
        hwe(x, {{"n-qubits",2},{"layers",1}});
93
94
95
96
97
98
    };
    return 0;
})hwe0";

const std::string hwe1 = R"hwe1(#include <vector>
int main(int argc, char** argv){
99
    int nq = 2;
100
    auto l = [&](std::vector<double> x) {
101
        hwe(x, {{"n-qubits", nq},{"layers",1}});
102
103
104
105
    };
    return 0;
})hwe1";

106
107
108
109
110
111
112
113
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";
114
115
116
117
118
119
120
121
122
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";
123
124
125
126
127
TEST(LambdaVisitorTester, checkSimple) {
  Rewriter rewriter1, rewriter2;
  auto action1 = new TestQCORFrontendAction(rewriter1);
  auto action2 = new TestQCORFrontendAction(rewriter2);

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

130
  std::vector<std::string> args{"-std=c++11"};
131
132
133
134
135

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

  const std::string expectedSrc = R"expectedSrc(
int main() {
136
    auto l = [&](){return "lambda_visitor_tester";};
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    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());

155
  EXPECT_TRUE(tooling::runToolOnCodeWithArgs(action2, param0, args));
156
157
158

  std::ifstream t2(".output.cpp");
  std::string src2((std::istreambuf_iterator<char>(t2)),
159
                   std::istreambuf_iterator<char>());
160
161
162
163
164
165
  std::remove(".output.cpp");

  EXPECT_EQ(expectedSrc, src2);
}

TEST(LambdaVisitorTester, checkGenerator) {
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    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);
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

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);
}
240
241
242
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


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

    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);
}
272
273
274
275
276
277
int main(int argc, char **argv) {
  qcor::Initialize(argc, argv);
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  return ret;
}