QuTech-Delft / OpenSquirrel

A flexible (Python-based) quantum program compiler
Apache License 2.0
7 stars 2 forks source link

Add constant folding pass #121

Closed elenbaasc closed 2 months ago

elenbaasc commented 7 months ago

Constant folding required because certain backend (languages) will not be able to evaluate certain complex expressions. This is generally done early in the compilation strategy.

arcturusannamalai commented 2 months ago

Can you elaborate more? What are the optimization opportunity for quantum circuit ? Is it like a double application of a gate etc. ? Or what's the scenario where you see the breakdown of current technique ?

rturrado commented 2 months ago

A quantum algorithm will contain also some classical code, concretely arithmetic operations. The constant folding pass is a typical classical compiler optimization where you just reduce branches of the AST from an arithmetic operation that can be evaluated in compile time, e.g., 5*sin(pi), to its evaluated value, e.g., -5.

arcturusannamalai commented 2 months ago

@rturrado - I understand the classical portion quite well; I'm not sure how in the QASM / reversible circuit concept of the Quantum algorithms we may encounter constant folding. An example algorithm maybe useful to see.

rturrado commented 2 months ago

Sure, an example may be:

version 3.0

qubit q
bit b

H q
b = measure q
if (b == true)
    X q

That b == true there could be just reduced to b with a constant folding pass. Notice though we still don't have control flow in the current version of the cQASM language.

In general, in a cQASM program, classical code is intertwined with quantum instructions. And this constant folding refers to a simplification of certain classical expressions.