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

Going to and from Braket Circuits and OpenQASM via to_ir and from_ir yields incorrect indexing in classical bit registers during measurements. #1004

Open Altanali opened 2 weeks ago

Altanali commented 2 weeks ago

Describe the bug When taking an OpenQASM program, creating a Circuit via the Circuit.from_ir method, and then translated back to OpenQASM via the Circuit.to_ir method, indices used in classical registers during qubit measurement are all set to 0, even if the indices in the original OpenQASM code were non-zero.

To reproduce Example:

program = """
OPENQASM 3.0;
input float alpha;

bit[2] b;
qubit[2] q;

h q[0];
h q[1];
rx(alpha) q[0];
rx(alpha) q[1];
b[0] = measure q[0];
b[1] = measure q[1];
"""

circuit = Circuit.from_ir(program)
openqasm_str = circuit.to_ir(IRType.OpenQASM).source
print(openqasm_str)

Expected behavior The indices used in the classical registers in the translated output should match the indices in the original openqasm program (even if the classical register name is changed). In this example, we should see

b[0] = measure q[0];
b[1] = measure q[1];

However, the output from running this code yields

b[0] = measure q[0];
b[0] = measure q[1];

Screenshots or logs Print out from running snippet in example:

OPENQASM 3.0;
input float alpha;
bit[2] b;
qubit[2] q;
h q[0];
h q[1];
rx(alpha) q[0];
rx(alpha) q[1];
b[0] = measure q[0];
b[0] = measure q[1];

System information A description of your system. Please provide:

Additional context Add any other context about the problem here.