Exemplo n.º 1
0
    def test_load(self):
        filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                'data', 'coo_qubo.qubo')

        with open(filepath, 'r') as fp:
            bqm = coo.load(fp, dimod.BinaryQuadraticModel, dimod.BINARY)

        self.assertEqual(
            bqm,
            dimod.BinaryQuadraticModel.from_qubo({
                (0, 0): -1,
                (1, 1): -1,
                (2, 2): -1,
                (3, 3): -1
            }))
Exemplo n.º 2
0
    def test_functional_file_empty_BINARY(self):

        bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY)

        tmpdir = tempfile.mkdtemp()
        filename = os.path.join(tmpdir, 'test.qubo')

        with open(filename, 'w') as file:
            coo.dump(bqm, fp=file)

        with open(filename, 'r') as file:
            new_bqm = coo.load(file, dimod.BinaryQuadraticModel, dimod.BINARY)

        shutil.rmtree(tmpdir)

        self.assertEqual(bqm, new_bqm)
Exemplo n.º 3
0
    def test_functional_file_SPIN(self):

        bqm = dimod.BinaryQuadraticModel({0: 1.}, {
            (0, 1): 2,
            (2, 3): .4
        }, 0.0, dimod.SPIN)

        tmpdir = tempfile.mkdtemp()
        filename = os.path.join(tmpdir, 'test.qubo')

        with open(filename, 'w') as file:
            coo.dump(bqm, fp=file)

        with open(filename, 'r') as file:
            new_bqm = coo.load(file, dimod.BinaryQuadraticModel, dimod.SPIN)

        shutil.rmtree(tmpdir)

        self.assertEqual(bqm, new_bqm)
Exemplo n.º 4
0
    def from_coo(cls, obj, vartype=None):
        """Deserialize a binary quadratic model from a COOrdinate format encoding.

        COOrdinate_ is a sparse encoding for binary quadratic models.

        .. _COOrdinate: https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_(COO)

        Args:
            obj: (str/file):
                Either a string or a `.read()`-supporting `file object`_ that represents
                linear and quadratic biases for a binary quadratic model. This data
                is stored as a list of 3-tuples, (i, j, bias), where :math:`i=j`
                for linear biases.

            vartype (:class:`.Vartype`/str/set, optional):
                Variable type for the binary quadratic model. Accepted input values:

                * :class:`.Vartype.SPIN`, ``'SPIN'``, ``{-1, 1}``
                * :class:`.Vartype.BINARY`, ``'BINARY'``, ``{0, 1}``

                If not provided, the vartype must be specified with a header in the
                file.

        .. _file object: https://docs.python.org/3/glossary.html#term-file-object

        .. note:: Variables must use index lables (numeric lables). Binary quadratic
            models created from COOrdinate format encoding have offsets set to
            zero.

        .. note:: This method will be deprecated in the future. The preferred
            pattern is to use :func:`~dimod.serialization.coo.load` or
            :func:`~dimod.serialization.coo.loads` directly.

        """
        import dimod.serialization.coo as coo

        if isinstance(obj, str):
            return coo.loads(obj, cls=cls, vartype=vartype)

        return coo.load(obj, cls=cls, vartype=vartype)
Exemplo n.º 5
0
import sys
import argparse
import dimod
from dimod.serialization import coo

__copyright__ = "hyperQ – Ewa Hendzel"
__license__ = "MIT"
__version__ = "0.1.0"
__status__ = "Dev"

parser = argparse.ArgumentParser(description='Solve QUBO problem.')
parser.add_argument('--input', type=str, help='input qubo file')
parser.add_argument('--output', type=str, help='output csv file')
args = parser.parse_args()

if not args.input or not args.output:
    parser.print_help()
    exit(-1)

with open(args.input) as f:
    qubo = coo.load(f, vartype=dimod.BINARY)
    result = dimod.ExactSolver().sample(qubo)
    result.to_pandas_dataframe().sort_values("energy").to_csv(args.output)