當前位置: 首頁>>代碼示例>>Python>>正文


Python dimod.BINARY屬性代碼示例

本文整理匯總了Python中dimod.BINARY屬性的典型用法代碼示例。如果您正苦於以下問題:Python dimod.BINARY屬性的具體用法?Python dimod.BINARY怎麽用?Python dimod.BINARY使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在dimod的用法示例。


在下文中一共展示了dimod.BINARY屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: select_random_subgraph

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def select_random_subgraph(bqm, n):
    """Select randomly `n` variables of the specified binary quadratic model.

    Args:
        bqm (:class:`dimod.BinaryQuadraticModel`):
            Binary quadratic model (BQM).

        n (int):
            Number of requested variables. Must be between 0 and `len(bqm)`.

    Returns:
        list: `n` variables selected randomly from the BQM.

    Examples:
        This example returns 2 variables of a 4-variable BQM.

        >>> import dimod
        >>> bqm = dimod.BQM({}, {'ab': 0, 'bc': 1, 'cd': 2}, 0, 'BINARY')
        >>> select_random_subgraph(bqm, 2)      # doctest: +SKIP
        ['d', 'b']

    """
    return random.sample(bqm.linear.keys(), n) 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:25,代碼來源:utils.py

示例2: random_sample_seq

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def random_sample_seq(size, vartype):
    """Return a random sample.

    Args:
        size (int):
            Sample size (number of variables).

        vartype (:class:`dimod.Vartype`):
            Variable type; for example, `Vartype.SPIN`, `BINARY`, or `{-1, 1}`.

    Returns:
        dict: Random sample of `size` in length, with values from `vartype`.

    Examples:
        >>> random_sample_seq(4, dimod.BINARY)      # doctest: +SKIP
        {0: 0, 1: 1, 2: 0, 3: 0}

    """
    values = list(vartype.value)
    return {i: random.choice(values) for i in range(size)} 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:22,代碼來源:utils.py

示例3: random_sample

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def random_sample(bqm):
    """Return a random sample for a binary quadratic model.

    Args:
        bqm (:obj:`.BinaryQuadraticModel`):
            Binary quadratic model (BQM).

    Returns:
        dict: A sample with random values for the BQM.

    Examples:
        >>> import dimod
        >>> bqm = dimod.BQM({}, {'ab': -1, 'bc': -1, 'ca': -1}, 0, 'BINARY')
        >>> random_sample(bqm)     # doctest: +SKIP
        {'a': 0, 'b': 1, 'c': 1}

    """
    values = list(bqm.vartype.value)
    return {i: random.choice(values) for i in bqm.variables} 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:21,代碼來源:utils.py

示例4: min_sample

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def min_sample(bqm):
    """Return a sample with all variables set to the minimal value for a binary
    quadratic model.

    Args:
        bqm (:obj:`.BinaryQuadraticModel`):
            Binary quadratic model (BQM).

    Returns:
        dict: A sample with minimal values for all variables of the BQM.

    Examples:
        >>> import dimod
        >>> bqm = dimod.BQM({}, {'ab': -1, 'bc': -1, 'ca': -1}, 0, 'BINARY')
        >>> min_sample(bqm)     # doctest: +SKIP
        {'a': 0, 'b': 0, 'c': 0}

    """
    value = min(bqm.vartype.value)
    return {i: value for i in bqm.variables} 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:22,代碼來源:utils.py

示例5: max_sample

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def max_sample(bqm):
    """Return a sample with all variables set to the maximal value for a binary
    quadratic model.

    Args:
        bqm (:obj:`.BinaryQuadraticModel`):
            Binary quadratic model (BQM).

    Returns:
        dict: A sample with maximal values for all variables of the BQM.

    Examples:
        >>> import dimod
        >>> bqm = dimod.BQM({}, {'ab': -1, 'bc': -1, 'ca': -1}, 0, 'BINARY')
        >>> max_sample(bqm)     # doctest: +SKIP
        {'a': 1, 'b': 1, 'c': 1}

    """
    value = max(bqm.vartype.value)
    return {i: value for i in bqm.variables} 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:22,代碼來源:utils.py

示例6: test_string_labels

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def test_string_labels(self):
        si, sj, st = 2, 2, 3
        ti, tj, tt = 1, 1, 4
        alpha = 'abcdefghijklmnopqrstuvwxyz'

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

        for u, v in reversed(list(dnx.chimera_graph(si, sj, st).edges)):
            bqm.add_interaction(alpha[u], alpha[v], 1)

        tiles = chimera_tiles(bqm, ti, tj, tt)

        self.assertEqual(len(tiles), 4)  # we have the correct number of tiles
        self.assertEqual(set().union(*tiles.values()), bqm.variables)  # all of the nodes are present
        for embedding in tiles.values():
            self.assertTrue(set(chain[0] for chain in embedding.values()).issubset(set(range(ti*tj*tt*2)))) 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:18,代碼來源:test_utils.py

示例7: generate_random_chimera_problem

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def generate_random_chimera_problem(adjacency, h_range, j_range, offset=0, vartype=dimod.BINARY):
    """Generate a random chimera problem, with
    h int chosen randomly from h_range, j int chosen randomly from j_range.

    Typically: h_range = [0, 0] and j_range = [-k, +k].

    Args:
        adjacency (dict[/{node: {neighbor_node_1, ...}}): Adjacency dictionary
        h_range (tuple/(upper,lower)): bounds for h
        j_range (tuple/(upper,lower)): bounds for j
        offset (float): energy offset
        vartype (dimod.Vartype): BQM's vartype
    
    Returns:
        dimod.BinaryQuadraticModel
    """

    h = {n: random.randint(*h_range) for n in adjacency.keys()}
    J = {(n,e): random.randint(*j_range)
            for n, edges in adjacency.items()
                for e in edges
                    if e > n}

    return dimod.BinaryQuadraticModel(h, J, offset, vartype) 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:26,代碼來源:generate.py

示例8: generate_chimera

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def generate_chimera(size, vartype, count, fmt, outdir):
    """Generate `count` of random Chimera-structured problems
    with `size` topology, with zero biases and random J's in +/-k range
    (where k goes from 1 to `count`).
    """

    def store(bqm, fp):
        if fmt == 'coo':
            fp.write(bqm.to_coo(vartype_header=True))
        elif fmt == 'json':
            fp.write(bqm.to_json())

    ext = {'SPIN': 'ising', 'BINARY': 'qubo'}

    adj = dnx.chimera_graph(*size).adj

    for k in range(1, count+1):
        bqm = generate_random_chimera_problem(adj, (0, 0), (-k, k), vartype=vartype)

        if outdir:
            path = os.path.join(outdir, '{}.{:0>2}.{}'.format(len(bqm), k, ext[vartype]))
            with open(path, 'w') as fp:
                store(bqm, fp)
        else:
            store(bqm, sys.stdout) 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:27,代碼來源:generate.py

示例9: test_x_vartype

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def test_x_vartype(self):
        samples = {'a': -1, 'b': 1}
        bqm = dimod.BQM.from_qubo({'ab': 1})

        init = Initialized().parse_initial_states(
            bqm=bqm, initial_states=samples, num_reads=10)

        self.assertIs(init.initial_states.vartype, dimod.BINARY)
        arr = init.initial_states.record.sample
        self.assertTrue(((arr == 1) ^ (arr == 0)).all())

        samples = {'a': 0, 'b': 1}
        bqm = dimod.BQM.from_ising({}, {'ab': 1})

        init = Initialized().parse_initial_states(
            bqm=bqm, initial_states=samples, num_reads=10)

        self.assertIs(init.initial_states.vartype, dimod.SPIN)
        arr = init.initial_states.record.sample
        self.assertTrue(((arr == 1) ^ (arr == -1)).all()) 
開發者ID:dwavesystems,項目名稱:dimod,代碼行數:22,代碼來源:test_initialized.py

示例10: test_array_like

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def test_array_like(self, name, BQM):
        D = np.ones((5, 5)).tolist()
        bqm = BQM(D, 'BINARY')
        assert_consistent_bqm(bqm)
        for u, v in itertools.combinations(range(5), 2):
            self.assertEqual(bqm.get_quadratic(u, v), 2)  # added
        for u in range(5):
            self.assertEqual(bqm.get_linear(u), 1)

        # with explicit kwarg
        bqm = BQM(D, vartype='BINARY')
        assert_consistent_bqm(bqm)
        for u, v in itertools.combinations(range(5), 2):
            self.assertEqual(bqm.get_quadratic(u, v), 2)  # added
        for u in range(5):
            self.assertEqual(bqm.get_linear(u), 1) 
開發者ID:dwavesystems,項目名稱:dimod,代碼行數:18,代碼來源:test_bqm.py

示例11: test_bqm_binary_to_spin

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def test_bqm_binary_to_spin(self):
        linear = {'a': -1, 'b': 1, 0: 1.5}
        quadratic = {(0, 1): -1, (0, 0): 1, (1, 2): 1.5, (2, 2): 4, (4, 5): 7}
        offset = 0
        vartype = dimod.BINARY
        for source, target in itertools.product(BQM_SUBCLASSES, repeat=2):
            with self.subTest(source=source, target=target):
                bqm = source(linear, quadratic, offset, vartype)
                new = target(bqm, vartype=dimod.SPIN)

                self.assertIsInstance(new, target)
                assert_consistent_bqm(new)
                self.assertEqual(new.vartype, dimod.SPIN)

                # change back for equality check
                new.change_vartype(dimod.BINARY)
                self.assertEqual(bqm.adj, new.adj)
                self.assertEqual(bqm.offset, new.offset)
                self.assertEqual(bqm.vartype, new.vartype) 
開發者ID:dwavesystems,項目名稱:dimod,代碼行數:21,代碼來源:test_bqm.py

示例12: test_vartype_only

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def test_vartype_only(self, name, BQM):
        bqm = BQM('SPIN')
        self.assertEqual(bqm.vartype, dimod.SPIN)
        self.assertEqual(bqm.shape, (0, 0))
        assert_consistent_bqm(bqm)

        bqm = BQM(vartype='SPIN')
        self.assertEqual(bqm.vartype, dimod.SPIN)
        self.assertEqual(bqm.shape, (0, 0))
        assert_consistent_bqm(bqm)

        bqm = BQM('BINARY')
        self.assertEqual(bqm.vartype, dimod.BINARY)
        self.assertEqual(bqm.shape, (0, 0))
        assert_consistent_bqm(bqm)

        bqm = BQM(vartype='BINARY')
        self.assertEqual(bqm.vartype, dimod.BINARY)
        self.assertEqual(bqm.shape, (0, 0))
        assert_consistent_bqm(bqm) 
開發者ID:dwavesystems,項目名稱:dimod,代碼行數:22,代碼來源:test_bqm.py

示例13: test_coo_functional_file_empty_BINARY

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def test_coo_functional_file_empty_BINARY(self, name, BQM):
        if not BQM.shapeable():
            return

        bqm = BQM.empty(dimod.BINARY)

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

        with open(filename, 'w') as file:
            bqm.to_coo(file)

        with open(filename, 'r') as file:
            new_bqm = BQM.from_coo(file, dimod.BINARY)

        shutil.rmtree(tmpdir)

        self.assertEqual(bqm, new_bqm) 
開發者ID:dwavesystems,項目名稱:dimod,代碼行數:20,代碼來源:test_bqm.py

示例14: test_to_ising_binary_to_ising

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def test_to_ising_binary_to_ising(self, name, BQM):
        linear = {0: 7.1, 1: 103}
        quadratic = {(0, 1): .97}
        offset = 0.3
        vartype = dimod.BINARY

        model = BQM(linear, quadratic, offset, vartype)

        h, J, off = model.to_ising()

        for spins in itertools.product((-1, 1), repeat=len(model)):
            spin_sample = dict(zip(range(len(spins)), spins))
            bin_sample = {v: (s + 1) // 2 for v, s in spin_sample.items()}

            # calculate the qubo's energy
            energy = off
            for (u, v), bias in J.items():
                energy += spin_sample[u] * spin_sample[v] * bias
            for v, bias in h.items():
                energy += spin_sample[v] * bias

            # and the energy of the model
            self.assertAlmostEqual(energy, model.energy(bin_sample)) 
開發者ID:dwavesystems,項目名稱:dimod,代碼行數:25,代碼來源:test_bqm.py

示例15: bqm_edges_between_variables

# 需要導入模塊: import dimod [as 別名]
# 或者: from dimod import BINARY [as 別名]
def bqm_edges_between_variables(bqm, variables):
    """Return edges connecting specified variables of a binary quadratic model.

    Args:
        bqm (:class:`dimod.BinaryQuadraticModel`):
            Binary quadratic model (BQM).

        variables (list/set):
            Subset of variables in the BQM.

    Returns:
        list: All edges connecting `variables` as tuples plus the variables
        themselves as tuples (v, v).

    Examples:
        This example returns connecting edges between 3 nodes of a BQM based on
        a 4-variable path graph.

        >>> import dimod
        >>> bqm = dimod.BQM({}, {(0, 1): 1, (1, 2): 1, (2, 3): 1}, 0, 'BINARY')
        >>> bqm_edges_between_variables(bqm, {0, 1, 3})
        [(0, 1), (0, 0), (1, 1), (3, 3)]

    """
    variables = set(variables)
    edges = [(start, end) for (start, end), coupling in bqm.quadratic.items()
                if start in variables and end in variables]
    edges.extend((v, v) for v in bqm.linear if v in variables)
    return edges 
開發者ID:dwavesystems,項目名稱:dwave-hybrid,代碼行數:31,代碼來源:utils.py


注:本文中的dimod.BINARY屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。