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

Support simple var assignment in PyXASM



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 6cc4bf0a
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -120,4 +120,18 @@ class pyxasm_visitor : public pyxasmBaseVisitor {

    return 0;
  }

  antlrcpp::Any visitExpr_stmt(pyxasmParser::Expr_stmtContext *ctx) override {
    if (ctx->ASSIGN().size() == 1 && ctx->testlist_star_expr().size() == 2) {
      // Handle simple assignment: a = expr
      std::stringstream ss;
      const std::string lhs = ctx->testlist_star_expr(0)->getText();
      const std::string rhs = ctx->testlist_star_expr(1)->getText();
      ss << "auto " << lhs << " = " << rhs << "; \n";
      result.first = ss.str();
      return 0;
    } else {
      return visitChildren(ctx);
    }
  }
};
 No newline at end of file
+28 −0
Original line number Diff line number Diff line
@@ -27,5 +27,33 @@ class TestSimpleKernelJIT(unittest.TestCase):
        self.assertTrue('00' in counts)
        self.assertTrue('11' in counts)

    def test_assignment(self):

        set_qpu('qpp', {'shots':8192})

        @qjit
        def varAssignKernel(q : qreg):
            # Simple value assignment
            angle = 3.14/4.0
            Rx(q[0], angle)
            CX(q[0], q[1])
            for i in range(q.size()):
                Measure(q[i])

        # Allocate 2 qubits
        q = qalloc(2)

        # Run the bell experiment
        varAssignKernel(q)

        # Print the results
        q.print()
        counts = q.counts()
        self.assertEqual(len(counts), 2)
        self.assertTrue('00' in counts)
        self.assertTrue('11' in counts)
        # Angle less than Pi/2 => 00 more than 11
        self.assertTrue(counts['00'] > counts['11'])

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