Skip to content

Commit

Permalink
Fix lint errors for latest pylint (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsp-ibm authored May 20, 2024
1 parent 7f2c579 commit b1348b0
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions .pylintdict
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ eigensolver
eigensolvers
eigenstate
eigenstates
elif
entangler
enum
eps
Expand Down
8 changes: 7 additions & 1 deletion qiskit_algorithms/amplitude_estimators/fae.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2017, 2023.
# (C) Copyright IBM 2017, 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -199,6 +199,12 @@ def estimate(self, estimation_problem: EstimationProblem) -> "FasterAmplitudeEst
def cos_estimate(power, shots):
return self._cos_estimate(problem, power, shots)

# v is first defined in an if below and referenced after in the else where static analysis
# e.g. lint, may determine that v might not be defined before used. So this defines it here
# to avoid lint error. Note the code cannot exit the first stage path until its defined so
# this value here will never get used in practice.
v = 0

for j in range(1, self._maxiter + 1):
num_steps += 1
if first_stage:
Expand Down
7 changes: 7 additions & 0 deletions qiskit_algorithms/eigensolvers/vqd.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ def compute_eigenvalues(
# the same parameters to the ansatz if we do multiple steps
prev_states = []

# These two variables are defined inside if statements and static analysis, e.g. lint can
# see this as a potential error of them not being defined before use. Following the logic
# they do end up being defined before use so the setting of these here, these values would
# not be used in practice.
initial_point = np.asarray([])
initial_points = np.asarray([])

num_initial_points = 0
if self.initial_point is not None:
initial_points = np.reshape(self.initial_point, (-1, self.ansatz.num_parameters))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2022, 2023.
# (C) Copyright IBM 2022, 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -131,6 +131,11 @@ def _run(
gradients = []
partial_sum_n = 0
for n in all_n:
# Ensure gradient is always defined for the append below after the if block
# otherwise lint errors out. I left the if block as it has been coded though
# as the values are checked in the constructor I could have made the last elif
# a simple else instead of defining this here.
gradient = None
if self._method == "central":
result = results.values[partial_sum_n : partial_sum_n + n]
gradient = (result[: n // 2] - result[n // 2 :]) / (2 * self._epsilon)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ def evolve(self, evolution_problem: TimeEvolutionProblem) -> TimeEvolutionResult
else:
observables = None

# Empty define to avoid possibly undefined lint error later here
single_step_evolution_gate = None

if t_param is None:
# the evolution gate
single_step_evolution_gate = PauliEvolutionGate(
Expand Down
4 changes: 3 additions & 1 deletion test/test_grover.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2018, 2023.
# (C) Copyright IBM 2018, 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -312,6 +312,8 @@ def _prepare_grover(
growth_rate=growth_rate,
sample_from_iterations=sample_from_iterations,
)
else:
raise RuntimeError("Unexpected `use_sampler` value {use_sampler}")
return grover


Expand Down
1 change: 1 addition & 0 deletions test/time_evolvers/test_trotter_qrte.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def _get_expected_trotter_qrte(operator, time, num_timesteps, init_state, observ
observables = [obs.to_matrix() for obs in observables]

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

Expand Down

0 comments on commit b1348b0

Please sign in to comment.