import networkx as nx
from qanneal import annealers, problems, schedulers
maxcutproblem = problems.MaxCutNXProblem(num_qubits=5, p=0.5, seed=42)
graph = maxcutproblem.g
poses = nx.spring_layout(maxcutproblem.g, seed=42)
nx.draw(graph, poses, with_labels=True)
qubo_problem = maxcutproblem.generate_qubo()
print(qubo_problem)
scheduler = schedulers.LinearScheduler(total_time=5, num_timesteps=1000)
annealer = annealers.QutipStateVectorAnnealer(scheduler, qubo_problem, num_qubits=len(graph.nodes))
annealer.anneal()
results = annealer.res
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
sns.set_theme()
figure = plt.figure(figsize=(12, 8))
plt.plot(results.times, np.real(annealer.expect))
plt.ylabel("The (negative) maximum cut (expectation) value")
plt.xlabel("Time")
plt.title("By the end of 5 seconds, we have successfully achieved a maximum cut of 5, which is optimal")
plt.show()
print(f"The optimal value that was possible was {annealer.get_optimal()}")
print("Assignments for our nodes are")
assignment = f'{results.states[-1].data.argmax():0{len(graph.nodes)}b}'
for idx, char in enumerate(assignment):
print(f'{idx} -> {char}')
eigvals = np.real(np.diagonal(annealer.target_hamil.data.todense()))
for idx, eigval in enumerate(eigvals):
print(f"State {idx:05b} -> {eigval}")