test_loop_stmts.cpp 2.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*******************************************************************************
 * Copyright (c) 2018-, UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the BSD 3-Clause License 
 * which accompanies this distribution. 
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *   Thien Nguyen - implementation
 *******************************************************************************/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "gtest/gtest.h"
#include "qcor_mlir_api.hpp"

TEST(qasm3VisitorTester, checkLoops) {
  const std::string for_stmt = R"#(OPENQASM 3;
include "qelib1.inc";

int[32] loop_count = 0;
for i in {11,22,33} {
    print(i);
    loop_count += 1;
}
QCOR_EXPECT_TRUE(loop_count == 3);

loop_count = 0;
for i in [0:10] {
    print(i);
    loop_count += 1;
    
}
QCOR_EXPECT_TRUE(loop_count == 10);
loop_count = 0;

for j in [0:2:4] {
    print("steps:", j);
    loop_count += 1;
}

QCOR_EXPECT_TRUE(loop_count == 2);
loop_count = 0;

for j in [0:4] {
    print("j in 0:4", j);
    loop_count += 1;
}

QCOR_EXPECT_TRUE(loop_count == 4);
loop_count = 0;

for i in [0:4] {
 for j in {1,2,3} {
     print(i,j);
     loop_count += 1;
 }
}
QCOR_EXPECT_TRUE(loop_count == 12);
57

58
59
60
61
62
63
64
65
loop_count = 0;
for i in [0:4] {
 for j in [0:3] {
     print(i,j);
     loop_count += 1;
 }
}
QCOR_EXPECT_TRUE(loop_count == 12);
66

67
)#";
68
  auto mlir = qcor::mlir_compile(for_stmt, "for_stmt",
69
70
                                 qcor::OutputType::MLIR, false);
  std::cout << "for_stmt MLIR:\n" << mlir << "\n";
71
  EXPECT_FALSE(qcor::execute(for_stmt, "for_stmt"));
72
73
74
75
76
77
78
79
80
81

  const std::string while_stmt = R"#(OPENQASM 3;
include "qelib1.inc";
int[32] i = 0;
while (i < 10) {
  print(i);
  i += 1;
}
QCOR_EXPECT_TRUE(i == 10);
)#";
82
  auto mlir2 = qcor::mlir_compile(while_stmt, "while_stmt",
83
84
                                 qcor::OutputType::MLIR, false);
  std::cout << mlir2 << "\n";
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
85
86
  // We're using SCF while loop:
  EXPECT_TRUE(mlir2.find("scf.while") != std::string::npos);
87
  EXPECT_FALSE(qcor::execute(while_stmt, "while_stmt"));
88
89
90
91
92
93
94

    const std::string decrement = R"#(OPENQASM 3;
include "qelib1.inc";
for j in [10:-1:0] {
  print(j);
}
)#";
95
  auto mlir3 = qcor::mlir_compile(decrement, "decrement",
96
97
                                 qcor::OutputType::MLIR, false);
  std::cout << mlir3 << "\n";
98
  EXPECT_FALSE(qcor::execute(decrement, "decrement"));
99
100
101
102
103
104
105
}

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