recruit-communications / pyqubo

Python DSL for constructing QUBOs from mathematical expressions.
https://pyqubo.readthedocs.io/
Apache License 2.0
177 stars 46 forks source link

Incorrect QUBO output for single binary variables #232

Open Zhanwei-Liu opened 4 months ago

Zhanwei-Liu commented 4 months ago

Describe the bug The output of the to_qubo() function in pyqubo does not correctly include the coefficients for single binary variables in the expected output.

To Reproduce

from pyqubo import Binary, Constraint

def output_qubo(H):
    model = H.compile()
    qubo, _ = model.to_qubo()
    print(qubo)

a, b = Binary('a'), Binary('b')
M = 5.0

output_qubo(2 * a + b)
output_qubo(2 * a + b + M * Constraint((a + b) ** 2, label='a+b=0'))

Actual Output

{('a', 'a'): 2.0, ('b', 'b'): 1.0}
{('a', 'a'): 7.0, ('a', 'b'): 10.0, ('b', 'b'): 6.0}

Expected Output The expected output should include the coefficients for the single binary variables as well as follows:

{('a'): 2.0, ('b'): 1.0}
{('a'): 2.0, ('b'): 1.0, ('a', 'a'): 5.0, ('a', 'b'): 10.0, ('b', 'b'): 5.0}