Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

1 QUBO Example

This example shows how to create the following QUBO:

3 3
∑ xii − ∑ xij
i =1 i,j=1
i< j

The SBM solver then returns the vector of binary decision variables xi ∈ [0, 1] that minimizes the
QUBO.
The steps for submitting this problem to SBM are the following:
1. Create a matrix Q such that the above diagonal elements are -1 and the elements on the
diagonal are 1.
2. Convert Q to a symmetric matrix.
3. Convert this symmetric matrix to a matrix in coordinate format. This can be done using the
coo_matrix function from scipy.sparse.
4. Save the matrix in coordinate format to a Matrix Market file. This can be done using the
mmwrite function from scipy.io.
5. Rename the file name so it is saved as a .txt file instead of .mtx

1.1 Create Q
[1]: import numpy as np

Q = np.array([[1, -1, -1], [0, 1, -1], [0, 0, 1]])


print(Q)

[[ 1 -1 -1]
[ 0 1 -1]
[ 0 0 1]]

1.2 Make Q symmetric

[2]: symmetric_Q = Q + np.triu(Q, 1).T


print(symmetric_Q)

[[ 1 -1 -1]
[-1 1 -1]
[-1 -1 1]]

1.3 Convert to a coordinate matrix


[3]: from scipy.sparse import coo_matrix

coordinate_matrix = coo_matrix(symmetric_Q)
print(coordinate_matrix)

CogniFrame Inc. All Rights Reserved.


(0, 0) 1
(0, 1) -1
(0, 2) -1
(1, 0) -1
(1, 1) 1
(1, 2) -1
(2, 0) -1
(2, 1) -1
(2, 2) 1

1.4 Save the coordinate matrix to a Matrix Market file


[4]: from scipy.io import mmwrite

file_name = 'example_Q'
mmwrite(file_name, a = coordinate_matrix, field = 'real', symmetry = 'symmetric')

1.5 Rename the file extension from .mtx to .txt


[5]: import os

my_file = file_name + '.mtx'


base = os.path.splitext(my_file)[0]
os.rename(my_file, base + '')

CogniFrame Inc. All Rights Reserved.

You might also like