当前位置: 首页>>代码示例>>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;未经允许,请勿转载。