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

[MLIR] Provide convenience gate builders in tablegen #1180

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

paul0403
Copy link
Contributor

@paul0403 paul0403 commented Oct 4, 2024

Context:
There's many scheduled circuit transformation work at the mlir layer, like merge rotations and control flow related transforms, or even hardware gateset decomposition, where creating new gates will be necessary.

However, it came to our realization that currently the way to create a new gate in mlir (aka a quantum.custom operation) is more complicated than necessary, since all the fields need to be specified, even when the field values are empty. For example, see https://github.com/PennyLaneAI/catalyst/pull/1154/files#diff-bc5ebab69d5c90919840a29058595f8718f5e8a150e141f721250b76a0278f44R49

In light of this, we provide convenience builders in tablegen.

Description of the Change:
Provide convenience gate builders (aka builders for quantum.custom operations) in tablegen;
Create a tester pass -test-gate-buidler.

Benefits:
No need to specify a bunch of unnecessary boilerplate arguments when creating a new quantum.custom operation in a pass.
This can also be greatly beneficial for external contributors.

Possible Drawbacks:

Related GitHub Issues:

@paul0403 paul0403 added the enhancement New feature or request label Oct 4, 2024
Copy link
Contributor

github-actions bot commented Oct 4, 2024

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md on your branch with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@paul0403 paul0403 changed the title [mlir] Provide convenience gate builders in tablegen [MLIR] Provide convenience gate builders in tablegen Oct 4, 2024
Copy link

codecov bot commented Oct 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.86%. Comparing base (3bea1b6) to head (e11923c).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1180   +/-   ##
=======================================
  Coverage   97.86%   97.86%           
=======================================
  Files          76       76           
  Lines       10819    10819           
  Branches     1283     1283           
=======================================
  Hits        10588    10588           
  Misses        179      179           
  Partials       52       52           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this!

Copy link
Contributor

@dime10 dime10 Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should give the same treatment to our other quantum gates :) (anything with control wires)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm adding them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

I am basing the category on whether the gate (a) has controls and (b) has parameters. This means there are 4 buidlers.

I think these sufficiently covers all the needs as of now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we really want tests in the source directory 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is, is there even another way to do mlir lit tests? The test directory is just a bunch of IR files.

Copy link
Contributor

@dime10 dime10 Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What your asking for is C++ unit tests, unfortunately we don't have a framework setup for it yet, although Erick implemented one here: #1105 (just needs review)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, nice!

builder.create<quantum::CustomOp>(loc, "SWAP", mlir::ValueRange({inQubit, pz->getResult(0)}));

builder.create<quantum::CustomOp>(loc, "Rot",
mlir::ValueRange({inQubit, pz->getResult(0)}),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all the arguments are wrapped in ValueRange(), thoughts about letting the helpers just take in a std::vector (or some other containers that can be implicitly passed via an initialization list), and converting them to ValueRange insider the helper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.

void f(std::vector<int> v){
    for (int x : v){
        std::cout << x;
    }
}

int main() {
    f({10, 21, 44});
    return 0;
}

------
102144

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid using vectors unless necessary. The mlir devs use specific data structures (like the ValueRange) in as many places as possible to avoid allocations, copies, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure you can pass a vector to a function expecting ValueRange though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mlir::ValueRange({inQubit, pz->getResult(0)}) is actually extremely dangerous though in a function call, because ValueRange is a non-owning object, your temporaries don't have a storage location for duration of the function call.

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

Successfully merging this pull request may close these issues.

2 participants