From 7f33cd89bc1682017f983b3671f57fab93f8598f Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Thu, 13 Apr 2023 00:08:13 +0100 Subject: [PATCH] Unify OpenQASM 2 exceptions Since gh-9784 added a new `qiskit.qasm2` package and suitable exceptions for both import and export of OpenQASM 2 programs, this commit now unifies the exception handling through the rest of the library to use these exceptions. To avoid breaks in compatibility, the old `qiskit.qasm.QasmError` is now a re-export of `qiskit.qasm2.QASM2Error`. This is the base error of both the exporter and importers, despite the old documentation of `QasmError` claiming to be specifically for parser errors. In practice, the exporter also emitted `QasmError`, so having that be `QASM2Error` allows people catching that error to get the same behaviour in these new forms. The actual exporter and importer are updated to emit the precise subclasses. --- qiskit/circuit/instruction.py | 5 +++-- qiskit/circuit/quantumcircuit.py | 8 +++++--- qiskit/qasm/__init__.py | 1 - qiskit/qasm/exceptions.py | 20 +++----------------- 4 files changed, 11 insertions(+), 23 deletions(-) diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py index 6721983d3bc9..c72705532338 100644 --- a/qiskit/circuit/instruction.py +++ b/qiskit/circuit/instruction.py @@ -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 @@ -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 diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py index ccf9ff724abb..051138caef31 100644 --- a/qiskit/circuit/quantumcircuit.py +++ b/qiskit/circuit/quantumcircuit.py @@ -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 @@ -1630,11 +1629,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", diff --git a/qiskit/qasm/__init__.py b/qiskit/qasm/__init__.py index 8f58bd10ede2..f442aaef845f 100644 --- a/qiskit/qasm/__init__.py +++ b/qiskit/qasm/__init__.py @@ -24,7 +24,6 @@ :toctree: ../stubs/ Qasm - QasmError Pygments diff --git a/qiskit/qasm/exceptions.py b/qiskit/qasm/exceptions.py index ad869c23e656..7bc40db35b8b 100644 --- a/qiskit/qasm/exceptions.py +++ b/qiskit/qasm/exceptions.py @@ -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