When converting to bqm with .compile().to_bqm()
, the original order of the variables is not maintained.
To reproduce:
from pyqubo import Array, Binary
n=3
m=12
X = Array.create('x', shape=(n,m), vartype='BINARY')
for i in range(n):
a=0
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?