qbraid-qir
qbraid-qir copied to clipboard
[FEATURE] External Function Calls
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.