Skip to content

Commit

Permalink
Fix the possibly-used-before-assignment in pylint (#12542)
Browse files Browse the repository at this point in the history
* fix the possibly-used-before-assignment in pylint

* qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py

* test.python.quantum_info.operators.test_utils

* Apply suggestions from code review

Co-authored-by: Jake Lishman <[email protected]>

* https://github.com/Qiskit/qiskit/pull/12542/files#r1636214044

* RuntimeError

* RuntimeError

---------

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
1ucian0 and jakelishman committed Jun 13, 2024
1 parent 8a1bcc2 commit 439de04
Show file tree
Hide file tree
Showing 24 changed files with 67 additions and 41 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ disable = [
"consider-using-f-string",
"no-member", # for dynamically created members
"not-context-manager",
"possibly-used-before-assignment",
"unnecessary-lambda-assignment", # do not want to implement
"unspecified-encoding", # do not want to implement
]
Expand Down
2 changes: 2 additions & 0 deletions qiskit/primitives/backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def _run_circuits(
max_circuits = getattr(backend.configuration(), "max_experiments", None)
elif isinstance(backend, BackendV2):
max_circuits = backend.max_circuits
else:
raise RuntimeError("Backend version not supported")
if max_circuits:
jobs = [
backend.run(circuits[pos : pos + max_circuits], **run_options)
Expand Down
9 changes: 7 additions & 2 deletions qiskit/pulse/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ def _measure_v1(
for qubit in qubits:
measure_groups.add(tuple(meas_map[qubit]))
for measure_group_qubits in measure_groups:
if qubit_mem_slots is not None:
unused_mem_slots = set(measure_group_qubits) - set(qubit_mem_slots.values())

unused_mem_slots = (
set()
if qubit_mem_slots is None
else set(measure_group_qubits) - set(qubit_mem_slots.values())
)

try:
default_sched = inst_map.get(measure_name, measure_group_qubits)
except exceptions.PulseError as ex:
Expand Down
1 change: 1 addition & 0 deletions qiskit/qpy/binary_io/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ def _parse_custom_operation(
) = custom_operations[gate_name]
else:
type_str, num_qubits, num_clbits, definition = custom_operations[gate_name]
base_gate_raw = ctrl_state = num_ctrl_qubits = None
# Strip the trailing "_{uuid}" from the gate name if the version >=11
if version >= 11:
gate_name = "_".join(gate_name.split("_")[:-1])
Expand Down
20 changes: 9 additions & 11 deletions qiskit/quantum_info/operators/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,29 +414,27 @@ def from_circuit(

from qiskit.synthesis.permutation.permutation_utils import _inverse_pattern

op = Operator(circuit)

if initial_layout is not None:
input_qubits = [None] * len(layout.input_qubit_mapping)
for q, p in layout.input_qubit_mapping.items():
input_qubits[p] = q

initial_permutation = initial_layout.to_permutation(input_qubits)
initial_permutation_inverse = _inverse_pattern(initial_permutation)
op = op.apply_permutation(initial_permutation, True)

if final_layout is not None:
if final_layout is not None:
final_permutation = final_layout.to_permutation(circuit.qubits)
final_permutation_inverse = _inverse_pattern(final_permutation)
op = op.apply_permutation(final_permutation_inverse, False)
op = op.apply_permutation(initial_permutation_inverse, False)
elif final_layout is not None:
final_permutation = final_layout.to_permutation(circuit.qubits)
final_permutation_inverse = _inverse_pattern(final_permutation)

op = Operator(circuit)

if initial_layout:
op = op.apply_permutation(initial_permutation, True)

if final_layout:
op = op.apply_permutation(final_permutation_inverse, False)

if initial_layout:
op = op.apply_permutation(initial_permutation_inverse, False)

return op

def is_unitary(self, atol=None, rtol=None):
Expand Down
20 changes: 10 additions & 10 deletions qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,19 @@ def __init__(

pauli_list = PauliList(data.copy() if copy and hasattr(data, "copy") else data)

if isinstance(coeffs, np.ndarray):
dtype = object if coeffs.dtype == object else complex
elif coeffs is not None:
if not isinstance(coeffs, (np.ndarray, Sequence)):
coeffs = [coeffs]
if any(isinstance(coeff, ParameterExpression) for coeff in coeffs):
dtype = object
else:
dtype = complex

if coeffs is None:
coeffs = np.ones(pauli_list.size, dtype=complex)
else:
if isinstance(coeffs, np.ndarray):
dtype = object if coeffs.dtype == object else complex
else:
if not isinstance(coeffs, Sequence):
coeffs = [coeffs]
if any(isinstance(coeff, ParameterExpression) for coeff in coeffs):
dtype = object
else:
dtype = complex

coeffs_asarray = np.asarray(coeffs, dtype=dtype)
coeffs = (
coeffs_asarray.copy()
Expand Down
14 changes: 7 additions & 7 deletions qiskit/transpiler/passes/basis/unroll_custom_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def run(self, dag):
if self._basis_gates is None and self._target is None:
return dag

device_insts = {"measure", "reset", "barrier", "snapshot", "delay", "store"}
if self._target is None:
basic_insts = {"measure", "reset", "barrier", "snapshot", "delay", "store"}
device_insts = basic_insts | set(self._basis_gates)
device_insts |= set(self._basis_gates)

for node in dag.op_nodes():
if isinstance(node.op, ControlFlowOp):
Expand All @@ -77,14 +77,14 @@ def run(self, dag):

controlled_gate_open_ctrl = isinstance(node.op, ControlledGate) and node.op._open_ctrl
if not controlled_gate_open_ctrl:
inst_supported = (
self._target.instruction_supported(
if self._target is not None:
inst_supported = self._target.instruction_supported(
operation_name=node.op.name,
qargs=tuple(dag.find_bit(x).index for x in node.qargs),
)
if self._target is not None
else node.name in device_insts
)
else:
inst_supported = node.name in device_insts

if inst_supported or self._equiv_lib.has_entry(node.op):
continue
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import numpy as np

from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.transpiler.basepasses import TransformationPass
from qiskit.transpiler.passmanager import PassManager
from qiskit.transpiler.passes.optimization.commutation_analysis import CommutationAnalysis
Expand Down Expand Up @@ -72,9 +71,6 @@ def run(self, dag):
Returns:
DAGCircuit: the optimized DAG.
Raises:
TranspilerError: when the 1-qubit rotation gates are not found
"""
var_z_gate = None
z_var_gates = [gate for gate in dag.count_ops().keys() if gate in self._var_z_map]
Expand Down Expand Up @@ -146,7 +142,7 @@ def run(self, dag):
or len(current_node.qargs) != 1
or current_node.qargs[0] != run_qarg
):
raise TranspilerError("internal error")
raise RuntimeError("internal error")

if current_node.name in ["p", "u1", "rz", "rx"]:
current_angle = float(current_node.op.params[0])
Expand All @@ -156,6 +152,10 @@ def run(self, dag):
current_angle = np.pi / 4
elif current_node.name == "s":
current_angle = np.pi / 2
else:
raise RuntimeError(
f"Angle for operation {current_node.name } is not defined"
)

# Compose gates
total_angle = current_angle + total_angle
Expand All @@ -167,6 +167,8 @@ def run(self, dag):
new_op = var_z_gate(total_angle)
elif cancel_set_key[0] == "x_rotation":
new_op = RXGate(total_angle)
else:
raise RuntimeError("impossible case")

new_op_phase = 0
if np.mod(total_angle, (2 * np.pi)) > _CUTOFF_PRECISION:
Expand Down
2 changes: 2 additions & 0 deletions qiskit/visualization/circuit/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,8 @@ def _flow_op_gate(self, node, node_data, glob_data):
flow_text = " For"
elif isinstance(node.op, SwitchCaseOp):
flow_text = "Switch"
else:
flow_text = node.op.name

# Some spacers. op_spacer moves 'Switch' back a bit for alignment,
# expr_spacer moves the expr over to line up with 'Switch' and
Expand Down
1 change: 1 addition & 0 deletions test/benchmarks/randomized_benchmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def clifford_2_qubit_circuit(num):
qc = QuantumCircuit(2)
if vals[0] == 0 or vals[0] == 3:
(form, i0, i1, j0, j1, p0, p1) = vals
k0, k1 = (None, None)
else:
(form, i0, i1, j0, j1, k0, k1, p0, p1) = vals
if i0 == 1:
Expand Down
2 changes: 2 additions & 0 deletions test/benchmarks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def random_circuit(
operation = rng.choice(two_q_ops)
elif num_operands == 3:
operation = rng.choice(three_q_ops)
else:
raise RuntimeError("not supported number of operands")
if operation in one_param:
num_angles = 1
elif operation in two_param:
Expand Down
7 changes: 4 additions & 3 deletions test/python/circuit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ def test_decompose_propagates_bound_parameters(self, target_type, parameter_type

if target_type == "gate":
inst = qc.to_gate()
elif target_type == "instruction":
else: # target_type == "instruction":
inst = qc.to_instruction()

qc2 = QuantumCircuit(1)
Expand Down Expand Up @@ -1132,7 +1132,7 @@ def test_decompose_propagates_deeply_bound_parameters(self, target_type, paramet

if target_type == "gate":
inst = qc1.to_gate()
elif target_type == "instruction":
else: # target_type == "instruction":
inst = qc1.to_instruction()

qc2 = QuantumCircuit(1)
Expand Down Expand Up @@ -1188,7 +1188,7 @@ def test_executing_parameterized_instruction_bound_early(self, target_type):

if target_type == "gate":
sub_inst = sub_qc.to_gate()
elif target_type == "instruction":
else: # target_type == "instruction":
sub_inst = sub_qc.to_instruction()

unbound_qc = QuantumCircuit(2, 1)
Expand Down Expand Up @@ -1405,6 +1405,7 @@ def _paramvec_names(prefix, length):

@ddt
class TestParameterExpressions(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Test expressions of Parameters."""

# supported operations dictionary operation : accuracy (0=exact match)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
@ddt
class TestBooleanExpression(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Test boolean expression."""

@data(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestOracleDecomposition(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Tests ClassicalFunction.decomposition."""

def test_grover_oracle(self):
Expand Down
1 change: 1 addition & 0 deletions test/python/classical_function_compiler/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestParseFail(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Tests bad_examples with the classicalfunction parser."""

def assertExceptionMessage(self, context, message):
Expand Down
1 change: 1 addition & 0 deletions test/python/classical_function_compiler/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
@ddt
class TestSimulate(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Tests LogicNetwork.simulate method"""

@data(*utils.example_list())
Expand Down
1 change: 1 addition & 0 deletions test/python/classical_function_compiler/test_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestSynthesis(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Tests ClassicalFunction.synth method."""

def test_grover_oracle(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestTweedledum2Qiskit(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Tests qiskit.transpiler.classicalfunction.utils.tweedledum2qiskit function."""

def test_x(self):
Expand Down
2 changes: 2 additions & 0 deletions test/python/classical_function_compiler/test_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestTypeCheck(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Tests classicalfunction compiler type checker (good examples)."""

def test_id(self):
Expand Down Expand Up @@ -74,6 +75,7 @@ def test_bool_or(self):

@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestTypeCheckFail(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Tests classicalfunction compiler type checker (bad examples)."""

def assertExceptionMessage(self, context, message):
Expand Down
2 changes: 2 additions & 0 deletions test/python/visualization/test_circuit_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_default_output(self):

@unittest.skipUnless(optionals.HAS_MATPLOTLIB, "Skipped because matplotlib is not available")
def test_mpl_config_with_path(self):
# pylint: disable=possibly-used-before-assignment
# It's too easy to get too nested in a test with many context managers.
tempdir = tempfile.TemporaryDirectory() # pylint: disable=consider-using-with
self.addCleanup(tempdir.cleanup)
Expand Down Expand Up @@ -128,6 +129,7 @@ def test_latex_unsupported_image_format_error_message(self):

@_latex_drawer_condition
def test_latex_output_file_correct_format(self):
# pylint: disable=possibly-used-before-assignment
with patch("qiskit.user_config.get_config", return_value={"circuit_drawer": "latex"}):
circuit = QuantumCircuit()
filename = "file.gif"
Expand Down
1 change: 1 addition & 0 deletions test/python/visualization/test_circuit_text_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def test_text_no_pager(self):


class TestTextDrawerGatesInCircuit(QiskitTestCase):
# pylint: disable=possibly-used-before-assignment
"""Gate by gate checks in different settings."""

def test_text_measure_cregbundle(self):
Expand Down
1 change: 1 addition & 0 deletions test/python/visualization/test_gate_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
@unittest.skipUnless(optionals.HAS_PIL, "PIL not available")
@unittest.skipUnless(optionals.HAS_SEABORN, "seaborn not available")
class TestGateMap(QiskitVisualizationTestCase):
# pylint: disable=possibly-used-before-assignment
"""visual tests for plot_gate_map"""

backends = [Fake5QV1(), Fake20QV1(), Fake7QPulseV1()]
Expand Down
1 change: 1 addition & 0 deletions test/python/visualization/test_plot_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

@unittest.skipUnless(optionals.HAS_MATPLOTLIB, "matplotlib not available.")
class TestPlotHistogram(QiskitVisualizationTestCase):
# pylint: disable=possibly-used-before-assignment
"""Qiskit plot_histogram tests."""

def test_different_counts_lengths(self):
Expand Down
4 changes: 2 additions & 2 deletions tools/build_standard_commutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ def _generate_commutation_dict(considered_gates: List[Gate] = None) -> dict:
commutation_relation = cc.commute(
op1, qargs1, cargs1, op2, qargs2, cargs2, max_num_qubits=4
)

gate_pair_commutation[relative_placement] = commutation_relation
else:
pass
# TODO

gate_pair_commutation[relative_placement] = commutation_relation

commutations[gate0.name, gate1.name] = gate_pair_commutation
return commutations

Expand Down

0 comments on commit 439de04

Please sign in to comment.