MCPcopy Create free account
hub / github.com/NVIDIA/cuda-quantum / resolve_callable_arg

Method resolve_callable_arg

python/cudaq/kernel/kernel_builder.py:1432–1485  ·  view source on GitHub ↗

`target` must be a callable. For a simple callable (a `func.FuncOp`), resolution is trivial. If the callable is a decorator with lambda lifted arguments, then all the lifted arguments must be resolved into a closure here. Returns a `CreateLambdaOp` closure.

(self, insPt,
                             target: DecoratorCapture | LinkedKernelCapture)

Source from the content-addressed store, hash-verified

1430 self.__applyControlOrAdjoint(target, False, [], *target_arguments)
1431
1432 def resolve_callable_arg(self, insPt,
1433 target: DecoratorCapture | LinkedKernelCapture):
1434 """
1435 `target` must be a callable. For a simple callable (a `func.FuncOp`),
1436 resolution is trivial. If the callable is a decorator with lambda lifted
1437 arguments, then all the lifted arguments must be resolved into a
1438 closure here.
1439 Returns a `CreateLambdaOp` closure.
1440 """
1441 match target:
1442 case DecoratorCapture(decorator=decorator, resolved=resolved_args):
1443 kernel_name = nvqppPrefix + decorator.uniqName
1444 merge_module = decorator.qkeModule
1445 signature = decorator.signature
1446 case LinkedKernelCapture(linkedKernel=kernel_name,
1447 qkeModule=merge_module):
1448 signature = KernelSignature.parse_from_mlir(
1449 merge_module, kernel_name)
1450 resolved_args = []
1451 case _:
1452 raise ValueError(f"Invalid callable arg: {target}")
1453
1454 # Add the target kernel to the current module.
1455 cudaq_runtime.updateModule(kernel_name, self.module, merge_module)
1456 fn = recover_func_op(self.module, kernel_name)
1457 funcTy = signature.get_lifted_type()
1458 callableTy = signature.get_callable_type()
1459 arg_types = signature.arg_types
1460
1461 # build the closure to capture the lifted `args`
1462 with insPt, self.loc:
1463 lamb = cc.CreateLambdaOp(callableTy, loc=self.loc)
1464 lamb.attributes.__setitem__('function_type', TypeAttr.get(funcTy))
1465 initRegion = lamb.initRegion
1466 initBlock = Block.create_at_start(initRegion, arg_types)
1467 inner = InsertionPoint(initBlock)
1468 with inner:
1469 vs = []
1470 for ba in initBlock.arguments:
1471 vs.append(ba)
1472 for arg in resolved_args:
1473 if isinstance(arg, (DecoratorCapture, LinkedKernelCapture)):
1474 # The recursive step
1475 v = self.resolve_callable_arg(inner, arg)
1476 else:
1477 v = self.__getMLIRValueFromPythonArg(arg)
1478 vs.append(v)
1479 if funcTy.results:
1480 call = func.CallOp(fn, vs).result
1481 cc.ReturnOp(call.results)
1482 else:
1483 func.CallOp(fn, vs)
1484 cc.ReturnOp([])
1485 return lamb
1486
1487 def c_if(self, measurement, function):
1488 """

Callers 1

apply_callMethod · 0.95

Calls 8

recover_func_opFunction · 0.85
parse_from_mlirMethod · 0.80
get_lifted_typeMethod · 0.80
get_callable_typeMethod · 0.80
__setitem__Method · 0.80
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected