```
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. ]]



```
###   Solving with Exact solver   ####
sampleset = dimod.ExactSolver().sample(bqm_qubo)

print(sampleset.to_pandas_dataframe())
```

       0  1  2  energy  num_occurrences
    0  0  0  0     2.4                1
    1  1  0  0    -2.0                1
    2  1  1  0     1.4                1
    3  0  1  0     3.0                1
    4  0  1  1     0.2                1
    5  1  1  1    -1.4                1
    6  1  0  1    -4.0                1
    7  0  0  1     0.4                1



```
####  Solving with Simulated Annealing sampler  ###

sampleset = dimod.SimulatedAnnealingSampler().sample(bqm_qubo,num_reads=10,num_sweeps=1000)

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



```
###  Sampling with Identity sampler  ###
sampleset = dimod.IdentitySampler().sample(bqm_qubo)

print(sampleset)
```

       0  1  2 energy num_oc.
    0  1  1  1   -1.4       1
    ['BINARY', 1 rows, 1 samples, 3 variables]



```
####   Sampling with Random sampler  ####

sampleset = dimod.RandomSampler().sample(bqm_qubo, num_reads=10)   # draws 10 random samples

print(sampleset.to_pandas_dataframe())
```

       0  1  2  energy  num_occurrences
    0  1  1  0     1.4                1
    1  1  1  1    -1.4                1
    2  1  1  0     1.4                1
    3  0  1  0     3.0                1
    4  0  0  0     2.4                1
    5  0  0  1     0.4                1
    6  1  1  1    -1.4                1
    7  0  1  0     3.0                1
    8  0  0  1     0.4                1
    9  1  0  0    -2.0                1



```
###  Sampling with Numlll sampler  ####
sampleset = dimod.NullSampler().sample(bqm_qubo)

print(sampleset)
```

    Empty SampleSet
    Record Fields: ['sample', 'energy', 'num_occurrences']
    Variables: [0, 1, 2]
    ['BINARY', 0 rows, 0 samples, 3 variables]



```

```

24124467540afd05d3afe164d59d7f179fa4a63f
ipynb
Jupyter Notebook
D'wave tutorials/4.Introduction to dimod samplers.ipynb
speQtrum/Ising_Annealing_Machines
f7da721c29314fc898436103a1501dcf6c05ce9c
MIT
2021-04-02T02:33:02.000Z
2021-04-03T02:08:14.000Z
D'wave tutorials/4.Introduction to dimod samplers.ipynb
speQtrum/Ising_Annealing_Machines
f7da721c29314fc898436103a1501dcf6c05ce9c
MIT
D'wave tutorials/4.Introduction to dimod samplers.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