diff --git a/pytket/extensions/cutensornet/backends/cutensornet_backend.py b/pytket/extensions/cutensornet/backends/cutensornet_backend.py index a48b55ff..9afc61df 100644 --- a/pytket/extensions/cutensornet/backends/cutensornet_backend.py +++ b/pytket/extensions/cutensornet/backends/cutensornet_backend.py @@ -313,10 +313,8 @@ def process_circuits( raise ValueError( "You must specify n_shots when using CuTensorNetShotsBackend." ) - if type(n_shots) == int: - all_shots = [n_shots] * len(circuits) - else: - all_shots = n_shots # type: ignore + + all_shots = [n_shots] * len(circuits) if type(n_shots) is int else n_shots circuit_list = list(circuits) if valid_check: @@ -340,5 +338,5 @@ def _check_all_unitary_or_measurements(circuit: Circuit) -> bool: if cmd.op.type != OpType.Measure: cmd.op.get_unitary() return True - except: + except: # noqa: E722 return False diff --git a/pytket/extensions/cutensornet/general_state/tensor_network_convert.py b/pytket/extensions/cutensornet/general_state/tensor_network_convert.py index 2480088d..b1dbcee8 100644 --- a/pytket/extensions/cutensornet/general_state/tensor_network_convert.py +++ b/pytket/extensions/cutensornet/general_state/tensor_network_convert.py @@ -159,7 +159,8 @@ def _get_gate_tensors(self, adj: bool = False) -> defaultdict[Any, list[Any]]: .reshape([2] * (2 * com.op.n_qubits)) ) self._logger.debug( - f"Adding unitary: \n {permute_rows_cols_in_unitary(com.op.get_unitary(), com_qix_permut).T.conjugate()}" # type: ignore + f"Adding unitary:\ +\n {permute_rows_cols_in_unitary(com.op.get_unitary(), com_qix_permut).T.conjugate()}" # type: ignore ) else: gate_tensors[i].append( @@ -168,7 +169,8 @@ def _get_gate_tensors(self, adj: bool = False) -> defaultdict[Any, list[Any]]: ).reshape([2] * (2 * com.op.n_qubits)) ) self._logger.debug( # type: ignore - f"Adding unitary: \n {permute_rows_cols_in_unitary(com.op.get_unitary(),com_qix_permut)}" # type: ignore + f"Adding unitary:\ +\n {permute_rows_cols_in_unitary(com.op.get_unitary(),com_qix_permut)}" # type: ignore ) break self._logger.debug(f"Gate tensors: \n{gate_tensors}\n") @@ -211,7 +213,8 @@ def _assign_node_tensors(self, adj: bool = False) -> list[Any]: ) self._logger.debug( f"criteria: " - f"{(src_ports[0] < src_ports[1]) != (unit_idx[0] < unit_idx[1])}" # pylint: disable=line-too-long + f"{(src_ports[0] < src_ports[1]) != + (unit_idx[0] < unit_idx[1])}" # pylint: disable=line-too-long ) if (src_ports[0] < src_ports[1]) != (unit_idx[0] < unit_idx[1]): node_tensors.append(self._gate_tensors[node[1]["desc"]][1]) diff --git a/pytket/extensions/cutensornet/general_state/tensor_network_state.py b/pytket/extensions/cutensornet/general_state/tensor_network_state.py index 7efed94f..aa26bb2c 100644 --- a/pytket/extensions/cutensornet/general_state/tensor_network_state.py +++ b/pytket/extensions/cutensornet/general_state/tensor_network_state.py @@ -96,7 +96,7 @@ def __init__( for com in self._circuit.get_commands(): try: gate_unitary = com.op.get_unitary() - except: + except: # noqa: E722 raise ValueError( "All commands in the circuit must be unitary gates. The circuit " f"contains {com}; no unitary matrix could be retrived for it." diff --git a/pytket/extensions/cutensornet/structured_state/general.py b/pytket/extensions/cutensornet/structured_state/general.py index 13541e82..4ebe3d40 100644 --- a/pytket/extensions/cutensornet/structured_state/general.py +++ b/pytket/extensions/cutensornet/structured_state/general.py @@ -229,7 +229,7 @@ def _apply_command( elif op.is_gate(): # Either a unitary gate or a not supported "gate" try: unitary = op.get_unitary() - except: + except: # noqa E722 raise ValueError(f"The command {op.type} introduced is not supported.") # Load the gate's unitary to the GPU memory diff --git a/pytket/extensions/cutensornet/structured_state/mps_gate.py b/pytket/extensions/cutensornet/structured_state/mps_gate.py index 83216582..5f9f1586 100644 --- a/pytket/extensions/cutensornet/structured_state/mps_gate.py +++ b/pytket/extensions/cutensornet/structured_state/mps_gate.py @@ -119,10 +119,7 @@ def _apply_2q_unitary(self, unitary: cp.ndarray, q0: Qubit, q1: Qubit) -> MPSxGa # S -> shared bond of the gate tensor's SVD # a,b,c -> the virtual bonds of the tensors - if l_pos == positions[0]: - gate_bonds = "LRlr" - else: # Implicit swap - gate_bonds = "RLrl" + gate_bonds = "LRlr" if l_pos == positions[0] else "RLrl" # Apply SVD on the gate tensor to remove any zero singular values ASAP svd_method = tensor.SVDMethod( @@ -255,10 +252,7 @@ def _apply_2q_unitary_nonadjacent( # a,b -> virtual bonds of the MPS # m,M -> virtual bonds connected to the "message tensor" - if l_pos == positions[0]: - gate_bonds = "LRlr" - else: # Implicit swap - gate_bonds = "RLrl" + gate_bonds = "LRlr" if l_pos == positions[0] else "RLrl" # Apply SVD on the gate tensor to remove any zero singular values ASAP svd_method = tensor.SVDMethod( diff --git a/pytket/extensions/cutensornet/structured_state/mps_mpo.py b/pytket/extensions/cutensornet/structured_state/mps_mpo.py index f483e473..52cfb275 100644 --- a/pytket/extensions/cutensornet/structured_state/mps_mpo.py +++ b/pytket/extensions/cutensornet/structured_state/mps_mpo.py @@ -240,10 +240,7 @@ def _apply_2q_unitary(self, unitary: cp.ndarray, q0: Qubit, q1: Qubit) -> MPSxMP # s -> virtual bond after QR decomposition # Assign the bond IDs for the gate - if l_pos == positions[0]: - gate_bonds = "LRlr" - else: # Implicit swap - gate_bonds = "RLrl" + gate_bonds = "LRlr" if l_pos == positions[0] else "RLrl" # Apply SVD on the gate tensor to remove any zero singular values ASAP svd_method = tensor.SVDMethod( @@ -450,11 +447,11 @@ def update_sweep_cache(pos: int, direction: DirMPS) -> None: interleaved_rep.append(r_cached_tensors[-1]) r_cached_bonds = self._get_column_bonds(pos + 1, DirMPS.LEFT) interleaved_rep.append(["r", "R"] + r_cached_bonds) - elif direction == DirMPS.RIGHT: - if pos != 0: # Otherwise, there is nothing cached yet - interleaved_rep.append(l_cached_tensors[-1]) - l_cached_bonds = self._get_column_bonds(pos - 1, DirMPS.RIGHT) - interleaved_rep.append(["l", "L"] + l_cached_bonds) + elif direction == DirMPS.RIGHT and pos != 0: + # Otherwise, there is nothing cached yet + interleaved_rep.append(l_cached_tensors[-1]) + l_cached_bonds = self._get_column_bonds(pos - 1, DirMPS.RIGHT) + interleaved_rep.append(["l", "L"] + l_cached_bonds) # Figure out the ID of the bonds of the contracted tensor if direction == DirMPS.LEFT: diff --git a/pytket/extensions/cutensornet/structured_state/ttn.py b/pytket/extensions/cutensornet/structured_state/ttn.py index 1b02f715..0a114bb9 100644 --- a/pytket/extensions/cutensornet/structured_state/ttn.py +++ b/pytket/extensions/cutensornet/structured_state/ttn.py @@ -161,12 +161,12 @@ def __init__( # Calculate the root path of this group path = [] - for l in reversed(range(n_levels)): - if k < 2**l: + for le in reversed(range(n_levels)): + if k < 2**le: path.append(DirTTN.LEFT) else: path.append(DirTTN.RIGHT) - k -= 2**l + k -= 2**le # Add each qubit to the qubit_position dictionary for i, q in enumerate(qubits): @@ -371,7 +371,7 @@ def canonicalise(self, center: RootPath | Qubit, unsafe: bool = False) -> Tensor for path in self.nodes: # Nodes towards children are closer to the root and coincide in the path if len(path) < len(target_path) and all( - path[l] == target_path[l] for l in range(len(path)) + path[le] == target_path[le] for le in range(len(path)) ): towards_child.append(path) # If the center is a physical bond (qubit), its node is skipped diff --git a/tests/test_utils.py b/tests/test_utils.py index 4421f888..8059afa5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -40,5 +40,5 @@ def test_device_properties_logger() -> None: try: with CuTensorNetHandle() as libhandle: libhandle.print_device_properties(set_logger("GeneralState", 10)) - except: + except: # noqa: E722 pytest.fail("Could not print device properties")