


'''simple docstring'''






__UpperCAmelCase					       =							256
# Modulus to hash a string
__UpperCAmelCase					       =							1_000_003


def       _snake_case     (  A				,					A )		->					bool:
							lowerCAmelCase__      =			len(A )
							lowerCAmelCase__      =			len(A )
							if p_len > t_len:
														return False

							lowerCAmelCase__      =			0
							lowerCAmelCase__      =			0
							lowerCAmelCase__      =			1

							# Calculating the hash of pattern and substring of text
							for i in range(A ):
														lowerCAmelCase__      =			(ord(pattern[i] ) + p_hash * alphabet_size) % modulus
														lowerCAmelCase__      =			(ord(text[i] ) + text_hash * alphabet_size) % modulus
														if i == p_len - 1:
																					continue
														lowerCAmelCase__      =			(modulus_power * alphabet_size) % modulus

							for i in range(0				,					t_len - p_len + 1 ):
														if text_hash == p_hash and text[i : i + p_len] == pattern:
																					return True
														if i == t_len - p_len:
																					continue
														# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
														lowerCAmelCase__      =			(
														    (text_hash - ord(text[i] ) * modulus_power) * alphabet_size
														    + ord(text[i + p_len] )
														) % modulus
							return False


def       _snake_case     (  )		->					None:
							lowerCAmelCase__      =			'''abc1abc12'''
							lowerCAmelCase__      =			'''alskfjaldsabc1abc1abc12k23adsfabcabc'''
							lowerCAmelCase__      =			'''alskfjaldsk23adsfabcabc'''
							assert rabin_karp(A				,					A ) and not rabin_karp(A				,					A )

							# Test 2)
							lowerCAmelCase__      =			'''ABABX'''
							lowerCAmelCase__      =			'''ABABZABABYABABX'''
							assert rabin_karp(A				,					A )

							# Test 3)
							lowerCAmelCase__      =			'''AAAB'''
							lowerCAmelCase__      =			'''ABAAAAAB'''
							assert rabin_karp(A				,					A )

							# Test 4)
							lowerCAmelCase__      =			'''abcdabcy'''
							lowerCAmelCase__      =			'''abcxabcdabxabcdabcdabcy'''
							assert rabin_karp(A				,					A )

							# Test 5)
							lowerCAmelCase__      =			'''Lü'''
							lowerCAmelCase__      =			'''Lüsai'''
							assert rabin_karp(A				,					A )
							lowerCAmelCase__      =			'''Lue'''
							assert not rabin_karp(A				,					A )
							print('''Success.''' )


if __name__ == "__main__":
		test_rabin_karp()



'''simple docstring'''






import numpy as np
import qiskit


def       _snake_case     (  A = 8				,					A = None )		->					str:
							lowerCAmelCase__      =			np.random.default_rng(seed=A )

							# Roughly 25% of the qubits will contribute to the key.
							# So we take more than we need.
							lowerCAmelCase__      =			6 * key_len
							# Measurement basis for Alice's qubits.
							lowerCAmelCase__      =			rng.integers(2				,					size=A )
							# The set of states Alice will prepare.
							lowerCAmelCase__      =			rng.integers(2				,					size=A )
							# Measurement basis for Bob's qubits.
							lowerCAmelCase__      =			rng.integers(2				,					size=A )

							# Quantum Circuit to simulate BB84
							lowerCAmelCase__      =			qiskit.QuantumCircuit(A				,					name='''BB84''' )

							# Alice prepares her qubits according to rules above.
							for index, _ in enumerate(A ):
														if alice_state[index] == 1:
																					bbaa_circ.x(A )
														if alice_basis[index] == 1:
																					bbaa_circ.h(A )
							bbaa_circ.barrier()

							# Bob measures the received qubits according to rules above.
							for index, _ in enumerate(A ):
														if bob_basis[index] == 1:
																					bbaa_circ.h(A )

							bbaa_circ.barrier()
							bbaa_circ.measure_all()

							# Simulate the quantum circuit.
							lowerCAmelCase__      =			qiskit.Aer.get_backend('''aer_simulator''' )
							# We only need to run one shot because the key is unique.
							# Multiple shots will produce the same key.
							lowerCAmelCase__      =			qiskit.execute(A				,					A				,					shots=1				,					seed_simulator=A )
							# Returns the result of measurement.
							lowerCAmelCase__      =			job.result().get_counts(A ).most_frequent()

							# Extracting the generated key from the simulation results.
							# Only keep measurement results where Alice and Bob chose the same basis.
							lowerCAmelCase__      =			''''''.join(
							    [
							        result_bit
							        for alice_basis_bit, bob_basis_bit, result_bit in zip(
							            A				,					A				,					A )
							        if alice_basis_bit == bob_basis_bit
							    ] )

							# Get final key. Pad with 0 if too short, otherwise truncate.
							lowerCAmelCase__      =			gen_key[:key_len] if len(A ) >= key_len else gen_key.ljust(A				,					'''0''' )
							return key


if __name__ == "__main__":
		print(f"""The generated key is : {bbaa(8, seed=0)}""")
		from doctest import testmod

		testmod()