When converting to bqm with .compile().to_bqm(), the original order of the variables is not maintained. To reproduce:

from pyqubo import Array, Binary


X = Array.create('x', shape=(n,m), vartype='BINARY')

for i in range(n):
    for j in range(m):
        a += 2.0 * X[i,j]

    diff = a - 1.5

    model = diff.compile()

    print("BEFORE: ", model.variables)

    print("AFTER: ", model.to_bqm().variables)

The first two printed lines are:

BEFORE:  ['x[0][0]', 'x[0][1]', 'x[0][2]', 'x[0][3]', 'x[0][4]', 'x[0][5]', 'x[0][6]', 'x[0][7]', 'x[0][8]', 'x[0][9]', 'x[0][10]', 'x[0][11]']
AFTER:  Variables(['x[0][6]', 'x[0][11]', 'x[0][10]', 'x[0][7]', 'x[0][4]', 'x[0][3]', 'x[0][2]', 'x[0][5]', 'x[0][8]', 'x[0][9]', 'x[0][1]', 'x[0][0]'])

How can I preserve the original order of the variables?



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.