def test_spinbitconversion(): a = np.array([0., 1., 0., 1., 0., 1., 0., 1., 0., 1.]) b = tools.bits2spins(a) c = tools.spins2bits(b) assert (np.all(a == [0, 1, 0, 1, 0, 1, 0, 1, 0, 1])) assert (np.all(b == [1, -1, 1, -1, 1, -1, 1, -1, 1, -1])) assert (np.all(c == a))
def bitstr2spins(vec): """ Take a bitstring and return a spinvector. """ a = [int(k) for k in vec] return tools.bits2spins(a)
return reduce(lambda x, y: x + y, [str(int(k)) for k in tools.spins2bits(vec)]) # Construct Ising matrix memMat = sp.matrix(memories).T isingJ = sp.triu(memMat * sp.linalg.pinv(memMat)) isingJ -= sp.diag(sp.diag(isingJ)) isingJ = sp.triu(memMat * sp.linalg.pinv(memMat)) isingJ += inpbias * sp.diag(vinput) isingJ = sps.dok_matrix(isingJ) # get energies of all states results = [] energies = np.zeros(2**nspins) for b in [bin(x)[2:].rjust(nspins, '0') for x in range(2**nspins)]: svec = tools.bits2spins(np.array([int(k) for k in b])) energies[int(b, 2)] = sa.ClassicalIsingEnergy(svec, isingJ) results.append([energies[int(b, 2)], b]) print("All possible states and their energies:") for res in sorted(results)[:10]: print("%s %2.4f" % tuple(res[::-1])) # Initialize random state spinVector = np.array([2 * rng.randint(2) - 1 for k in range(nspins)], dtype=np.float) confs = np.tile(spinVector, (trotterslices, 1)).T # Generate list of nearest-neighbors for each spin neighbors = tools.GenerateNeighbors(nspins, isingJ, 8) # keep a count of the populations coinc_sa = np.zeros(2**nspins) coinc_qa = np.zeros(2**nspins)
def bitstr2spins(vec): """ Take a bitstring and return a spinvector. """ a = [ int(k) for k in vec ] return tools.bits2spins(a)
print ("Initial state energy: ", sorted([ sa.ClassicalIsingEnergy(v, isingJ) for v in svec ])) print '\n' # Generate list of nearest-neighbors for each spin neighbors = tools.GenerateNeighbors(nspins, isingJ, 4) # SA annealing schedule tannealingsched = np.linspace(preannealingtemp, annealingtemp, annealingsteps) # Normal annealing routine t0 = time.time() for vec in svec: sa.Anneal(tannealingsched, annealingmcsteps, vec, neighbors, rng) t1 = time.time() print ("Final SA energy: ", sorted([ sa.ClassicalIsingEnergy(v, isingJ) for v in svec ])) print "SA time (seconds): ", str(t1-t0) print '\n' # Now try multispin encoding t0 = time.time() sa.Anneal_multispin(tannealingsched, annealingmcsteps, msvec, neighbors, rng) t1 = time.time() print ("Final SA-multispin energies: ", sorted([ sa.ClassicalIsingEnergy(tools.bits2spins(v), isingJ) for v in msvec ])) print "SA-multispin time (seconds): ", str(t1-t0) print '\n'
svec = 2 * msvec - 1 print("Initial state energy: ", sorted([sa.ClassicalIsingEnergy(v, isingJ) for v in svec])) print '\n' # Generate list of nearest-neighbors for each spin neighbors = tools.GenerateNeighbors(nspins, isingJ, 4) # SA annealing schedule tannealingsched = np.linspace(preannealingtemp, annealingtemp, annealingsteps) # Normal annealing routine t0 = time.time() for vec in svec: sa.Anneal(tannealingsched, annealingmcsteps, vec, neighbors, rng) t1 = time.time() print("Final SA energy: ", sorted([sa.ClassicalIsingEnergy(v, isingJ) for v in svec])) print "SA time (seconds): ", str(t1 - t0) print '\n' # Now try multispin encoding t0 = time.time() sa.Anneal_multispin(tannealingsched, annealingmcsteps, msvec, neighbors, rng) t1 = time.time() print( "Final SA-multispin energies: ", sorted( [sa.ClassicalIsingEnergy(tools.bits2spins(v), isingJ) for v in msvec])) print "SA-multispin time (seconds): ", str(t1 - t0) print '\n'
def getbitstr(vec): """ Return bitstring from spin vector array. """ return reduce(lambda x,y: x+y, [ str(int(k)) for k in tools.spins2bits(vec) ]) # Construct Ising matrix memMat = sp.matrix(memories).T isingJ = sp.triu(memMat * sp.linalg.pinv(memMat)) isingJ -= sp.diag(sp.diag(isingJ)) isingJ = sp.triu(memMat * sp.linalg.pinv(memMat)) isingJ += inpbias*sp.diag(vinput) isingJ = sps.dok_matrix(isingJ) # get energies of all states results = [] energies = np.zeros(2**nspins) for b in [ bin(x)[2:].rjust(nspins, '0') for x in range(2**nspins) ]: svec = tools.bits2spins(np.array([ int(k) for k in b ])) energies[int(b,2)] = sa.ClassicalIsingEnergy(svec, isingJ) results.append([energies[int(b,2)], b]) print("All possible states and their energies:") for res in sorted(results)[:10]: print("%s %2.4f" % tuple(res[::-1])) # Initialize random state spinVector = np.array([ 2*rng.randint(2)-1 for k in range(nspins) ], dtype=np.float) confs = np.tile(spinVector, (trotterslices, 1)).T # Generate list of nearest-neighbors for each spin neighbors = tools.GenerateNeighbors(nspins, isingJ, 8) # keep a count of the populations coinc_sa = np.zeros(2**nspins) coinc_qa = np.zeros(2**nspins)