Skip to content

Commit 7570295

Browse files
authored
Improve custom equals fns (#226)
1 parent ca48697 commit 7570295

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

qiskit_algorithms/optimizers/gradient_descent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ class GradientDescentState(OptimizerState):
4646
# too as it does not appear to use super by default and without this failed
4747
# the exact same way. Note it does not include learning rate as that field
4848
# is not included in the compare as pre the field decorator.
49-
def __eq__(self, other: GradientDescentState):
49+
def __eq__(self, other: object):
50+
if not isinstance(other, GradientDescentState):
51+
return NotImplemented
5052
return super().__eq__(other) and self.stepsize == other.stepsize
5153

5254

qiskit_algorithms/optimizers/steppable_optimizer.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ class OptimizerState:
7676
# Under Python 3.13 the auto-generated equal fails with a error around
7777
# using numpy all or any. See https://github.com/qiskit-community/qiskit-algorithms/pull/225
7878
# for further information. Hence this custom function was added.
79-
def __eq__(self, other: OptimizerState):
79+
def __eq__(self, other: object):
80+
if not isinstance(other, OptimizerState):
81+
return NotImplemented
8082
return (
8183
(
8284
self.x == other.x
83-
if isinstance(self.x, float)
84-
else (self.x.shape == other.x.shape and (self.x == other.x).all())
85+
if isinstance(self.x, float) and isinstance(other.x, float)
86+
else (
87+
False
88+
if isinstance(self.x, float) or isinstance(other.x, float)
89+
else self.x.shape == other.x.shape and (self.x == other.x).all()
90+
)
8591
)
8692
and self.fun == other.fun
8793
and self.jac == other.jac

0 commit comments

Comments
 (0)