amazon-braket / autoqasm

AutoQASM is an experimental module offering a new quantum-imperative programming experience in Python for developing quantum programs.
Apache License 2.0
15 stars 9 forks source link

AutoQASM generates OpenQASM which assumes subroutine access to input variables #7

Open ajberdy opened 11 months ago

ajberdy commented 11 months ago

The following code is technically against the OpenQASM spec, as you can only close over global const variables (https://openqasm.com/language/scope.html#subroutine-and-gate-scope)

We may want to choose to interpret this as inputs allowed, since it's ambiguous whether those can change at runtime (be overwritten)

@aq.subroutine
def rx_alpha(qubit: int):
    rx(qubit, FreeParameter("alpha"))

@aq.main(num_qubits=3)
def parametric():
    rx_alpha(2)

expected = """OPENQASM 3.0;
def rx_alpha(int[32] qubit) {
    rx(alpha) __qubits__[qubit];
}
input float[64] alpha;
qubit[3] __qubits__;
rx_alpha(2);"""

Note that the generated rx_alpha subroutine references the alpha input variable, which is defined only at global scope.