Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] qjit-compatible implementation of itertools.product #1340

Open
josh146 opened this issue Nov 28, 2024 · 1 comment
Open

[Feature] qjit-compatible implementation of itertools.product #1340

josh146 opened this issue Nov 28, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@josh146
Copy link
Member

josh146 commented Nov 28, 2024

It's quite common to see workflows that make use of itertools related functionality, in particular

  • itertools.product,
  • itertools.combinations, and
  • itertools.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.

@josh146 josh146 added the enhancement New feature or request label Nov 28, 2024
@josh146
Copy link
Member Author

josh146 commented Nov 28, 2024

Longer term, we could also do the same thing for itertools.combinations and itertools.permutations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant