Skip to content

Commit

Permalink
Raise OpenQASM3ParseFailure instead of QSSCompilerEOFFailure (ope…
Browse files Browse the repository at this point in the history
…nqasm#305)

## Summary

This PR adds tests for the updates made in
openqasm/qe-qasm#34. This update is made so that
the compiler doesn't fail assertion, but instead raises appropriate
errors
  • Loading branch information
SooluThomas authored and Zhaoyilunnn committed Mar 28, 2024
1 parent 50a3c3d commit 4dc2502
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion conan/qasm/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sources:
hash: "a9cf9fa3599b2045941d154dc91aba5a45beabb7"
hash: "f6d695fd9f18462e65f6290d05ccb4ccb371b288"
requirements:
- "gmp/6.2.1"
- "mpfr/4.1.0"
Expand Down
2 changes: 1 addition & 1 deletion conan/qasm/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class QasmConan(ConanFile):
name = "qasm"
version = "0.3.1"
version = "0.3.2"
url = "https://github.com/openqasm/qe-qasm.git"
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "examples": [True, False]}
Expand Down
2 changes: 1 addition & 1 deletion conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ requirements:
- pybind11/2.11.1
- clang-tools-extra/17.0.5-0@
- llvm/17.0.5-0@
- qasm/0.3.1@qss/stable
- qasm/0.3.2@qss/stable
14 changes: 14 additions & 0 deletions test/python_lib/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ def example_warning_not_in_errors():
"""


@pytest.fixture
def example_incorrect_qasm3():
return """OPENQASM 3;
qubit $0;
gate sx q {}
gate rz(phi) q {}
sx $0;
rz("A") $0;
sx $0;
bit b;
b = measure $0;
"""


@pytest.fixture
def example_unsupported_qasm3_tmpfile(tmp_path, example_unsupported_qasm3_str):
return __create_tmpfile(tmp_path, example_unsupported_qasm3_str)
33 changes: 33 additions & 0 deletions test/python_lib/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,39 @@ def test_warning_not_in_errors(example_warning_not_in_errors):
)


def test_incorrect_qasm3(example_incorrect_qasm3):
"""Test incorrect qasm3 raises error."""

with pytest.raises(exceptions.OpenQASM3ParseFailure) as compfail:
compile_str(
example_incorrect_qasm3,
return_diagnostics=True,
input_type=InputType.QASM3,
output_type=OutputType.MLIR,
output_file=None,
)

assert hasattr(compfail.value, "diagnostics")

diags = compfail.value.diagnostics

assert any(
diag.severity == Severity.Error
and diag.category == ErrorCategory.OpenQASM3ParseFailure
and "1 inconsistent parameters in the gate call for the corresponding gate definition"
in diag.message
for diag in diags
)
assert any("OpenQASM 3 parse error" in str(diag) for diag in diags)

# check string representation of the exception to contain diagnostic messages
assert "Error: OpenQASM 3 parse error" in str(
compfail.value
) and "1 inconsistent parameters in the gate call for the corresponding gate definition" in str(
compfail.value
)


def test_failure_no_hang():
"""Test no hang on malformed inputs."""
with pytest.raises(exceptions.QSSCompilerEOFFailure):
Expand Down

0 comments on commit 4dc2502

Please sign in to comment.