qiskit-nature icon indicating copy to clipboard operation
qiskit-nature copied to clipboard

Slow behaviour of functools reduce() in VibrationalOp

Open paulineollitrault opened this issue 2 years ago • 2 comments

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.

paulineollitrault avatar Jul 13 '22 15:07 paulineollitrault

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?

mrossinek avatar Jul 13 '22 16:07 mrossinek

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()

paulineollitrault avatar Aug 18 '22 15:08 paulineollitrault