def _compute_grover_oracle_matrix(bitstring_map):
"""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.
:type bitstring_map: Dict[String, Int]
:return: a numpy array corresponding to the unitary matrix for oracle for the given
bitstring_map
:rtype: numpy.ndarray
"""
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
评论列表
文章目录