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

Method createInvariantForLoop

python/cudaq/kernel/kernel_builder.py:558–595  ·  view source on GitHub ↗

Create an invariant loop using the CC dialect.

(self,
                               endVal,
                               bodyBuilder,
                               startVal=None,
                               stepVal=None,
                               isDecrementing=False)

Source from the content-addressed store, hash-verified

556 f"type {pyType} to internal IR value.")
557
558 def createInvariantForLoop(self,
559 endVal,
560 bodyBuilder,
561 startVal=None,
562 stepVal=None,
563 isDecrementing=False):
564 """
565 Create an invariant loop using the CC dialect.
566 """
567 startVal = self.getConstantInt(0) if startVal == None else startVal
568 stepVal = self.getConstantInt(1) if stepVal == None else stepVal
569
570 iTy = self.getIntegerType()
571 inputs = [startVal]
572 resultTys = [iTy]
573
574 loop = cc.LoopOp(resultTys, inputs, BoolAttr.get(False))
575
576 whileBlock = Block.create_at_start(loop.whileRegion, [iTy])
577 with InsertionPoint(whileBlock):
578 condPred = IntegerAttr.get(
579 iTy, 2) if not isDecrementing else IntegerAttr.get(iTy, 4)
580 cc.ConditionOp(
581 arith.CmpIOp(condPred, whileBlock.arguments[0], endVal).result,
582 whileBlock.arguments)
583
584 bodyBlock = Block.create_at_start(loop.bodyRegion, [iTy])
585 with InsertionPoint(bodyBlock):
586 bodyBuilder(bodyBlock.arguments[0])
587 cc.ContinueOp(bodyBlock.arguments)
588
589 stepBlock = Block.create_at_start(loop.stepRegion, [iTy])
590 with InsertionPoint(stepBlock):
591 incr = arith.AddIOp(stepBlock.arguments[0], stepVal).result
592 cc.ContinueOp([incr])
593
594 loop.attributes.__setitem__('invariant', UnitAttr.get())
595 return
596
597 def __createQuakeValue(self, value):
598 return QuakeValue(value, self)

Callers 3

u3Method · 0.95
__generalOperationFunction · 0.45

Calls 4

getConstantIntMethod · 0.95
getIntegerTypeMethod · 0.95
__setitem__Method · 0.80
getMethod · 0.45

Tested by

no test coverage detected