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

Class PyKernel

python/cudaq/kernel/kernel_builder.py:233–1864  ·  view source on GitHub ↗

The :class:`Kernel` provides an API for dynamically constructing quantum circuits. The :class:`Kernel` programmatically represents the circuit as an MLIR function using the Quake dialect. Attributes: name (:obj:`str`): The name of the :class:`Kernel` function. Read-only.

Source from the content-addressed store, hash-verified

231
232
233class PyKernel(object):
234 """
235 The :class:`Kernel` provides an API for dynamically constructing quantum
236 circuits. The :class:`Kernel` programmatically represents the circuit as an
237 MLIR function using the Quake dialect.
238
239 Attributes:
240 name (:obj:`str`): The name of the :class:`Kernel` function. Read-only.
241 arguments (List[:class:`QuakeValue`]): The arguments accepted by the
242 :class:`Kernel` function. Read-only.
243 argument_count (int): The number of arguments accepted by the
244 :class:`Kernel` function. Read-only.
245
246 """
247
248 def __init__(self, argTypeList):
249 self.ctx = getMLIRContext()
250
251 self.conditionalOnMeasure = False
252 self.regCounter = 0
253 self.loc = Location.unknown(context=self.ctx)
254 self.module = Module.create(loc=self.loc)
255 self.uniqId = id(self)
256 self.uniqName = "PythonKernelBuilderInstance.." + hex(self.uniqId)
257 self.name = self.uniqName
258 self.funcName = nvqppPrefix + self.name
259 self.funcNameEntryPoint = self.uniqName + '.PyKernelFakeEntryPoint'
260 strAttr = StringAttr.get(self.funcNameEntryPoint, context=self.ctx)
261 attr = DictAttr.get({self.funcName: strAttr}, context=self.ctx)
262 self.module.operation.attributes.__setitem__('quake.mangled_name_map',
263 attr)
264
265 # List of in-place applied noise channels (rather than pre-registered
266 # noise classes)
267 self.appliedNoiseChannels = []
268
269 with self.ctx, InsertionPoint(self.module.body), self.loc:
270 self.mlirArgTypes = [
271 mlirTypeFromPyType(argType[0], self.ctx, argInstance=argType[1])
272 for argType in
273 [self.__processArgType(ty) for ty in argTypeList]
274 ]
275
276 # `cudaq.make_kernel(...)` produces an entry-point kernel by
277 # construction. Reject any handle-containing parameter type before
278 # tagging so the AST-bridge boundary check (which never runs on
279 # this path) cannot be sidestepped via `cudaq.make_kernel`.
280 for argTy in self.mlirArgTypes:
281 if containsMeasureHandle(argTy):
282 emitFatalError(boundaryDiagnostic)
283
284 self.funcOp = func.FuncOp(self.funcName, (self.mlirArgTypes, []),
285 loc=self.loc)
286 self.funcOp.attributes.__setitem__('cudaq-entrypoint',
287 UnitAttr.get())
288 self.funcOp.attributes.__setitem__('cudaq-kernel', UnitAttr.get())
289 e = self.funcOp.add_entry_block()
290 self.arguments = [self.__createQuakeValue(b) for b in e.arguments]

Callers 1

make_kernelFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected