Пример #1
0
    def test_seed(self):
        sampler = Neal()
        num_vars = 40
        h = {v: -1 for v in range(num_vars)}
        J = {(u, v): -1 for u in range(num_vars) for v in range(u, num_vars) if u != v}
        num_reads = 1000

        # test seed exceptions
        for bad_seed in (3.5, float("inf"), "string", [], {}):
            self.assertRaises(TypeError, sampler.sample_ising, {}, {}, seed=bad_seed)
        for bad_seed in (-1, -100, 2**65):
            self.assertRaises(ValueError, sampler.sample_ising, {}, {}, seed=bad_seed)

        # make sure it can accept large seeds
        sampler.sample_ising(h, J, seed=2**63, num_reads=1, num_sweeps=1)

        # no need to do a bunch of sweeps, in fact the less we do the more
        # sure we can be that the same seed is returning the same result
        all_samples = []

        for seed in (1, 25, 2352, 736145, 5682453, 923759283623):
            response0 = sampler.sample_ising(h, J, num_reads=num_reads, num_sweeps=10, seed=seed)
            response1 = sampler.sample_ising(h, J, num_reads=num_reads, num_sweeps=10, seed=seed)

            samples0 = response0.record.sample
            samples1 = response1.record.sample

            self.assertTrue(np.array_equal(samples0, samples1), "Same seed returned different results")

            for previous_sample in all_samples:
                self.assertFalse(np.array_equal(samples0, previous_sample), "Different seed returned same results")

            all_samples.append(samples0)
Пример #2
0
    def test_initial_states(self):
        sampler = Neal()
        var_labels = ["a", "b", "c", "d"]
        num_vars = len(var_labels)
        h = {v: -1 for v in var_labels}
        J = {(u, v): 1 for u, v in itertools.combinations(var_labels, 2)}
        num_reads = 100
        seed = 1234567890

        np_rand = np.random.RandomState(seed)
        initial_state_array = 2 * np_rand.randint(
            2, size=(num_reads, num_vars)) - 1
        init_labels = dict(zip(var_labels, np_rand.permutation(num_vars)))

        resp = sampler.sample_ising(h,
                                    J,
                                    num_reads=num_reads,
                                    sweeps=0,
                                    seed=seed,
                                    initial_states=(initial_state_array,
                                                    init_labels))

        for v in var_labels:
            self.assertTrue(
                np.array_equal(resp.record.sample[:,
                                                  resp.variables.index(v)],
                               initial_state_array[:, init_labels[v]]),
                "Samples were not the same as initial states with "
                "no sweeps")
Пример #3
0
    def test_basic_response(self):
        sampler = Neal()
        h = {'a': 0, 'b': -1}
        J = {('a', 'b'): -1}
        response = sampler.sample_ising(h, J)

        self.assertIsInstance(response, dimod.SampleSet, "Sampler returned an unexpected response type")
Пример #4
0
    def test_cubic_lattice_with_geometric(self):
        # Set up all lattice edges in a cube. Each edge is labelled by a 3-D coordinate system
        def get_cubic_lattice_edges(N):
            for x, y, z in itertools.product(range(N), repeat=3):
                u = x, y, z
                yield u, ((x + 1) % N, y, z)
                yield u, (x, (y + 1) % N, z)
                yield u, (x, y, (z + 1) % N)

        # Add a J-bias to each edge
        np_rand = np.random.RandomState(128)
        J = {e: np_rand.choice((-1, 1)) for e in get_cubic_lattice_edges(12)}

        # Solve ising problem
        sampler = Neal()
        response = sampler.sample_ising({},
                                        J,
                                        beta_schedule_type="geometric",
                                        num_reads=10)
        _, response_energy, _ = next(response.data())

        # Note: lowest energy found was -3088 with a different benchmarking tool
        threshold = -3000
        self.assertLess(response_energy, threshold,
                        ("response_energy, {}, exceeds "
                         "threshold").format(response_energy))
Пример #5
0
    def test_geometric_schedule(self):
        sampler = Neal()
        num_vars = 40
        h = {v: -1 for v in range(num_vars)}
        J = {(u, v): -1 for u in range(num_vars) for v in range(u, num_vars) if u != v}
        num_reads = 10

        resp = sampler.sample_ising(h, J, num_reads=num_reads, beta_schedule_type='geometric')

        row, col = resp.record.sample.shape

        self.assertEqual(row, num_reads)
        self.assertEqual(col, num_vars)  # should get back two variables
        self.assertIs(resp.vartype, dimod.SPIN)  # should be ising

        with self.assertRaises(ValueError):
            sampler.sample_ising(h, J, num_reads=num_reads, beta_schedule_type='asd')
Пример #6
0
    def test_num_reads(self):
        sampler = Neal()

        h = {}
        J = {('a', 'b'): .5, (0, 'a'): -1, (1, 'b'): 0.0}

        for num_reads in (1, 10, 100, 3223, 10352):
            response = sampler.sample_ising(h, J, num_reads=num_reads)
            row, col = response.record.sample.shape

            self.assertEqual(row, num_reads)
            self.assertEqual(col, 4)

        for bad_num_reads in (0, -1, -100):
            with self.assertRaises(ValueError):
                sampler.sample_ising(h, J, num_reads=bad_num_reads)

        for bad_num_reads in (3.5, float("inf"), "string", [], {}):
            with self.assertRaises(TypeError):
                sampler.sample_ising(h, J, num_reads=bad_num_reads)
Пример #7
0
    def test_empty_problem(self):
        sampler = Neal()
        h = {'a': 0, 'b': -1}
        J = {('a', 'b'): -1}
        eh, eJ = {}, {}

        for h in (h, eh):
            for J in (J, eJ):
                _h = copy.deepcopy(h)
                _J = copy.deepcopy(J)
                r = sampler.sample_ising(_h, _J)
Пример #8
0
    def test_interrupt_error(self):
        sampler = Neal()
        num_vars = 40
        h = {v: -1 for v in range(num_vars)}
        J = {(u, v): -1 for u in range(num_vars) for v in range(u, num_vars) if u != v}
        num_reads = 100

        def f():
            raise NotImplementedError

        resp = sampler.sample_ising(h, J, num_reads=num_reads, interrupt_function=f)

        self.assertEqual(len(resp), 1)
Пример #9
0
    def test_disconnected_problem(self):
        sampler = Neal()
        h = {}
        J = {
            # K_3
            (0, 1): -1,
            (1, 2): -1,
            (0, 2): -1,

            # disonnected K_3
            (3, 4): -1,
            (4, 5): -1,
            (3, 5): -1,
        }

        resp = sampler.sample_ising(h, J, sweeps=1000, num_reads=100)

        row, col = resp.record.sample.shape

        self.assertEqual(row, 100)
        self.assertEqual(col, 6)  # should get back two variables
        self.assertIs(resp.vartype, dimod.SPIN)  # should be ising