Skip to content

Instantly share code, notes, and snippets.

@marekyggdrasil
Last active October 2, 2018 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marekyggdrasil/58cf64279d0c913ccddc08e6a2267b99 to your computer and use it in GitHub Desktop.
Save marekyggdrasil/58cf64279d0c913ccddc08e6a2267b99 to your computer and use it in GitHub Desktop.
Running AQC (Adiabatic Quantum Computation) on spin chain of size N=8 and N=2 for debugging. Printing probabilities of each sping configuration for N=8 in the middle of time evolution and for N=2 at the end of time evolution (for debugging).
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from qutip import *
from scipy import *
def operators(N) :
si = qeye(2)
sx = sigmax()
sy = sigmay()
sz = sigmaz()
sx_list = []
sy_list = []
sz_list = []
for n in range(N):
op_list = []
for m in range(N):
op_list.append(si)
op_list[n] = sx
sx_list.append(tensor(op_list))
op_list[n] = sy
sy_list.append(tensor(op_list))
op_list[n] = sz
sz_list.append(tensor(op_list))
psi_list = [basis(2,0) for n in range(N)]
psi0 = tensor(psi_list)
return sx_list, sy_list, sz_list, psi_list, psi0
def solve(H0, H1, N, M, psi0, taulist, taumax) :
args = {'t_max': max(taulist)}
h_t = [[H0, lambda t, args : (args['t_max']-t)/args['t_max']],
[H1, lambda t, args : t/args['t_max']]]
evals_mat = np.zeros((len(taulist),M))
P_mat = np.zeros((len(taulist),M))
idx = [0]
def process_rho(tau, psi):
# evaluate the Hamiltonian with gradually switched on interaction
H = qobj_list_evaluate(h_t, tau, args)
# find the M lowest eigenvalues of the system
evals, ekets = H.eigenstates(eigvals=M)
evals_mat[idx[0],:] = real(evals)
# find the overlap between the eigenstates and psi
for n, eket in enumerate(ekets):
P_mat[idx[0],n] = abs((eket.dag().data * psi.data)[0,0])**2
idx[0] += 1
mesolve(h_t, psi0, taulist, [], process_rho, args)
return evals_mat, P_mat
def plot(evals_mat, taulist, taumax, P_mat, N, M) :
fig, axes = plt.subplots(2, 1, figsize=(12,10))
#
# plot the energy eigenvalues
#
# first draw thin lines outlining the energy spectrum
for n in range(len(evals_mat[0,:])):
ls,lw = ('b',1) if n == 0 else ('k', 0.25)
axes[0].plot(taulist/max(taulist), evals_mat[:,n] / (2*pi), ls, lw=lw)
# second, draw line that encode the occupation probability of each state in
# its linewidth. thicker line => high occupation probability.
for idx in range(len(taulist)-1):
for n in range(len(P_mat[0,:])):
lw = 0.5 + 4*P_mat[idx,n]
if lw > 0.55:
axes[0].plot(array([taulist[idx], taulist[idx+1]])/taumax,
array([evals_mat[idx,n], evals_mat[idx+1,n]])/(2*pi),
'r', linewidth=lw)
axes[0].set_xlabel(r'$\tau$')
axes[0].set_ylabel('Eigenenergies')
axes[0].set_title("Energyspectrum (%d lowest values) of a chain of %d spins.\n " % (M,N)
+ "The occupation probabilities are encoded in the red line widths.")
#
# plot the occupation probabilities for the few lowest eigenstates
#
for n in range(len(P_mat[0,:])):
if n == 0:
axes[1].plot(taulist/max(taulist), 0 + P_mat[:,n], 'r', linewidth=2)
else:
axes[1].plot(taulist/max(taulist), 0 + P_mat[:,n])
axes[1].set_xlabel(r'$\tau$')
axes[1].set_ylabel('Occupation probability')
axes[1].set_title("Occupation probability of the %d lowest " % M +
"eigenstates for a chain of %d spins" % N)
axes[1].legend(("Ground state",));
plt.show()
def printProbabilities(taulist, P_mat, idx, N) :
print 'propabilities of each spin configuration at t=%s\n' % str(idx)
s = 0.
for n in range(len(P_mat[0,:])):
lw = P_mat[idx,n]
s += lw
print "{0:{fill}{n}b} - ".format(n, fill='0', n=N), lw
print '\nsums up to %s' % str(s)
propabilities of each spin configuration at t=99
00 - 0.9994490956271472
01 - 0.00027541278731277574
10 - 0.00027541278731277574
11 - 7.879822735150804e-08
sums up to 1.0000000000000002
propabilities of each spin configuration at t=49
00000000 - 0.003906462465320872
00000001 - 1.1045557664927843e-19
00000010 - 1.4295080344534068e-08
00000011 - 4.6609294371895095e-08
00000100 - 1.5235258405150954e-11
00000101 - 4.656877716123509e-12
00000110 - 0.030708506395049507
00000111 - 0.0005412100498141933
00001000 - 1.342594983171164e-22
00001001 - 4.678192967812127e-30
00001010 - 1.2764704097068673e-22
00001011 - 5.510153279366488e-32
00001100 - 6.929612509977237e-26
00001101 - 1.1719517873133457e-09
00001110 - 4.0586899484759796e-15
00001111 - 2.913078729784403e-06
00010000 - 0.00047421700085367713
00010001 - 3.248182166940278e-23
00010010 - 2.6018597717822735e-15
00010011 - 6.383654903002076e-32
00010100 - 2.449060747697179e-22
00010101 - 1.5096682619922724e-23
00010110 - 0.008366309633835983
00010111 - 0.08546814413276474
00011000 - 3.2096649905275904e-30
00011001 - 3.7834574117600816e-24
00011010 - 7.377651044640432e-17
00011011 - 3.2722052173373266e-06
00011100 - 0.015057606677999292
00011101 - 4.21049607408217e-31
00011110 - 7.219862988427224e-16
00011111 - 1.993021637108723e-31
00100000 - 3.9235404907935154e-07
00100001 - 8.933449994355897e-13
00100010 - 2.4440438725386756e-22
00100011 - 3.3278782897094846e-14
00100100 - 1.5892618210972276e-15
00100101 - 9.750194830764778e-19
00100110 - 1.9170443825893702e-32
00100111 - 6.485484746004345e-32
00101000 - 3.723262759845694e-13
00101001 - 5.496255409656716e-32
00101010 - 1.2940417806083644e-32
00101011 - 1.937219392132533e-32
00101100 - 2.866224332530895e-31
00101101 - 1.0362150311674495e-15
00101110 - 3.071158475874862e-26
00101111 - 1.0798222762020825e-31
00110000 - 4.666015327937644e-28
00110001 - 7.584019775810523e-25
00110010 - 2.5554578776510706e-16
00110011 - 1.4005596842689667e-11
00110100 - 1.9691317127686566e-32
00110101 - 7.305620249179311e-10
00110110 - 0.0005418899514782906
00110111 - 5.359903352107817e-25
00111000 - 1.7812902645962147e-13
00111001 - 5.4181075516041506e-17
00111010 - 1.0796035322529038e-16
00111011 - 0.00047636900377845317
00111100 - 2.7084060216474284e-05
00111101 - 1.5786080338718004e-07
00111110 - 3.718562918272181e-17
00111111 - 2.8452132787873956e-05
01000000 - 0.001433776683087685
01000001 - 3.3083836164310145e-26
01000010 - 1.0305833631165545e-07
01000011 - 5.772406735486936e-18
01000100 - 0.0007783560638988486
01000101 - 1.212361335365758e-22
01000110 - 9.89895551771929e-09
01000111 - 2.644187286831022e-12
01001000 - 1.819477141712068e-13
01001001 - 1.1008560271861586e-11
01001010 - 0.034080677746419555
01001011 - 1.3160990064107862e-11
01001100 - 2.2144116821181034e-10
01001101 - 8.558314425635619e-26
01001110 - 2.080535663384786e-32
01001111 - 4.66555646350809e-10
01010000 - 0.022664784162521493
01010001 - 1.9367177344185657e-22
01010010 - 9.026458268142445e-13
01010011 - 2.2630717764470422e-11
01010100 - 1.7489851307466586e-32
01010101 - 0.008307905118284074
01010110 - 0.15040803516842272
01010111 - 1.6012031428701363e-14
01011000 - 1.6317215758905498e-31
01011001 - 8.665520657357805e-27
01011010 - 7.387476532347223e-33
01011011 - 8.127109332828114e-32
01011100 - 1.2067342886897408e-32
01011101 - 1.8722068816763566e-32
01011110 - 5.6860542540235356e-33
01011111 - 5.009994539023816e-32
01100000 - 4.537042312281863e-25
01100001 - 4.2363053162109e-33
01100010 - 4.491798313996533e-11
01100011 - 6.920788714416906e-24
01100100 - 4.751815909919558e-29
01100101 - 2.4498487046179763e-25
01100110 - 1.0658912575516172e-33
01100111 - 1.034143506124609e-32
01101000 - 2.5582566851083996e-31
01101001 - 1.1703713999451865e-21
01101010 - 2.0471573850526149e-22
01101011 - 9.240325992943837e-32
01101100 - 4.915254974999177e-12
01101101 - 6.05332874046635e-09
01101110 - 2.5847021728692966e-06
01101111 - 2.601887734273869e-19
01110000 - 2.3678683233786993e-06
01110001 - 1.3371818046913952e-27
01110010 - 0.005321933122407188
01110011 - 2.375752658270128e-13
01110100 - 1.3815273324513494e-08
01110101 - 1.538425433962114e-29
01110110 - 8.158463441377404e-23
01110111 - 1.5425238148306824e-28
01111000 - 6.813245990317991e-30
01111001 - 2.9587497351565745e-23
01111010 - 0.0006324253702772711
01111011 - 0.04661754405304573
01111100 - 0.0004284207359789199
01111101 - 5.1876449863126266e-27
01111110 - 0.0035707923678424476
01111111 - 2.0045507786810107e-09
10000000 - 4.393649307272989e-21
10000001 - 1.4805327838719697e-31
10000010 - 0.02859365636760421
10000011 - 1.219201560304031e-07
10000100 - 0.02882815206911526
10000101 - 3.85617368369541e-17
10000110 - 0.0006473157480817158
10000111 - 2.0543546672671867e-16
10001000 - 1.6423400648771556e-13
10001001 - 1.1408632107059559e-17
10001010 - 1.2969280562227035e-07
10001011 - 6.99246091353701e-05
10001100 - 0.07149696852475254
10001101 - 2.6034140872341597e-15
10001110 - 6.489163082610122e-18
10001111 - 2.3167472544352636e-16
10010000 - 1.9195330922305632e-24
10010001 - 2.391673628153468e-16
10010010 - 3.6189363586682664e-25
10010011 - 0.08650893443805473
10010100 - 9.406982880026019e-32
10010101 - 0.0007139910442780133
10010110 - 4.629791818645832e-29
10010111 - 3.0043863973452523e-13
10011000 - 2.8304211434061656e-06
10011001 - 8.510579099953362e-32
10011010 - 1.0017142758838365e-12
10011011 - 6.852774715011286e-32
10011100 - 9.605148734488975e-17
10011101 - 5.446770623970151e-34
10011110 - 1.5668900601087792e-24
10011111 - 8.603737027256028e-33
10100000 - 7.211187644554405e-33
10100001 - 8.468825540612154e-33
10100010 - 1.3383181859253068e-31
10100011 - 4.4323777739280584e-33
10100100 - 1.9513793078687333e-19
10100101 - 1.1942740485710242e-31
10100110 - 1.4954098403759763e-31
10100111 - 1.1600029383033344e-14
10101000 - 1.8458242256846013e-12
10101001 - 1.5848532055300442e-32
10101010 - 4.794750493822471e-24
10101011 - 3.9713195021767875e-34
10101100 - 1.3190244783056374e-12
10101101 - 7.794888268566871e-11
10101110 - 1.0016545557471463e-11
10101111 - 1.7695018108094462e-32
10110000 - 4.1269003254490172e-19
10110001 - 1.8943991617241293e-09
10110010 - 1.966196397096475e-09
10110011 - 1.5591757069306822e-34
10110100 - 2.1951262469103093e-09
10110101 - 1.2199668285969471e-32
10110110 - 0.022709102823521683
10110111 - 0.0017093278334326288
10111000 - 0.026442552353328232
10111001 - 0.0036905669301959534
10111010 - 1.5212906509441506e-19
10111011 - 1.5858605891774823e-12
10111100 - 0.0005002105974949734
10111101 - 7.671791548666362e-15
10111110 - 1.8871933324456426e-10
10111111 - 0.0004583118549683788
11000000 - 0.00044784549705660024
11000001 - 3.413337355283073e-14
11000010 - 6.266501085368213e-09
11000011 - 0.10943168046428982
11000100 - 4.4148328903799014e-07
11000101 - 0.0001221679520230828
11000110 - 6.85136900514097e-26
11000111 - 9.72453446531925e-07
11001000 - 8.408894371070423e-26
11001001 - 0.04317033908257296
11001010 - 0.0001228592504257116
11001011 - 5.90198980957429e-16
11001100 - 0.007124383926931651
11001101 - 6.39645666466671e-28
11001110 - 4.222948145784682e-26
11001111 - 2.487636227097162e-16
11010000 - 1.9585859705057416e-19
11010001 - 0.0027973347279851724
11010010 - 3.5393711725292775e-32
11010011 - 2.4316281731024935e-05
11010100 - 1.0666640686181611e-27
11010101 - 1.6581785923814883e-17
11010110 - 2.3786491777238042e-26
11010111 - 1.7589224778698656e-30
11011000 - 6.926622038530165e-17
11011001 - 9.641307644713357e-27
11011010 - 4.0531797532737e-32
11011011 - 2.873813324059804e-31
11011100 - 4.346177168193797e-15
11011101 - 1.9908898108432438e-20
11011110 - 1.7109106396050214e-18
11011111 - 7.4076315599512e-21
11100000 - 0.00495346920392532
11100001 - 1.4598431245078052e-09
11100010 - 9.40503759901495e-18
11100011 - 0.027763742961467046
11100100 - 7.319695394307264e-17
11100101 - 0.018637625652596163
11100110 - 4.9010798465479096e-14
11100111 - 2.916629236492447e-17
11101000 - 0.0001565246523697966
11101001 - 2.151591166185074e-06
11101010 - 3.7342699414438066e-07
11101011 - 0.016995495837825753
11101100 - 1.1487181915591664e-07
11101101 - 0.004778126877400488
11101110 - 0.001909558727537797
11101111 - 0.025446297207534607
11110000 - 0.008723941004699816
11110001 - 2.0331157781350488e-26
11110010 - 8.889970609699356e-06
11110011 - 2.330272726419925e-12
11110100 - 8.296564190338841e-32
11110101 - 4.973187459910869e-33
11110110 - 1.0789027996753311e-11
11110111 - 2.3651266024214562e-12
11111000 - 0.0003940709182304036
11111001 - 0.00012387355797407678
11111010 - 2.285524160791754e-06
11111011 - 0.0002834484368186283
11111100 - 2.6379109939493125e-11
11111101 - 0.03044651575201581
11111110 - 5.302360935102528e-15
11111111 - 0.003906252743384673
sums up to 1.000000000000011
import numpy as np
from aqc import operators, solve, printProbabilities, plot
N = 2 # number of spins
M = 4 # number of eigenenergies to plot
# increase taumax to get make the sweep more adiabatic
taumax = 5.0
taulist = np.linspace(0, taumax, 100)
sx, sy, sz, psi_list, psi0 = operators(N)
H0 = 0
for n in range(N):
H0 += - 0.5 * 2.5 * sx[n]
H1 = 0
#interactions = [(0, 4), (0, 5), (1, 4), (1, 6), (2, 5), (2, 7), (3, 6), (3, 7)]
interactions = [(0, 1)]
for j1, j2 in interactions :
H1 += -2. * sz[j2] * sz[j2]
evals_mat, P_mat = solve(H0, H1, N, M, psi0, taulist, taumax)
printProbabilities(taulist, P_mat, len(taulist)-1, N)
plot(evals_mat, taulist, taumax, P_mat, N, M)
import numpy as np
from aqc import operators, solve, printProbabilities, plot
N = 8 # number of spins
M = 256 # number of eigenenergies to plot
# increase taumax to get make the sweep more adiabatic
taumax = 5.0
taulist = np.linspace(0, taumax, 100)
sx, sy, sz, psi_list, psi0 = operators(N)
H0 = 0
for n in range(N):
H0 += - 0.5 * 2.5 * sx[n]
H1 = 0
interactions = [(0, 4), (0, 5), (1, 4), (1, 6), (2, 5), (2, 7), (3, 6), (3, 7)]
for j1, j2 in interactions :
H1 += -2. * sz[j2] * sz[j2]
evals_mat, P_mat = solve(H0, H1, N, M, psi0, taulist, taumax)
printProbabilities(taulist, P_mat, (len(taulist)/2)-1, N)
plot(evals_mat, taulist, taumax, P_mat, N, M)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment