qiskit-optimization
qiskit-optimization copied to clipboard
Conversion performance improvement
Summary
The purpose of this merge request is to find easy wins in improving the performance of converters.
Details and comments
I ran some tests to see where the bottleneck in conversion lies. I used the enlight11.mps file, and tried to convert it to QUBO. On my machine, this is the breakdown of timings in seconds per the conversion step for this instance:
reading in mps file: 0.011665105819702148
translating: 0.008737564086914062
<class 'qiskit_optimization.converters.linear_inequality_to_penalty.LinearInequalityToPenalty'> converter: 0.014148950576782227
<class 'qiskit_optimization.converters.inequality_to_equality.InequalityToEquality'> converter: 0.01540994644165039
<class 'qiskit_optimization.converters.integer_to_binary.IntegerToBinary'> converter: 0.09988164901733398
<class 'qiskit_optimization.converters.linear_equality_to_penalty.LinearEqualityToPenalty'> converter: 10.31824779510498
<class 'qiskit_optimization.converters.flip_problem_sense.MaximizeToMinimize'> converter: 1.1920928955078125e-05
Total conversion time: 10.447956562042236
By far the most time is spent in the LinearEqualityToPenalty converter. Out of ~10.3 seconds spent in this converter, ~9.3 seconds are spent in the _coeffs_to_dok_matrix function while processing the quadratic coefficients of the resulting QUBO.
This function first builds the QUBO matrix out of a dictionary of coefficients in full form (both upper and lower triangular elements are added), and then transforms it into an upper diagonal form via matrix operations. As these operations can be expensive, the proposed change simply directly builds the matrix in upper triangular form, avoiding several intermediate steps
In the following table, the conversion time is compared before and after the improvements for a few instances:
| Instance | old | new |
|---|---|---|
| enlight11.mps | 10.6 | 6.6 |
| mzzv11.mps | 32.3 | 28.5 |
| acc-tight5.mps | 3.8 | 3.1 |
| air04.mps | 82.7 | 53.6 |
| iis-glass-cov.mps | 21.5 | 18.0 |
| f2gap40400.mps | 0.7 | 0.4 |
| cod105.mps | 13.3 | 11.0 |
✅ I have added the tests to cover my changes. ✅ I have updated the documentation accordingly. [nothing to be updated] ✅ I have read the CONTRIBUTING document.