


'''simple docstring'''






import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
 import torch_xla.core.xla_model as xm
 import torch_xla.debug.metrics as met




class  a     (     _SCREAMING_SNAKE_CASE       ):
 def __init__( self      ,				*__magic_name__      ,				__magic_name__=None      ,				__magic_name__=None      ,				**__magic_name__						)					->							Tuple:
      super().__init__(*__magic_name__      ,				**__magic_name__						)
      _a			      =							eval_examples
      _a			      =							post_process_function
 def 							__UpperCAmelCase		( self      ,				__magic_name__ = None      ,				__magic_name__=None      ,				__magic_name__ = None      ,				__magic_name__ = "eval"      ,				**__magic_name__      ,				)					->							Dict[str, float]:
      _a			      =							gen_kwargs.copy()
      _a			      =							(
          gen_kwargs['max_length'] if gen_kwargs.get('max_length'						) is not None else self.args.generation_max_length
      )
      _a			      =							(
          gen_kwargs['num_beams'] if gen_kwargs.get('num_beams'						) is not None else self.args.generation_num_beams
      )
      _a			      =							gen_kwargs

      _a			      =							self.eval_dataset if eval_dataset is None else eval_dataset
      _a			      =							self.get_eval_dataloader(__magic_name__						)
      _a			      =							self.eval_examples if eval_examples is None else eval_examples

      # Temporarily disable metric computation, we will do it in the loop here.
      _a			      =							self.compute_metrics
      _a			      =							None
      _a			      =							time.time()
      _a			      =							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
      try:
           _a			      =							eval_loop(
               __magic_name__      ,				description='Evaluation'      ,				prediction_loss_only=True if compute_metrics is None else None      ,				ignore_keys=__magic_name__      ,				metric_key_prefix=__magic_name__      ,				)
      finally:
           _a			      =							compute_metrics
      _a			      =							self.args.eval_batch_size * self.args.world_size
      if f'{metric_key_prefix}_jit_compilation_time' in output.metrics:
           start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time']
      output.metrics.update(
          speed_metrics(
              __magic_name__      ,				__magic_name__      ,				num_samples=output.num_samples      ,				num_steps=math.ceil(output.num_samples / total_batch_size						)      ,				)						)

      if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
           # Only the main node write the results by default
           _a			      =							self.post_process_function(__magic_name__      ,				__magic_name__      ,				__magic_name__						)
           _a			      =							self.compute_metrics(__magic_name__						)

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys()						):
                if not key.startswith(f'{metric_key_prefix}_'						):
                     _a			      =							metrics.pop(__magic_name__						)

           metrics.update(output.metrics						)
      else:
           _a			      =							output.metrics

      if self.args.should_log:
           # Only the main node log the results by default
           self.log(__magic_name__						)

      if self.args.tpu_metrics_debug or self.args.debug:
           # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
           xm.master_print(met.metrics_report()						)

      _a			      =							self.callback_handler.on_evaluate(self.args      ,				self.state      ,				self.control      ,				__magic_name__						)
      return metrics




 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__=None      ,				__magic_name__ = "test"      ,				**__magic_name__						)					->							Tuple:
      _a			      =							gen_kwargs.copy()

      _a			      =							self.get_test_dataloader(__magic_name__						)

      # Temporarily disable metric computation, we will do it in the loop here.
      _a			      =							self.compute_metrics
      _a			      =							None
      _a			      =							time.time()
      _a			      =							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
      try:
           _a			      =							eval_loop(
               __magic_name__      ,				description='Prediction'      ,				prediction_loss_only=True if compute_metrics is None else None      ,				ignore_keys=__magic_name__      ,				metric_key_prefix=__magic_name__      ,				)
      finally:
           _a			      =							compute_metrics

      _a			      =							self.args.eval_batch_size * self.args.world_size
      if f'{metric_key_prefix}_jit_compilation_time' in output.metrics:
           start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time']
      output.metrics.update(
          speed_metrics(
              __magic_name__      ,				__magic_name__      ,				num_samples=output.num_samples      ,				num_steps=math.ceil(output.num_samples / total_batch_size						)      ,				)						)
      if self.post_process_function is None or self.compute_metrics is None:
           return output

      _a			      =							self.post_process_function(__magic_name__      ,				__magic_name__      ,				__magic_name__      ,				'predict'						)
      _a			      =							self.compute_metrics(__magic_name__						)

      # Prefix all keys with metric_key_prefix + '_'
      for key in list(metrics.keys()						):
           if not key.startswith(f'{metric_key_prefix}_'						):
                _a			      =							metrics.pop(__magic_name__						)
      metrics.update(output.metrics						)
      return PredictionOutput(predictions=predictions.predictions      ,				label_ids=predictions.label_ids      ,				metrics=__magic_name__						)





'''simple docstring'''






import numpy as np
import qiskit






def 	_A				(lowerCAmelCase__  :int = 8				,						lowerCAmelCase__  :int | None = None		)   ->  str:




     '''simple docstring'''



     _a			      =							np.random.default_rng(seed=lowerCAmelCase__		)

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

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

