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

### BQM with SPIN variables
\begin{equation}
H_{1}^{ising}=-1.5s_{1}+0.8s_{2}-1.2s_{3}+0.7s_{1}s_{2}-0.2s_{2}s_{3}
\end{equation}


```
h = {0: -1.5, 1: 0.8, 2: -1.2}  # linear
J = {(0,1): 0.7, (1,2):-0.2}    # quadratic
offset = 0.0

bqm_ising = dimod.BinaryQuadraticModel(h,J,offset,dimod.Vartype.SPIN)
print(bqm_ising)
```

    BinaryQuadraticModel({0: -1.5, 1: 0.8, 2: -1.2}, {(0, 1): 0.7, (1, 2): -0.2}, 0.0, 'SPIN')


### BQM as QUBO
\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. ]]


# Creating EMPTY BQM


```
######  creating empty ISING BQM
empty_ising_bqm = dimod.BinaryQuadraticModel.empty(dimod.SPIN)
print(empty_ising_bqm)
```

    BinaryQuadraticModel({}, {}, 0.0, 'SPIN')



```
###### creating empty QUBO BQM
empty_qubo_bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY)
print(empty_qubo_bqm)
```

    BinaryQuadraticModel({}, {}, 0.0, 'BINARY')


# Extracting BQM parameters 


```
print("BQM:",bqm_qubo)

print("\n\nLinear term:    ",bqm_qubo.linear)   # bqm_qubo.variables
print("Quadratic term: ",bqm_qubo.quadratic)
print("Offset term:    ",bqm_qubo.offset)
print("Variable type:  ",bqm_qubo.vartype)
```

    BQM: BinaryQuadraticModel({0: -4.4, 1: 0.6, 2: -2.0}, {(0, 1): 2.8, (1, 2): -0.8}, 2.4, 'BINARY')
    
    
    Linear term:     {0: -4.4, 1: 0.6, 2: -2.0}
    Quadratic term:  {(0, 1): 2.8, (1, 2): -0.8}
    Offset term:     2.4
    Variable type:   Vartype.BINARY



```
############   Getting Bias and coupling strength  #############

print('linear bias of 1st variable',bqm_qubo.get_linear(0))
print('Coupling strength of 2nd & 3rd variable',bqm_qubo.get_quadratic(1,2))
```

    linear bias of 1st variable -4.4
    Coupling strength of 2nd & 3rd variable -0.8



```
###########   Getting number of variables  ###########

print("Number of variables:",bqm_qubo.num_variables)


###########   Getting number of couplings  ###########

print("Number of couplings:",bqm_qubo.num_interactions)


##########    Getting shape of QUBO   ###########

print('shape is:',bqm_qubo.shape)
```

    Number of variables: 3
    Number of couplings: 2
    shape is: (3, 2)


# Change variable type : from Ising to QUBO


```
print(bqm_ising)

print('\n\n',bqm_ising.to_qubo())   # does not save. Temporary operation

print('\n\n',bqm_ising)
```

    BinaryQuadraticModel({0: -1.5, 1: 0.8, 2: -1.2}, {(0, 1): 0.7, (1, 2): -0.2}, 0.0, 'SPIN')
    
    
     ({(0, 1): 2.8, (1, 2): -0.8, (0, 0): -4.4, (1, 1): 0.6000000000000002, (2, 2): -2.0}, 2.4)
    
    
     BinaryQuadraticModel({0: -1.5, 1: 0.8, 2: -1.2}, {(0, 1): 0.7, (1, 2): -0.2}, 0.0, 'SPIN')



```
print(bqm_ising)

print('\n\n',bqm_ising.change_vartype('BINARY'))   # permanent operation

print('\n\n',bqm_ising)
```

    BinaryQuadraticModel({0: -1.5, 1: 0.8, 2: -1.2}, {(0, 1): 0.7, (1, 2): -0.2}, 0.0, 'SPIN')
    
    
     BinaryQuadraticModel({0: -4.4, 1: 0.6000000000000002, 2: -2.0}, {(0, 1): 2.8, (1, 2): -0.8}, 2.3999999999999995, 'BINARY')
    
    
     BinaryQuadraticModel({0: -4.4, 1: 0.6000000000000002, 2: -2.0}, {(0, 1): 2.8, (1, 2): -0.8}, 2.3999999999999995, 'BINARY')


# Numpy matrix and QUBO


```
print(bqm_qubo.to_numpy_matrix().astype(float))    # converts BQM to numpy matrix
```

    [[-4.4  2.8  0. ]
     [ 0.   0.6 -0.8]
     [ 0.   0.  -2. ]]



```
### creating QUBO from Numpy matrix

my_array= np.array([[1,2,3],
             [2,7,5],
             [3,5,9]])
              
print(my_array)  # Real symmetric matrix

bqm_qubo_1 = dimod.BinaryQuadraticModel.from_numpy_matrix(my_array)
print(bqm_qubo_1,'\n')
print(bqm_qubo_1.to_numpy_matrix().astype(float))    # In upper triangular form
```

    [[1 2 3]
     [2 7 5]
     [3 5 9]]
    BinaryQuadraticModel({0: 1.0, 1: 7.0, 2: 9.0}, {(0, 1): 4, (0, 2): 6, (1, 2): 10}, 0.0, 'BINARY') 
    
    [[ 1.  4.  6.]
     [ 0.  7. 10.]
     [ 0.  0.  9.]]


# Numpy Vectors and QUBO


```
print(bqm_qubo)

a = bqm_qubo.to_numpy_vectors()    # converts BQM to numpy vectors
print(a)
```

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



```
# creating QUBO from numpy vectors
# m = np.asarray([1,7,9])
# n = (np.asarray([4]),np.asarray([6]),np.asarray([10]))
# bqm_qubo_2 = dimod.BinaryQuadraticModel.from_numpy_vectors(m,n,0, dimod.BINARY)
# print(bqm_qubo_2)
```

# Serializable JSON & BQM


```
########  converting into JSON
import json

s = json.dumps(bqm_qubo.to_serializable(), indent=2)
print(s)  # copy the json and paste it to json viewer
```

    {
      "type": "BinaryQuadraticModel",
      "version": {
        "bqm_schema": "3.0.0"
      },
      "use_bytes": false,
      "index_type": "uint16",
      "bias_type": "float32",
      "num_variables": 3,
      "num_interactions": 2,
      "variable_labels": [
        0,
        1,
        2
      ],
      "variable_type": "BINARY",
      "offset": 2.4000000953674316,
      "info": {},
      "linear_biases": [
        -4.400000095367432,
        0.6000000238418579,
        -2.0
      ],
      "quadratic_biases": [
        2.799999952316284,
        -0.800000011920929
      ],
      "quadratic_head": [
        0,
        1
      ],
      "quadratic_tail": [
        1,
        2
      ]
    }



```
#### creating BQM from JSON

bqm_qubo_3 = dimod.BinaryQuadraticModel.from_serializable(json.loads(s))
print(bqm_qubo_3.to_numpy_matrix().astype(float))
```

    [[-4.4000001   2.79999995  0.        ]
     [ 0.          0.60000002 -0.80000001]
     [ 0.          0.         -2.        ]]



```
## Create random symmetric square matrices
```


```

```

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