Skip to content

Commit

Permalink
Removing broad-exception-raised lint rule and updates (Qiskit#12356)
Browse files Browse the repository at this point in the history
* Removing broad-exception-raised lint rule and updates

* updating assertion types based on review

* updating tests based on review
  • Loading branch information
joesho112358 committed May 10, 2024
1 parent fe275a0 commit f20dad0
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 13 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ disable = [
# remove from here and fix the issues. Else, move it above this section and add a comment
# with the rationale
"arguments-renamed",
"broad-exception-raised",
"consider-using-enumerate",
"consider-using-f-string",
"no-member",
Expand Down
2 changes: 1 addition & 1 deletion qiskit/quantum_info/quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __mul__(self, r):
out_data[3] = r(0) * q(3) - r(1) * q(2) + r(2) * q(1) + r(3) * q(0)
return Quaternion(out_data)
else:
raise Exception("Multiplication by other not supported.")
return NotImplemented

def norm(self):
"""Norm of quaternion."""
Expand Down
2 changes: 1 addition & 1 deletion qiskit/synthesis/clifford/clifford_decompose_bm.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _cx_cost(clifford):
return _cx_cost2(clifford)
if clifford.num_qubits == 3:
return _cx_cost3(clifford)
raise Exception("No Clifford CX cost function for num_qubits > 3.")
raise RuntimeError("No Clifford CX cost function for num_qubits > 3.")


def _rank2(a, b, c, d):
Expand Down
4 changes: 2 additions & 2 deletions qiskit/visualization/bloch.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def set_label_convention(self, convention):
self.zlabel = ["$\\circlearrowleft$", "$\\circlearrowright$"]
self.xlabel = ["$\\leftrightarrow$", "$\\updownarrow$"]
else:
raise Exception("No such convention.")
raise ValueError("No such convention.")

def __str__(self):
string = ""
Expand Down Expand Up @@ -396,7 +396,7 @@ def add_annotation(self, state_or_vector, text, **kwargs):
if isinstance(state_or_vector, (list, np.ndarray, tuple)) and len(state_or_vector) == 3:
vec = state_or_vector
else:
raise Exception("Position needs to be specified by a qubit " + "state or a 3D vector.")
raise TypeError("Position needs to be specified by a qubit state or a 3D vector.")
self.annotations.append({"position": vec, "text": text, "opts": kwargs})

def make_sphere(self):
Expand Down
4 changes: 2 additions & 2 deletions qiskit/visualization/transition_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def __mul__(self, b):
return self._multiply_with_quaternion(b)
elif isinstance(b, (list, tuple, np.ndarray)):
if len(b) != 3:
raise Exception(f"Input vector has invalid length {len(b)}")
raise ValueError(f"Input vector has invalid length {len(b)}")
return self._multiply_with_vector(b)
else:
raise Exception(f"Multiplication with unknown type {type(b)}")
return NotImplemented

def _multiply_with_quaternion(self, q_2):
"""Multiplication of quaternion with quaternion"""
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def random_circuit(
Exception: when invalid options given
"""
if max_operands < 1 or max_operands > 3:
raise Exception("max_operands must be between 1 and 3")
raise ValueError("max_operands must be between 1 and 3")

one_q_ops = [
IGate,
Expand Down
6 changes: 4 additions & 2 deletions test/python/quantum_info/test_quaternions.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ def test_mul_by_quat(self):
def test_mul_by_array(self):
"""Quaternions cannot be multiplied with an array."""
other_array = np.array([0.1, 0.2, 0.3, 0.4])
self.assertRaises(Exception, self.quat_unnormalized.__mul__, other_array)
with self.assertRaises(TypeError):
_ = self.quat_unnormalized * other_array

def test_mul_by_scalar(self):
"""Quaternions cannot be multiplied with a scalar."""
other_scalar = 0.123456789
self.assertRaises(Exception, self.quat_unnormalized.__mul__, other_scalar)
with self.assertRaises(TypeError):
_ = self.quat_unnormalized * other_scalar

def test_rotation(self):
"""Multiplication by -1 should give the same rotation."""
Expand Down
2 changes: 1 addition & 1 deletion test/python/transpiler/test_preset_passmanagers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def mock_get_passmanager_stage(
elif stage_name == "layout":
return PassManager([])
else:
raise Exception("Failure, unexpected stage plugin combo for test")
raise RuntimeError("Failure, unexpected stage plugin combo for test")


def emptycircuit():
Expand Down
2 changes: 1 addition & 1 deletion test/python/transpiler/test_sabre_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ def _visit_block(circuit, qubit_mapping=None):
qargs = tuple(qubit_mapping[x] for x in instruction.qubits)
if not isinstance(instruction.operation, ControlFlowOp):
if len(qargs) > 2 or len(qargs) < 0:
raise Exception("Invalid number of qargs for instruction")
raise RuntimeError("Invalid number of qargs for instruction")
if len(qargs) == 2:
self.assertIn(qargs, self.coupling_edge_set)
else:
Expand Down
2 changes: 1 addition & 1 deletion test/python/transpiler/test_stochastic_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ def _visit_block(circuit, qubit_mapping=None):
qargs = tuple(qubit_mapping[x] for x in instruction.qubits)
if not isinstance(instruction.operation, ControlFlowOp):
if len(qargs) > 2 or len(qargs) < 0:
raise Exception("Invalid number of qargs for instruction")
raise RuntimeError("Invalid number of qargs for instruction")
if len(qargs) == 2:
self.assertIn(qargs, self.coupling_edge_set)
else:
Expand Down

0 comments on commit f20dad0

Please sign in to comment.