<jupyter_start><jupyter_text>Show embedding of a given problem into pegasus graph Problem statement:Given $A\in Z^{mxn}, b\in Z^{m}, c\in Z^{n}$ find $x\in F_2^n$ with:$\min c^T x $ with constraint $Ax\le b$<jupyter_code>var = 3
cstr = 3

multiple = 1
penalty = 1

lp_bin = RandomLP.create_random_binary_prog("test_" + str(0), cstr, var, multiple=multiple, penalty=penalty)
qp = lp_bin.qubo

lp_bin.complexity()<jupyter_output><empty_output><jupyter_text>Construct graph G for quadratic program as qubo<jupyter_code>bqm = dimod.AdjVectorBQM(qp.objective.linear.to_array(),
                         qp.objective.quadratic.to_dict(),
                         qp.objective.constant,
                         vartype=dimod.BINARY)

G = bqm.to_networkx_graph()
labels = {}
variables, constraints = [], []
for key, value in qp.variables_index.items():
    if 'slack' in key:
        label = ''
        constraints.append(value)
    else:
        label = key[1:]
        variables.append(value)
    labels[value] = label
    
var_edges, cstr_edges = G.subgraph(variables).edges, G.subgraph(constraints).edges
options = {"node_size": 600, "alpha": 0.9}
plt.figure(1, figsize=(12,12)) 

#pos = nx.circular_layout(G)
pos = nx.spring_layout(G, k=0.2, iterations=15, seed=1)
# k controls the distance between the nodes and varies between 0 and 1
    # iterations is the number of times simulated annealing is run
    # default k =0.1 and iterations=50
    
# draw each component individually ? 

nx.draw_networkx_nodes(G, pos, nodelist=variables, node_color="r", **options)
nx.draw_networkx_nodes(G, pos, nodelist=constraints, node_color="b", **options)

nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
nx.draw_networkx_edges(
    G,
    pos,
    edgelist=var_edges,
    width=3,
    alpha=0.5,
    edge_color="r",
)
nx.draw_networkx_edges(
    G,
    pos,
    edgelist=cstr_edges,
    width=2,
    alpha=0.5,
    edge_color="b",
)

nx.draw_networkx_labels(G, pos, labels, font_size=10)

plt.axis("off")
plt.show()<jupyter_output><empty_output><jupyter_text>Find embedding for qubo problem<jupyter_code>P_16 = dnx.pegasus_graph(16)
embedding = find_embedding(G, P_16, timeout=60, tries=50)
P_16_sub = P_16.subgraph(list(itertools.chain(*embedding.values())))
P_16_labels = {}
P_16_constraints = []
P_16_variables = []
P_16_chains = []
for key, value in embedding.items():
    for node in value:
        if key in constraints:
            P_16_constraints.append(node)
        else:
            P_16_variables.append(node)
        P_16_labels[node] = labels[key]
    if len(value) > 1:
        P_16_chains.append(value)

P_16_var_edges, P_16_cstr_edges = P_16_sub.subgraph(P_16_variables).edges, P_16_sub.subgraph(P_16_constraints).edges
options = {"node_size": 300, "alpha": 0.9}
plt.figure(2, figsize=(12,12)) 

pos = dnx.pegasus_layout(P_16_sub)


nx.draw_networkx_nodes(P_16_sub, pos, nodelist=P_16_variables, node_color="r", **options)
nx.draw_networkx_nodes(P_16_sub, pos, nodelist=P_16_constraints, node_color="b", **options)

nx.draw_networkx_edges(P_16_sub, pos, width=1.0, alpha=0.5)
nx.draw_networkx_edges(
    P_16_sub,
    pos,
    edgelist=P_16_var_edges,
    width=3,
    alpha=0.5,
    edge_color="r",
)
nx.draw_networkx_edges(
    P_16_sub,
    pos,
    edgelist=P_16_cstr_edges,
    width=2,
    alpha=0.5,
    edge_color="b",
)

nx.draw_networkx_edges(
    P_16_sub,
    pos,
    edgelist=P_16_chains,
    width=4,
    alpha=0.5,
    edge_color="g",
)
nx.draw_networkx_labels(P_16_sub, pos, P_16_labels, font_size=10)

plt.axis("off")
plt.show()
structured_sampler = dimod.StructureComposite(DWaveSampler(), P_16.nodes, P_16.edges)
sampler = FixedEmbeddingComposite(structured_sampler, embedding=embedding)
res = sampler.sample(bqm, label="embedding_of_random_lp", num_reads=100)
res.to_pandas_dataframe()
solution = res.first.sample

solution
qp.get_feasibility_info(solution.values())
from qiskit.optimization.algorithms import CplexOptimizer
cplex = CplexOptimizer()
cplex.solve(qp)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12866368604073522, 'fraction_numerical': 0.03551912568306011, 'mean_word_length': 4.312664907651715, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '239578', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1547, 'n_words': 276, 'path': 'comparison/Ocean/Embedding/QUBO_Binary_LP_Sparse.ipynb', 'repo_name': 'tomtuamnuq/compare-qiskit-ocean', 'size': '4026'}