# QUBO Generator 

----

###### Note:
If you viewing this notebook directly at github.com, the LaTeX formulas may not be rendered correctly. 
Thus, we highly recommend to use [nbviewer](https://nbviewer.jupyter.org/) for this purpose. 

You can view this notebook directly at: https://nbviewer.jupyter.org/github/quantum-circle/qc_qubogen/blob/master/examples/example.ipynb

---

<br>

## A Minimal Example
Let's assume that we are given the following combinatorial optimization problem:

$$\min _{x} f(x) = 2x + 3 \; , $$

where $x \in \{0, 1\}$, i.e. a binary variable. Our goal is now to reformulate the above objective function as a QUBO model, i.e. a symmetric (or at least upper diagonal) matrix $\mathbf{Q} $ and a constant offset term $c$, that can be described as: 

$$\min _{\mathbf{x}} f(\mathbf{x}) = \mathbf{x}^T \mathbf{Q} \mathbf{x} + c$$

Note that we used $\mathbf{x}$ here to denote a vector, yet in our example $\mathbf{x}$ consists of only one element $x_i$.

<br>

To accomplish this task, we are going to use `qc_qubogen.QUBOModel` built-in functionality:
1.   Define the objective function using sympy's symbolic variables to represent the binary variables $x_i$. This is be done by creating an instance of `sympy.Symbol`, which allows to use an arbitrary variable name (*str*), e.g. "x_0". 
2. We can then create an instance of `qc_qubogen.QUBOModel` and pass the previoulsy created objective function as first argument. 
3. Once the QUBO model is created, we can convert it to matrix form using the `qc_qubogen.QUBOModel().model_to_qubo()` method. Note that this method will not return the constant offset term $c$ by default, since it will not affect the optimization procedure. However, we can set the argument `return_offset=True`, to return both,  the QUBO matrix $\mathbf{Q} $ as $2-$dim `numpdy.ndarray` **and** the constant $c$ as `float`. 


```python
from sympy import Symbol
from qc_qubogen import QUBOModel


# 1. Create a symbolic binary variable with sympy.Symbol
x_0 = Symbol("x_0")

# 2. Define the function to be minimized
objective = 2*x_0 + 3

# 3. Initialize the `QUBOModel` using the created objective function
model = QUBOModel(objective)

# 4. Convert the problem and return the QUBO matrix and offset constant
qubo_matrix, qubo_offset = model.model_to_qubo(return_offset=True)

print(f"QUBO matrix: Q = {qubo_matrix}")
print(f"QUBO offset: c = {qubo_offset}")
```

    QUBO matrix: Q = [[2.]]
    QUBO offset: c = 3.0


## Another Illustrative Example

To illustrate a slightly more advanced example, we choose to solve the optimization problem given by Glover et. al.$^{[1]}$:

$$ f(\mathbf{x}) = -5x_0 -3x_1 -8x_2 -6x_3 + 4x_0x_1 + 8x_0x_2 + 2x_1x_2 + 10x_2x_3 \; ,$$

which will be converted to the QUBO problem:

$$ f(\mathbf{x})  = \left(\begin{matrix} x_0 & x_1 & x_3 & x_4 \end{matrix}\right) \left(\begin{matrix} -5 & 2 & 4 & 0 \\ 2 & -3 & 1 & 0 \\ 4 & 1 & -8 & 5 \\ 0 & 0 & 5 & -6 \end{matrix} \right) \left(\begin{matrix} x_0\\ x_1 \\ x_2 \\ x_3 \end{matrix}\right) $$

Note that we omit the constant offset $c$ here, since $c = 0$.

<br>

Here, we're going to use the helper function `qc_qubogen.get_symbolic_binary_variables(n=4)` , which generates a list of $4$ `sympy.Symbol` symbolic binary variables.

---

[1] Glover, F., Kochenberger, G. and Du, Yu. Quantum Bridge Analytics I: a tutorial on formulating and using QUBO models. *Available at:* https://arxiv.org/pdf/1811.11538.pdf


```python
from qc_qubogen import QUBOModel, get_symbolic_binary_variables


x = get_symbolic_binary_variables(n=4)

objective = - 5*x[0] - 3*x[1] - 8*x[2] - 6*x[3] + 4*x[0]*x[1] + 8*x[0]*x[2] + 2*x[1]*x[2] + 10*x[2]*x[3]

model = QUBOModel(objective)
qubo_matrix, qubo_offset = model.model_to_qubo(return_offset=True)

print(f"QUBO matrix: Q = \n{qubo_matrix}")
```

    QUBO matrix: Q = 
    [[-5.  2.  4.  0.]
     [ 2. -3.  1.  0.]
     [ 4.  1. -8.  5.]
     [ 0.  0.  5. -6.]]


6ae81c716d6b7a47d2fb6cf03820b1b334a884ac
ipynb
Jupyter Notebook
examples/example.ipynb
quantum-circle/qc_qubogen
77966b1f4cb7bec21f8662ff6b4905cf1bce1d1d
Apache-2.0
examples/example.ipynb
quantum-circle/qc_qubogen
77966b1f4cb7bec21f8662ff6b4905cf1bce1d1d
Apache-2.0
examples/example.ipynb
quantum-circle/qc_qubogen
77966b1f4cb7bec21f8662ff6b4905cf1bce1d1d
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES