QuTech-Delft / OpenQL

OpenQL: A Portable Quantum Programming Framework for Quantum Accelerators. https://dl.acm.org/doi/10.1145/3474222
https://openql.readthedocs.io
Other
100 stars 44 forks source link

Registering classical functions needs revision #451

Open jvansomeren opened 2 years ago

jvansomeren commented 2 years ago

Libqasm and OpenQL both have ideas on the presence of functions that support classical operations. Libqasm has defaults for them but OpenQL overrides those selectively when constructing a libqasm based cqasm reader. By construction, doing the latter blocks applying the old ones during constant folding in libqasm. So libqasm and OpenQL interfere in this respect and this needs a redesign. See the following quote from the code: Quoting from arch::cc::pass::gen::vq1asm::detail::codegen.h, especially see that last sentence:

/**

Somehow this set of functions is not a proper part of the IR now. More and more passes will need this list so standardization is required. And it needs to be at least a superset of what all OpenQL targets support. And then there needs to be pass for each target that checks and enforces the entry conditions of the code generator (which can have run-time equivalents, etc.). There maybe even functions to which calls are created by the compiler and that are not created because calls were found in a cqasm file; this certainly is true when you consider that there are multiple cqasm versions, but also when another language (e.g. OpenQASM) would be read by OpenQL. So the function list needs to be standardized as part of the IR of OpenQL; and we need to be able to distinguish such standard functions from regular functions.

Agreed. Functions need a proper redesign anyway when the ideas in https://github.com/QuTech-Delft/cQASM-spec/tree/95ee081c0910548b8201c510f88ebc89be9284e8 are implemented. This would also improve the user experience a lot, because currently gate decomposition works a lot like a function call, but otherwise user defined functions don't exist, so there is no way to reuse pieces of code

wvlothuizen commented 2 years ago

moving constant folding and its consequences entirely to OpenQL

IIRC from a conversation with Jeroen, the constant folding was partly intended to allow simple libqasm users (e.g. a simulator) to have this functionality without having to implement anything locally.

The functions are part of the IR (the name, profile and return type are defined in ir->platform), but more information is often needed and there is no easy way to maintain consistency (ql/pass/opt/const_prop/detail/platfom_functions might be extended for that purpose).