How to use the openjij.SASampler function in openjij

To help you get started, we’ve selected a few openjij examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OpenJij / OpenJij / tests / test_sampler.py View on Github external
def test_sa_sweeps(self):
        iteration = 10
        sampler = oj.SASampler()
        res = sampler.sample_ising(self.h, self.J, num_reads=iteration)
        self.assertEqual(iteration, len(res.energies))

        sampler = oj.SASampler(num_reads=iteration)
        res = sampler.sample_ising(self.h, self.J)
        self.assertEqual(iteration, len(res.energies))
github OpenJij / OpenJij / tests / test_sampler.py View on Github external
def test_sa(self):
        initial_state = [1 for _ in range(self.size)]

        response = oj.SASampler().sample_ising(
            self.h, self.J, initial_state=initial_state, seed=1)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [-1, -1, -1])

        response = oj.SASampler(beta_max=100).sample_qubo(self.Q, seed=1)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [0, 0, 0])

        vaild_sche = [(beta, 1) for beta in np.linspace(-1, 1, 5)]
        with self.assertRaises(ValueError):
            sampler = oj.SASampler(schedule=vaild_sche)
            sampler.sample_ising({}, {})
github OpenJij / OpenJij / tests / test_sampler.py View on Github external
def test_sa_sweeps(self):
        iteration = 10
        sampler = oj.SASampler()
        res = sampler.sample_ising(self.h, self.J, num_reads=iteration)
        self.assertEqual(iteration, len(res.energies))

        sampler = oj.SASampler(num_reads=iteration)
        res = sampler.sample_ising(self.h, self.J)
        self.assertEqual(iteration, len(res.energies))
github OpenJij / OpenJij / tests / test_sampler.py View on Github external
def test_sa(self):
        initial_state = [1 for _ in range(self.size)]

        response = oj.SASampler().sample_ising(
            self.h, self.J, initial_state=initial_state, seed=1)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [-1, -1, -1])

        response = oj.SASampler(beta_max=100).sample_qubo(self.Q, seed=1)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [0, 0, 0])

        vaild_sche = [(beta, 1) for beta in np.linspace(-1, 1, 5)]
        with self.assertRaises(ValueError):
            sampler = oj.SASampler(schedule=vaild_sche)
            sampler.sample_ising({}, {})
github OpenJij / OpenJij / tests / test_sampler.py View on Github external
def test_reverse_annealing(self):
        seed_for_mc = 1
        initial_state = [0, 0, 0]
        qubo = {
            (0, 0): 1, (1, 1): -1, (2, 2): 2,
            (0, 1): 1, (1, 2): -1, (2, 0): -1
        }
        # solution is [0, 1, 0]
        solution = [0, 1, 0]

        # Reverse simulated annealing
        # beta, step_length
        reverse_schedule = [
            [10, 3], [1, 3], [0.5, 3], [1, 3], [10, 5]
        ]
        rsa_sampler = oj.SASampler(schedule=reverse_schedule, iteration=10)
        res = rsa_sampler.sample_qubo(
            qubo, initial_state=initial_state, seed=seed_for_mc)
        self.assertListEqual(
            solution,
            list(res.min_samples['states'][0])
        )

        # Reverse simulated quantum annealing
        # annealing parameter s, step_length
        reverse_schedule = [
            [1, 1], [0.3, 3], [0.1, 5], [0.3, 3], [1, 3]
        ]
        rqa_sampler = oj.SQASampler(schedule=reverse_schedule, iteration=10)
        res = rqa_sampler.sample_qubo(
            qubo, initial_state=initial_state, seed=seed_for_mc)
        self.assertListEqual(
github OpenJij / OpenJij / tests / test_sampler.py View on Github external
def test_swendsenwang(self):
        sampler = oj.SASampler()
        initial_state = [1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1]
        h = {0: -1, 10: -1}
        J = {(i, i+1): -1 for i in range(10)}
        res = sampler.sample_ising(h, J,
                                   updater="swendsenwang",
                                   seed=1, initial_state=initial_state)
        self.assertListEqual(res.states[0], [1]*11)
github OpenJij / OpenJij / tests / test_sampler.py View on Github external
def test_sa(self):
        initial_state = [1 for _ in range(self.size)]

        response = oj.SASampler().sample_ising(
            self.h, self.J, initial_state=initial_state, seed=1)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [-1, -1, -1])

        response = oj.SASampler(beta_max=100).sample_qubo(self.Q, seed=1)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [0, 0, 0])

        vaild_sche = [(beta, 1) for beta in np.linspace(-1, 1, 5)]
        with self.assertRaises(ValueError):
            sampler = oj.SASampler(schedule=vaild_sche)
            sampler.sample_ising({}, {})
github OpenJij / OpenJij / examples / python / optimizaiton.py View on Github external
import numpy as np
import openjij as oj


if __name__ == '__main__':
    h = {0: -1}
    J = {(0, 1): -1, (1, 2): -1}

    # Simulated annealing (classical) 10 times
    response = oj.SASampler(iteration=10).sample_ising(h, J)
    # show the lowest energy solution in ten times
    min_index = np.argmin(response.energies)
    print("SA results: ", response.states[min_index])
    # > SA results:  [1, 1, 1]


    # Simulated quantum annealing (quantum simulation) 10 times
    response = oj.SQASampler(iteration=10).sample_ising(h, J)
    # show the lowest energy solution in ten times
    min_index = np.argmin(response.energies)
    print("SQA results: ", response.states[min_index])
    # > SQA results:  [1, 1, 1]
github OpenJij / OpenJij / examples / python / benchmark.py View on Github external
import matplotlib.pyplot as plt
import openjij as oj


if __name__ == "__main__":

    # make target instance
    N = 10
    h = {0: 1, 1: 1}
    J = {}
    for i in range(N-1):
        for j in range(i+1, N):
            J[(i, j)] = -1.0
    
    true_ground_state = [-1]*N
    sa_samp = oj.SASampler()
    
    ground_energy = oj.BinaryQuadraticModel(h, J).calc_energy(true_ground_state)

    # make benchmark target solver
    def solver(time_param, iteration):
        sa_samp.step_num = time_param 
        sa_samp.iteration = iteration
        return sa_samp.sample_ising(h, J)

    # benchmarking
    b_res = oj.benchmark([true_ground_state], ground_energy, solver, time_param_list=np.arange(1, 161, 50))

    plt.xlabel('annealing time')
    plt.ylabel('error probability')
    plt.plot(b_res['time'], b_res['error'])
    plt.show()