






'''simple docstring'''





import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel




class 			snake_case__   (							pl.LightningModule):

       '''simple docstring'''



       def __init__(							self			,						a__    ) ->      Optional[int]:






            '''simple docstring'''





            super().__init__()
            __snake_case			:Optional[int]          =						model
            __snake_case			:List[Any]          =						2
            __snake_case			:List[Any]          =						nn.Linear(self.model.config.hidden_size			,						self.num_labels    )



       def 					__lowercase						(							self    ) ->      Optional[Any]:






            '''simple docstring'''





            pass






def   UpperCamelCase  (       snake_case__     :   Optional[int]	,snake_case__     :   Union[str, Any]	,snake_case__     :   List[str]  ):




     '''simple docstring'''






     __snake_case			:Any          =						LongformerModel.from_pretrained(_A  )
     __snake_case			:Any          =						LightningModel(_A  )

     __snake_case			:Any          =						torch.load(_A	,map_location=torch.device("""cpu"""  )  )
     lightning_model.load_state_dict(ckpt["""state_dict"""]  )

     # init longformer question answering model
     __snake_case			:int          =						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__":
     lowerCamelCase__							=     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."""
     )
     lowerCamelCase__							=     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   UpperCamelCase  (       snake_case__     :   int = 8	,snake_case__     :   int | None = None  ):




     '''simple docstring'''






     __snake_case			:Tuple          =						np.random.default_rng(seed=snake_case__  )

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

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

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

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

     bbaa_circ.barrier()
     bbaa_circ.measure_all()

     # Simulate the quantum circuit.
     __snake_case			:Optional[int]          =						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			:List[Any]          =						qiskit.execute(snake_case__	,snake_case__	,shots=1	,seed_simulator=snake_case__  )
     # Returns the result of measurement.
     __snake_case			:Tuple          =						job.result().get_counts(snake_case__  ).most_frequent()

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

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


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

     testmod()


