qiskit-nature
qiskit-nature copied to clipboard
Slow behaviour of functools reduce() in VibrationalOp
What is the expected enhancement?
This line: https://github.com/Qiskit/qiskit-nature/blob/76c9f976c08ba49c6397a40cc138e62a9f827b7a/qiskit_nature/operators/second_quantization/vibrational_op.py#L162 is too slow to treat large system sizes.
This is literally just summing the list of operators, ops
. Since that basically amounts to simply concatenating the underlying list of labels and coefficients, this can definitely improved significantly by simply performing said list concatenation in place.
@paulineollitrault Would you be interested in working on a fix for this yourself?
Can be fixed by modifying these lines https://github.com/Qiskit/qiskit-nature/blob/f0a4be91e82133b35560dcb34149d1956ad1b431/qiskit_nature/operators/second_quantization/vibrational_op.py#L149-L164 as follows:
else:
# Sparse label
dense_labels = self._convert_to_dense_labels(data, num_modals)
new_dense_labels = []
new_dense_coeffs = []
labels = []
for dense_label, coeff in dense_labels:
new_op = reduce(
lambda a, b: a @ b,
(VibrationalOp((label, 1), num_modes, num_modals) for label in dense_label),
)
# We ignore the type here because mypy only sees the complex coefficient
labels.extend(new_op._labels.copy())
self._labels = labels.copy()