Commit 1ea8cc1b authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Add multi-control Python test



To test the functionality at runtime.

Also, add ability to define a local array-like variable in Python.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 998f0edf
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -265,11 +265,31 @@ class pyxasm_visitor : public pyxasmBaseVisitor {
          }
        }
      } else {
        // Handle Python list assignment:
        // i.e. lhs = [a, b, c] => { a, b, c }
        // NOTE: we only support simple lists, i.e. no nested.
        const auto transformListAssignmentIfAny =
            [](const std::string &in_expr) -> std::string {
          const auto whitespace = " ";
          // Trim leading and trailing spaces:
          const auto strBegin = in_expr.find_first_not_of(whitespace);
          const auto strEnd = in_expr.find_last_not_of(whitespace);
          const auto strRange = strEnd - strBegin + 1;
          const auto trim_expr = in_expr.substr(strBegin, strRange);

          if (trim_expr.front() == '[' && trim_expr.back() == ']') {
            return "{" + trim_expr.substr(1, trim_expr.size() - 2) + "}";
          }

          // Returns the original expression:
          return in_expr;
        };

        if (xacc::container::contains(declared_var_names, lhs)) {
          ss << lhs << " = " << rhs << "; \n";
          ss << lhs << " = " << transformListAssignmentIfAny(rhs) << "; \n";
        } else {
          // New variable: need to add *auto*
          ss << "auto " << lhs << " = " << rhs << "; \n";
          ss << "auto " << lhs << " = " << transformListAssignmentIfAny(rhs) << "; \n";
          new_var = lhs;
        }
      }
+7 −0
Original line number Diff line number Diff line
@@ -52,3 +52,10 @@ add_test (NAME qcor_python_jit_pass_manager
 )
 set_tests_properties(qcor_python_jit_pass_manager
     PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}:$ENV{PYTHONPATH}")

add_test (NAME qcor_python_jit_multi_ctrl
  COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_jit_multi_ctrl.py
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
set_tests_properties(qcor_python_jit_multi_ctrl
  PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}:$ENV{PYTHONPATH}")
 No newline at end of file
+23 −6
Original line number Diff line number Diff line
@@ -6,19 +6,36 @@ from qcor import *

class TestKernelJIT(unittest.TestCase):
    def test_multiple_control_kernel(self):
        set_qpu('qpp', {'shots':1024})

        @qjit
        def apply_X_at_idx(q : qreg, idx: int):
            X(q[idx])

        @qjit
        def test_ccx_gate(q : qreg):
           apply_X_at_idx.ctrl([1, 2], q, 0)
        def test_cccx_gate(q : qreg):
            for i in range(q.size()):
                X(q[i])
            # 3 control bits
            ctrl_idxs =  [q[1], q[2], q[3]] 
            apply_X_at_idx.ctrl(ctrl_idxs, q, 0)
            for i in range(q.size()):
                Measure(q[i])
        
        q = qalloc(3)
        comp = test_ccx_gate.extract_composite(q)
        q = qalloc(4)
        comp = test_cccx_gate.extract_composite(q)
        print(comp)

        # Run experiment
        test_cccx_gate(q)

        # Print the results
        q.print()
        counts = q.counts()
        print(counts)
        self.assertEqual(len(counts), 1)
        # q0: 1 --> 0
        self.assertTrue('0111' in counts)

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