예제 #1
0
    def test_ignore_labels(self, name, BQM, version):
        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'SPIN')

        labeled_bqm = bqm.relabel_variables(dict(enumerate('abcde')),
                                            inplace=False)

        if version < (2, 0):
            with self.assertRaises(ValueError):
                FileView(labeled_bqm, version=version, ignore_labels=True)
            return

        with FileView(labeled_bqm, version=version, ignore_labels=True) as fv:
            new = load(fv)

        self.assertEqual(new, bqm)
    def test_readinto_partial_sliding17(self, name, BQM):

        bqm = BQM(np.tril(np.arange(25).reshape((5, 5))), 'BINARY')
        bqm.offset = -6

        fv = FileView(bqm)

        buff = fv.readall()

        for pos in range(fv.quadratic_end):
            self.assertEqual(pos, fv.seek(pos))
            subbuff = bytearray(17)
            num_read = fv.readinto(subbuff)
            self.assertGreater(num_read, 0)
            self.assertEqual(subbuff[:num_read], buff[pos:pos + num_read])
    def test_readinto_partial_front_to_back(self, name, BQM):

        bqm = BQM(np.triu(np.arange(9).reshape((3, 3))), 'BINARY')
        bqm.offset = 14

        fv = FileView(bqm)

        buff = fv.readall()

        for nb in range(fv.quadratic_end):
            self.assertEqual(fv.seek(0), 0)
            subbuff = bytearray(nb)
            num_read = fv.readinto(subbuff)
            self.assertEqual(num_read, nb)
            self.assertEqual(subbuff[:num_read], buff[:num_read])
예제 #4
0
    def test_unhashable_variables(self, name, BQM, version):
        bqm = BQM({(0, 1): 1}, {}, 'SPIN')

        with FileView(bqm, version=version) as fv:
            new = load(fv)

        self.assertEqual(new, bqm)
예제 #5
0
    def test_seek_end(self, name, BQM, version):
        bqm = BQM.from_qubo({'ab': 1, 'aa': 2})

        with FileView(bqm, version=version) as fv:
            buff = fv.readall()

            self.assertEqual(len(buff), fv.seek(0, io.SEEK_END))
예제 #6
0
    def test_empty_bytes(self, name, BQM, version):
        bqm = BQM('SPIN')
        with FileView(bqm, version=version) as fp:
            new = load(fp.readall())

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
예제 #7
0
    def test_typical(self, name, BQM, version):
        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'SPIN')
        bqm.offset = -1

        with FileView(bqm, version=version) as fp:
            new = load(fp)

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
예제 #8
0
    def test_labelled_bytes(self, name, BQM, version):

        bqm = BQM({'a': -1}, {'ab': 1}, 7, 'SPIN')

        with FileView(bqm, version=version) as fp:
            new = load(fp.readall())

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
    def test_functional_labelled(self, name, BQM):

        bqm = BQM({'a': -1}, {'ab': 1}, 7, 'SPIN')

        with FileView(bqm) as fp:
            new = load(fp)

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
    def test_readinto_partial_sliding17(self, BQM):
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        bqm = BQM(np.tril(np.arange(25).reshape((5, 5))), 'BINARY')
        bqm.offset = -6

        fv = FileView(bqm)

        buff = fv.readall()

        for pos in range(fv.quadratic_end):
            self.assertEqual(pos, fv.seek(pos))
            subbuff = bytearray(17)
            num_read = fv.readinto(subbuff)
            self.assertGreater(num_read, 0)
            self.assertEqual(subbuff[:num_read], buff[pos:pos+num_read])
    def test_functional_empty(self, name, BQM):

        bqm = BQM('SPIN')

        with FileView(bqm) as fp:
            new = load(fp)

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
    def test_readinto_partial_front_to_back(self, BQM):
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        bqm = BQM(np.triu(np.arange(9).reshape((3, 3))), 'BINARY')
        bqm.offset = 14

        fv = FileView(bqm)

        buff = fv.readall()

        for nb in range(fv.quadratic_end):
            self.assertEqual(fv.seek(0), 0)
            subbuff = bytearray(nb)
            num_read = fv.readinto(subbuff)
            self.assertEqual(num_read, nb)
            self.assertEqual(subbuff[:num_read], buff[:num_read])
예제 #13
0
    def test_empty_bytes(self, name, BQM, version):
        bqm = BQM('SPIN')
        with self.assertWarns(DeprecationWarning):
            with FileView(bqm, version=version) as fp:
                new = load(fp.read())

        self.assertEqual(bqm, new)
        if bqm.dtype != np.dtype('O'):
            self.assertEqual(bqm.dtype, new.dtype)
    def test_readall_dense(self, BQM):
        # construct the bytes by hand
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        arr = np.triu(np.arange(4).reshape((2, 2)))

        bqm = BQM(arr, 'BINARY')
        bqm.offset = 17

        fv = FileView(bqm)

        b = fv.readall()

        # offset
        offset_bytes = bqm.dtype.type(17).tobytes()

        self.assertEqual(b[fv.offset_start:fv.offset_end], offset_bytes)

        # linear
        ltype = np.dtype([('n', bqm.ntype), ('b', bqm.dtype)], align=False)
        ldata = np.empty(2, dtype=ltype)
        ldata['n'][0] = 0
        ldata['n'][1] = 1
        ldata['b'][0] = 0
        ldata['b'][1] = 3
        linear_bytes = ldata.tobytes()

        self.assertEqual(b[fv.linear_start:fv.linear_end], linear_bytes)

        # quadratic
        qtype = np.dtype([('v', bqm.itype), ('b', bqm.dtype)], align=False)
        qdata = np.empty(2, dtype=qtype)
        qdata['v'][0] = 1
        qdata['v'][1] = 0
        qdata['b'][0] = 1
        qdata['b'][1] = 1
        quadratic_bytes = qdata.tobytes()

        self.assertEqual(b[fv.quadratic_start:fv.quadratic_end], quadratic_bytes)

        # and finally check everything
        self.assertEqual(fv.header + offset_bytes + linear_bytes + quadratic_bytes, b)
    def _sample(self, bqm, **kwargs):
        """Sample from the given BQM."""
        # get a FileView-compatibile BQM
        bqm = dimod.as_bqm(
            bqm, cls=[dimod.AdjArrayBQM, dimod.AdjMapBQM, dimod.AdjVectorBQM])

        with FileView(bqm, version=2) as fv:
            sapi_problem_id = self.solver.upload_bqm(fv).result()

        return self.solver.sample_bqm(sapi_problem_id, **kwargs).sampleset
예제 #16
0
    def test_labelled_bytes(self, name, BQM, version):
        bqm = BQM({'a': -1}, {'ab': 1}, 7, 'SPIN')

        with self.assertWarns(DeprecationWarning):
            with FileView(bqm, version=version) as fp:
                new = load(fp.read())

        if bqm.dtype != np.dtype('O'):
            self.assertEqual(bqm.dtype, new.dtype)
        self.assertEqual(bqm, new)
예제 #17
0
    def test_typical(self, name, BQM, version):
        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'SPIN')
        bqm.offset = -1

        with self.assertWarns(DeprecationWarning):
            with FileView(bqm, version=version) as fp:
                new = load(fp)

        if bqm.dtype != np.dtype('O'):
            self.assertEqual(bqm.dtype, new.dtype)
        self.assertEqual(bqm, new)
    def test_unhashable_variables(self, BQM):
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        bqm = BQM({(0, 1): 1}, {}, 'SPIN')

        with FileView(bqm) as fv:
            new = load(fv)

        self.assertEqual(new, bqm)
예제 #19
0
    def test_dense_v1(self, name, BQM):
        arr = np.triu(np.arange(4).reshape((2, 2)))

        bqm = BQM(arr, 'BINARY')
        bqm.offset = 17

        fv = FileView(bqm, version=1)

        b = fv.readall()

        # offset
        offset_bytes = bqm.dtype.type(17).tobytes()

        self.assertEqual(b[fv.offset_start:fv.offset_end], offset_bytes)

        # linear
        ltype = np.dtype([('n', bqm.ntype), ('b', bqm.dtype)], align=False)
        ldata = np.empty(2, dtype=ltype)
        ldata['n'][0] = 0
        ldata['n'][1] = 1
        ldata['b'][0] = 0
        ldata['b'][1] = 3
        linear_bytes = ldata.tobytes()

        self.assertEqual(b[fv.linear_start:fv.linear_end], linear_bytes)

        # quadratic
        qtype = np.dtype([('v', bqm.itype), ('b', bqm.dtype)], align=False)
        qdata = np.empty(2, dtype=qtype)
        qdata['v'][0] = 1
        qdata['v'][1] = 0
        qdata['b'][0] = 1
        qdata['b'][1] = 1
        quadratic_bytes = qdata.tobytes()

        self.assertEqual(b[fv.quadratic_start:fv.quadratic_end],
                         quadratic_bytes)

        # and finally check everything
        self.assertEqual(
            fv.header + offset_bytes + linear_bytes + quadratic_bytes, b)
예제 #20
0
    def test_labelled_shapeable_bytes(self, name, BQM, version):
        if not BQM.shapeable():
            raise unittest.SkipTest("test only applies to shapeable bqms")

        bqm = BQM({'a': -1}, {'ab': 1}, 7, 'SPIN')
        bqm.add_variable()

        with FileView(bqm, version=version) as fp:
            new = load(fp.readall())

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
    def test_functional_empty(self, BQM):
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        bqm = BQM('SPIN')

        with FileView(bqm) as fp:
            new = load(fp)

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
    def test_functional(self, BQM):
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'SPIN')
        bqm.offset = -1

        with FileView(bqm) as fp:
            new = load(fp)

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
예제 #23
0
    def test_ignore_labels(self, name, BQM, version):
        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'SPIN')

        labeled_bqm = bqm.relabel_variables(dict(enumerate('abcde')),
                                            inplace=False)

        with self.assertWarns(DeprecationWarning):
            with FileView(labeled_bqm, version=version,
                          ignore_labels=True) as fv:
                new = load(fv)

        self.assertEqual(new, bqm)
        if bqm.dtype != np.dtype('O'):
            self.assertEqual(bqm.dtype, new.dtype)
    def test_functional_labelled_shapeable(self, BQM):
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        if not BQM.shapeable():
            raise unittest.SkipTest("test only applies to shapeable bqms")

        bqm = BQM({'a': -1}, {'ab': 1}, 7, 'SPIN')
        bqm.add_variable()

        with FileView(bqm) as fp:
            new = load(fp)

        self.assertIs(type(new), type(bqm))
        self.assertEqual(bqm, new)
    def _sample_large(self, bqm, **kwargs):
        """Sample from the unlabelled version of the BQM, then apply the
        labels to the returned sampleset.
        """
        # get a FileView-compatibile BQM
        # it is also important that the BQM be ordered
        bqm = dimod.as_bqm(
            bqm, cls=[dimod.AdjArrayBQM, dimod.AdjMapBQM, dimod.AdjVectorBQM])

        with FileView(bqm, version=2, ignore_labels=True) as fv:
            sapi_problem_id = self.solver.upload_bqm(fv).result()

        sampleset = self.solver.sample_bqm(sapi_problem_id, **kwargs).sampleset

        # relabel, as of dimod 0.9.5+ this is not blocking
        mapping = dict(enumerate(bqm.iter_variables()))
        return sampleset.relabel_variables(mapping)
예제 #26
0
    def test_saved_adjvector_5x5_v1(self):
        bqm = dimod.AdjVectorBQM(np.triu(np.arange(25).reshape((5, 5))),
                                 'BINARY')

        filename = os.path.join(TEST_DATA_DIR, '5x5_v1.bqm')

        # with open(filename, 'wb') as fp:
        #     with FileView(bqm, version=1) as fv:
        #         fp.write(fv.readall())

        with open(filename, 'rb') as fp:
            buff = fp.read()

        # test that we still encode the same way
        with FileView(bqm, version=1) as fv:
            self.assertEqual(buff, fv.read())

        # and that loading gives the same
        new = load(buff)
        self.assertEqual(new, bqm)
    def test_readinto_partial_back_to_front(self, name, BQM):

        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'BINARY')
        bqm.offset = 14

        fv = FileView(bqm)

        buff = fv.readall()

        for pos in range(1, fv.quadratic_end):
            fv.seek(-pos, io.SEEK_END)

            subbuff = bytearray(pos)  # length pos

            self.assertEqual(fv.readinto(subbuff), len(subbuff))
            self.assertEqual(buff[-pos:], subbuff)
    def test_readinto(self, name, BQM):

        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'BINARY')
        bqm.offset = 14

        fv = FileView(bqm)

        num_bytes = fv.quadratic_end

        # make a buffer that's a bit bigger
        buff = bytearray(num_bytes + 10)

        self.assertEqual(fv.readinto(buff), num_bytes)

        # reset and make sure that a total read it the same
        fv.seek(0)
        self.assertEqual(fv.read(), buff[:num_bytes])
    def test_readinto_partial_back_to_front(self, BQM):
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'BINARY')
        bqm.offset = 14

        fv = FileView(bqm)

        buff = fv.readall()

        for pos in range(1, fv.quadratic_end):
            fv.seek(-pos, io.SEEK_END)

            subbuff = bytearray(pos)  # length pos

            self.assertEqual(fv.readinto(subbuff), len(subbuff))
            self.assertEqual(buff[-pos:], subbuff)
    def test_readinto(self, BQM):
        if issubclass(BQM, dimod.AdjDictBQM):
            # not (yet) implemented for non cybqms
            return

        bqm = BQM(np.triu(np.arange(25).reshape((5, 5))), 'BINARY')
        bqm.offset = 14

        fv = FileView(bqm)

        num_bytes = fv.quadratic_end

        # make a buffer that's a bit bigger
        buff = bytearray(num_bytes + 10)

        self.assertEqual(fv.readinto(buff), num_bytes)

        # reset and make sure that a total read it the same
        fv.seek(0)
        self.assertEqual(fv.read(), buff[:num_bytes])