


'''simple docstring'''






import numpy as np
import qiskit





def      UpperCAmelCase      (		lowerCamelCase_  :int = 8				,      lowerCamelCase_  :int | None = None ):







		'''simple docstring'''
		snake_case_		:							Tuple			       =						np.random.default_rng(seed=lowerCamelCase_ )

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

		# Quantum Circuit to simulate BB84
		snake_case_		:							Optional[int]			       =						qiskit.QuantumCircuit(lowerCamelCase_				,      name="""BB84""" )

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

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

		bbaa_circ.barrier()
		bbaa_circ.measure_all()

		# Simulate the quantum circuit.
		snake_case_		:							Union[str, Any]			       =						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_		:							Tuple			       =						qiskit.execute(lowerCamelCase_				,      lowerCamelCase_				,      shots=1				,      seed_simulator=lowerCamelCase_ )
		# Returns the result of measurement.
		snake_case_		:							Tuple			       =						job.result().get_counts(lowerCamelCase_ ).most_frequent()

		# Extracting the generated key from the simulation results.
		# Only keep measurement results where Alice and Bob chose the same basis.
		snake_case_		:							Optional[Any]			       =						"""""".join(
		    [
		        result_bit
		        for alice_basis_bit, bob_basis_bit, result_bit in zip(
		            lowerCamelCase_				,      lowerCamelCase_				,      lowerCamelCase_ )
		        if alice_basis_bit == bob_basis_bit
		    ] )

		# Get final key. Pad with 0 if too short, otherwise truncate.
		snake_case_		:							str			       =						gen_key[:key_len] if len(lowerCamelCase_ ) >= key_len else gen_key.ljust(lowerCamelCase_				,      """0""" )
		return key


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

							testmod()



'''simple docstring'''






from __future__ import annotations

import os
from collections.abc import Mapping

__A	:	str     =     tuple[int, int]
class 						__UpperCamelCase	:





							def __init__(  self		:Union[str, Any]  ,_UpperCamelCase		:set[int]  ,_UpperCamelCase		:Mapping[EdgeT, int]			):
									snake_case_		:							set[int]			       =						vertices
									snake_case_		:							dict[EdgeT, int]			       =						{
									    (min(_UpperCamelCase			), max(_UpperCamelCase			)): weight for edge, weight in edges.items()
									}





							def 		a__   (  self		:Tuple  ,_UpperCamelCase		:EdgeT  ,_UpperCamelCase		:int			):
									self.vertices.add(edge[0]			)
									self.vertices.add(edge[1]			)
									snake_case_		:							str			       =						weight





							def 		a__   (  self		:Tuple			):
									snake_case_		:							Graph			       =						Graph({min(self.vertices			)}  ,{}			)
									snake_case_		:							EdgeT
									snake_case_		:							int
									snake_case_		:							EdgeT
									snake_case_		:							int

									while len(subgraph.vertices			) < len(self.vertices			):
											snake_case_		:							int			       =						max(self.edges.values()			) + 1
											for edge, weight in self.edges.items():
													if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices):
															if weight < min_weight:
																	snake_case_		:							Dict			       =						edge
																	snake_case_		:							Dict			       =						weight

											subgraph.add_edge(_UpperCamelCase  ,_UpperCamelCase			)

									return subgraph





def      UpperCAmelCase      (		lowerCamelCase_  :str = "p107_network.txt" ):







		'''simple docstring'''
		snake_case_		:							str			       =						os.path.abspath(os.path.dirname(lowerCamelCase_ ) )
		snake_case_		:							str			       =						os.path.join(lowerCamelCase_				,      lowerCamelCase_ )
		snake_case_		:							dict[EdgeT, int]			       =						{}
		snake_case_		:							list[str]
		snake_case_		:							int
		snake_case_		:							int

		with open(lowerCamelCase_ ) as f:
				snake_case_		:							Optional[int]			       =						f.read().strip().split("""\n""" )

		snake_case_		:							Any			       =						[line.split(""",""" ) for line in data]

		for edgea in range(1				,      len(lowerCamelCase_ ) ):
				for edgea in range(lowerCamelCase_ ):
						if adjaceny_matrix[edgea][edgea] != "-":
								snake_case_		:							str			       =						int(adjaceny_matrix[edgea][edgea] )

		snake_case_		:							Graph			       =						Graph(set(range(len(lowerCamelCase_ ) ) )				,      lowerCamelCase_ )

		snake_case_		:							Graph			       =						graph.prims_algorithm()

		snake_case_		:							int			       =						sum(graph.edges.values() )
		snake_case_		:							int			       =						sum(subgraph.edges.values() )

		return initial_total - optimal_total


if __name__ == "__main__":
							print(F'{solution() = }')