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

Improve error message on bad OpenQASM 3 basis_gates argument (backport #12945) #12946

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion qiskit/qasm3/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,13 @@ def build_program(self):
if builtin in _BUILTIN_GATES:
# It's built into the langauge; we don't need to re-add it.
continue
self.symbols.register_gate_without_definition(builtin, None)
try:
self.symbols.register_gate_without_definition(builtin, None)
except QASM3ExporterError as exc:
raise QASM3ExporterError(
f"Cannot use '{builtin}' as a basis gate for the reason in the prior exception."
" Consider renaming the gate if needed, or omitting this basis gate if not."
) from exc

header = ast.Header(ast.Version("3.0"), list(self.build_includes()))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
The OpenQASM 3 exporter will now correctly error when asked to use a keyword or other invalid
identifier as a "basis gate", as it has no way of putting out correct output in these cases.
8 changes: 8 additions & 0 deletions test/python/qasm3/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2699,3 +2699,11 @@ def test_disallow_export_of_inner_scope(self):
QASM3ExporterError, "cannot export an inner scope.*as a top-level program"
):
dumps(qc)

def test_no_basis_gate_with_keyword(self):
"""Test that keyword cannot be used as a basis gate."""
qc = QuantumCircuit()
with self.assertRaisesRegex(QASM3ExporterError, "Cannot use 'reset' as a basis gate") as cm:
dumps(qc, basis_gates=["U", "reset"])
self.assertIsInstance(cm.exception.__cause__, QASM3ExporterError)
self.assertRegex(cm.exception.__cause__.message, "cannot use the keyword 'reset'")
Loading