
import argparse
import os

from accelerate.test_utils import execute_subprocess_async






def 		_UpperCAmelCase   (	UpperCamelCase:     str=None  ):






	"""simple docstring"""


	if subparsers is not None:
		__lowerCAmelCase         =							subparsers.add_parser("test"  )
	else:
		__lowerCAmelCase         =							argparse.ArgumentParser("Accelerate test command"  )

	parser.add_argument(
	    "--config_file"			,							default=_lowercase			,							help=(
	        "The path to use to store the config file. Will default to a file named default_config.yaml in the cache "
	        "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
	        "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
	        "with 'huggingface'."
	    )			,							)

	if subparsers is not None:
		parser.set_defaults(func=_lowercase  )
	return parser






def 		_UpperCAmelCase   (	UpperCamelCase:     str  ):






	"""simple docstring"""


	__lowerCAmelCase         =							os.path.sep.join(__file__.split(os.path.sep  )[:-2] + ["test_utils", "scripts", "test_script.py"]  )

	if args.config_file is None:
		__lowerCAmelCase         =							script_name
	else:
		__lowerCAmelCase         =							F"--config_file={args.config_file} {script_name}"

	__lowerCAmelCase         =							["accelerate-launch"] + test_args.split()
	__lowerCAmelCase         =							execute_subprocess_async(_lowercase			,							env=os.environ.copy()  )
	if result.returncode == 0:
		print("Test is a success! You are ready for your distributed training!"  )






def 		_UpperCAmelCase   (	):






	"""simple docstring"""


	__lowerCAmelCase         =							test_command_parser()
	__lowerCAmelCase         =							parser.parse_args()
	test_command(_lowercase  )


if __name__ == "__main__":
					main()








import numpy as np
import qiskit






def 		_UpperCAmelCase   (	UpperCamelCase:     int = 8			,							UpperCamelCase:     int | None = None  ):






	"""simple docstring"""


	__lowerCAmelCase         =							np.random.default_rng(seed=UpperCamelCase  )

	# 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=UpperCamelCase  )
	# The set of states Alice will prepare.
	__lowerCAmelCase         =							rng.integers(2			,							size=UpperCamelCase  )
	# Measurement basis for Bob's qubits.
	__lowerCAmelCase         =							rng.integers(2			,							size=UpperCamelCase  )

	# Quantum Circuit to simulate BB84
	__lowerCAmelCase         =							qiskit.QuantumCircuit(UpperCamelCase			,							name="BB84"  )

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

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

	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(UpperCamelCase			,							UpperCamelCase			,							shots=1			,							seed_simulator=UpperCamelCase  )
	# Returns the result of measurement.
	__lowerCAmelCase         =							job.result().get_counts(UpperCamelCase  ).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(
	            UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase  )
	        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(UpperCamelCase  ) >= key_len else gen_key.ljust(UpperCamelCase			,							"0"  )
	return key


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

					testmod()






