<jupyter_start><jupyter_text>Necessary conditions for Graph partition  Install pyQUBO from Recruit Communications Co. Ltd.    pip install pyqubo Install openJij from Jij Inc.  (startup from Tohoku University)    pip install openjij  Add networkx for dealing with graph theory         pip install networkx  Solve Graph Partition  import pyQUBO, openJij and numpy<jupyter_code>from pyqubo  import Array,Constraint, Placeholder
import openjij as jij
import numpy as np<jupyter_output><empty_output><jupyter_text>Array, Constrains and Placeholders are convenient classes from pyQUBO  import networkx<jupyter_code>import networkx as nx<jupyter_output><empty_output><jupyter_text>Prepare some graph<jupyter_code>nodes = [0, 1, 2, 3, 4, 5]
edges = [
    (0, 1), (1, 2), (2, 0), 
    (1, 5), (0, 3),
    (3, 4), (4, 5), (5, 1)
    ]<jupyter_output><empty_output><jupyter_text>Set nodes and edges on Graph G<jupyter_code>G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
import matplotlib.pyplot as plt
nx.draw_networkx(G)
plt.axis("off")
plt.show()<jupyter_output><empty_output><jupyter_text>Prepare spin variables<jupyter_code>N = 6
vartype = "SPIN"
x = Array.create("x",shape=N,vartype=vartype)<jupyter_output><empty_output><jupyter_text>"x" is name of variables  shape specifies the shape of variables as vector, matrix, or...  vartype selects -1 or 1 by "SPIN" and 0 or 1by "BINARY"<jupyter_code>print(x)<jupyter_output>Array([Spin(x[0]), Spin(x[1]), Spin(x[2]), Spin(x[3]), Spin(x[4]), Spin(x[5])])<jupyter_text>Define cost function<jupyter_code>E1 = Constraint((np.sum(x))**2,"equal")
E2 = 0
for e in edges:
    E2 += 0.5*(1-x[e[0]]*x[e[1]])
Lam = Placeholder('Lam')
E_cost = Lam*E1+E2<jupyter_output><empty_output><jupyter_text>Compile the cost function<jupyter_code>model = E_cost.compile()<jupyter_output><empty_output><jupyter_text>Get qubo matrix<jupyter_code>feed_dict = {'Lam': 5.0}
h,J, offset = model.to_ising(feed_dict=feed_dict)<jupyter_output><empty_output><jupyter_text>Prepare simulation of quantum annealing<jupyter_code>#simulated quantum annealing
sampler = jij.SQASampler(beta=10.0, gamma=1.0, trotter=4, num_sweeps=100)
#simulated annealing
#sampler = jij.SASampler(num_sweeps=1000)<jupyter_output><empty_output><jupyter_text>This is done by quantum Monte-Carlo simulation  gamma = strength of quantum fluctuation  trotter = Trotter number  num_sweeps = length of MCS  Let's simulate quantum annealing<jupyter_code>response = sampler.sample_ising(h,J)<jupyter_output><empty_output><jupyter_text>Show results<jupyter_code>print(response)
response.record["sample"]<jupyter_output><empty_output><jupyter_text>show resulting graph<jupyter_code>spin = response.record["sample"][0]
node_colors = [spin[node]>0 for node in G.nodes()]

nx.draw_networkx(G,node_color=node_colors)
plt.axis("off")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12705635281764088, 'fraction_numerical': 0.021701085054252712, 'mean_word_length': 7.5825825825825826, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '431678', 'license': 'MIT', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1069, 'n_words': 228, 'path': 'notes/graph_partition.ipynb', 'repo_name': 'mohzeki222/quantum_annealing', 'size': '2857'}