# Python to Julia: Boolean Optimization

**URL:** <https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062>\
**Category:** New to Julia\
**Tags:** optimization\
**Created:** [March 18, 2022, 2:13am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062 "2022-03-18T02:13:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 18, 2022, 2:13am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/1 "2022-03-18T02:13:48Z")

</div>

Hello Julians:

I am looking to find the lowest energy  
state for the following system:

(5x\_a) - (0.1x\_0) - (2x\_a\*x\_0) - 1.5

Using the following truth table:  
x\_a | x\_0  
0 | | 0  
0 | | 1  
1 | | 0  
1 | | 1

The first step in this series is to  
initialize the empty qubo object  
as:

```julia
qubo = QUBO()

```

Then build the qubo elements  
with:

```julia
qubo[('a',)] += 5
qubo[(0,)] -= 0.1
qubo[(0, 'a')] -= 2
qubo -= 1.5
model_solution = qubo.solve_bruteforce()

```

After, find the optimum value  
using

```julia
qubo.value(model_solution)

```

Could any of you point me to  
resources that could help  
convert these process steps  
to Julia?

Thanks,

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 19, 2022, 12:46am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/2 "2022-03-19T00:46:04Z")

</div>

Sorry, what is a QUBO object? I am from the area of optimization and have never heard of it.

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [March 19, 2022, 2:03am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/3 "2022-03-19T02:03:52Z")

</div>

I guess it stands for [quadratic unconstrained binary optimization](https://en.wikipedia.org/wiki/Quadratic_unconstrained_binary_optimization)? As someone who has next to no experience with optimization stuff, I found the README in this related repo ([https://github.com/lanl/find-qubo](https://github.com/lanl/find-qubo)) a lot more helpful, but I could be totally off here

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 19, 2022, 4:44am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/4 "2022-03-19T04:44:27Z")

</div>

So you want to find which one of those four options gives the lowest value for the function? I would probably do

```julia
x_a = [false, true, false, true]
x_0 = [false, false, true, true]
y = 5*x_a .- 0.1*x_0 .- 2*x_a.*x_0 .+ 1.5

y_1, i = findmin(y)

x_a1 = x_a[i]
x_01 = x_0[i]
```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 19, 2022, 2:17pm UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/5 "2022-03-19T14:17:18Z")

</div>

Well, if what Python is doing is a brute-force (as hinted by `solve_bruteforce()`), then @gustaphe is adequate (while it probably allocates more memory than necessary).

I remember the Quadratic Binary Problem, I just did not associate with the acronym and the example. It is NP-Hard and you probably will need something more elaborate if you want to solve larger instances.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 19, 2022, 8:43pm UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/6 "2022-03-19T20:43:46Z")

</div>

As @icweaver expressed, QUBO, is a quadratic function  
on boolean variables. The QUBO problem, then finds the  
assignment of boolean variables that five a minimum  
function value. In this scenario, (1,0) giving (-1.6) – you  
are probably familiar, was only expanding the knowledge.

@Henrique_Becker – do you have any idea what packages/  
modules might help me deal with larger D and N? Or might  
this require a native build?

Thank you,

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 19, 2022, 8:58pm UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/7 "2022-03-19T20:58:43Z")

</div>

I did not work on QUBO, so I am not aware if the Julia ecosystem has something specific aimed at it. I do know that JuMP supports commercial solvers (with free academic licenses) which support non-linear optimization. And QUBO is not the hardest problem to model (the gap between the input and a trivial formulation is small). Did you already consider using Gurobi, CPLEX, or other solver?

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [March 25, 2022, 12:45am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/9 "2022-03-25T00:45:29Z")

</div>

Just found this and might be of interest:  
[https://github.com/psrenergy/ToQUBO.jl](https://github.com/psrenergy/ToQUBO.jl)  
[https://psrenergy.github.io/ToQUBO.jl/dev/](https://psrenergy.github.io/ToQUBO.jl/dev/)

@pedromxavier @joaquimg

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [March 25, 2022, 6:50pm UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/10 "2022-03-25T18:50:58Z")

</div>

This was just sent to the registry! 2 more days before it’s there.  
The goal is to convert “any” JuMP model into a QUBO approximation and then solve it with Annealers (classical or quantum).  
You can also write your QUBO directly in JuMP (just make all your variables binary and pass your quadratic objective). In this case, ToQUBO won’t need to reformulate but will send the problem to your selected annealer.

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 26, 2022, 2:17am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/12 "2022-03-26T02:17:14Z")

</div>

@joaquimg

How would you write the I/O request in Pluto 1.8.\*  
(web, not the REPL):

```julia
$ git clone https://github.com/psrenergy/ToQUBO.jl
$ cd ToQUBO.jl
$ julia --project=.

```

I started by using the HTTP package so that

```julia
using HTTP
r = HTTP.request("GET", "https://github.com/psrenergy/ToQUBO.jl")

```

How might I change the directory to (“ToQUBO.jl”) here?

Also: I do not believe git clone is a recognized command  
in the REPL. (dev) might be the most viable substitute?

Thank you,

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [March 26, 2022, 3:55am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/13 "2022-03-26T03:55:37Z")

</div>

Hopefully the registration will make this unnecessary, but in general see here:  
[https://pkgdocs.julialang.org/v1/managing-packages/#Adding-unregistered-packages](https://pkgdocs.julialang.org/v1/managing-packages/#Adding-unregistered-packages)  
But also in general, add this code to the beginning of the notebook:

```julia
using Pkg
Pkg.add(url="https://github.com/psrenergy/ToQUBO.jl", rev="master")

```

(Change the string after `rev=` to the desired branch name).

---

<div class="post-metadata">

**Author:** ![YummyPampers2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yummypampers2/32/27328_2.png) [@YummyPampers2](https://discourse.julialang.org/u/YummyPampers2)\
**Post date:** [March 27, 2022, 3:36am UTC](https://discourse.julialang.org/t/python-to-julia-boolean-optimization/78062/14 "2022-03-27T03:36:28Z")

</div>

Thank you for this @jd-foster

I implemented the following using Git  
recommended by a colleague here

```julia
using Git
run(git(["clone", "https://github.com/psrenergy/ToQUBO.jl"]))
cd("ToQUBO.jl")

```

How would you implement in  
a Pluto.jl session (NOT the REPL)

```julia
julia --project=.

```
