From b8ea2b0bc9cf25ff8bf88c476831843a4f6ffb98 Mon Sep 17 00:00:00 2001 From: Daiki Murata <42103240+daiki0623@users.noreply.github.com> Date: Tue, 14 Jun 2022 19:58:23 +0900 Subject: [PATCH] Add label assigned check in SparsePauliOp (#8101) * add assign check * Update qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py Co-authored-by: Ikko Hamamura * fix check condition * update reno * update reno section header Co-authored-by: Ikko Hamamura Co-authored-by: Julien Gacon Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- .../quantum_info/operators/symplectic/sparse_pauli_op.py | 6 +++++- .../add_check_from_sparse_list-97f13fde87c7bcb6.yaml | 6 ++++++ .../operators/symplectic/test_sparse_pauli_op.py | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/add_check_from_sparse_list-97f13fde87c7bcb6.yaml diff --git a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py index 3545e63fddce..388b88788f89 100644 --- a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py +++ b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py @@ -612,7 +612,7 @@ def from_list(obj): return SparsePauliOp(paulis, coeffs, copy=False) @staticmethod - def from_sparse_list(obj, num_qubits): + def from_sparse_list(obj, num_qubits, do_checks=True): """Construct from a list of local Pauli strings and coefficients. Each list element is a 3-tuple of a local Pauli string, indices where to apply it, @@ -637,6 +637,7 @@ def from_sparse_list(obj, num_qubits): Args: obj (Iterable[Tuple[str, List[int], complex]]): The list 3-tuples specifying the Paulis. num_qubits (int): The number of qubits of the operator. + do_checks (bool): The flag of checking if the input indices are not duplicated. Returns: SparsePauliOp: The SparsePauliOp representation of the Pauli terms. @@ -644,6 +645,7 @@ def from_sparse_list(obj, num_qubits): Raises: QiskitError: If the list of Paulis is empty. QiskitError: If the number of qubits is incompatible with the indices of the Pauli terms. + QiskitError: If the designated qubit is already assigned. """ obj = list(obj) # To convert zip or other iterable @@ -655,6 +657,8 @@ def from_sparse_list(obj, num_qubits): labels = np.zeros(size, dtype=f"