diff --git a/python/cudaq/kernel/ast_bridge.py b/python/cudaq/kernel/ast_bridge.py index 6f04773c3f..caa54d3977 100644 --- a/python/cudaq/kernel/ast_bridge.py +++ b/python/cudaq/kernel/ast_bridge.py @@ -1332,18 +1332,22 @@ def visit_Call(self, node): def get_full_module_path(partial_path): parts = partial_path.split('.') - for module_name, module in sys.modules.items(): - if module_name.endswith(parts[0]): - try: - obj = module - for part in parts[1:]: - obj = getattr(obj, part) - return f"{module_name}.{'.'.join(parts[1:])}" - except AttributeError: - continue - return partial_path - - devKey = get_full_module_path(devKey) + if len(parts[0]) == 0: + return partial_path + if parts[0] in sys.modules: + module = sys.modules[parts[0]] + else: + raise ImportError(f"Module '{parts[0]}' is not imported") + obj = module + for attribute in parts[1:]: + obj = getattr(obj, attribute) + if hasattr(obj, + '__module__') and obj.__module__ != obj.__name__: + return f"{obj.__module__}" + return f"{module.__name__}" + + devKey = get_full_module_path("{}.{}".format( + devKey, node.func.attr)) if cudaq_runtime.isRegisteredDeviceModule(devKey): maybeKernelName = cudaq_runtime.checkRegisteredCppDeviceKernel( self.module, devKey + '.' + node.func.attr)