-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Labels
type: feature requestNew feature or requestNew feature or request
Description
What should we add?
I'm not certain but for large SparsePauliOp objects, it seems like the type-checking is a bottleneck for initializing the object (which also happens inside various SPO methods such as compose()
):
qiskit/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Lines 134 to 142 in 02cb814
if isinstance(coeffs, np.ndarray) and coeffs.dtype == object: | |
dtype = object | |
elif coeffs is not None: | |
if not isinstance(coeffs, (np.ndarray, Sequence)): | |
coeffs = [coeffs] | |
if any(isinstance(coeff, ParameterExpression) for coeff in coeffs): | |
dtype = object | |
else: | |
dtype = complex |
In the most common case, we can avoid calling isinstance
thousands of times by changing the logic to:
if isinstance(coeffs, np.ndarray):
if coeffs.dtype == object:
dtype = object
else:
dtype = complex
elif coeffs is not None:
if not isinstance(coeffs, (np.ndarray, Sequence)):
coeffs = [coeffs]
if any(isinstance(coeff, ParameterExpression) for coeff in coeffs):
dtype = object
else:
dtype = complex
Very crude timing test (seconds indicated at lower left); may want to confirm benchmark before committing. I repeated with seed=2
(not shown).
Metadata
Metadata
Assignees
Labels
type: feature requestNew feature or requestNew feature or request