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 program transpilation should mangle OpenQASM reserved keywords (if they're not Python keywords) #12

Closed rmshaffer closed 5 months ago

rmshaffer commented 6 months ago

Summary

In AutoQASM programs, Python symbol names which are reserved words in OpenQASM result in syntax errors in the serialized OpenQASM program. For example, a variable named input could be transpiled to input_ to avoid this problem.

Repro Steps

To reproduce the bug:

@aq.subroutine
def make_input_state(input: int, theta: float):
    rx(input, theta)

@aq.main(num_qubits=3)
def teleportation(theta):
    input, alice, bob = 0, 1, 2
    make_input_state(input, theta)
    c = measure(bob)

sim = LocalSimulator()
task = sim.run(teleportation, inputs={"theta": math.pi/2}, shots=100)

Expected Result

This should build and run the program successfully.

Actual Result

This fails because input is a reserved OpenQASM keyword, and so the generated OpenQASM program results in a syntax error.

atharva-satpute commented 5 months ago

I would like work on this issue as part of unitary hack.

With my local changes, test test_multi_bind_parameters (and few others) are failing. But usually this test works fine. qubit variable declared in this function is not causing any problems. Or is this issue valid only when using simulators?

rmshaffer commented 5 months ago

@atharva-satpute can you share your local changes, for example, by pushing them to a branch or opening a draft PR? A draft PR would be best, since then we can trigger GitHub to run the unit tests and see the error messages.

The simulator is used in the issue description because currently the OpenQASM syntax is only validated by the simulator - there is not currently any syntax validation during the AutoQASM serialization process itself.

atharva-satpute commented 5 months ago

@rmshaffer, I have opened a draft PR

atharva-satpute commented 5 months ago

@rmshaffer, could you take a look at the comments

rmshaffer commented 5 months ago

Sure - replied to your comment in the PR #28. Feel free to push a new revision to that PR whenever you're ready!

atharva-satpute commented 5 months ago

I have added the changes. Let me know if any keywords are missing or if the list needs to be somewhere else. Thanks!