Skip to content

SparsePauliOp should allow initialization from empty list #10159

@kevinsung

Description

@kevinsung

What should we add?

The following code raises an error. I don't think it should. I think it should return the zero operator. @ikkoham

from qiskit.quantum_info import SparsePauliOp

SparsePauliOp.from_sparse_list([], num_qubits=1)
---------------------------------------------------------------------------
QiskitError                               Traceback (most recent call last)
Cell In[2], line 3
      1 from qiskit.quantum_info import SparsePauliOp
----> 3 SparsePauliOp.from_sparse_list([], num_qubits=1)

File ~/projects/qiskit-terra/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py:839, in SparsePauliOp.from_sparse_list(obj, num_qubits, do_checks, dtype)
    837 size = len(obj)  # number of Pauli terms
    838 if size == 0:
--> 839     raise QiskitError("Input Pauli list is empty.")
    841 coeffs = np.zeros(size, dtype=dtype)
    842 labels = np.zeros(size, dtype=f"<U{num_qubits}")

QiskitError: 'Input Pauli list is empty.'

Metadata

Metadata

Assignees

No one assigned

    Labels

    mod: quantum infoRelated to the Quantum Info module (States & Operators)type: feature requestNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions