Exemplo n.º 1
0
def test_generateneighbors():
    J = sps.dok_matrix((5, 5), dtype=np.float64)
    #
    #  We're going to build this graph:
    #
    #   0 --- 1
    #   |   / |
    #   |  4  |
    #   | /   |
    #   3 --- 2
    #
    J[0, 1] = 1
    J[1, 2] = 1
    J[2, 3] = 1
    J[3, 4] = 1
    J[0, 3] = 1
    J[1, 4] = 1
    J[0, 4] = 1
    J[0, 0] = 1
    J[1, 1] = 1
    J[2, 2] = 1
    J[3, 3] = 1
    J[4, 4] = 1
    true_nb = np.array([[[1., 1.], [0., 1.], [4., 1.], [3., 1.]],
                        [[0., 1.], [2., 1.], [4., 1.], [1., 1.]],
                        [[1., 1.], [2., 1.], [3., 1.], [0., 0.]],
                        [[3., 1.], [2., 1.], [0., 1.], [4., 1.]],
                        [[4., 1.], [1., 1.], [0., 1.], [3., 1.]]])
    generated_nb = tools.GenerateNeighbors(nspins=5,
                                           J=J,
                                           maxnb=4,
                                           savepath=None)
    assert (np.all(true_nb == generated_nb))
Exemplo n.º 2
0
 def setUp(self):
     # Define some parameters
     self.nspins = 8
     self.preannealingtemp = 1.0
     self.annealingtemp = 0.01
     self.annealingsteps = 10
     self.annealingmcsteps = 3
     self.trotterslices = 5
     self.fieldstart = 0.5
     self.fieldend = 1e-8
     # Random number generator
     self.rng = np.random.RandomState(123)
     # Construct Ising matrix
     kvp = [
         [1, 2, 1.0],
         [1, 4, 1.0],
         [1, 5, 1.0],
         [2, 3, 1.0],
         [2, 6, 1.0],
         [3, 4, 1.0],
         [3, 7, 1.0],
         [4, 8, 1.0],
         [1, 1, 1.0],
         [2, 2, 1.0],
         [3, 3, 1.0],
         [4, 4, 1.0],
         [5, 5, -1.0],
         [6, 6, -1.0],
         [7, 7, -1.0],
         [8, 8, -1.0]
         ]
     self.isingJ = sps.dok_matrix((self.nspins,self.nspins))
     for i,j,val in kvp:
         self.isingJ[i-1,j-1] = val
     # get the neighbors data structure (tested elsewhere)
     self.nbs = tools.GenerateNeighbors(self.nspins,
                                        self.isingJ,
                                        4, None)
Exemplo n.º 3
0
for b in [bin(x)[2:].rjust(nspins, '0') for x in range(2**nspins)]:
    bvec = np.array([int(k) for k in b])
    svec = bitstr2spins(b)
    bstr = reduce(lambda x, y: x + y, [str(k) for k in bvec])
    results.append([sa.ClassicalIsingEnergy(svec, isingJ), bstr])
for res in sorted(results):
    print res

# Initialize random state
spinVector = np.array([2 * rng.randint(2) - 1 for k in range(nspins)],
                      dtype=np.float)
spinVector_original = spinVector.copy()
configurations = np.tile(spinVector, (trotterslices, 1)).T
# Generate list of nearest-neighbors for each spin
neighbors = tools.GenerateNeighbors(nspins, isingJ, 4)
# Generate annealing schedules
tannealingsched = np.linspace(preannealingtemp, annealingtemp, annealingsteps)
annealingsched = np.linspace(fieldstart, fieldend, annealingsteps)
# Try using SA (deterministic start)
print("SA results using same starting state:")
print("Starting state: ", sa.ClassicalIsingEnergy(spinVector, isingJ),
      getbitstr(spinVector))
for sa_itr in range(trotterslices):
    spinVector = spinVector_original.copy()
    sa.Anneal(tannealingsched, preannealingmcsteps, spinVector, neighbors, rng)
    print(sa.ClassicalIsingEnergy(spinVector, isingJ), getbitstr(spinVector))
# Try using SA (random start)
print("SA results using random state (start and end):")
for sa_itr in range(trotterslices):
    spinVector = np.array([2 * rng.randint(2) - 1 for k in range(nspins)],
Exemplo n.º 4
0
fieldstart = 1.5
fieldend = 1e-8
seed = None
trotterslices = 5
# Random number generator
rng = np.random.RandomState(seed)

# Read from textfile directly to be sure
inputfname = 'ising_instances/santoro_80x80.txt'
loaded = np.loadtxt(inputfname)
isingJ = sps.dok_matrix((nspins, nspins))
for i, j, val in loaded:
    isingJ[i - 1, j - 1] = val

# Get list of nearest-neighbors for each spin
neighbors = tools.GenerateNeighbors(
    nspins, isingJ, 4, 'ising_instances/santoro_80x80_neighbors.npy')
# neighbors = np.load('ising_instances/santoro_80x80_neighbors.npy')

# initialize the state
spinVector = np.array([2 * rng.randint(2) - 1 for k in range(nspins)],
                      dtype=np.float)
configurations = np.tile(spinVector, (trotterslices, 1)).T
# Try just using SA
tannealingsched = np.linspace(preannealingtemp, annealingtemp, annealingsteps)
cProfile.runctx(
    "sa.Anneal(tannealingsched, annealingmcsteps, "
    "spinVector, neighbors, rng)", globals(), locals(), "sa.prof")
s = pstats.Stats("sa.prof")
s.strip_dirs().sort_stats("time").print_stats()
# Now do PIQA
annealingsched = np.linspace(fieldstart, fieldend, annealingsteps)
Exemplo n.º 5
0
def bitstr2spins(vec):
    """ Take a bitstring and return a spinvector. """
    a = [int(k) for k in vec]
    return tools.bits2spins(a)


for b in [bin(x)[2:].rjust(nspins, '0') for x in range(2**nspins)]:
    bvec = np.array([int(k) for k in b])
    svec = bitstr2spins(b)
    bstr = reduce(lambda x, y: x + y, [str(k) for k in bvec])
    results.append([sa.ClassicalIsingEnergy(svec, isingJ), bstr])
for res in sorted(results):  #[:100]:
    print res
print("\n")
# Generate list of nearest-neighbors for each spin
neighbors = np.asarray(tools.GenerateNeighbors(nspins, isingJ, 5))
# Generate annealing schedules
tannealingsched = np.linspace(preannealingtemp, annealingtemp, annealingsteps)
# Generate random states to compare
svecs = np.asarray([[2 * rng.randint(2) - 1 for k in range(nspins)]
                    for j in xrange(samples)],
                   dtype=np.float)
# Try using SA (random start)
for sa_itr in xrange(samples):
    # spinVector = np.array([ 2*rng.randint(2)-1 for k in range(nspins) ],
    #                       dtype=np.float)
    spinVector = svecs[sa_itr].copy()
    starten, startstate = (sa.ClassicalIsingEnergy(spinVector, isingJ),
                           getbitstr(spinVector))
    sa.Anneal(tannealingsched, annealingmcsteps, spinVector, neighbors, rng)
    bitstr = getbitstr(spinVector)