Beispiel #1
0
    def test_qubo_energy(self):
        N0, N1 = 8, 5
        m = 1

        an = self.anpkg.bipartite_graph_annealer(dtype=self.dtype)
        b0, b1, W = bipartite_graph_random(N0, N1, self.dtype)
        an.set_qubo(b0, b1, W)
        an.set_preferences(n_trotters=m)
        an.prepare()

        iMax = 1 << N0
        jMax = 1 << N1

        for i in range(iMax):
            x0 = common.create_bitset_sequence((i, ), N0)
            q0 = sq.bit_to_spin(x0)
            for j in range(jMax):
                x1 = common.create_bitset_sequence((j, ), N1)
                q1 = sq.bit_to_spin(x1)
                Ebf = np.dot(b0, x0.transpose()) + np.dot(b1, x1.transpose()) \
                      + np.dot(x1, np.matmul(W, x0.transpose()))
                an.set_q((q0, q1))
                an.calculate_E()
                Ean = an.get_E()
                if not np.allclose(Ebf, Ean, atol=self.epu):
                    print(i, j, Ebf, Ean)
                self.assertTrue(np.allclose(Ebf, Ean, atol=self.epu))
Beispiel #2
0
    def test_qubo_energy(self):
        N0 = 8
        N1 = 5
        an = py.bipartite_graph_annealer()
        W = np.random.random((N1, N0)) - 0.5
        b0 = np.random.random((N0)) - 0.5
        b1 = np.random.random((N1)) - 0.5
        an.set_qubo(b0, b1, W)
        an.set_preferences(n_trotters=1)
        an.prepare()

        iMax = 1 << N0
        jMax = 1 << N1

        for i in range(iMax):
            x0 = common.create_bitset_sequence((i, ), N0)
            for j in range(jMax):
                x1 = common.create_bitset_sequence((j, ), N1)
                Ebf = np.dot(b0, x0.transpose()) + np.dot(b1, x1.transpose()) \
                      + np.dot(x1, np.matmul(W, x0.transpose()))
                an.set_x(x0, x1)
                an.calculate_E()
                Ean = an.get_E()
                if not np.allclose(Ebf, Ean):
                    print(i, j, Ebf, Ean)
                self.assertTrue(np.allclose(Ebf, Ean))
 def create_sequence(self, N0, N1):
     N = N0 + N1
     xlist = common.create_bitset_sequence(range(0, 2**N), N)
     x0, x1 = np.empty((2**N, N0), np.int8), np.empty((2**N, N1), np.int8)
     for idx in range(2**N):
         x0[idx][...] = xlist[idx][0:N0]
         x1[idx][...] = xlist[idx][N0:]
     return x0, x1
 def test_compare_engery_batched(self):
     N = 8
     W = self.new_W(N)
     xlist = common.create_bitset_sequence(range(0, 2**N), N)
     Ebatched = sq.py.formulas.dense_graph_batch_calculate_E(
         W, xlist, self.dtype)
     E = self.pkg.formulas.dense_graph_batch_calculate_E(
         W, xlist, self.dtype)
     self.assertTrue(np.allclose(Ebatched, E))
 def test_energy_batch_1(self):
     N = 8
     W = self.new_W(N)
     xlist = common.create_bitset_sequence(range(0, 2**N), N)
     Ebatched = self.pkg.formulas.dense_graph_batch_calculate_E(
         W, xlist, self.dtype)
     E = np.empty((2**N))
     for i in range(0, 2**N):
         E[i] = self.pkg.formulas.dense_graph_batch_calculate_E(
             W, xlist[i], self.dtype)[0]
     self.assertTrue(np.allclose(Ebatched, E, atol=self.epu))
Beispiel #6
0
    def test_engery_of_batched_qubo_energy_func(self):
        N = 8
        W = common.generate_random_symmetric_W((N), dtype=np.float64)
        xlist = common.create_bitset_sequence(range(0, 2**N), N)

        E = []
        for i in range(0, 1 << N):
            E.append(formulas.dense_graph_calculate_E(W, xlist[i]))

        Ebatch = formulas.dense_graph_batch_calculate_E(W, xlist)

        self.assertTrue(np.allclose(E, Ebatch))
Beispiel #7
0
    def test_engery_of_batched_spin_energy_func(self):
        N = 8
        W = common.generate_random_symmetric_W((N), dtype=np.float64)
        xlist = common.create_bitset_sequence(range(0, 2**N), N)
        qlist = common.bit_to_spin(xlist)
        h, J, c = formulas.dense_graph_calculate_hamiltonian(W)

        E = []
        for i in range(0, 1 << N):
            E.append(
                formulas.dense_graph_calculate_E_from_spin(h, J, c, qlist[i]))

        Ebatch = formulas.dense_graph_batch_calculate_E_from_spin(
            h, J, c, qlist)

        self.assertTrue(np.allclose(E, Ebatch))
    def test_hamiltonian_energy_batched(self):
        N = 8
        W = common.generate_random_symmetric_W((N), dtype=np.float64)
        h, J, c = self.pkg.formulas.dense_graph_calculate_hamiltonian(
            W, self.dtype)

        # identity
        self.assertTrue(
            np.allclose(0., -np.sum(h) + np.sum(J) + c, atol=self.epu))

        xlist = common.create_bitset_sequence(range(0, 2**N), N)
        Equbo = self.pkg.formulas.dense_graph_batch_calculate_E(
            W, xlist, self.dtype)
        qlist = 2 * xlist - 1
        Eising = sq.py.formulas.dense_graph_batch_calculate_E_from_spin(
            h, J, c, qlist, self.dtype)
        self.assertTrue(np.allclose(Equbo, Eising, atol=self.epu))
    def test_compare_hamiltonian_energy(self):
        N = 8
        W = common.generate_random_symmetric_W((N), dtype=np.float64)
        h0, J0, c0 = sq.py.formulas.dense_graph_calculate_hamiltonian(
            W, self.dtype)
        h1, J1, c1 = self.pkg.formulas.dense_graph_calculate_hamiltonian(
            W, self.dtype)

        # identity
        self.assertTrue(
            np.allclose(0., -np.sum(h1) + np.sum(J1) + c1, atol=self.epu))

        xlist = common.create_bitset_sequence(range(0, 2**N), N)
        Equbo = sq.py.formulas.dense_graph_batch_calculate_E(
            W, xlist, self.dtype)
        Eising = np.empty((2**N))
        for idx in range(0, 2**N):
            x = xlist[idx]
            q = 2 * x - 1
            Eising[idx] = self.pkg.formulas.dense_graph_calculate_E_from_spin(
                h1, J1, c1, q, self.dtype)
        self.assertTrue(np.allclose(Equbo, Eising, atol=self.epu))
    def test_qubo_energy(self):
        N = 8
        an = self.new_annealer(N, 1)
        W = dense_graph_random(N, self.dtype)
        an.set_qubo(W)
        an.set_preferences(n_trotters=1)
        an.prepare()

        nMax = 1 << N
        res = True
        for i in range(nMax):
            x = common.create_bitset_sequence((i, ), N)
            Eref = sq.py.formulas.dense_graph_calculate_E(W, x)

            q = sq.bit_to_spin(x)
            an.set_q(q)
            an.calculate_E()
            Ean = an.get_E()
            if not np.allclose(Eref, Ean, atol=self.epu):
                print(Eref, Ean, Eref - Ean)
            res &= np.allclose(Eref, Ean, atol=self.epu)

        self.assertTrue(res)
Beispiel #11
0
 def test_engery_of_dense_graph(self):
     N = 8
     W = common.generate_random_symmetric_W((N), dtype=np.float64)
     xlist = common.create_bitset_sequence(range(0, 2**N), N)
     self.compare_energy(W, xlist)
Beispiel #12
0
 def test_engery_of_known_W(self):
     # self.verbose = True
     N = 8
     W = np.ones((N, N), dtype=np.float64)
     xlist = common.create_bitset_sequence(range(0, 2**N), N)
     self.compare_energy(W, xlist)