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

Unify OpenQASM 2 exceptions #9956

Merged
merged 3 commits into from
Jul 14, 2023
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
5 changes: 3 additions & 2 deletions qiskit/circuit/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from qiskit.circuit.exceptions import CircuitError
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit.classicalregister import ClassicalRegister, Clbit
from qiskit.qasm.exceptions import QasmError
from qiskit.qobj.qasm_qobj import QasmQobjInstruction
from qiskit.circuit.parameter import ParameterExpression
from qiskit.circuit.operation import Operation
Expand Down Expand Up @@ -438,10 +437,12 @@ def __deepcopy__(self, _memo=None):

def _qasmif(self, string):
"""Print an if statement if needed."""
from qiskit.qasm2 import QASM2ExportError # pylint: disable=cyclic-import

if self.condition is None:
return string
if not isinstance(self.condition[0], ClassicalRegister):
raise QasmError(
raise QASM2ExportError(
"OpenQASM 2 can only condition on registers, but got '{self.condition[0]}'"
)
return "if(%s==%d) " % (self.condition[0].name, self.condition[1]) + string
Expand Down
8 changes: 5 additions & 3 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from qiskit.circuit.instruction import Instruction
from qiskit.circuit.gate import Gate
from qiskit.circuit.parameter import Parameter
from qiskit.qasm.exceptions import QasmError
from qiskit.circuit.exceptions import CircuitError
from qiskit.utils import optionals as _optionals
from .parameterexpression import ParameterExpression, ParameterValueType
Expand Down Expand Up @@ -1635,11 +1634,14 @@ def qasm(
Raises:
MissingOptionalLibraryError: If pygments is not installed and ``formatted`` is
``True``.
QasmError: If circuit has free parameters.
QASM2ExportError: If circuit has free parameters.
"""
from qiskit.qasm2 import QASM2ExportError # pylint: disable=cyclic-import

if self.num_parameters > 0:
raise QasmError("Cannot represent circuits with unbound parameters in OpenQASM 2.")
raise QASM2ExportError(
"Cannot represent circuits with unbound parameters in OpenQASM 2."
)

existing_gate_names = {
"barrier",
Expand Down
1 change: 0 additions & 1 deletion qiskit/qasm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
:toctree: ../stubs/

Qasm
QasmError


Pygments
Expand Down
20 changes: 3 additions & 17 deletions qiskit/qasm/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,7 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
Exception for errors raised while parsing OPENQASM.
"""
"""Exception for errors raised while handling OpenQASM 2.0."""

from qiskit.exceptions import QiskitError


class QasmError(QiskitError):
"""Base class for errors raised while parsing OPENQASM."""

def __init__(self, *msg):
"""Set the error message."""
super().__init__(*msg)
self.msg = " ".join(msg)

def __str__(self):
"""Return the message."""
return repr(self.msg)
# Re-export from the new place to ensure that old code continues to work.
from qiskit.qasm2.exceptions import QASM2Error as QasmError # pylint: disable=unused-import
2 changes: 2 additions & 0 deletions test/python/circuit/test_gate_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ class TestGateEquivalenceEqual(QiskitTestCase):
"PermutationGate",
"Commuting2qBlock",
"PauliEvolutionGate",
"_U0Gate",
"_DefinedGate",
}
# Amazingly, Python's scoping rules for class bodies means that this is the closest we can get
# to a "natural" comprehension or functional iterable definition:
Expand Down