Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Fix a bug of the integer to binary converter #1256

Merged
merged 3 commits into from
Sep 18, 2020

Conversation

t-imamichi
Copy link
Contributor

@t-imamichi t-imamichi commented Sep 17, 2020

Summary

Fix a bug reported at stackexchange.
This PR fixes the bug and add a unit test to cover this case.

The following code raises an error in IntegerToBinary.

from qiskit.optimization import QuadraticProgram
from qiskit.optimization.converters import IntegerToBinary
mod = QuadraticProgram()
mod.integer_var(name='x', lowerbound=0, upperbound=1)
mod.quadratic_constraint({'x': 1}, {('x', 'x'): 2}, '==', 1)
mod2 = IntegerToBinary().convert(mod)
Traceback (most recent call last):
  File "d.py", line 6, in <module>
    mod2 = IntegerToBinary().convert(mod)
  File "/Users/ima/envs/qiskit/lib/python3.7/site-packages/qiskit/optimization/converters/integer_to_binary.py", line 98, in convert
    self._substitute_int_var()
  File "/Users/ima/envs/qiskit/lib/python3.7/site-packages/qiskit/optimization/converters/integer_to_binary.py", line 201, in _substitute_int_var
    constraint.quadratic.to_dict()
  File "/Users/ima/envs/qiskit/lib/python3.7/site-packages/qiskit/optimization/converters/integer_to_binary.py", line 139, in _convert_quadratic_coefficients_dict
    x = self._src.get_variable(name_i)
  File "/Users/ima/envs/qiskit/lib/python3.7/site-packages/qiskit/optimization/problems/quadratic_program.py", line 245, in get_variable
    return self.variables[self._variables_index[i]]
KeyError: 0

Details and comments

The main reason is that there were some codes missing use_name=True of LinearExpression.to_dict and QuadraticExpression.to_dict.

I also notice that the return type does not match the definition QuadraticExpression.to_dict.
Especially, the signature of to_dict is Dict[Union[Tuple[int, int], Tuple[str, str]], float];
but, the following returns Tuple[np.int32, np.int32], float]. Because isinstance(value, int) == False with value: np.int32. This is the reason why only quadratic constraints raises this bug. I fix this issue as well.
https://github.com/Qiskit/qiskit-aqua/blob/793ffc5cfc3cfbab470ba0243fb80d42fcd28ebb/qiskit/optimization/problems/quadratic_expression.py#L171

@t-imamichi t-imamichi requested a review from a-matsuo September 17, 2020 14:40
@woodsp-ibm woodsp-ibm added the Changelog: Bugfix Include in the Fixed section of the changelog label Sep 17, 2020
@woodsp-ibm woodsp-ibm added this to the 0.8 milestone Sep 17, 2020
@t-imamichi
Copy link
Contributor Author

Thank you for your review!

@woodsp-ibm woodsp-ibm merged commit 939baf4 into qiskit-community:master Sep 18, 2020
@t-imamichi t-imamichi deleted the fix_int2bin branch September 18, 2020 13:28
manoelmarques added a commit to qiskit-community/qiskit-optimization that referenced this pull request Jan 14, 2021
…-aqua#1256)

* fix a bug of the integer to binary converter

* fix the return signature of QuadraticExpression.to_dict

Co-authored-by: Manoel Marques <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Changelog: Bugfix Include in the Fixed section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants