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

Work on Pythonic XASM



Added ability to query globals inside the QJIT function: that includes imported module info (and alias) as well as variables.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent ff9f4a50
Loading
Loading
Loading
Loading
+26 −2
Original line number Diff line number Diff line
@@ -88,9 +88,33 @@ class qjit(object):
                self.allowed_type_cpp_map[str(_type)] + ' ' + arg
        cpp_arg_str = cpp_arg_str[1:]

        globalVarDecl = []
        # Get all globals currently defined at this stack frame
        globalsInStack = inspect.stack()[1][0].f_globals
        globalVars = globalsInStack.copy()
        importedModules = {}
        for key in globalVars:
            descStr = str(globalVars[key])
            # Cache module import and its potential alias
            # e.g. import abc as abc_alias
            if descStr.startswith("<module "):
                moduleName = descStr.split()[1].replace("'", "")
                importedModules[key] = moduleName
            else:
                # Import global variables:
                # Only support float atm
                if (isinstance(globalVars[key], float)):
                    globalVarDecl.append(key + " = " + str(globalVars[key]))
        
        # Inject these global declarations into the function body.
        separator = "\n"
        globalDeclStr = separator.join(globalVarDecl)

        # TODO: Handle common module like numpy or math
        # e.g. if seeing `import numpy as np`, we'll have <'np' -> 'numpy'> in the importedModules dict.  
        # Create the qcor quantum kernel function src for QJIT and the Clang syntax handler
        self.src = '__qpu__ void '+self.function.__name__ + \
            '('+cpp_arg_str+') {\nusing qcor::pyxasm;\n'+fbody_src+"}\n"
            '('+cpp_arg_str+') {\nusing qcor::pyxasm;\n' + globalDeclStr + '\n' + fbody_src +"}\n"
        
        # Run the QJIT compile step to store function pointers internally
        self._qjit.internal_python_jit_compile(self.src)
+25 −0
Original line number Diff line number Diff line
import unittest
from qcor import *

# Some global variables for testing
MY_PI = 3.1416
class TestSimpleKernelJIT(unittest.TestCase):
    def test_simple_bell(self):

@@ -55,5 +57,28 @@ class TestSimpleKernelJIT(unittest.TestCase):
        # Angle less than Pi/2 => 00 more than 11
        self.assertTrue(counts['00'] > counts['11'])
    
    def test_globalVar(self):

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

        @qjit
        def kernelUseGlobals(q : qreg):
            Rx(q[0], MY_PI)
            CX(q[0], q[1])
            for i in range(q.size()):
                Measure(q[i])

        # Allocate 2 qubits
        q = qalloc(2)

        # Run the bell experiment
        kernelUseGlobals(q)

        # Print the results
        q.print()
        counts = q.counts()
        # Pi pulse -> X gate
        self.assertTrue(counts['11'] > 8000)
        
if __name__ == '__main__':
  unittest.main()
 No newline at end of file