|
import numpy as np |
|
import pickle |
|
import itertools |
|
|
|
# utilities |
|
from qutip import basis, tensor, mesolve, Options, qobj_list_evaluate, expect |
|
|
|
# operators |
|
from qm import Sx |
|
|
|
# Hamiltonians |
|
from hamiltonians import HMIS_ |
|
|
|
# solver for Minimum Vertex Cover we developed previously |
|
from solvers import solveAll |
|
|
|
# utilities |
|
from utils import osum, makePairs |
|
|
|
# load the instance |
|
data = None |
|
with open('instance.pickle', 'rb') as f: |
|
data = pickle.load(f) |
|
|
|
print('Kanto League map problem successfully unpickled!') |
|
G, V, E = data |
|
|
|
pairs = makePairs(V, E) |
|
N = len(V) |
|
|
|
# quantum adiabatic computation section |
|
|
|
tmax = 25. |
|
T = tmax*np.pi |
|
res = 300 |
|
|
|
Hi = osum([Sx(N, i) for i in range(N)]) |
|
Hf = HMIS_(N, pairs) |
|
|
|
H = [[Hi, (lambda t, args: 1. - t/T)], [Hf, (lambda t, args: t/T)]] |
|
|
|
|
|
def measureEnergy(t, psi): |
|
# evaluate time-dependent part |
|
H_t = qobj_list_evaluate(H, t, {}) |
|
return expect(H_t, psi) |
|
|
|
|
|
minus = (basis(2, 0) - basis(2, 1)).unit() |
|
psi0 = tensor([minus for i in range(N)]) |
|
|
|
times = np.linspace(0., T, res) |
|
opts = Options(store_final_state=True) |
|
result = mesolve(H, psi0, times, e_ops=measureEnergy, options=opts) |
|
|
|
psif = result.final_state |
|
measurements = result.expect |
|
|
|
np.save('energies.npy', measurements) |
|
|
|
|
|
def extractFidelities(N, Hf, ket, top): |
|
# produce all possible binary configurations of length N |
|
configurations = list(itertools.product([0, 1], repeat=N)) |
|
fidelities = np.zeros(2**N) |
|
energies = np.zeros(2**N) |
|
for i, configuration in enumerate(configurations): |
|
refket = tensor([basis(2, value) for value in configuration]) |
|
fidelity = np.abs(refket.overlap(ket))**2. |
|
fidelities[i] = fidelity |
|
energy = expect(Hf, refket) |
|
energies[i] = energy |
|
# get the indices highest fidelities |
|
best = (-fidelities).argsort()[:top] |
|
# return those configurations and corresponding fidelities and energies |
|
top_configurations = [configurations[c] for c in best] |
|
top_fidelities = [fidelities[c] for c in best] |
|
top_energies = [energies[c] for c in best] |
|
return top_configurations, top_fidelities, top_energies |
|
|
|
|
|
configurations, fidelities, energies = extractFidelities(N, Hf, psif, 10) |
|
|
|
print('Fidelities') |
|
print('{0:3} {1:20} {2:10} {3:10}'.format('#', '|psi>', 'Fidelity', 'Energy')) |
|
for i, (configuration, fidelity, energy) in enumerate(zip(configurations, fidelities, energies)): |
|
str_ket = '|' + ''.join([str(v) for v in configuration]) + '>' |
|
print('{0:3} {1:20} {2:10} {3:10}'.format(str(i), str_ket, str(np.round(fidelity, 5)), str(np.round(energy, 5)))) |
|
print() |
|
|
|
print('Top 3 fidelities add up to') |
|
print(np.round(np.sum(fidelities[0:3]), 5)) |
|
|
|
lst, min = solveAll([], 0, 9999, V, G) |
|
|
|
sol_kets = [] |
|
for sol in lst: |
|
# print(sol) |
|
sll = [1 for i in range(N)] |
|
for loc in sol: |
|
idx = V.index(loc) |
|
sll[idx] = 0 |
|
ket = tensor([basis(2, v) for v in sll]) |
|
sol_kets.append(ket) |
|
|
|
|
|
def measureEnergyFidelities(t, psi): |
|
# evaluate time-dependent part |
|
H_t = qobj_list_evaluate(H, t, {}) |
|
# get current energy of the system |
|
energy = expect(H_t, psi) |
|
# get fidelity of all the solutions |
|
fidelities = [] |
|
for ket in sol_kets: |
|
fidelity = np.abs(psi.overlap(ket))**2. |
|
fidelities.append(fidelity) |
|
return tuple([energy] + fidelities + [np.sum(fidelities)]) |
|
|
|
|
|
runs = 20 |
|
fidelities = np.zeros((5, runs, res)) |
|
for run, tmax in enumerate(np.linspace(1., 25., runs)): |
|
T = tmax*np.pi |
|
times = np.linspace(0., T, res) |
|
|
|
result = mesolve(H, psi0, times, e_ops=measureEnergyFidelities, options=opts) |
|
|
|
measurements = result.expect |
|
for i, measurement in enumerate(measurements): |
|
for j in range(5): |
|
# first one is energy, next 3 are individual fidelities and last is summed fidelities |
|
fidelities[j, run, i] = measurement[j] |
|
|
|
np.save('fidelities.npy', fidelities) |
Hi @Mocha1208
I had the same error as you. I changed the
osum
function as follows:def osum(lst): return sum(lst)
, then the mesolve works. Not sure if this works for you as well, but you can try.