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

Method __groupValues

python/cudaq/kernel/ast_bridge.py:1542–1611  ·  view source on GitHub ↗

Helper function that visits the given AST nodes (`pyvals`), and groups them according to the specified list. The list contains integers or. tuples of two integers. Integer values have to be positive or -1, where -1 indicates that any number of values is acceptable.

(self, pyvals, groups: list[int | tuple[int, int]])

Source from the content-addressed store, hash-verified

1540 return startVal, endVal, stepVal, isDecrementing
1541
1542 def __groupValues(self, pyvals, groups: list[int | tuple[int, int]]):
1543 """Helper function that visits the given AST nodes (`pyvals`), and
1544 groups them according to the specified list. The list contains integers
1545 or.
1546
1547 tuples of two integers. Integer values have to be positive or -1, where
1548 -1 indicates that any number of values is acceptable. Tuples of two
1549 integers (min, max) indicate that any number of values in [min, max] is
1550 acceptable. The list may only contain at most one negative integer or
1551 tuple (enforced via assert only).
1552
1553 Emits a fatal error if any of the given `pyvals` did not generate a
1554 value. Emits a fatal error if there are too many or too few values to
1555 satisfy the requested grouping.
1556
1557 Returns a tuple of value groups. Each value group is either a single
1558 value (if the corresponding entry in `groups` equals 1), or a list of
1559 values.
1560 """
1561
1562 def group_values(numExpected, values, reverse):
1563 groupedVals = []
1564 current_idx = 0
1565 for nArgs in numExpected:
1566 if (isinstance(nArgs, int) and nArgs == 1 and
1567 current_idx < len(values)):
1568 groupedVals.append(values[current_idx])
1569 current_idx += 1
1570 continue
1571 if isinstance(nArgs, tuple):
1572 minNumArgs, maxNumArgs = nArgs
1573 if minNumArgs == maxNumArgs:
1574 nArgs = minNumArgs
1575 if not isinstance(nArgs, int) or nArgs < 0:
1576 break
1577 if current_idx + nArgs > len(values):
1578 self.emitFatalError("missing value", self.currentNode)
1579 groupedVals.append(values[current_idx:current_idx + nArgs])
1580 if reverse:
1581 groupedVals[-1].reverse()
1582 current_idx += nArgs
1583 remaining = values[current_idx:]
1584 numExpected = numExpected[len(groupedVals):]
1585 if reverse:
1586 remaining.reverse()
1587 groupedVals.reverse()
1588 numExpected.reverse()
1589 return groupedVals, numExpected, remaining
1590
1591 [self.visit(arg) for arg in pyvals]
1592 values = self.popAllValues(len(pyvals))
1593 groups.reverse()
1594 backVals, groups, values = group_values(groups, values, reverse=True)
1595 frontVals, groups, values = group_values(groups, values, reverse=False)
1596 if not groups:
1597 if values:
1598 self.emitFatalError("too many values", self.currentNode)
1599 groupedVals = *frontVals, *backVals

Callers 5

_lowerQECOpMethod · 0.95
processQuakeCtorMethod · 0.95
processDecoratorCallMethod · 0.95
visit_CallMethod · 0.95

Calls 3

visitMethod · 0.95
popAllValuesMethod · 0.95
emitFatalErrorMethod · 0.95

Tested by

no test coverage detected