

import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel



class SCREAMING_SNAKE_CASE     (  pl.LightningModule			):
  """simple docstring"""






  def __init__(      self			:   Dict	,							lowerCAmelCase			:   List[str]   )							->     List[str]:






     """simple docstring"""







     super().__init__()
     __lowerCAmelCase  :      Union[str, Any]    =						model
     __lowerCAmelCase  :      Tuple    =						2
     __lowerCAmelCase  :      Dict    =						nn.Linear(self.model.config.hidden_size	,							self.num_labels   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple   )							->     List[str]:






     """simple docstring"""







     pass






def      snake_case_				(__A					:						str ,       __A					:						str ,       __A					:						str					)      ->     Union[str, Any]:
   # load longformer model from model identifier
   __lowerCAmelCase  :      Union[str, Any]    =						LongformerModel.from_pretrained(__A					)
   __lowerCAmelCase  :      List[str]    =						LightningModel(__A					)

   __lowerCAmelCase  :      Optional[Any]    =						torch.load(__A ,       map_location=torch.device("""cpu"""					)					)
   lightning_model.load_state_dict(ckpt["""state_dict"""]					)

   # init longformer question answering model
   __lowerCAmelCase  :      Any    =						LongformerForQuestionAnswering.from_pretrained(__A					)

   # transfer weights
   longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict()					)
   longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict()					)
   longformer_for_qa.eval()

   # save model
   longformer_for_qa.save_pretrained(__A					)

   print(f'''Conversion successful. Model saved under {pytorch_dump_folder_path}'''					)


if __name__ == "__main__":
   __UpperCAmelCase								=			argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       """--longformer_model""",
       default=None,
       type=str,
       required=True,
       help="""model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.""",
   )
   parser.add_argument(
       """--longformer_question_answering_ckpt_path""",
       default=None,
       type=str,
       required=True,
       help="""Path the official PyTorch Lightning Checkpoint.""",
   )
   parser.add_argument(
       """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
   )
   __UpperCAmelCase								=			parser.parse_args()
   convert_longformer_qa_checkpoint_to_pytorch(
       args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path
   )





import numpy as np
import qiskit





def      snake_case_				(__A					:						int = 8 ,       __A					:						int | None = None					)      ->     str:
   __lowerCAmelCase  :      List[Any]    =						np.random.default_rng(seed=__A					)

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

   # Quantum Circuit to simulate BB84
   __lowerCAmelCase  :      int    =						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  :      Optional[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.
   __lowerCAmelCase  :      Optional[Any]    =						qiskit.execute(__A ,       __A ,       shots=1 ,       seed_simulator=__A					)
   # Returns the result of measurement.
   __lowerCAmelCase  :      List[Any]    =						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  :      Optional[int]    =						"""""".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  :      Tuple    =						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()


