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


Python numpy.binary_repr方法代碼示例

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


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

示例1: _compute_grover_oracle_matrix

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def _compute_grover_oracle_matrix(bitstring_map: Dict[str, int]) -> np.ndarray:
        """
        Computes the unitary matrix that encodes the oracle function for Grover's algorithm

        :param bitstring_map: dict with string keys corresponding to bitstrings,
         and integer values corresponding to the desired phase on the output state.
        :return: a numpy array corresponding to the unitary matrix for oracle for the given
         bitstring_map
        """
        n_bits = len(list(bitstring_map.keys())[0])
        oracle_matrix = np.zeros(shape=(2 ** n_bits, 2 ** n_bits))
        for b in range(2 ** n_bits):
            pad_str = np.binary_repr(b, n_bits)
            phase_factor = bitstring_map[pad_str]
            oracle_matrix[b, b] = phase_factor
        return oracle_matrix 
開發者ID:rigetti,項目名稱:grove,代碼行數:18,代碼來源:grover.py

示例2: decimal_to_binary

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def decimal_to_binary(decimal_val, max_num_digits=20, fractional_part_only=False):
    """ decimal to binary """
    decimal_val_fractional_part = abs(decimal_val - int(decimal_val))
    current_binary_position_val = 1 / 2
    binary_fractional_part_digits = []
    while decimal_val_fractional_part >= 0 and len(binary_fractional_part_digits) < max_num_digits:
        if decimal_val_fractional_part >= current_binary_position_val:
            binary_fractional_part_digits.append('1')
            decimal_val_fractional_part -= current_binary_position_val
        else:
            binary_fractional_part_digits.append('0')
        current_binary_position_val /= 2

    binary_repr_fractional_part = ''.join(binary_fractional_part_digits)

    if fractional_part_only:
        return binary_repr_fractional_part
    else:
        return binary_repr(int(decimal_val)) + '.' + binary_repr_fractional_part 
開發者ID:Qiskit,項目名稱:qiskit-aqua,代碼行數:21,代碼來源:decimal_to_binary.py

示例3: _run

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def _run(self):
        if self._quantum_instance.is_statevector:
            qc = self.construct_circuit(measurement=False)
            result = self._quantum_instance.execute(qc)
            complete_state_vec = result.get_statevector(qc)
            variable_register_density_matrix = get_subsystem_density_matrix(
                complete_state_vec,
                range(len(self._oracle.variable_register), qc.width())
            )
            variable_register_density_matrix_diag = np.diag(variable_register_density_matrix)
            measurements = {
                np.binary_repr(idx, width=len(self._oracle.variable_register)):
                    abs(variable_register_density_matrix_diag[idx]) ** 2
                for idx in range(len(variable_register_density_matrix_diag))
                if not variable_register_density_matrix_diag[idx] == 0
            }
        else:
            qc = self.construct_circuit(measurement=True)
            measurements = self._quantum_instance.execute(qc).get_counts(qc)

        self._ret['result'] = self._interpret_measurement(measurements)
        return self._ret 
開發者ID:Qiskit,項目名稱:qiskit-aqua,代碼行數:24,代碼來源:simon.py

示例4: test_simon

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def test_simon(self, simon_input, mct_mode, optimization, simulator):
        """ Simon test """
        # find the two keys that have matching values
        nbits = int(math.log(len(simon_input[0]), 2))
        vals = list(zip(*simon_input))[::-1]

        def find_pair():
            for i, val in enumerate(vals):
                for j in range(i + 1, len(vals)):
                    if val == vals[j]:
                        return i, j
            return 0, 0

        k_1, k_2 = find_pair()
        hidden = np.binary_repr(k_1 ^ k_2, nbits)

        backend = BasicAer.get_backend(simulator)
        oracle = TruthTableOracle(simon_input, optimization=optimization, mct_mode=mct_mode)
        algorithm = Simon(oracle)
        quantum_instance = QuantumInstance(backend)
        result = algorithm.run(quantum_instance=quantum_instance)
        # print(result['circuit'].draw(line_length=10000))
        self.assertEqual(result['result'], hidden) 
開發者ID:Qiskit,項目名稱:qiskit-aqua,代碼行數:25,代碼來源:test_simon.py

示例5: _brute_force

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def _brute_force(self):
        # brute-force way: try every possible assignment!
        has_sol = False

        def bitfield(n, length):
            result = np.binary_repr(n, length)
            return [int(digit) for digit in result]  # [2:] to chop off the "0b" part

        subsets = len(self.list_of_subsets)
        maximum = 2 ** subsets
        for i in range(maximum):
            cur = bitfield(i, subsets)
            cur_v = exact_cover.check_solution_satisfiability(cur, self.list_of_subsets)
            if cur_v:
                has_sol = True
                break
        return has_sol 
開發者ID:Qiskit,項目名稱:qiskit-aqua,代碼行數:19,代碼來源:test_exact_cover.py

示例6: _brute_force

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def _brute_force(self):
        # brute-force way
        def bitfield(n, length):
            result = np.binary_repr(n, length)
            return [int(digit) for digit in result]  # [2:] to chop off the "0b" part

        nodes = self.num_nodes
        maximum = 2 ** nodes
        minimal_v = np.inf
        for i in range(maximum):
            cur = bitfield(i, nodes)

            cur_v = vertex_cover.check_full_edge_coverage(np.array(cur), self.w)
            if cur_v:
                nonzerocount = np.count_nonzero(cur)
                if nonzerocount < minimal_v:
                    minimal_v = nonzerocount

        return minimal_v 
開發者ID:Qiskit,項目名稱:qiskit-aqua,代碼行數:21,代碼來源:test_vertex_cover.py

示例7: _brute_force

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def _brute_force(self):
        # use the brute-force way to generate the oracle
        def bitfield(n, length):
            result = np.binary_repr(n, length)
            return [int(digit) for digit in result]  # [2:] to chop off the "0b" part

        nodes = self.num_nodes
        maximum = 2 ** nodes
        minimal_v = np.inf
        for i in range(maximum):
            cur = bitfield(i, nodes)

            how_many_nonzero = np.count_nonzero(cur)
            if how_many_nonzero * 2 != nodes:  # not balanced
                continue

            cur_v = graph_partition.objective_value(np.array(cur), self.w)
            if cur_v < minimal_v:
                minimal_v = cur_v
        return minimal_v 
開發者ID:Qiskit,項目名稱:qiskit-aqua,代碼行數:22,代碼來源:test_graph_partition.py

示例8: _brute_force

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def _brute_force(self):
        # brute-force way: try every possible assignment!
        def bitfield(n, length):
            result = np.binary_repr(n, length)
            return [int(digit) for digit in result]  # [2:] to chop off the "0b" part

        subsets = len(self.list_of_subsets)
        maximum = 2 ** subsets
        max_v = -np.inf
        for i in range(maximum):
            cur = bitfield(i, subsets)
            cur_v = set_packing.check_disjoint(cur, self.list_of_subsets)
            if cur_v:
                if np.count_nonzero(cur) > max_v:
                    max_v = np.count_nonzero(cur)
        return max_v 
開發者ID:Qiskit,項目名稱:qiskit-aqua,代碼行數:18,代碼來源:test_set_packing.py

示例9: test_insufficient_width_positive

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def test_insufficient_width_positive(self):
        args = (10,)
        kwargs = {'width': 2}

        self.message = ("Insufficient bit width provided. This behavior "
                        "will raise an error in the future.")
        self.assert_deprecated(np.binary_repr, args=args, kwargs=kwargs) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:9,代碼來源:test_deprecations.py

示例10: test_insufficient_width_negative

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def test_insufficient_width_negative(self):
        args = (-5,)
        kwargs = {'width': 2}

        self.message = ("Insufficient bit width provided. This behavior "
                        "will raise an error in the future.")
        self.assert_deprecated(np.binary_repr, args=args, kwargs=kwargs) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:9,代碼來源:test_deprecations.py

示例11: test_zero

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def test_zero(self):
        assert_equal(np.binary_repr(0), '0') 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:4,代碼來源:test_numeric.py

示例12: test_positive

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def test_positive(self):
        assert_equal(np.binary_repr(10), '1010')
        assert_equal(np.binary_repr(12522),
                     '11000011101010')
        assert_equal(np.binary_repr(10736848),
                     '101000111101010011010000') 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:8,代碼來源:test_numeric.py

示例13: test_sufficient_width

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def test_sufficient_width(self):
        assert_equal(np.binary_repr(0, width=5), '00000')
        assert_equal(np.binary_repr(10, width=7), '0001010')
        assert_equal(np.binary_repr(-5, width=7), '1111011') 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:6,代碼來源:test_numeric.py

示例14: test_neg_width_boundaries

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def test_neg_width_boundaries(self):
        # see gh-8670

        # Ensure that the example in the issue does not
        # break before proceeding to a more thorough test.
        assert_equal(np.binary_repr(-128, width=8), '10000000')

        for width in range(1, 11):
            num = -2**(width - 1)
            exp = '1' + (width - 1) * '0'
            assert_equal(np.binary_repr(num, width=width), exp) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:13,代碼來源:test_numeric.py

示例15: test_binary_repr_0

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import binary_repr [as 別名]
def test_binary_repr_0(self):
        # Ticket #151
        assert_equal('0', np.binary_repr(0)) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:5,代碼來源:test_regression.py


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