Skip to content

Commit b1348b0

Browse files
authored
Fix lint errors for latest pylint (qiskit-community#177)
1 parent 7f2c579 commit b1348b0

File tree

7 files changed

+28
-3
lines changed

7 files changed

+28
-3
lines changed

.pylintdict

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ eigensolver
7777
eigensolvers
7878
eigenstate
7979
eigenstates
80+
elif
8081
entangler
8182
enum
8283
eps

qiskit_algorithms/amplitude_estimators/fae.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2017, 2023.
3+
# (C) Copyright IBM 2017, 2024.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -199,6 +199,12 @@ def estimate(self, estimation_problem: EstimationProblem) -> "FasterAmplitudeEst
199199
def cos_estimate(power, shots):
200200
return self._cos_estimate(problem, power, shots)
201201

202+
# v is first defined in an if below and referenced after in the else where static analysis
203+
# e.g. lint, may determine that v might not be defined before used. So this defines it here
204+
# to avoid lint error. Note the code cannot exit the first stage path until its defined so
205+
# this value here will never get used in practice.
206+
v = 0
207+
202208
for j in range(1, self._maxiter + 1):
203209
num_steps += 1
204210
if first_stage:

qiskit_algorithms/eigensolvers/vqd.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ def compute_eigenvalues(
248248
# the same parameters to the ansatz if we do multiple steps
249249
prev_states = []
250250

251+
# These two variables are defined inside if statements and static analysis, e.g. lint can
252+
# see this as a potential error of them not being defined before use. Following the logic
253+
# they do end up being defined before use so the setting of these here, these values would
254+
# not be used in practice.
255+
initial_point = np.asarray([])
256+
initial_points = np.asarray([])
257+
251258
num_initial_points = 0
252259
if self.initial_point is not None:
253260
initial_points = np.reshape(self.initial_point, (-1, self.ansatz.num_parameters))

qiskit_algorithms/gradients/finite_diff/finite_diff_estimator_gradient.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2022, 2023.
3+
# (C) Copyright IBM 2022, 2024.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -131,6 +131,11 @@ def _run(
131131
gradients = []
132132
partial_sum_n = 0
133133
for n in all_n:
134+
# Ensure gradient is always defined for the append below after the if block
135+
# otherwise lint errors out. I left the if block as it has been coded though
136+
# as the values are checked in the constructor I could have made the last elif
137+
# a simple else instead of defining this here.
138+
gradient = None
134139
if self._method == "central":
135140
result = results.values[partial_sum_n : partial_sum_n + n]
136141
gradient = (result[: n // 2] - result[n // 2 :]) / (2 * self._epsilon)

qiskit_algorithms/time_evolvers/trotterization/trotter_qrte.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ def evolve(self, evolution_problem: TimeEvolutionProblem) -> TimeEvolutionResult
214214
else:
215215
observables = None
216216

217+
# Empty define to avoid possibly undefined lint error later here
218+
single_step_evolution_gate = None
219+
217220
if t_param is None:
218221
# the evolution gate
219222
single_step_evolution_gate = PauliEvolutionGate(

test/test_grover.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2018, 2023.
3+
# (C) Copyright IBM 2018, 2024.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -312,6 +312,8 @@ def _prepare_grover(
312312
growth_rate=growth_rate,
313313
sample_from_iterations=sample_from_iterations,
314314
)
315+
else:
316+
raise RuntimeError("Unexpected `use_sampler` value {use_sampler}")
315317
return grover
316318

317319

test/time_evolvers/test_trotter_qrte.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def _get_expected_trotter_qrte(operator, time, num_timesteps, init_state, observ
262262
observables = [obs.to_matrix() for obs in observables]
263263

264264
psi = Statevector(init_state).data
265+
ops = [] # Define to avoid possibly undefined error later on the line where it's used
265266
if t_param is None:
266267
ops = [Pauli(op).to_matrix() * np.real(coeff) for op, coeff in operator.to_list()]
267268

0 commit comments

Comments
 (0)