Created
November 4, 2021 18:19
-
-
Save Roger-luo/9054748aa1c353913c3555430999deb7 to your computer and use it in GitHub Desktop.
use Maddie's code to find the ratio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qsim | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from qsim.evolution import hamiltonian | |
from qsim.graph_algorithms.graph import unit_disk_grid_graph, rydberg_graph | |
from qsim.graph_algorithms.adiabatic import SimulateAdiabatic | |
from qsim import schrodinger_equation | |
import matplotlib.gridspec as gridspec | |
import scipy.sparse | |
import scipy.optimize | |
import pandas as pd | |
import os | |
def find_ratio(tails_graph, graph, tf, graph_index=None, size=None): | |
cost = hamiltonian.HamiltonianMIS(graph, IS_subspace=True) | |
# print('Starting driver') | |
driver = hamiltonian.HamiltonianDriver(IS_subspace=True, graph=graph) | |
# print('Starting rydberg') | |
rydberg = hamiltonian.HamiltonianRydberg(tails_graph, graph, IS_subspace=True, energies=(2 * np.pi,)) | |
pulse = np.loadtxt(os.path.join('data', 'AWG', 'for_AWG_{}.000000.txt'.format(6))) | |
t_pulse_max = np.max(pulse[:, 0]) - 2 * 0.312 | |
def schedule(t, T): | |
# Linear ramp on the detuning, experiment-like ramp on the driver | |
k = 50 | |
a = .95 | |
b = 3.1 | |
x = t / T | |
amplitude = ( | |
-1 / (1 + np.e ** (k * (x - a))) ** b - 1 / (1 + np.e ** (-k * (x - (1 - a)))) ** b + 1) / \ | |
(-1 / ((1 + np.e ** (k * (1 / 2 - a))) ** b) - 1 / ( | |
(1 + np.e ** (-k * (1 / 2 - (1 - a)))) ** b) + 1) | |
cost.energies = (2 * np.pi * (-(11 + 15) / T * t + 15),) | |
driver.energies = (2 * np.pi * 2 * amplitude,) | |
def schedule_old(t, T): | |
# Linear ramp on the detuning, experiment-like ramp on the driver | |
k = 50 | |
a = .95 | |
b = 3.1 | |
x = t / T | |
amplitude = ( | |
-1 / (1 + np.e ** (k * (x - a))) ** b - 1 / (1 + np.e ** (-k * (x - (1 - a)))) ** b + 1) / \ | |
(-1 / ((1 + np.e ** (k * (1 / 2 - a))) ** b) - 1 / ( | |
(1 + np.e ** (-k * (1 / 2 - (1 - a)))) ** b) + 1) | |
cost.energies = (-2*np.pi*11*2*(1/2-t/T),)#(2 * np.pi * (-(11 + 15) / T * t + 15),) | |
driver.energies = (2*np.pi*2*amplitude,)#(2 * np.pi * 2 * amplitude,) | |
def schedule_exp_optimized(t, T): | |
if t < .312: | |
driver.energies = (2 * np.pi * 2 * t / .312,) | |
cost.energies = (2 * np.pi * 15,) | |
elif .312 <= t <= T - .331: | |
t_pulse = (t - 0.312) / (T - 2 * 0.312) * t_pulse_max + 0.312 | |
driver.energies = (2 * np.pi * np.interp(t_pulse, pulse[:, 0], pulse[:, 1] / 2),) | |
cost.energies = (2 * np.pi * np.interp(t_pulse, pulse[:, 0], -pulse[:, 2]),) | |
else: | |
driver.energies = (2 * np.pi * 2 * (T - t) / .312,) | |
cost.energies = (-2 * np.pi * 11,) | |
# print(t, cost.energies) | |
def schedule_exp_linear(t, T): | |
if t < .312: | |
driver.energies = (2 * np.pi * 2 * t / .312,) | |
cost.energies = (2 * np.pi * 15,) | |
elif .312 <= t <= T - .312: | |
driver.energies = (2 * np.pi * 2,) | |
cost.energies = (2 * np.pi * (-(11.5 + 15) / (T - 2 * .312) * (t - .312) + 15),) | |
else: | |
driver.energies = (2 * np.pi * 2 * (T - t) / .312,) | |
cost.energies = (-2 * np.pi * 11.5,) | |
# print(t, cost.energies) | |
# Uncomment this to print the schedule at t=0 | |
# schedule(0, 1) | |
# print(cost.hamiltonian*2*np.pi) | |
# print(driver.hamiltonian) | |
ad = SimulateAdiabatic(graph=graph, hamiltonian=[cost, driver, rydberg], cost_hamiltonian=cost, | |
IS_subspace=True) | |
# print('Starting evolution') | |
ars = [] | |
probs = [] | |
for i in range(len(tf)): | |
states, data = ad.run(tf[i], schedule_exp_linear, num=int(10 * tf[i]), method='odeint', full_output=False) | |
cost.energies = (1,) | |
ar = 1.0 - cost.approximation_ratio(states[-1]) | |
prob = cost.optimum_overlap(states[-1]) | |
np.savez_compressed('{}x{}_{}_{}.npz'.format(size, size, graph_index, i), state=states[-1]) | |
print(tf[i], ar, prob) | |
ars.append(ar) | |
probs.append(prob) | |
return ars, probs | |
import sys | |
n_points = 10 | |
# tf = [4.0 + 0.312 * 2 - 1e-3]# 2 ** np.linspace(-2.5, 4.5 / 6 * (n_points - 1) - 2.5, n_points) + .312 * 2 | |
tf = [0.7999999999999973, 0.9199999999999905, 1.1239999999999788, 1.4639999999999598, 2.037999999999927, 3.0009999999999977, 4.623000000000366] | |
index = 1 | |
# time_index = index % len(tf) | |
# index = index #/ len(tf) | |
size = 5 | |
size_indices = np.array([5, 6, 7, 8, 9, 10]) | |
size_index = np.argwhere(size == size_indices)[0, 0] | |
graph_index = 410 | |
graph_data = { | |
'graph_index': 410, | |
'MIS_size': 7, | |
'degeneracy': 2, | |
'side_length': 5, | |
'graph_mask': np.array([[ True, True, True, True, True], | |
[ True, True, True, False, True], | |
[False, True, True, True, False], | |
[ True, True, True, True, True], | |
[ True, False, False, True, True]]), | |
'number_of_nodes': 20 | |
} | |
grid = graph_data['graph_mask'] | |
# grid = np.ones((1, 10)) | |
graph = unit_disk_grid_graph(grid, periodic=False, visualize=False, generate_mixer=True) | |
tails_graph = rydberg_graph(grid, visualize=False) | |
ratio, probs = find_ratio(tails_graph, graph, tf, graph_index=graph_index, size=size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment