Skip to content

operator precedence error in parser #190

@awcross1

Description

@awcross1

The operator precedence appears to be incorrect while parsing expressions. I noticed this while testing PR #171.

from qiskit import qasm
ast = qasm.Qasm(data="""OPENQASM 2.0;
include "qelib1.inc";
qreg qr[1];
creg cr[1];
u1(0.2*pi+0.3*pi) qr[0]; // precedence error in parser!
measure qr[0] -> cr[0];""").parse()
print(ast.qasm())

Expected Behavior

Multiplication is expected to take precedence over addition.

Current Behavior

The abridged output is

qreg qr[1];
creg cr[1];
u1(((0.200000000000000*(3.141592653589793+0.300000000000000))*3.141592653589793)) qr[0];
measure qr[0] -> cr[0];
on the current master branch (prior to accepting PR #171, which will replace pi by a symbol)

Here addition is taking precedence.

Possible Solution

The problem is near line 955 in _qasmparser.py

Steps to Reproduce (for bugs)

Run the example program above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions