PennyLaneAI / PennyLaneAI/catalyst
[Feature] qjit-compatible implementation of `itertools.product`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 234
- Forks
- 84
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 66
Description
It's quite common to see workflows that make use of itertools related functionality, in particular
itertools.product,itertools.combinations, anditertools.permutations.
Here, I focus only on product as this is part of the workflow I am using.
For example, consider:
@qml.qjit(autograph=True)
def fn(x):
for i, j in itertools.product(range(2), repeat=2):
x += i + j - i * j * jnp.sin(j)
return x
Here, product has the following rough semantics:
def p(*iterables, repeat=1):
result = [[]]
if repeat < 0:
raise ValueError('repeat argument cannot be negative')
for l in range(repeat):
for pool in map(tuple, iterables):
_temp = []
for i in result:
for j in pool:
_temp.append(i + [j])
result = _temp
for prod in result:
yield tuple(prod)
(note this is not exactly the implementation, as the actual implementation does not generate intermediate lists in memory).
This will fail to compile, since Autograph is not able to recognize and convert itertools.product (even though the arguments are all compile-time constant).
It is also non-trivial to re-write as pure catalyst loops using range in complex cases.
I propose adding our own catalyst.product, similar to our existing range, that AutoGraph can use as the conversion target. This would then lower to a fast implementation of product (likely as nested for loops) at the MLIR layer. An advantage here is that this would allow product to work even with dynamic arguments.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the AutoGraph conversion handling for itertools.product and the existing catalyst.range implementation, then trace how supported constructs lower through the MLIR layer. Done means catalyst.product supports the described qjit loop, including repeat and dynamic arguments, with coverage for the requested semantics and compilation behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100