Python numpy.binary_repr() Examples
The following are 30 code examples for showing how to use numpy.binary_repr(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: grove Author: rigetti File: grover.py License: Apache License 2.0 | 6 votes |
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
Example 2
Project: qiskit-aqua Author: Qiskit File: decimal_to_binary.py License: Apache License 2.0 | 6 votes |
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
Example 3
Project: qiskit-aqua Author: Qiskit File: simon.py License: Apache License 2.0 | 6 votes |
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
Example 4
Project: qiskit-aqua Author: Qiskit File: test_simon.py License: Apache License 2.0 | 6 votes |
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)
Example 5
Project: qiskit-aqua Author: Qiskit File: test_exact_cover.py License: Apache License 2.0 | 6 votes |
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
Example 6
Project: qiskit-aqua Author: Qiskit File: test_vertex_cover.py License: Apache License 2.0 | 6 votes |
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
Example 7
Project: qiskit-aqua Author: Qiskit File: test_graph_partition.py License: Apache License 2.0 | 6 votes |
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
Example 8
Project: qiskit-aqua Author: Qiskit File: test_set_packing.py License: Apache License 2.0 | 6 votes |
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
Example 9
Project: recruit Author: Frank-qlu File: test_deprecations.py License: Apache License 2.0 | 5 votes |
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)
Example 10
Project: recruit Author: Frank-qlu File: test_deprecations.py License: Apache License 2.0 | 5 votes |
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)
Example 11
Project: recruit Author: Frank-qlu File: test_numeric.py License: Apache License 2.0 | 5 votes |
def test_zero(self): assert_equal(np.binary_repr(0), '0')
Example 12
Project: recruit Author: Frank-qlu File: test_numeric.py License: Apache License 2.0 | 5 votes |
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')
Example 13
Project: recruit Author: Frank-qlu File: test_numeric.py License: Apache License 2.0 | 5 votes |
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')
Example 14
Project: recruit Author: Frank-qlu File: test_numeric.py License: Apache License 2.0 | 5 votes |
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)
Example 15
Project: recruit Author: Frank-qlu File: test_regression.py License: Apache License 2.0 | 5 votes |
def test_binary_repr_0(self): # Ticket #151 assert_equal('0', np.binary_repr(0))
Example 16
Project: recruit Author: Frank-qlu File: test_regression.py License: Apache License 2.0 | 5 votes |
def test_binary_repr_0_width(self): assert_equal(np.binary_repr(0, width=3), '000')
Example 17
Project: auto-alt-text-lambda-api Author: abhisuri97 File: test_numeric.py License: MIT License | 5 votes |
def test_zero(self): assert_equal(np.binary_repr(0), '0')
Example 18
Project: auto-alt-text-lambda-api Author: abhisuri97 File: test_numeric.py License: MIT License | 5 votes |
def test_large(self): assert_equal(np.binary_repr(10736848), '101000111101010011010000')
Example 19
Project: auto-alt-text-lambda-api Author: abhisuri97 File: test_numeric.py License: MIT License | 5 votes |
def test_negative(self): assert_equal(np.binary_repr(-1), '-1') assert_equal(np.binary_repr(-1, width=8), '11111111')
Example 20
Project: auto-alt-text-lambda-api Author: abhisuri97 File: test_regression.py License: MIT License | 5 votes |
def test_binary_repr_0(self,level=rlevel): # Ticket #151 assert_equal('0', np.binary_repr(0))
Example 21
Project: auto-alt-text-lambda-api Author: abhisuri97 File: test_regression.py License: MIT License | 5 votes |
def test_binary_repr_0_width(self, level=rlevel): assert_equal(np.binary_repr(0, width=3), '000')
Example 22
Project: vnpy_crypto Author: birforce File: test_deprecations.py License: MIT License | 5 votes |
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)
Example 23
Project: vnpy_crypto Author: birforce File: test_deprecations.py License: MIT License | 5 votes |
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)
Example 24
Project: vnpy_crypto Author: birforce File: test_numeric.py License: MIT License | 5 votes |
def test_zero(self): assert_equal(np.binary_repr(0), '0')
Example 25
Project: vnpy_crypto Author: birforce File: test_numeric.py License: MIT License | 5 votes |
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')
Example 26
Project: vnpy_crypto Author: birforce File: test_numeric.py License: MIT License | 5 votes |
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')
Example 27
Project: vnpy_crypto Author: birforce File: test_numeric.py License: MIT License | 5 votes |
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)
Example 28
Project: vnpy_crypto Author: birforce File: test_regression.py License: MIT License | 5 votes |
def test_binary_repr_0(self): # Ticket #151 assert_equal('0', np.binary_repr(0))
Example 29
Project: vnpy_crypto Author: birforce File: test_regression.py License: MIT License | 5 votes |
def test_binary_repr_0_width(self): assert_equal(np.binary_repr(0, width=3), '000')
Example 30
Project: scikit-multiflow Author: scikit-multiflow File: classifier_chains.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def predict(self, X): """ Predict classes for the passed data. Parameters ---------- X : numpy.ndarray of shape (n_samples, n_features) The set of data samples to predict the labels for. Returns ------- A numpy.ndarray with all the predictions for the samples in X. Notes ----- Explores all possible branches of the probability tree (i.e., all possible 2^L label combinations). """ N, D = X.shape Yp = np.zeros((N, self.L)) # for each instance for n in range(N): w_max = 0. # for each and every possible label combination for b in range(2**self.L): # put together a label vector y_ = np.array(list(map(int, np.binary_repr(b, width=self.L)))) # ... and gauge a probability for it (given x) w_ = P(y_, X[n], self) # if it performs well, keep it, and record the max if w_ > w_max: Yp[n, :] = y_[:].copy() w_max = w_ return Yp