test_declaration.cpp 2.45 KB
Newer Older
1
2
3
4
5
6
#include "gtest/gtest.h"
#include "qcor_mlir_api.hpp"

TEST(qasm3VisitorTester, checkDeclaration) {
  const std::string src = R"#(OPENQASM 3;
include "qelib1.inc";
7
8
9
10
int[10] x = 5;
int[10] y;
print(x);
QCOR_EXPECT_TRUE(x == 5);
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
QCOR_EXPECT_TRUE(y == 0);

int[5] xx=2, yy=1;
QCOR_EXPECT_TRUE(xx == 2);
QCOR_EXPECT_TRUE(yy == 1);

qubit q1[6], q2;
bit b1[4]="0100", b2 = "1";
QCOR_EXPECT_TRUE(b1[0] == 0);
QCOR_EXPECT_TRUE(b1[1] == 1);
QCOR_EXPECT_TRUE(b1[2] == 0);
QCOR_EXPECT_TRUE(b1[3] == 0);

bit k, kk[22];
QCOR_EXPECT_TRUE(k == 0);
QCOR_EXPECT_TRUE(kk[13] == 0);
bool bb = False;
bool m=True, n=bool(xx);
QCOR_EXPECT_TRUE(m == 1);
QCOR_EXPECT_TRUE(bb == 0);
QCOR_EXPECT_TRUE(n == 0);
const c = 5.5e3, d=5;
const e = 2.2;
QCOR_EXPECT_TRUE(c == 5500.0);
QCOR_EXPECT_TRUE(d == 5);
QCOR_EXPECT_TRUE(e == 2.2);
x q2;
k = measure q2;
QCOR_EXPECT_TRUE(k == 1);
for i in [0:22] {
    QCOR_EXPECT_TRUE(kk[i] == 0);
}
43
44
45
46

float[64] f = 3.14;
float[64] test = 3.14 - f;
QCOR_EXPECT_TRUE(test < .001);
47
48
)#";
  auto mlir =
49
      qcor::mlir_compile(src, "test", qcor::OutputType::MLIR, true);
50
  std::cout << "MLIR:\n" << mlir << "\n";
51
      auto llvmi =
52
      qcor::mlir_compile(src, "test", qcor::OutputType::LLVMMLIR, true);
53
54
  std::cout << "LLVM:\n" << llvmi << "\n";
    auto llvm =
55
      qcor::mlir_compile(src, "test", qcor::OutputType::LLVMIR, true);
56
  std::cout << "LLVM:\n" << llvm << "\n";
57
  EXPECT_FALSE(qcor::execute(src, "test"));
58
59
60
61
62
63
64
65
66
67

  const std::string src2 = R"#(OPENQASM 3;
include "qelib1.inc";
bit kk[22];
for i in [0:22] {
    print("should only see 0 bc next statement will fail: ", i);
    QCOR_EXPECT_TRUE(kk[i] == 1);
}
)#";
  auto mlir2 =
68
      qcor::mlir_compile(src2, "test", qcor::OutputType::MLIR, true);
69
  std::cout << "MLIR:\n" << mlir2 << "\n";
70
  EXPECT_TRUE(qcor::execute(src2, "test"));
71
72
}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
TEST(qasm3VisitorTester, checkGate) {
  const std::string gate_def = R"#(OPENQASM 3;
gate cphase(x) a, b
{
  U(0, 0, x / 2) a;
  CX a, b;
  U(0, 0, -x / 2) b;
  CX a, b;
  U(0, 0, x / 2) b;
}
)#";
  auto mlir = qcor::mlir_compile(gate_def, "gate_def",
                                 qcor::OutputType::MLIR, false);

  std::cout << "gate_def MLIR:\n" << mlir << "\n";

  qcor::execute(gate_def, "gate_def");

  std::cout << "LLVM:\n"
            << qcor::mlir_compile(gate_def, "gate_def",
                                  qcor::OutputType::LLVMIR, false)
            << "\n";
}
96
97
98
99
100
101

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  return ret;
}