Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] pyqasm.unroll() handling of include statements #81

Closed
ryanhill1 opened this issue Nov 14, 2024 · 0 comments · Fixed by #86
Closed

[BUG] pyqasm.unroll() handling of include statements #81

ryanhill1 opened this issue Nov 14, 2024 · 0 comments · Fixed by #86
Labels
bug Something isn't working

Comments

@ryanhill1
Copy link
Member

Right now, pyqasm.unroll() seems to be adding an include "stdgates.inc" statement to all programs, even if the original qasm program didn't have it. This is problematic when using together with a library that doesn't support "include" statements, e.g. Amazon Braket:

from braket.circuits import Circuit
from braket.circuits.serialization import IRType
from braket.ir.openqasm import Program

from pyqasm import dumps, loads

bk_bell = Circuit().h(0).cnot(0, 1).measure(0).measure(1)

qasm_str = bk_bell.to_ir(IRType.OPENQASM).source

program = Program(source=qasm_str)

# Reconstruct the original Circuit from QASM string - this works as expected
circuit = Circuit.from_ir(source=program.source, inputs=program.inputs)

module = loads(qasm_str)

module.unroll()

qasm_unrolled = dumps(module)

program_unrolled = Program(source=qasm_unrolled)

# Attempt to reconstruct a Circuit from the unrolled QASM string - this raises a FileNotFoundError
# Error: FileNotFoundError: [Errno 2] No such file or directory: 'stdgates.inc'
circuit_unrolled = Circuit.from_ir(source=program_unrolled.source, inputs=program_unrolled.inputs)

The include "stdgates.inc" statement should only be added to the unrolled qasm if it was present in the original, input program.

Another related issue, let's say I unroll a qasm string with 2 custom include statements:

from pyqasm import dumps, loads

custom_qasm = """
OPENQASM 3.0;
include "customgates.inc";
include "othergates.inc";
bit[2] b;
qubit[2] q;
h q[0];
cnot q[0], q[1];
b[0] = measure q[0];
b[1] = measure q[1];
"""

module = loads(custom_qasm)

module.unroll()

qasm_unrolled = dumps(module)
OPENQASM 3.0;
include "stdgates.inc";
bit[2] b;
qubit[2] q;
h q[0];
cx q[0], q[1];
b[0] = measure q[0];
b[1] = measure q[1];

Using pyqasm.unroll() all existing include statements are overwritten by include "stdgates.inc";, which shouldn't happen.

@ryanhill1 ryanhill1 added the bug Something isn't working label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant