Reverse Annealing (part-2)
2 min readOct 16, 2023
#quantum30
#day-16
#generating a random problem
import numpy as np
h = {v: 0.0 for v in sampler.nodelist}
J = {tuple(c): np.random.choice([-1, 1]) for c in sampler.edgelist}
print("Bias 0 assigned to", len(h), "qubits.")
print("Strengths -1/+1 assigned randomly to",len(J),"couplers.")
#output Bias 0 assigned to 5627 qubits.
#output Strengths -1/+1 assigned randomly to 40279 couplers.
#defining annealing schedule
from helpers.schedule import make_reverse_anneal_schedule # To see helper functions, select Jupyter File Explorer View from the Online Learning page
reverse_schedule = make_reverse_anneal_schedule(s_target=0.45, hold_time=80, ramp_up_slope=max_slope)
time_total = reverse_schedule[3][0]
print(reverse_schedule)
print("Total anneal-schedule time is {} us".format(time_total))
#output [[0.0, 1.0], [2.75, 0.45], [82.75, 0.45], [83.025, 1.0]]
#output Total anneal-schedule time is 83.025 us
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure(1, figsize=(3, 3))
plt.plot(*np.array(reverse_schedule).T)
plt.title("Reverse Anneal Schedule")
plt.xlabel("Time [us]")
plt.ylabel("Annealing Parameter s")
plt.ylim([0.0,1.0])
plt.show()#setting the initial states
forward_answer = sampler.sample_ising(h, J,
num_reads=1000,
annealing_time=time_total,
label='Notebook - Reverse Annealing',
answer_mode='histogram')
forward_solutions, forward_energies = forward_answer.record.sample, forward_answer.record.energy
i5 = int(5.0/95*len(forward_answer)) # Index i5 is about a 5% indexial move from the sample of lowest energy
initial = dict(zip(forward_answer.variables, forward_answer.record[i5].sample))
print("Lowest energy found: {}".format(forward_answer.record.energy[0]))
print("Average energy is {:.2f} with standard deviation {:.2f}".format(forward_energies.mean(), forward_energies.std()))
print("\nSetting the initial state to a sample with energy: {}".format(forward_answer.record.energy[i5]))
#output Lowest energy found: -15677.0
#output Average energy is -15639.09 with standard deviation 13.68
#output Setting the initial state to a sample with energy: -15659.0
#running reverse annealing
reverse_anneal_params = dict(anneal_schedule=reverse_schedule, initial_state=initial, reinitialize_state=False)
reverse_answer = sampler.sample_ising(h, J,
num_reads=1000,
label='Notebook - Reverse Annealing',
answer_mode='histogram',
**reverse_anneal_params)
reverse_solutions, reverse_energies = reverse_answer.record.sample, reverse_answer.record.energy
print("Lowest energy found:", reverse_answer.record.energy[0])
print("Average energy is {:.2f} with standard deviation {:.2f}".format(reverse_energies.mean(), reverse_energies.std()))
#output Lowest energy found: -15665.0
#output Average energy is -15658.42 with standard deviation 3.40Go through complete code and give some review .
Get Inderpal Suthar’s stories in your inbox
Join Medium for free to get updates from this writer.
Thanks for reading my article.
#quantum30
#day16



