<jupyter_start><jupyter_text>Defining our starting QUBO<jupyter_code>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))<jupyter_output>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. ]]<jupyter_text>Adding Variable<jupyter_code># Adding linear interaction to 2nd variable in QUBO

bqm_qubo.add_variable(1,0.4)  

print(bqm_qubo.to_numpy_matrix().astype(float))
# Adding a NEW 3rd variable with linear interaction in QUBO

bqm_qubo.add_variable(3,0.5)  

print(bqm_qubo.to_numpy_matrix().astype(float))
# Adding more than one variable (linear bias) using dictionary
bqm_qubo.add_variables_from({4:7.7,5:6.6}) 

print(bqm_qubo.to_numpy_matrix().astype(float))<jupyter_output>[[-4.4  2.8  0.   0.   0.   0. ]
 [ 0.   1.  -0.8  0.   0.   0. ]
 [ 0.   0.  -2.   0.   0.   0. ]
 [ 0.   0.   0.   0.5  0.   0. ]
 [ 0.   0.   0.   0.   7.7  0. ]
 [ 0.   0.   0.   0.   0.   6.6]]<jupyter_text>Removing Variable<jupyter_code>## Removing a single variable

bqm_qubo.remove_variable(3)

print(bqm_qubo)     # 4th variable removed
# Removing multiple variables

bqm_qubo.remove_variables_from([4,5])

print(bqm_qubo)<jupyter_output>BinaryQuadraticModel({0: -4.4, 1: 1.0, 2: -2.0}, {(0, 1): 2.8, (1, 2): -0.8}, 2.4, 'BINARY')<jupyter_text>Adding Interactions<jupyter_code># Adding interaction between 0-2

bqm_qubo.add_interaction(0, 2, 0.9)
print(bqm_qubo.to_numpy_matrix().astype(float))
# Adding new (4th) variable and interaction

bqm_qubo.add_interaction(1, 3, 1.5)
print(bqm_qubo.to_numpy_matrix().astype(float))
# Adding multiple interactions

bqm_qubo.add_interactions_from({(0,3):1.1,(2,3):3.3})
print(bqm_qubo.to_numpy_matrix().astype(float))<jupyter_output>[[-4.4  2.8  0.9  1.1]
 [ 0.   1.  -0.8  1.5]
 [ 0.   0.  -2.   3.3]
 [ 0.   0.   0.   0. ]]<jupyter_text>Removing Interactions<jupyter_code># Removing single interaction (keeps the variable)

bqm_qubo.remove_interaction(0,3)
print(bqm_qubo.to_numpy_matrix().astype(float))
# Reemoving multiple interactions (keeps variable)

bqm_qubo.remove_interactions_from([(1,3),(2,3)])
print(bqm_qubo.to_numpy_matrix().astype(float))<jupyter_output>[[-4.4  2.8  0.9  0. ]
 [ 0.   1.  -0.8  0. ]
 [ 0.   0.  -2.   0. ]
 [ 0.   0.   0.   0. ]]<jupyter_text>Adding and Removing offset<jupyter_code>print(bqm_qubo)  # look here the offset is 2.4
# Removing the offset
bqm_qubo.remove_offset()

print(bqm_qubo)   # Notice the offset is zero
# Adding the offset
bqm_qubo.add_offset(0.99)

print(bqm_qubo)<jupyter_output>BinaryQuadraticModel({0: -4.4, 1: 1.0, 2: -2.0, 3: 0.0}, {(0, 1): 2.8, (0, 2): 0.9, (1, 2): -0.8}, 0.99, 'BINARY')<jupyter_text>Normalizing a BQM<jupyter_code># Normalizing a QUBO
bqm_qubo.normalize()

print(bqm_qubo.to_numpy_matrix().astype(float))
print(bqm_qubo) # offset changed<jupyter_output>[[-1.          0.63636364  0.20454545  0.        ]
 [ 0.          0.22727273 -0.18181818  0.        ]
 [ 0.          0.         -0.45454545  0.        ]
 [ 0.          0.          0.          0.        ]]
BinaryQuadraticModel({0: -1.0, 1: 0.22727272727272727, 2: -0.45454545454545453, 3: 0.0}, {(0, 1): 0.6363636363636364, (0, 2): 0.20454545454545453, (1, 2): -0.18181818181818182}, 0.22499999999999998, 'BINARY')<jupyter_text>Multiplying BQM with scalar<jupyter_code>bqm_qubo.scale(10)

print(bqm_qubo.to_numpy_matrix().astype(float))
print(bqm_qubo)<jupyter_output>[[-10.           6.36363636   2.04545455   0.        ]
 [  0.           2.27272727  -1.81818182   0.        ]
 [  0.           0.          -4.54545455   0.        ]
 [  0.           0.           0.           0.        ]]
BinaryQuadraticModel({0: -10.0, 1: 2.2727272727272725, 2: -4.545454545454545, 3: 0.0}, {(0, 1): 6.363636363636363, (0, 2): 2.0454545454545454, (1, 2): -1.8181818181818183}, 2.25, 'BINARY')
{'fraction_non_alphanumeric': 0.17322050771528125, 'fraction_numerical': 0.1408661025385764, 'mean_word_length': 3.113613101330604, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '640644', 'license': 'MIT', 'n_tokens_mistral': 2156, 'n_tokens_neox': 1898, 'n_words': 337, 'path': "D'wave tutorials/2.Deep Dive into dimod.ipynb", 'repo_name': 'speQtrum/Ising_Annealing_Machines', 'size': '4018'}