-
Notifications
You must be signed in to change notification settings - Fork 39
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
[rfc] Support for writing kernels as methods of a class #204
Comments
First off - I've been thinking about this for a long time, and have wanted to incorporate custom class definitions with qpu kernel methods. I think this is a great idea, thank you for adding this Issue tracker for it. There are many reasons one would want to do this. One question I have about your reason for bringing this up for grover - you mention that for different grover oracles (with different signatures) you have to create a new KernelSignature, and therefore a new You could of course define an Oracle supertype with a very simple |
To just add on to my thinking on this - we could define like a |
qclass TestClass {
public:
__qpu__ void qmethod(qreg q) { ...}
}; macro rewrites to __qpu__ void TestClass(qreg q) {
class TestClass {
...
};
} TokenCollector rewrites to __qpu__ internal_qmethod(qreg q) {
...
}
class TestClass {
public:
void qmethod(qreg q) {
internal_qmethod(q);
}
}; We will need to autogen the qpu declared internal_qmethod manually since the SyntaxHandler will have already run (maybe we could forward declare it and it will be picked up immediately after...). |
Adding to Alex's comments above, I think in general a Grover oracle can be represented as a
In case there are extra variables that we want to provide to the oracle, the oracle's |
Summary
Allow writing kernels as methods of a class.
Example: Implementing Grover oracles
Grover's algorithm requires an oracle to perform its iterations. However, each oracle varies in the number of inputs it needs. This means that we would need to implement a grover function for each oracle that varies in its kernel signature.
For example, consider the following two oracle signatures:
We cannot write trivially write a generic grover kernel that works on both of these kernels using only functions. We would need separate grover kernels for each oracle.
This leads to duplication of code. The problem exacerbates if grover uses any internal functions (for modularity and separation of concerns sake), because we will need to have separate implementations of those functions two.
We could use variadic function arguments to implement it. However, we lose some of the guarantees that typechecking offers.
C++ classes, however, provide the right abstraction for the task. We can have a
Oracle
abstract base class. Concrete oracles with extend and implement its interface.Note how here we can define a
grover
kernel that can work with any oracle that satisfies theOracle
interfaceThe text was updated successfully, but these errors were encountered: