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 <ikkoham@users.noreply.github.com>

* fix check condition

* update reno

* update reno section header

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
Co-authored-by: Julien Gacon <gaconju@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Daiki Murata 2022-06-14 19:58:23 +09:00 committed by GitHub
parent be5e71a157
commit b8ea2b0bc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 1 deletions

View File

@ -612,7 +612,7 @@ class SparsePauliOp(LinearOp):
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 @@ class SparsePauliOp(LinearOp):
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 @@ class SparsePauliOp(LinearOp):
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 @@ class SparsePauliOp(LinearOp):
labels = np.zeros(size, dtype=f"<U{num_qubits}")
for i, (paulis, indices, coeff) in enumerate(obj):
if do_checks and len(indices) != len(set(indices)):
raise QiskitError("Input indices are duplicated.")
# construct the full label based off the non-trivial Paulis and indices
label = ["I"] * num_qubits
for pauli, index in zip(paulis, indices):

View File

@ -0,0 +1,6 @@
---
features:
- |
Allow checking for duplicate qubit indices in :meth:`~qiskit.quantum_info.SparsePauliOp.from_sparse_list`.
The checks can be turned off by setting the new keyword argument ``do_checks`` to ``False``
(it is ``True`` per default).

View File

@ -183,6 +183,15 @@ class TestSparsePauliOpConversions(QiskitTestCase):
with self.assertRaises(QiskitError):
_ = SparsePauliOp.from_sparse_list([("Z", [2], 1)], 1)
def test_from_index_list_same_index(self):
"""Test from_list via Pauli + number of qubits raises correctly, if indices duplicate."""
with self.assertRaises(QiskitError):
_ = SparsePauliOp.from_sparse_list([("ZZ", [0, 0], 1)], 2)
with self.assertRaises(QiskitError):
_ = SparsePauliOp.from_sparse_list([("ZI", [0, 0], 1)], 2)
with self.assertRaises(QiskitError):
_ = SparsePauliOp.from_sparse_list([("IZ", [0, 0], 1)], 2)
def test_from_zip(self):
"""Test from_list method for zipped input."""
labels = ["XXZ", "IXI", "YZZ", "III"]