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] External Function Calls #167

Open
christian512 opened this issue Oct 4, 2024 · 6 comments · May be fixed by #182
Open

[FEATURE] External Function Calls #167

christian512 opened this issue Oct 4, 2024 · 6 comments · May be fixed by #182
Labels
enhancement New feature or request

Comments

@christian512
Copy link

Feature Description

I have a custom gate definition in OpenQASM3 and a backend that supports this custom gate natively. When converting to QIR I want that qbraid_qir creates a call to an externally linked function for this native gate. Currently the visitor inserts the custom gate operation via _visit_custom_gate_operation().

An example:
I have a backend that natively supports rxx gates. Since rxx is not part of the stdgates.inc, this creates a custom gate definition in OpenQASM3:

OPENQASM 3.0;
include "stdgates.inc";
gate rxx(p0) _gate_q_0, _gate_q_1 {
  h _gate_q_0;
  h _gate_q_1;
  cx _gate_q_0, _gate_q_1;
  rz(p0) _gate_q_1;
  cx _gate_q_0, _gate_q_1;
  h _gate_q_1;
  h _gate_q_0;
}
bit[2] c;
qubit[2] q;
rxx(0.5) q[0], q[1];

I could declare an externally linked function in pyqir for the rxx gate:

__quantum__qis__rxx__body (%Qubit* target1, %Qubit* target2, double theta)

The visitor should then add a call to this function instead of replacing the rxx with the custom gate definition.

This issue might occur more often, since the quantum instruction set is not (yet) fixed and backends have different native gate sets, which are not in stdgates.inc nor in the QIS.

Implementation (Optional)

I suggest to add a dictionary _external_gates to the BasicQasmVisitor class, that holds the external gate functions:

self._external_gates: dict[str, pyqir.Function] = {}

When visiting a generic gate operation, it should be first checked if an external function call is possible (probably insert here).

The initialization of the pyqir.Function objects might be a bit difficult, since qbraid_qir would have to add the external function declarations to the qir module using mod.add_external_function(). Hence, the user would have to pass the pyqir.FunctionType and the name of the function to the BasicQasmVisitor.

An example of externally linked functions in pyqir can be found here.

@christian512 christian512 added the enhancement New feature or request label Oct 4, 2024
@TheGupta2012
Copy link
Collaborator

TheGupta2012 commented Oct 4, 2024

Hi @christian512 , thanks for the suggestion! We are right now in the process of decoupling the BasicQasmVisitor into an independent semantic analyser which would change how we are handling these conversions.

We are envisioning something like an unrolled qasm program which would more simply be converted as a list of statements to QIR like -

import pyqasm 
from qbraid_qir.qasm3 import QasmQIRConverter
def convert_to_qir(raw_qasm_str, ....): 
    unrolled_qasm = pyqasm.unroll(raw_qasm_str) 
    qir_converter = QasmQIRConverter(...)
    ....
    return qir_converter.convert(unrolled_qasm) 

In this structure, an external gate list could be passed to pyqasm.unroll() which forces the function to leave out certain gates during conversion. Similarly, these can be then passed to the QasmQIRConverter initialization before we call the convert method. Internally, then, we can construct the external function definition and inject it.

I think, it'll be best to wait till the de-coupling is completed (as the repo is already intimately linked with pyqir, and we don't want any more coupling right now!). Post that, we'll be happy to incorporate this functionality and discuss on the implementation details.

@ryanhill1
Copy link
Member

I like the idea @christian512 !

@christian512
Copy link
Author

Thanks for the feedback, @TheGupta2012 and @ryanhill1!

I like the idea of decoupling from pyqir. Is there an approximate timeline for the changes to the independent semantic analyser?

If there is a branch where I can contribute let me know :)

@TheGupta2012
Copy link
Collaborator

TheGupta2012 commented Oct 7, 2024

It is happening right now and a rough timeline for v0.1 would probably be in 1-2 weeks.
Also, thanks for your interest in contributing! We will surely let you know about the details.

@ryanhill1
Copy link
Member

@christian512 Migration / development on the new project is currently underway at https://github.com/qBraid/pyqasm. Once we've merged PR #3 the project will (hopefully) be stable-enough to then allow us to generate concrete issues and accept new contributors. Like @TheGupta2012 mentioned, this will likely be within 1-2 weeks. Would love to have you as one of the first contributors!

@TheGupta2012
Copy link
Collaborator

TheGupta2012 commented Oct 15, 2024

Hi @christian512 , #3 has now merged and we are working towards integration of pyqasm with qbraid-qir in #173 .

Fee free to look at some issues which I opened up in pyqasm or if you wanna start the implementation of the custom gates feature first that'll be cool too!

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 a pull request may close this issue.

3 participants