microsoft / qsharp-compiler

Q# compiler, command line tool, and Q# language server
https://docs.microsoft.com/quantum
MIT License
684 stars 172 forks source link

Exponentiation of constants should be evaluated at compile time #1582

Open swernli opened 1 year ago

swernli commented 1 year ago

QIR generation makes use of some limited constant folding to enable features like loop unrolling and array access. However, exponentiation is not constant folded even when both parts of the call are known to be constant. This can cause otherwise valid loops to fail unrolling for hardware execution.

For example:

let limit = 4;
for _ in 1 .. 2^(limit) {
  ...
}

will not be unrolled while the equivalent:

let limit = 4;
for _ in 1 .. 2 <<< limit {
  ...
}

will be unrolled fully.