Esempio n. 1
0
    def test_sqa(self):
        response = oj.SQASampler().sample_ising(self.h, self.J)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [-1, -1, -1])

        response = oj.SQASampler().sample_qubo(self.Q)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [0, 0, 0])
Esempio n. 2
0
    def test_time_sqa(self):
        fast_res = oj.SQASampler(
            num_sweeps=10, iteration=10).sample_ising(self.h, self.J, seed=1)
        slow_res = oj.SQASampler(
            num_sweeps=100, iteration=10).sample_ising(self.h, self.J, seed=1)

        self.assertEqual(len(fast_res.info['list_exec_times']), 10)
        self.assertTrue(fast_res.info['execution_time']
                        < slow_res.info['execution_time'])
Esempio n. 3
0
    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(solution, list(res.min_samples['states'][0]))
Esempio n. 4
0
def main():
    conf = load_config(default_conf)
    model = KeymapModel(conf)

    text_path = Path("text/alice.txt")

    weight = {
        "w_1hot": 2200,
        "w_key_unique": 2100,
    }
    annealing_params = {
        "beta": 0.02,
        "gamma": 100,
        "num_sweeps": 100000,
        "trotter": 4,
        "num_reads": 100,
    }

    logger = Logger(Path("result"), model)

    print("building model")
    with open(text_path, "r") as f:
        text = f.readline()
        while text:
            model.update_weight(text)
            text = f.readline()

    qubo = model.qubo(**weight)
    print("annealing")
    sampler = oj.SQASampler(**annealing_params)
    result = sampler.sample_qubo(qubo)

    states = result.states
    logger.log(weight, annealing_params, states)
Esempio n. 5
0
    def test_sqa(self):
        response = oj.SQASampler().sample_ising(self.h, self.J)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [-1, -1, -1])
        self.assertEqual(response.energies[0], -18)

        response = oj.SQASampler().sample_qubo(self.Q)
        self.assertEqual(len(response.states), 1)
        self.assertListEqual(response.states[0], [0, 0, 0])

        schedule = [(s, 10) for s in np.arange(0, 1, 5)] + [(0.99, 100)]
        response = oj.SQASampler(schedule=schedule).sample_qubo(self.Q)
        self.assertListEqual(response.states[0], [0, 0, 0])

        vaild_sche = [(s, 10) for s in np.linspace(0, 1, 5)]
        with self.assertRaises(ValueError):
            sampler = oj.SQASampler(schedule=vaild_sche)
Esempio n. 6
0
    def test_sqa(self):
        sampler = oj.SQASampler()

        self.samplers(sampler)
        self.samplers(sampler,
                      init_state=[1 for _ in range(len(self.ground_state))],
                      init_q_state=[1 for _ in range(len(self.ground_state))])
        self.samplers(sampler,
                      init_state={i: 1
                                  for i in range(len(self.ground_state))})

        # schedule [[s, one_mc_steps], ...]
        # schedule test (list of list, temperature fixed)
        self.samplers(sampler,
                      init_state={i: 1
                                  for i in range(len(self.ground_state))},
                      schedule=[[0.1, 10], [0.5, 10], [0.9, 10]])

        # schedule test (list of tuple, temperature fixed)
        self.samplers(sampler,
                      init_state={i: 1
                                  for i in range(len(self.ground_state))},
                      schedule=[(0.1, 10), (0.5, 10), (0.9, 10)])

        # schedule [[s, beta, one_mc_steps], ...]
        # schedule test (list of list, temperature non-fixed)
        self.samplers(sampler,
                      init_state={i: 1
                                  for i in range(len(self.ground_state))},
                      schedule=[[0.1, 0.1, 10], [0.5, 1, 10], [0.9, 10, 10]])

        # schedule test (list of tuple, temperature non-fixed)
        self.samplers(sampler,
                      init_state={i: 1
                                  for i in range(len(self.ground_state))},
                      schedule=[(0.1, 0.1, 10), (0.5, 1, 10), (0.9, 10, 10)])

        self._test_num_reads(oj.SQASampler)

        #antiferromagnetic one-dimensional Ising model
        sampler = oj.SQASampler(num_reads=100)
        res = sampler.sample_ising(self.afih, self.afiJ, seed=1)
        self.assertDictEqual(self.afiground, res.first.sample)
Esempio n. 7
0
    def test_sqa_response(self):
        iteration = 10
        trotter = 4
        sampler = oj.SQASampler(iteration=iteration, trotter=trotter)
        response = sampler.sample_ising(h=self.h, J=self.J)

        self.assertEqual(len(response.states), iteration)
        self.assertEqual(len(response.q_states), iteration)
        self.assertEqual(len(response.q_states[0]), trotter)
        self.assertTrue(
            isinstance(response.q_states[0][0][0], (int, np.int, np.int64)))
Esempio n. 8
0
def reverse_annealing():
    initial_state = [0, 0, 0]
    qubo = make_qubo()
    reverse_schedule = [[10, 3], (1, 3), (0.5, 3), (1, 3), (10, 5)]
    sqa = oj.SASampler(schedule=reverse_schedule, iteration=20)
    res = sqa.sample_qubo(qubo, initial_state=initial_state)
    print(res.min_samples)
    model = oj.BinaryQuadraticModel(Q=qubo, vartype='BINARY')
    print(model.calc_energy(res.min_samples['min_states'][0]))

    print('RQA')
    reverse_schedule = [[1, 1], [0.3, 3], [0.1, 5], [0.3, 3], [1, 3]]
    rqa_sampler = oj.SQASampler(schedule=reverse_schedule,
                                iteration=10,
                                beta=10)
    rqa_sampler = SQASampler(schedule=reverse_schedule, iteration=10)
    res = rqa_sampler.sample_qubo(qubo, initial_state=initial_state, seed=1)
    print(res.min_samples)
Esempio n. 9
0
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]
Esempio n. 10
0
                                    J,
                                    num_sweeps=step,
                                    num_reads=NUM_READS,
                                    beta_max=BETA_MAX,
                                    beta_min=BETA_MIN,
                                    sparse=True)
    elapsed_time = time.time() - start

    openjij_sparse_time.append(elapsed_time)
    openjij_sparse_energy.append(np.mean(response.energies))

    print("\telapsed_time:{0}".format(elapsed_time) + "[sec]")

# Benchmark OpenJij SQA
print('OpenJij SQA Dense')
sampler = oj.SQASampler(beta=10, trotter=8)
openjij_sql_time = []
openjij_sql_energy = []
for step in steps_openjij:
    print('num_sweeps:', step)
    start = time.time()
    response = sampler.sample_ising(h, J, num_sweeps=step, num_reads=NUM_READS)
    elapsed_time = time.time() - start

    openjij_sql_time.append(elapsed_time)
    openjij_sql_energy.append(np.mean(response.energies))

    print("\telapsed_time:{0}".format(elapsed_time) + "[sec]")

print('OpenJij SQA Sparse')
sampler = oj.SQASampler(beta=10, trotter=8)
Esempio n. 11
0
File: test.py Progetto: y-yu/OpenJij
 def test_sqa(self):
     response = oj.SQASampler().sample_ising(self.h, self.J)
     self.assertEqual(len(response.states), 1)
     self.assertListEqual(response.states[0], [1, 1, 1])
Esempio n. 12
0
 def test_sqa_with_negative_interactions(self):
     # sa with negative interactions
     sampler = oj.SQASampler()
     sampler.sample_ising({}, {(0, 1): -1})
     sampler.sample_ising({2: -1}, {(0, 1): -1})