Skip to content

Commit

Permalink
Add label assigned check in SparsePauliOp (#8101)
Browse files Browse the repository at this point in the history
* add assign check

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

Co-authored-by: Ikko Hamamura <[email protected]>

* fix check condition

* update reno

* update reno section header

Co-authored-by: Ikko Hamamura <[email protected]>
Co-authored-by: Julien Gacon <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jun 14, 2022
1 parent be5e71a commit b8ea2b0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -637,13 +637,15 @@ 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.
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

Expand All @@ -655,6 +657,8 @@ def from_sparse_list(obj, num_qubits):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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).
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ def test_from_index_list_raises(self):
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"]
Expand Down

0 comments on commit b8ea2b0

Please sign in to comment.