```
import numpy as np
import pandas as pd
import dimod
```

### Solving a QUBO with dimod Samplers
\begin{equation}
H_{1}^{QUBO}=-4.4x_{1}^2+0.6x_{2}^2-2x_{3}^2+2.8x_{1}x_{2}-0.8x_{2}x_{3}+2.4
\end{equation}


```
linear = {0: -4.4, 1: 0.6, 2: -2}
quadratic = {(0,1): 2.8, (1,2):-0.8}
offset = 2.4

bqm_qubo = dimod.BinaryQuadraticModel(linear,quadratic,offset,dimod.Vartype.BINARY)
print(bqm_qubo)
print('\n',bqm_qubo.to_numpy_matrix().astype(float))
```

    BinaryQuadraticModel({0: -4.4, 1: 0.6, 2: -2.0}, {(0, 1): 2.8, (1, 2): -0.8}, 2.4, 'BINARY')
    
     [[-4.4  2.8  0. ]
     [ 0.   0.6 -0.8]
     [ 0.   0.  -2. ]]


# Using dwave.neal
neal also provides Simulated annealing sampler


```
import neal

sampleset = neal.SimulatedAnnealingSampler().sample(bqm_qubo, num_reads=10)

print(sampleset.to_pandas_dataframe())
```

       0  1  2  energy  num_occurrences
    0  1  0  1    -4.0                1
    1  1  0  1    -4.0                1
    2  1  0  1    -4.0                1
    3  1  0  1    -4.0                1
    4  1  0  1    -4.0                1
    5  1  0  1    -4.0                1
    6  1  0  1    -4.0                1
    7  1  0  1    -4.0                1
    8  1  0  1    -4.0                1
    9  1  0  1    -4.0                1


# Using dwave.greedy
An implementation of a steepest descent solver for binary quadratic models.

Steepest descent is the discrete analogue of gradient descent, but the best move is computed using a local minimization rather rather than computing a gradient. At each step, we determine the dimension along which to descend based on the highest energy drop caused by a variable flip.


```
import greedy

sampleset = greedy.SteepestDescentSolver().sample(bqm_qubo)

print(sampleset.to_pandas_dataframe())
```

       0  1  2  energy  num_occurrences
    0  1  0  1    -4.0                1



```
import greedy

sampleset = greedy.SteepestDescentSampler().sample(bqm_qubo)

print(sampleset.to_pandas_dataframe())
```

       0  1  2  energy  num_occurrences
    0  1  0  1    -4.0                1


# Using dwave.tabu
The TabuSampler sampler implements the MST2 multistart tabu search algorithm for quadratic unconstrained binary optimization (QUBO) problems with a dimod Python wrapper.


```
import tabu

sampleset = tabu.TabuSampler().sample(bqm_qubo,num_reads=5)

print(sampleset.to_pandas_dataframe())
```

       0  1  2  energy  num_occurrences
    0  1  0  1    -4.0                1
    1  1  0  1    -4.0                1
    2  1  0  1    -4.0                1
    3  1  0  1    -4.0                1
    4  1  0  1    -4.0                1



```
#####  Creates Random QUBO of specific size  ##########

def Random_QUBO(n):
    Z = np.zeros((n,n))
    for i in range(n):
        for j in range(n):
            if i==j:
                Z[i,j]=np.random.uniform(-2,2)
            elif i!=j:
                Z[i,j]=Z[j,i]=np.random.uniform(-1,1)

    qubo = dimod.BinaryQuadraticModel.from_numpy_matrix(Z)
    return qubo.to_numpy_matrix().astype(float)
```

# Using dwave.qbsolv



```
from dwave_qbsolv import QBSolv


w = Random_QUBO(15)  # creating random QUBO
```


```
sampleset = QBSolv().sample_qubo(w, solver_limit=10)  # broken in two sub problems

print(sampleset)
```

       0  1  2  3  4  5  6  7  8  9 10 11 12 13 14     energy num_oc.
    0  1  0  1  0  1  0  0  1  1  1  0  1  0  1  0 -11.916516      51
    ['BINARY', 1 rows, 51 samples, 15 variables]



```

```

6cf77232373b20e3d9628f327dcc273f37ab4413
ipynb
Jupyter Notebook
D'wave tutorials/5.D-wave Classical Solvers.ipynb
speQtrum/Ising_Annealing_Machines
f7da721c29314fc898436103a1501dcf6c05ce9c
MIT
2021-04-02T02:33:02.000Z
2021-04-03T02:08:14.000Z
D'wave tutorials/5.D-wave Classical Solvers.ipynb
speQtrum/Ising_Annealing_Machines
f7da721c29314fc898436103a1501dcf6c05ce9c
MIT
D'wave tutorials/5.D-wave Classical Solvers.ipynb
speQtrum/Ising_Annealing_Machines
f7da721c29314fc898436103a1501dcf6c05ce9c
MIT
2021-04-06T13:34:47.000Z
2022-03-17T00:45:08.000Z
Qwen/Qwen-72B
1. YES
2. YES