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

Method for_loop

python/cudaq/kernel/kernel_builder.py:1504–1589  ·  view source on GitHub ↗

Add a for loop that starts from the given `start` index, ends at the given `stop` index (non inclusive), applying the provided `function` within `self` at each iteration. The step value is provided to mutate the iteration variable after every iteration. Args

(self, start, stop, function)

Source from the content-addressed store, hash-verified

1502 "`c_if` is no longer supported. Use kernel mode with `run` API.")
1503
1504 def for_loop(self, start, stop, function):
1505 """
1506 Add a for loop that starts from the given `start` index, ends at the
1507 given `stop` index (non inclusive), applying the provided `function`
1508 within `self` at each iteration. The step value is provided to mutate
1509 the iteration variable after every iteration.
1510
1511 Args:
1512 start (int or :class:`QuakeValue`): The beginning iterator value for
1513 the for loop.
1514 stop (int or :class:`QuakeValue`): The final iterator value
1515 (non-inclusive) for the for loop.
1516 function (Callable): The callable function to apply within the `kernel`
1517 at each iteration.
1518
1519 ```python
1520 # Example:
1521 # Create a kernel function that takes an `int` argument.
1522 kernel, size = cudaq.make_kernel(int)
1523 # Parameterize the allocated number of qubits by the int.
1524 qubits = kernel.qalloc(size)
1525 kernel.h(qubits[0])
1526
1527 def foo(index: int):
1528 # A function that will be applied to `kernel` in a for loop.
1529 kernel.cx(qubits[index], qubits[index+1])
1530
1531 # Create a for loop in `kernel`, parameterized by the `size`
1532 # argument for its `stop` iterator.
1533 kernel.for_loop(start=0, stop=size-1, function=foo)
1534
1535 # Execute the kernel, passing along a concrete value (5) for
1536 # the `size` argument.
1537 counts = cudaq.sample(kernel, 5)
1538 print(counts)
1539 ```
1540 """
1541 with self.insertPoint, self.loc:
1542 iTy = mlirTypeFromPyType(int, self.ctx)
1543 startVal = None
1544 endVal = None
1545 stepVal = None
1546
1547 if isinstance(start, int):
1548 startVal = arith.ConstantOp(iTy, IntegerAttr.get(iTy,
1549 start)).result
1550 elif isinstance(start, QuakeValue):
1551 startVal = start.mlirValue
1552 else:
1553 emitFatalError(
1554 f"invalid start value passed to for_loop: {start}")
1555
1556 if isinstance(stop, int):
1557 endVal = arith.ConstantOp(iTy, IntegerAttr.get(iTy,
1558 stop)).result
1559 elif isinstance(stop, QuakeValue):
1560 endVal = stop.mlirValue
1561 else:

Callers 8

testGetStateAsyncFunction · 0.45
test_pauli_word_inputFunction · 0.45
test_QuakeValueInForLoopFunction · 0.45
test_for_loopFunction · 0.45
test_sample_nFunction · 0.45
CUDAQ_TESTFunction · 0.45
TESTFunction · 0.45
CUDAQ_TESTFunction · 0.45

Calls 6

__createQuakeValueMethod · 0.95
mlirTypeFromPyTypeFunction · 0.85
functionFunction · 0.85
__setitem__Method · 0.80
emitFatalErrorFunction · 0.70
getMethod · 0.45

Tested by 8

testGetStateAsyncFunction · 0.36
test_pauli_word_inputFunction · 0.36
test_QuakeValueInForLoopFunction · 0.36
test_for_loopFunction · 0.36
test_sample_nFunction · 0.36
CUDAQ_TESTFunction · 0.36
TESTFunction · 0.36
CUDAQ_TESTFunction · 0.36