Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing consider-using-enumerate from lint exclusions and updates #12286

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ disable = [
# with the rationale
"arguments-renamed",
"broad-exception-raised",
"consider-using-enumerate",
"consider-using-f-string",
"no-member",
"no-value-for-parameter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ def evaluate(self, x: float) -> float:
"""

y = 0
for i in range(0, len(self.breakpoints)):
y = y + (x >= self.breakpoints[i]) * (np.poly1d(self.mapped_coeffs[i][::-1])(x))
for i, breakpt in enumerate(self.breakpoints):
y = y + (x >= breakpt) * (np.poly1d(self.mapped_coeffs[i][::-1])(x))

return y

Expand Down
12 changes: 6 additions & 6 deletions qiskit/synthesis/stabilizer/stabilizer_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def synth_circuit_from_stabilizers(
circuit = QuantumCircuit(num_qubits)

used = 0
for i in range(len(stabilizer_list)):
curr_stab = stabilizer_list[i].evolve(Clifford(circuit), frame="s")
for i, stabilizer in enumerate(stabilizer_list):
curr_stab = stabilizer.evolve(Clifford(circuit), frame="s")

# Find pivot.
pivot = used
Expand All @@ -81,17 +81,17 @@ def synth_circuit_from_stabilizers(
if pivot == num_qubits:
if curr_stab.x.any():
raise QiskitError(
f"Stabilizer {i} ({stabilizer_list[i]}) anti-commutes with some of "
f"Stabilizer {i} ({stabilizer}) anti-commutes with some of "
"the previous stabilizers."
)
if curr_stab.phase == 2:
raise QiskitError(
f"Stabilizer {i} ({stabilizer_list[i]}) contradicts "
f"Stabilizer {i} ({stabilizer}) contradicts "
"some of the previous stabilizers."
)
if curr_stab.z.any() and not allow_redundant:
raise QiskitError(
f"Stabilizer {i} ({stabilizer_list[i]}) is a product of the others "
f"Stabilizer {i} ({stabilizer}) is a product of the others "
"and allow_redundant is False. Add allow_redundant=True "
"to the function call if you want to allow redundant stabilizers."
)
Expand Down Expand Up @@ -133,7 +133,7 @@ def synth_circuit_from_stabilizers(
circuit.swap(pivot, used)

# fix sign
curr_stab = stabilizer_list[i].evolve(Clifford(circuit), frame="s")
curr_stab = stabilizer.evolve(Clifford(circuit), frame="s")
if curr_stab.phase == 2:
circuit.x(used)
used += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ def _run_on_self_inverse(self, dag: DAGCircuit):
partitions = []
chunk = []
max_index = len(gate_cancel_run) - 1
for i in range(len(gate_cancel_run)):
if gate_cancel_run[i].op == gate:
chunk.append(gate_cancel_run[i])
for i, cancel_gate in enumerate(gate_cancel_run):
if cancel_gate.op == gate:
chunk.append(cancel_gate)
else:
if chunk:
partitions.append(chunk)
chunk = []
continue
if i == max_index or gate_cancel_run[i].qargs != gate_cancel_run[i + 1].qargs:
if i == max_index or cancel_gate.qargs != gate_cancel_run[i + 1].qargs:
partitions.append(chunk)
chunk = []
# Remove an even number of gates from each chunk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def _find_forward_candidates(self, node_id_t):
"""
matches = []

for i in range(0, len(self.match)):
matches.append(self.match[i][0])
for match in self.match:
matches.append(match[0])

pred = matches.copy()
if len(pred) > 1:
Expand Down
4 changes: 2 additions & 2 deletions qiskit/transpiler/passmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def _finalize_layouts(self, dag):
# Ordered list of original qubits
original_qubits_reverse = {v: k for k, v in original_qubit_indices.items()}
original_qubits = []
for i in range(len(original_qubits_reverse)):
original_qubits.append(original_qubits_reverse[i])
for v, k in original_qubits_reverse.items():
original_qubits.append(k)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a behavioural change to me. Dictionary iteration order is insertion order, but the previous form was specifically looking up in numerical order of the v items.

The vs are ints here (from 0 to n-1) and the k are Qubit instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, i see it now. ok, i'll have to update this


virtual_permutation_layout_inv = virtual_permutation_layout.inverse(
original_qubits, original_qubits
Expand Down
37 changes: 16 additions & 21 deletions qiskit/visualization/bloch.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,11 @@ def plot_axes_labels(self):
def plot_vectors(self):
"""Plot vector"""
# -X and Y data are switched for plotting purposes
for k in range(len(self.vectors)):
for k, vector in enumerate(self.vectors):

xs3d = self.vectors[k][1] * np.array([0, 1])
ys3d = -self.vectors[k][0] * np.array([0, 1])
zs3d = self.vectors[k][2] * np.array([0, 1])
xs3d = vector[1] * np.array([0, 1])
ys3d = -vector[0] * np.array([0, 1])
zs3d = vector[2] * np.array([0, 1])

color = self.vector_color[np.mod(k, len(self.vector_color))]

Expand All @@ -617,15 +617,10 @@ def plot_vectors(self):
def plot_points(self):
"""Plot points"""
# -X and Y data are switched for plotting purposes
for k in range(len(self.points)):
num = len(self.points[k][0])
for k, point in enumerate(self.points):
num = len(point[0])
dist = [
np.sqrt(
self.points[k][0][j] ** 2
+ self.points[k][1][j] ** 2
+ self.points[k][2][j] ** 2
)
for j in range(num)
np.sqrt(point[0][j] ** 2 + point[1][j] ** 2 + point[2][j] ** 2) for j in range(num)
]
if any(abs(dist - dist[0]) / dist[0] > 1e-12):
# combine arrays so that they can be sorted together
Expand All @@ -637,9 +632,9 @@ def plot_points(self):
indperm = np.arange(num)
if self.point_style[k] == "s":
self.axes.scatter(
np.real(self.points[k][1][indperm]),
-np.real(self.points[k][0][indperm]),
np.real(self.points[k][2][indperm]),
np.real(point[1][indperm]),
-np.real(point[0][indperm]),
np.real(point[2][indperm]),
s=self.point_size[np.mod(k, len(self.point_size))],
alpha=1,
edgecolor=None,
Expand All @@ -656,9 +651,9 @@ def plot_points(self):
marker = self.point_marker[np.mod(k, len(self.point_marker))]
pnt_size = self.point_size[np.mod(k, len(self.point_size))]
self.axes.scatter(
np.real(self.points[k][1][indperm]),
-np.real(self.points[k][0][indperm]),
np.real(self.points[k][2][indperm]),
np.real(point[1][indperm]),
-np.real(point[0][indperm]),
np.real(point[2][indperm]),
s=pnt_size,
alpha=1,
edgecolor=None,
Expand All @@ -670,9 +665,9 @@ def plot_points(self):
elif self.point_style[k] == "l":
color = self.point_color[np.mod(k, len(self.point_color))]
self.axes.plot(
np.real(self.points[k][1]),
-np.real(self.points[k][0]),
np.real(self.points[k][2]),
np.real(point[1]),
-np.real(point[0]),
np.real(point[2]),
alpha=0.75,
zdir="z",
color=color,
Expand Down
6 changes: 3 additions & 3 deletions test/python/synthesis/test_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ def assertDebugOnly(self): # FIXME: when at python 3.10+ replace with assertNoL
"""Context manager, asserts log is emitted at level DEBUG but no higher"""
with self.assertLogs("qiskit.synthesis", "DEBUG") as ctx:
yield
for i in range(len(ctx.records)):
for i, record in enumerate(ctx.records):
self.assertLessEqual(
ctx.records[i].levelno,
record.levelno,
logging.DEBUG,
msg=f"Unexpected logging entry: {ctx.output[i]}",
)
self.assertIn("Requested fidelity:", ctx.records[i].getMessage())
self.assertIn("Requested fidelity:", record.getMessage())

def assertRoundTrip(self, weyl1: TwoQubitWeylDecomposition):
"""Fail if eval(repr(weyl1)) not equal to weyl1"""
Expand Down