Commit 6595a0be authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Use a more focused condition for arrays



Also, add a test for grover

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 9ad2d539
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -57,7 +57,8 @@ class pyxasm_visitor : public pyxasmBaseVisitor {
    // Only processes these for sub-expressesions, 
    // e.g. re-entries to this function
    if (is_processing_sub_expr) {
      if (context->atom() && context->atom()->testlist_comp()) {
      if (context->atom() && context->atom()->OPEN_BRACK() &&
          context->atom()->CLOSE_BRACK() && context->atom()->testlist_comp()) {
        // Array type expression:
        std::cout << "Array atom expression: "
                  << context->atom()->testlist_comp()->getText() << "\n";
@@ -77,7 +78,9 @@ class pyxasm_visitor : public pyxasmBaseVisitor {
        return 0;
      }

      if (context->atom() && context->atom()->dictorsetmaker()) {
      // We don't have a re-write rule for this one (py::dict)
      if (context->atom() && context->atom()->OPEN_BRACE() &&
          context->atom()->CLOSE_BRACE() && context->atom()->dictorsetmaker()) {
        // Dict:
        std::cout << "Dict atom expression: "
                  << context->atom()->dictorsetmaker()->getText() << "\n";
+27 −1
Original line number Diff line number Diff line
@@ -234,6 +234,32 @@ quantum::exp(q, theta, exponent_op);
  EXPECT_EQ(expectedCodeGen, ss.str());
}

TEST(PyXASMTokenCollectorTester, checkCommonMath) {
  LexerHelper helper;
  auto [tokens, PP] = helper.Lex(R"(
    out_parity = oneCount - 2 * (oneCount / 2)
    # Power
    index = 2**n 
)");

  clang::CachedTokens cached;
  for (auto &t : tokens) {
    cached.push_back(t);
  }

  std::stringstream ss;
  auto xasm_tc = xacc::getService<qcor::TokenCollector>("pyxasm");
  xasm_tc->collect(*PP.get(), cached, {""}, ss);
  std::cout << "heres the test\n";
  std::cout << ss.str() << "\n";
  const std::string expectedCodeGen =
      R"#(auto out_parity = oneCount-2*(oneCount/2); 
auto index = std::pow(2, n);
)#";
  EXPECT_EQ(expectedCodeGen, ss.str());
}


int main(int argc, char **argv) {
  std::string xacc_config_install_dir = std::string(XACC_INSTALL_DIR);
  std::string qcor_root = std::string(QCOR_INSTALL_DIR);
+10 −1
Original line number Diff line number Diff line
@@ -59,3 +59,12 @@ add_test (NAME qcor_python_jit_multi_ctrl
)
set_tests_properties(qcor_python_jit_multi_ctrl
  PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}:$ENV{PYTHONPATH}")

add_test (NAME qcor_python_jit_grover
  COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_jit_grover.py
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(qcor_python_jit_grover
  PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}:$ENV{PYTHONPATH}")

  
 No newline at end of file
+45 −0
Original line number Diff line number Diff line
import faulthandler
faulthandler.enable()

import unittest
from qcor import *

class TestKernelJIT(unittest.TestCase):
    def test_grover(self):
        set_qpu('qpp', {'shots':1024})
        
        @qjit
        def oracle_fn(q: qreg):
            CZ(q[0], q[2])
            CZ(q[1], q[2])
        
        @qjit
        def reflect_about_uniform(q: qreg):
            with compute:
                H(q)
                X(q)
            with action:
                Z.ctrl(q[0: q.size() - 1], q[q.size() - 1])
            
        @qjit
        def run_grover(q: qreg, iterations: int):
            H(q)
            #Iteratively apply the oracle then reflect
            for i in range(iterations):
                oracle_fn(q)
                reflect_about_uniform(q)
            # Measure all qubits
            Measure(q)
        
        q = qalloc(3)
        # comp = run_grover.extract_composite(q, 1)
        # print(comp)
        run_grover(q, 1)
        q.print()
        counts = q.counts()
        print(counts)
        # Only 2 bitstrings
        self.assertEqual(len(counts), 2)

if __name__ == '__main__':
  unittest.main()
 No newline at end of file