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

Comments at the end of an OpenQasm 2.0 circuits gives an error #10770

Closed
francabrera opened this issue Sep 5, 2023 · 1 comment · Fixed by #10773
Closed

Comments at the end of an OpenQasm 2.0 circuits gives an error #10770

francabrera opened this issue Sep 5, 2023 · 1 comment · Fixed by #10773
Assignees
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export Rust This PR or issue is related to Rust code in the repository
Milestone

Comments

@francabrera
Copy link
Member

Environment

  • Qiskit Terra version: 0.25.0
  • Python version: 3.10.8
  • Operating system:

What is happening?

If there is a comment as the last line of the OpenQasm 2, the from_qasm_str fails with the next error:

Traceback (most recent call last):
  Cell In[16], line 27
    QuantumCircuit.from_qasm_str(qasm_end_comment) # <- fails
  File /opt/conda/lib/python3.10/site-packages/qiskit/circuit/quantumcircuit.py:2537 in from_qasm_str
    return qasm2.loads(
  File /opt/conda/lib/python3.10/site-packages/qiskit/qasm2/__init__.py:452 in loads
    return _parse.from_bytecode(
  File /opt/conda/lib/python3.10/site-packages/qiskit/qasm2/parse.py:214 in from_bytecode
    for op in bc:
QASM2ParseError: '<input>:10,1: needed a start-of-statement token, but instead got /'

This was working in previous versions of Qiskit (can't confirm which version introduced the error).

How can we reproduce the issue?

from qiskit import QuantumCircuit

qasm_end_comment = """
OPENQASM 2.0;
include "qelib1.inc";

qreg q[2];
creg c[2];
h q[0];
cx q[0], q[1];
measure q -> c;
//comment"""

qasm_end_blank_line= """
OPENQASM 2.0;
include "qelib1.inc";

qreg q[2];
creg c[2];
h q[0];
cx q[0], q[1];
measure q -> c;
//comment
"""


QuantumCircuit.from_qasm_str(qasm_end_comment) # <- fails
QuantumCircuit.from_qasm_str(qasm_end_blank_line) # <- works

What should happen?

The OpenQasm is valid, so it should convert it to OpenQasm 2.0.

Any suggestions?

No response

@francabrera francabrera added the bug Something isn't working label Sep 5, 2023
@jakelishman
Copy link
Member

This will have been introduced by #9784 and consequently released in Terra 0.24 (so the version before the most recent one). Thanks for the report - looks like the new parser has a slight bug where it doesn't chomp whitespace after skipping a comment correctly, though it's a fun edge case!

@jakelishman jakelishman added mod: qasm2 Relating to OpenQASM 2 import or export Rust This PR or issue is related to Rust code in the repository labels Sep 5, 2023
@jakelishman jakelishman self-assigned this Sep 5, 2023
@jakelishman jakelishman added this to the 0.25.2 milestone Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: qasm2 Relating to OpenQASM 2 import or export Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants