amazon-braket / amazon-braket-sdk-python

A Python SDK for interacting with quantum devices on Amazon Braket
https://aws.amazon.com/braket/
Apache License 2.0
294 stars 118 forks source link

Bug: Cannot iteratively update variables in AutoQASM program #929

Closed rmshaffer closed 3 months ago

rmshaffer commented 3 months ago

Repro

Some classical instructions in for loops are not showing up in the output inside the for loop. For this input:

@aq.main(num_qubits=3)
def main():
    val = 0.5  # or aq.FloatVar(0.5)
    for i in aq.range(3):
        val = val + measure(i)
    return val

We get this result:

OPENQASM 3.0;
output float[64] val;
qubit[3] __qubits__;
for int i in [0:3 - 1] {
    bit __bit_1__;
    __bit_1__ = measure __qubits__[i];
}
val = 0.5 + __bit_1__;  # This is wrong!

By instantiating as aq.FloatVar(0.5), the initial assignment shows up float[64] val = 0.5;, but this also shouldn't have a float type declaration, as it's already declared by the output statement. Either way, the val update does not show up inside the for loop.

Additional examples without return statement

We almost succeed with gate arguments, but val doesn't get updated:

@aq.main(num_qubits=3)
def main():
    val = aq.FloatVar(0.5)
    for i in aq.range(3):
        val = val + measure(i)
        rx(0, val)

result:

OPENQASM 3.0;
qubit[3] __qubits__;
float[64] val = 0.5;
for int i in [0:3 - 1] {
    bit __bit_1__;
    __bit_1__ = measure __qubits__[i];
    rx(val + __bit_1__) __qubits__[0];
    # Missing val update statement
}

Another example, although val has no effect, so the missing statement is ineffectual:

@aq.main(num_qubits=3)
def main():
    val = aq.FloatVar(0.5)
    for i in aq.range(3):
        val = val + measure(i)

result:

OPENQASM 3.0;
qubit[3] __qubits__;
float[64] val = 0.5;
for int i in [0:3 - 1] {
    bit __bit_1__;
    __bit_1__ = measure __qubits__[i];
    # Missing val update statement
}
rmshaffer commented 3 months ago

Resolved by #930.