microsoft / qsharp

Azure Quantum Development Kit, including the Q# programming language, resource estimator, and Quantum Katas
https://microsoft.github.io/qsharp/
MIT License
367 stars 73 forks source link

RCA: Allow updates to mutable variables within a dynamic scope if the variable is also defined within that scope #1581

Open swernli opened 1 month ago

swernli commented 1 month ago

RCA correctly identifies an update of a mutable variable within a dynamic scope as using dynamism on that type. For example, this code is flagged as using a dynamic double:

use q = Qubit();
mutable angle = 0.0;
if M(q) == One {
    set angle = 1.0;
}

However, if the mutable variable is limited to the same or smaller scope than the dynamism, it should be allowed:

use q = Qubit();
if M(q) == One {
    mutable angle = 0.0;
    set angle = 1.0;
}

But this also gets rejected. The check for expression kinds like Assign and AssignUpdate (plus the array variants) should be enlightened to know when the variable being updated is limited in scope and not include flags for dynamism in that case.