






import numpy as np
import qiskit





def 		__UpperCAmelCase     (	a_ = 8				,	a_ = None):
							snake_case_           =				np.random.default_rng(seed=a_)

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

							# Quantum Circuit to simulate BB84
							snake_case_           =				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.
							snake_case_           =				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.
							snake_case_           =				qiskit.execute(a_				,	a_				,	shots=1				,	seed_simulator=a_)
							# Returns the result of measurement.
							snake_case_           =				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.
							snake_case_           =				''.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.
							snake_case_           =				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()













import numpy as np





def 		__UpperCAmelCase     (	a_):
							return 1 / (1 + np.exp(-vector))


if __name__ == "__main__":
	import doctest

	doctest.testmod()





