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}')
The optimal value that was possible was -5.0
Assignments for our nodes are
0 -> 0
1 -> 0
2 -> 0
3 -> 1
4 -> 1
eigvals = np.real(np.diagonal(annealer.target_hamil.data.todense()))
for idx, eigval in enumerate(eigvals):
print(f"State {idx:05b} -> {eigval}")
State 00000 -> 7.0
State 00001 -> 1.0
State 00010 -> 1.0
State 00011 -> -5.0
State 00100 -> 1.0
State 00101 -> -1.0
State 00110 -> -1.0
State 00111 -> -3.0
State 01000 -> 1.0
State 01001 -> -1.0
State 01010 -> -1.0
State 01011 -> -3.0
State 01100 -> -1.0
State 01101 -> 1.0
State 01110 -> 1.0
State 01111 -> 3.0
State 10000 -> 3.0
State 10001 -> 1.0
State 10010 -> 1.0
State 10011 -> -1.0
State 10100 -> -3.0
State 10101 -> -1.0
State 10110 -> -1.0
State 10111 -> 1.0
State 11000 -> -3.0
State 11001 -> -1.0
State 11010 -> -1.0
State 11011 -> 1.0
State 11100 -> -5.0
State 11101 -> 1.0
State 11110 -> 1.0
State 11111 -> 7.0