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

Fixed a bug in adjoint kernel construction



instruction replacements (e.g. T->Tdg) were not reflected.

Add unit test for pyxasm adjoint.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent e21fb98f
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -301,6 +301,31 @@ class TestSimpleKernelJIT(unittest.TestCase):
        print(q.counts())
        self.assertEqual(q.counts()['100'], 1024)

    def test_adjoint_kernel(self):
        @qjit
        def test_kernel(q : qreg):
            CX(q[0], q[1])
            Rx(q[0], 1.234)
            T(q[0])
            X(q[0])

        @qjit
        def check_adjoint(q : qreg):
            test_kernel.adjoint(q)
        
        q = qalloc(2)
        comp = check_adjoint.extract_composite(q)
        print(comp.toString())
        self.assertEqual(comp.nInstructions(), 4)   
        # Reverse
        self.assertEqual(comp.getInstruction(0).name(), "X") 
        # Check T -> Tdg
        self.assertEqual(comp.getInstruction(1).name(), "Tdg") 
        self.assertEqual(comp.getInstruction(2).name(), "Rx") 
        # Check angle -> -angle
        self.assertAlmostEqual((float)(comp.getInstruction(2).getParameter(0)), -1.234)
        self.assertEqual(comp.getInstruction(3).name(), "CNOT") 


if __name__ == '__main__':
  unittest.main()
 No newline at end of file
+6 −2
Original line number Diff line number Diff line
@@ -108,7 +108,6 @@ public:
    }

    auto provider = qcor::__internal__::get_provider();
    std::reverse(instructions.begin(), instructions.end());
    for (int i = 0; i < instructions.size(); i++) {
      auto inst = derived.parent_kernel->getInstruction(i);
      // Parametric gates:
@@ -127,8 +126,13 @@ public:
      }
    }

    // We update/replace instructions in the derived.parent_kernel composite,
    // hence collecting these new instructions and reversing the sequence.
    auto new_instructions = derived.parent_kernel->getInstructions();
    std::reverse(new_instructions.begin(), new_instructions.end());

    // add the instructions to the current parent kernel
    parent_kernel->addInstructions(instructions);
    parent_kernel->addInstructions(new_instructions);

    // no measures, so no execute
  }