diff --git a/mypy.ini b/mypy.ini index f7333f8847..7d5b2f6f34 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,6 +2,9 @@ warn_unused_configs = True ignore_missing_imports = True strict_optional = False +no_implicit_optional = True +warn_redundant_casts = True +warn_unused_ignores = True ### Output show_error_codes = True diff --git a/qiskit_nature/algorithms/excited_states_solvers/qeom.py b/qiskit_nature/algorithms/excited_states_solvers/qeom.py index d90a62b8dd..faa37d227a 100644 --- a/qiskit_nature/algorithms/excited_states_solvers/qeom.py +++ b/qiskit_nature/algorithms/excited_states_solvers/qeom.py @@ -238,7 +238,7 @@ def _build_one_sector(available_hopping_ops, untapered_op, z2_symmetries): all_matrix_operators[f"v_{m_u}_{n_u}"] = v_mat_op try: - z2_symmetries = self._gsc.qubit_converter.z2symmetries # type: ignore + z2_symmetries = self._gsc.qubit_converter.z2symmetries except AttributeError: z2_symmetries = Z2Symmetries([], [], []) diff --git a/qiskit_nature/algorithms/pes_samplers/bopes_sampler.py b/qiskit_nature/algorithms/pes_samplers/bopes_sampler.py index b966ec7aa1..13d815dbfc 100644 --- a/qiskit_nature/algorithms/pes_samplers/bopes_sampler.py +++ b/qiskit_nature/algorithms/pes_samplers/bopes_sampler.py @@ -232,7 +232,7 @@ def _run_single_point(self, point: float) -> EigenstateResult: # Save optimal point to bootstrap if isinstance(self._gss.solver, VariationalAlgorithm): # type: ignore # at every point evaluation, the optimal params are updated - optimal_params = result.raw_result.optimal_point # type: ignore + optimal_params = result.raw_result.optimal_point self._points_optparams[point] = optimal_params return result diff --git a/qiskit_nature/drivers/bosonic_bases/harmonic_basis.py b/qiskit_nature/drivers/bosonic_bases/harmonic_basis.py index 6b04551a15..f9abd43b18 100644 --- a/qiskit_nature/drivers/bosonic_bases/harmonic_basis.py +++ b/qiskit_nature/drivers/bosonic_bases/harmonic_basis.py @@ -12,7 +12,7 @@ """ Bosonic Harmonic Basis """ -from typing import Dict, List, Tuple, cast +from typing import Dict, List, Tuple import numpy as np @@ -164,7 +164,7 @@ class to be mapped to a qubit hamiltonian. } for entry in self._watson.data: # Entry is coeff (float) followed by indices (ints) - coeff0 = cast(float, entry[0]) + coeff0 = entry[0] indices = np.asarray(entry[1:], dtype=int) kinetic_term = False diff --git a/qiskit_nature/drivers/pyscfd/pyscfdriver.py b/qiskit_nature/drivers/pyscfd/pyscfdriver.py index 7fa0e1ee2e..8ae5c37695 100644 --- a/qiskit_nature/drivers/pyscfd/pyscfdriver.py +++ b/qiskit_nature/drivers/pyscfd/pyscfdriver.py @@ -144,7 +144,7 @@ def _check_valid(): MissingOptionalLibraryError: If PySCF is not installed. """ try: - spec = importlib.util.find_spec("pyscf") # type: ignore + spec = importlib.util.find_spec("pyscf") if spec is not None: return except Exception as ex: # pylint: disable=broad-except diff --git a/qiskit_nature/drivers/second_quantization/electronic_structure_molecule_driver.py b/qiskit_nature/drivers/second_quantization/electronic_structure_molecule_driver.py index 2c846814f7..78af4c1162 100644 --- a/qiskit_nature/drivers/second_quantization/electronic_structure_molecule_driver.py +++ b/qiskit_nature/drivers/second_quantization/electronic_structure_molecule_driver.py @@ -81,8 +81,8 @@ def driver_class_from_type( raise MissingOptionalLibraryError( libname=driver_type, name="ElectronicStructureDriverType" ) - driver_class.check_installed() # type: ignore - driver_class.check_method_supported(method) # type: ignore + driver_class.check_installed() + driver_class.check_method_supported(method) logger.debug("%s found from type %s.", driver_class.__name__, driver_type.value) return driver_class diff --git a/qiskit_nature/drivers/second_quantization/pyscfd/pyscfdriver.py b/qiskit_nature/drivers/second_quantization/pyscfd/pyscfdriver.py index f287f0ec67..de20943f41 100644 --- a/qiskit_nature/drivers/second_quantization/pyscfd/pyscfdriver.py +++ b/qiskit_nature/drivers/second_quantization/pyscfd/pyscfdriver.py @@ -159,7 +159,7 @@ def __init__( validate_min("max_cycle", max_cycle, 1) # we use the property-setter to deal with conversion - self.atom = atom # type: ignore + self.atom = atom self._unit = unit self._charge = charge self._spin = spin @@ -337,10 +337,7 @@ def from_molecule( if key not in ["self"] and key in args: kwargs[key] = value - kwargs["atom"] = [ # type: ignore - " ".join(map(str, (name, *coord))) # type: ignore - for (name, coord) in molecule.geometry - ] + kwargs["atom"] = [" ".join(map(str, (name, *coord))) for (name, coord) in molecule.geometry] kwargs["charge"] = molecule.charge kwargs["spin"] = molecule.multiplicity - 1 kwargs["unit"] = molecule.units @@ -367,7 +364,7 @@ def check_installed() -> None: MissingOptionalLibraryError: If PySCF is not installed. """ try: - spec = importlib.util.find_spec("pyscf") # type: ignore + spec = importlib.util.find_spec("pyscf") if spec is not None: return except Exception as ex: # pylint: disable=broad-except diff --git a/qiskit_nature/drivers/second_quantization/vibrational_structure_molecule_driver.py b/qiskit_nature/drivers/second_quantization/vibrational_structure_molecule_driver.py index 35cbb2f1b2..7654aa90d8 100644 --- a/qiskit_nature/drivers/second_quantization/vibrational_structure_molecule_driver.py +++ b/qiskit_nature/drivers/second_quantization/vibrational_structure_molecule_driver.py @@ -73,7 +73,7 @@ def driver_class_from_type( raise MissingOptionalLibraryError( libname=driver_type, name="VibrationalStructureDriverType" ) - driver_class.check_installed() # type: ignore + driver_class.check_installed() logger.debug("%s found from type %s.", driver_class.__name__, driver_type.value) return driver_class diff --git a/qiskit_nature/operators/second_quantization/fermionic_op.py b/qiskit_nature/operators/second_quantization/fermionic_op.py index ee7131c745..0c40a3b5c6 100644 --- a/qiskit_nature/operators/second_quantization/fermionic_op.py +++ b/qiskit_nature/operators/second_quantization/fermionic_op.py @@ -288,7 +288,7 @@ def __init__( self._data = [ ( self._substituted_label([(c, int(i)) for i, c in enumerate(label)]), - complex(coeff), # type: ignore + complex(coeff), ) for label, coeff in data ] @@ -298,7 +298,7 @@ def __init__( self._substituted_label( [(c[0], int(c[2:])) for c in label.split()] # type: ignore ), - complex(coeff), # type: ignore + complex(coeff), ) for label, coeff in data ] @@ -436,7 +436,7 @@ def add(self, other: "FermionicOp") -> "FermionicOp": def to_list( self, display_format: Optional[str] = None, - ) -> List[Tuple[str, complex]]: # type: ignore + ) -> List[Tuple[str, complex]]: """Returns the operators internal contents in list-format. Args: diff --git a/qiskit_nature/problems/second_quantization/electronic/builders/hopping_ops_builder.py b/qiskit_nature/problems/second_quantization/electronic/builders/hopping_ops_builder.py index d6bc992b46..c32a5a0815 100644 --- a/qiskit_nature/problems/second_quantization/electronic/builders/hopping_ops_builder.py +++ b/qiskit_nature/problems/second_quantization/electronic/builders/hopping_ops_builder.py @@ -66,7 +66,6 @@ def _build_qeom_hopping_ops( if isinstance(excitations, (str, int)) or ( isinstance(excitations, list) and all(isinstance(exc, int) for exc in excitations) ): - excitations = cast(Union[str, int, List[int]], excitations) ansatz = UCC(qubit_converter, (num_alpha, num_beta), num_spin_orbitals, excitations) excitations_list = ansatz._get_excitation_list() else: diff --git a/qiskit_nature/problems/second_quantization/vibrational/builders/hopping_ops_builder.py b/qiskit_nature/problems/second_quantization/vibrational/builders/hopping_ops_builder.py index 357932fe7d..bce55bbc63 100644 --- a/qiskit_nature/problems/second_quantization/vibrational/builders/hopping_ops_builder.py +++ b/qiskit_nature/problems/second_quantization/vibrational/builders/hopping_ops_builder.py @@ -58,7 +58,6 @@ def _build_qeom_hopping_ops( if isinstance(excitations, (str, int)) or ( isinstance(excitations, list) and all(isinstance(exc, int) for exc in excitations) ): - excitations = cast(Union[str, int, List[int]], excitations) ansatz = UVCC(qubit_converter, num_modals, excitations) excitations_list = ansatz._get_excitation_list() else: @@ -90,7 +89,7 @@ def _build_qeom_hopping_ops( # at the moment we do not have any type of commutativity in the bosonic case. type_of_commutativities: Dict[str, List[bool]] = {} - return hopping_operators, type_of_commutativities, excitation_indices # type: ignore + return hopping_operators, type_of_commutativities, excitation_indices def _build_single_hopping_operator( diff --git a/qiskit_nature/properties/second_quantization/electronic/angular_momentum.py b/qiskit_nature/properties/second_quantization/electronic/angular_momentum.py index deb94a667f..19574f2702 100644 --- a/qiskit_nature/properties/second_quantization/electronic/angular_momentum.py +++ b/qiskit_nature/properties/second_quantization/electronic/angular_momentum.py @@ -139,7 +139,7 @@ def interpret(self, result: EigenstateResult) -> None: if not isinstance(result.aux_operator_eigenvalues, list): aux_operator_eigenvalues = [result.aux_operator_eigenvalues] else: - aux_operator_eigenvalues = result.aux_operator_eigenvalues # type: ignore[assignment] + aux_operator_eigenvalues = result.aux_operator_eigenvalues for aux_op_eigenvalues in aux_operator_eigenvalues: if aux_op_eigenvalues is None: continue @@ -147,7 +147,7 @@ def interpret(self, result: EigenstateResult) -> None: _key = self.name if isinstance(aux_op_eigenvalues, dict) else 1 if aux_op_eigenvalues[_key] is not None: - total_angular_momentum = aux_op_eigenvalues[_key][0].real # type: ignore + total_angular_momentum = aux_op_eigenvalues[_key][0].real result.total_angular_momentum.append(total_angular_momentum) if expected is not None: diff --git a/qiskit_nature/properties/second_quantization/electronic/dipole_moment.py b/qiskit_nature/properties/second_quantization/electronic/dipole_moment.py index 660cc55c95..cde487bebf 100644 --- a/qiskit_nature/properties/second_quantization/electronic/dipole_moment.py +++ b/qiskit_nature/properties/second_quantization/electronic/dipole_moment.py @@ -236,7 +236,7 @@ def interpret(self, result: EigenstateResult) -> None: if not isinstance(result.aux_operator_eigenvalues, list): aux_operator_eigenvalues = [result.aux_operator_eigenvalues] else: - aux_operator_eigenvalues = result.aux_operator_eigenvalues # type: ignore[assignment] + aux_operator_eigenvalues = result.aux_operator_eigenvalues for aux_op_eigenvalues in aux_operator_eigenvalues: if aux_op_eigenvalues is None: @@ -250,9 +250,9 @@ def interpret(self, result: EigenstateResult) -> None: for prop in iter(self): moment: Optional[Tuple[complex, complex]] try: - moment = aux_op_eigenvalues[axes_order[prop._axis] + 3] # type: ignore + moment = aux_op_eigenvalues[axes_order[prop._axis] + 3] except KeyError: - moment = aux_op_eigenvalues.get(prop.name, None) # type: ignore + moment = aux_op_eigenvalues.get(prop.name, None) if moment is not None: dipole_moment[axes_order[prop._axis]] = moment[0].real # type: ignore diff --git a/qiskit_nature/properties/second_quantization/electronic/magnetization.py b/qiskit_nature/properties/second_quantization/electronic/magnetization.py index 2e9d257e7f..b1c0eb80b2 100644 --- a/qiskit_nature/properties/second_quantization/electronic/magnetization.py +++ b/qiskit_nature/properties/second_quantization/electronic/magnetization.py @@ -94,7 +94,7 @@ def interpret(self, result: EigenstateResult) -> None: if not isinstance(result.aux_operator_eigenvalues, list): aux_operator_eigenvalues = [result.aux_operator_eigenvalues] else: - aux_operator_eigenvalues = result.aux_operator_eigenvalues # type: ignore[assignment] + aux_operator_eigenvalues = result.aux_operator_eigenvalues for aux_op_eigenvalues in aux_operator_eigenvalues: if aux_op_eigenvalues is None: continue @@ -102,6 +102,6 @@ def interpret(self, result: EigenstateResult) -> None: _key = self.name if isinstance(aux_op_eigenvalues, dict) else 2 if aux_op_eigenvalues[_key] is not None: - result.magnetization.append(aux_op_eigenvalues[_key][0].real) # type: ignore + result.magnetization.append(aux_op_eigenvalues[_key][0].real) else: result.magnetization.append(None) diff --git a/qiskit_nature/properties/second_quantization/electronic/particle_number.py b/qiskit_nature/properties/second_quantization/electronic/particle_number.py index fd840cdc1f..f2be099aaf 100644 --- a/qiskit_nature/properties/second_quantization/electronic/particle_number.py +++ b/qiskit_nature/properties/second_quantization/electronic/particle_number.py @@ -190,7 +190,7 @@ def interpret(self, result: EigenstateResult) -> None: if not isinstance(result.aux_operator_eigenvalues, list): aux_operator_eigenvalues = [result.aux_operator_eigenvalues] else: - aux_operator_eigenvalues = result.aux_operator_eigenvalues # type: ignore[assignment] + aux_operator_eigenvalues = result.aux_operator_eigenvalues for aux_op_eigenvalues in aux_operator_eigenvalues: if aux_op_eigenvalues is None: continue @@ -198,7 +198,7 @@ def interpret(self, result: EigenstateResult) -> None: _key = self.name if isinstance(aux_op_eigenvalues, dict) else 0 if aux_op_eigenvalues[_key] is not None: - n_particles = aux_op_eigenvalues[_key][0].real # type: ignore + n_particles = aux_op_eigenvalues[_key][0].real result.num_particles.append(n_particles) if not np.isclose( diff --git a/qiskit_nature/properties/second_quantization/vibrational/occupied_modals.py b/qiskit_nature/properties/second_quantization/vibrational/occupied_modals.py index 5251ed738a..0437a8da24 100644 --- a/qiskit_nature/properties/second_quantization/vibrational/occupied_modals.py +++ b/qiskit_nature/properties/second_quantization/vibrational/occupied_modals.py @@ -105,7 +105,7 @@ def interpret(self, result: EigenstateResult) -> None: if not isinstance(result.aux_operator_eigenvalues, list): aux_operator_eigenvalues = [result.aux_operator_eigenvalues] else: - aux_operator_eigenvalues = result.aux_operator_eigenvalues # type: ignore[assignment] + aux_operator_eigenvalues = result.aux_operator_eigenvalues num_modes = len(self._basis._num_modals_per_mode) @@ -114,7 +114,7 @@ def interpret(self, result: EigenstateResult) -> None: for mode in range(num_modes): _key = str(mode) if isinstance(aux_op_eigenvalues, dict) else mode if aux_op_eigenvalues[_key] is not None: - occ_modals.append(aux_op_eigenvalues[_key][0].real) # type: ignore + occ_modals.append(aux_op_eigenvalues[_key][0].real) else: occ_modals.append(None) - result.num_occupied_modals_per_mode.append(occ_modals) # type: ignore + result.num_occupied_modals_per_mode.append(occ_modals) diff --git a/qiskit_nature/properties/second_quantization/vibrational/vibrational_energy.py b/qiskit_nature/properties/second_quantization/vibrational/vibrational_energy.py index 4d7514137b..1696bbbfee 100644 --- a/qiskit_nature/properties/second_quantization/vibrational/vibrational_energy.py +++ b/qiskit_nature/properties/second_quantization/vibrational/vibrational_energy.py @@ -149,9 +149,9 @@ def second_q_ops(self) -> ListOrDictType[VibrationalOp]: ops.append(ints.to_second_q_op()) if not settings.dict_aux_operators: - return [sum(ops)] # type: ignore[list-item] + return [sum(ops)] - return {self.name: sum(ops)} # type: ignore[dict-item] + return {self.name: sum(ops)} def interpret(self, result: EigenstateResult) -> None: """Interprets an :class:`~qiskit_nature.results.EigenstateResult` in this property's context. diff --git a/qiskit_nature/transformers/second_quantization/electronic/active_space_transformer.py b/qiskit_nature/transformers/second_quantization/electronic/active_space_transformer.py index 00ad6b1e44..051e189dfe 100644 --- a/qiskit_nature/transformers/second_quantization/electronic/active_space_transformer.py +++ b/qiskit_nature/transformers/second_quantization/electronic/active_space_transformer.py @@ -410,7 +410,7 @@ def _transform_property(self, prop: Property) -> Property: transformed_property._shift[self.__class__.__name__] = e_inactive elif isinstance(prop, ParticleNumber): - p_n = cast(ParticleNumber, prop) + p_n = prop active_occ_alpha = p_n.occupation_alpha[self._active_orbs_indices] active_occ_beta = p_n.occupation_beta[self._active_orbs_indices] transformed_property = ParticleNumber( diff --git a/qiskit_nature/transformers/second_quantization/electronic/freeze_core_transformer.py b/qiskit_nature/transformers/second_quantization/electronic/freeze_core_transformer.py index 8a670550e0..a2b535475d 100644 --- a/qiskit_nature/transformers/second_quantization/electronic/freeze_core_transformer.py +++ b/qiskit_nature/transformers/second_quantization/electronic/freeze_core_transformer.py @@ -12,7 +12,7 @@ """The Freeze-Core Reduction interface.""" -from typing import cast, List, Optional, Tuple +from typing import List, Optional, Tuple from qiskit_nature import QiskitNatureError from qiskit_nature.properties.second_quantization.electronic import ElectronicStructureDriverResult @@ -78,7 +78,6 @@ def _determine_active_space( "The FreezeCoreTransformer requires an `ElectronicStructureDriverResult`, not a " f"property of type {type(grouped_property)}." ) - grouped_property = cast(ElectronicStructureDriverResult, grouped_property) molecule = grouped_property.molecule particle_number = grouped_property.get_property("ParticleNumber")