def run_UBQC_sim(runtimes=1, num_bits=20, fibre_len=10**-9, noise_model=None, loss_init=0, loss_len=0): ns.sim_reset() # nodes==================================================================== nodeA = Node("Alice", port_names=["portQA_1", "portCA_1", "portCA_2"]) nodeB = Node("Bob", port_names=["portQB_1", "portCB_1", "portCB_2"]) # processors=============================================================== Alice_processor = sendableQProcessor( "processor_A", num_positions=5, mem_noise_models=None, phys_instructions=[ PhysicalInstruction(INSTR_INIT, duration=1, parallel=True), PhysicalInstruction(INSTR_X, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_CNOT, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_MEASURE, duration=1, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=1, parallel=True) ]) Bob_processor = sendableQProcessor( "processor_B", num_positions=100, mem_noise_models=None, phys_instructions=[ PhysicalInstruction(INSTR_INIT, duration=1, parallel=True), PhysicalInstruction(INSTR_X, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_CNOT, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_MEASURE, duration=1, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=1, parallel=True) ]) # fibres================================================================== MyQfiber = QuantumFibre("QFibre_A->B", length=fibre_len, quantum_loss_model=None, p_loss_init=loss_init, p_loss_length=loss_len) nodeA.connect_to(nodeB, MyQfiber, local_port_name=nodeA.ports["portQA_1"].name, remote_port_name=nodeB.ports["portQB_1"].name) MyCfiber = DirectConnection( "CFibreConn_B->A", ClassicalFibre("CFibre_B->A", length=fibre_len)) MyCfiber2 = DirectConnection( "CFibreConn_A->B", ClassicalFibre("CFibre_A->B", length=fibre_len)) nodeB.connect_to(nodeA, MyCfiber, local_port_name="portCB_1", remote_port_name="portCA_1") nodeA.connect_to(nodeB, MyCfiber2, local_port_name="portCA_2", remote_port_name="portCB_2") Alice = ClientProtocol(nodeA, Alice_processor, 2, 2) BOb = ServerProtocol(nodeB, Bob_processor, 2, 2) Alice.start() BOb.start() #ns.logger.setLevel(1) ns.sim_run()
def run_E91_sim(runtimes=1, num_bits=20, fibre_len=10**-9, noise_model=None, loss_init=0, loss_len=0): MyE91List_A = [] # local protocol list A MyE91List_B = [] # local protocol list B for i in range(runtimes): ns.sim_reset() # nodes==================================================================== nodeA = Node("Alice", port_names=["portQA_1", "portCA_1", "portCA_2"]) nodeB = Node("Bob", port_names=["portQB_1", "portCB_1", "portCB_2"]) # processors==================================================================== noise_model = None Alice_processor = sendableQProcessor( "processor_A", num_positions=100, mem_noise_models=None, phys_instructions=[ PhysicalInstruction(INSTR_INIT, duration=1, parallel=True), PhysicalInstruction(INSTR_X, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_CNOT, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_MEASURE, duration=1, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=1, parallel=True) ]) Bob_processor = sendableQProcessor( "processor_B", num_positions=100, mem_noise_models=None, phys_instructions=[ PhysicalInstruction(INSTR_INIT, duration=1, parallel=True), PhysicalInstruction(INSTR_X, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_CNOT, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_MEASURE, duration=1, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=1, parallel=True) ]) # fibres======================================================================= MyQfiber = QuantumFibre("QFibre_A->B", length=fibre_len, quantum_loss_model=None, p_loss_init=loss_init, p_loss_length=loss_len) nodeA.connect_to(nodeB, MyQfiber, local_port_name=nodeA.ports["portQA_1"].name, remote_port_name=nodeB.ports["portQB_1"].name) MyCfiber = DirectConnection( "CFibreConn_B->A", ClassicalFibre("CFibre_B->A", length=fibre_len)) MyCfiber2 = DirectConnection( "CFibreConn_A->B", ClassicalFibre("CFibre_A->B", length=fibre_len)) nodeB.connect_to(nodeA, MyCfiber, local_port_name="portCB_1", remote_port_name="portCA_1") nodeA.connect_to(nodeB, MyCfiber2, local_port_name="portCA_2", remote_port_name="portCB_2") Alice_protocol = AliceProtocol(nodeA, Alice_processor, num_bits) Bob_protocol = BobProtocol(nodeB, Bob_processor, num_bits) Alice_protocol.start() Bob_protocol.start() #ns.logger.setLevel(1) stats = ns.sim_run() MyE91List_A.append(Alice_protocol.key) MyE91List_B.append(Bob_protocol.key) return MyE91List_A, MyE91List_B
def run_E91_sim(runtimes=1,num_bits=20,fibre_len=10**-9,memNoiseMmodel=None,processorNoiseModel=None ,loss_init=0,loss_len=0,QChV=2.083*10**-4,CChV=2.083*10**-4): MyE91List_A=[] # local protocol list A MyE91List_B=[] # local protocol list B MyKeyRateList=[] A_originBasisList=[] B_measureBasisList=[] for i in range(runtimes): ns.sim_reset() # nodes==================================================================== nodeA = Node("Alice", port_names=["portQA_1","portCA_1","portCA_2"]) nodeB = Node("Bob" , port_names=["portQB_1","portCB_1","portCB_2"]) # processors=============================================================== #noise_model=None Alice_processor=QuantumProcessor("processor_A", num_positions=10**5, mem_noise_models=memNoiseMmodel, phys_instructions=[ PhysicalInstruction(INSTR_INIT, duration=1, parallel=True), PhysicalInstruction(INSTR_X, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_CNOT,duration=10,q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_MEASURE, duration=10,q_noise_model=processorNoiseModel, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=10,q_noise_model=processorNoiseModel, parallel=True)]) Bob_processor=QuantumProcessor("processor_B", num_positions=10**5, mem_noise_models=memNoiseMmodel, phys_instructions=[ PhysicalInstruction(INSTR_INIT, duration=1, parallel=True), PhysicalInstruction(INSTR_X, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_CNOT,duration=10,q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_MEASURE, duration=10,q_noise_model=processorNoiseModel, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=10,q_noise_model=processorNoiseModel, parallel=True)]) # channels================================================================== MyQChannel=QuantumChannel("QChannel_A->B",delay=10 ,length=fibre_len ,models={"quantum_loss_model": FibreLossModel(p_loss_init=loss_init,p_loss_length=loss_len, rng=None), "delay_model": FibreDelayModel(c=QChV)}) nodeA.connect_to(nodeB, MyQChannel, local_port_name =nodeA.ports["portQA_1"].name, remote_port_name=nodeB.ports["portQB_1"].name) MyCChannel = ClassicalChannel("CChannel_B->A",delay=0 ,length=fibre_len) MyCChannel2= ClassicalChannel("CChannel_A->B",delay=0 ,length=fibre_len) nodeB.connect_to(nodeA, MyCChannel, local_port_name="portCB_1", remote_port_name="portCA_1") nodeA.connect_to(nodeB, MyCChannel2, local_port_name="portCA_2", remote_port_name="portCB_2") startTime=ns.sim_time() #print("startTime:",startTime) Alice_protocol = AliceProtocol(nodeA,Alice_processor,num_bits) Bob_protocol = BobProtocol(nodeB,Bob_processor,num_bits) Bob_protocol.start() Alice_protocol.start() #ns.logger.setLevel(1) stats = ns.sim_run() ''' endTime=Bob_protocol.endTime #print("endTime:",endTime) MyE91List_A.append(Alice_protocol.key) MyE91List_B.append(Bob_protocol.key) #simple key length calibration s = SequenceMatcher(None, Alice_protocol.key, Bob_protocol.key)# unmatched rate MyKeyRateList.append((len(Bob_protocol.key)*(1-s.ratio()))*10**9/(endTime-startTime)) #second ''' MyE91List_A.append(Alice_protocol.key) MyE91List_B.append(Bob_protocol.key) A_originBasisList.append(Alice_protocol.basisList) B_measureBasisList.append(Bob_protocol.basisList) #return MyE91List_A, MyE91List_B, MyKeyRateList return MyE91List_A, MyE91List_B, A_originBasisList, B_measureBasisList
class BB84(Protocol): #BB84 functions=========================================================== def BB84_Alice_sendQubits(self): self.stateList, qlist = Create_random_qubits(self.num_bits) self.node_A.ports["portQA"].tx_output(qlist) def BB84_Bob_measure_send(self, qlist): # some qubits might be lost qlist = qlist.items if not isinstance( qlist[0], ns.qubits.qubit.Qubit): #case of not receiving qubits pass else: # B measuring B_basis, self.res_measure = Random_ZX_measure(self.num_bits, qlist) if B_basis[0] == 0 or B_basis[0] == 1 or B_basis[0] == 2: # B send measurement self.node_B.ports["portCB"].tx_output(B_basis) else: print("B measuring failed!!") print(B_basis[0]) def BB84_Alice_measure_send_keygen(self, opList): # A measuring matchList = Compare_measurement(self.num_bits, self.stateList, opList.items) #get opList from B # A return matchList to B self.node_A.ports["portCA"].tx_output(matchList) for i in matchList: self.key_A.append(self.stateList[i] % 2) #quantum state 0,+:0 1,-:1 return self.key_A def BB84_Bob_keygen(self, matchList): for i in matchList.items: self.key_B.append(self.res_measure[int(i)][0]) return self.key_B #control functions=========================================================== def __init__(self, num_bits=8, fibre_len=10**-6, fibre_loss_init=0.2, fibre_loss_length=0.25): #,fibreLossModel=default super().__init__() self.node_A = Node("A", ID=0, port_names=["portQA", "portCA"]) self.node_B = Node("B", ID=1, port_names=["portQB", "portCB"]) self.MyQfiber = None self.MyCfiber = None self.num_bits = num_bits self.stateList = None self.res_measure = None self.key_A = [] self.key_B = [] self.fiberLenth = fibre_len self.fibre_loss_init = fibre_loss_init self.fibre_loss_length = fibre_loss_length self.start() def stop(self): super().stop() self._running = False def is_connected(): super().is_connected() pass def start(self): super().start() # connect and connect quantum fibres self.MyQfiber = QuantumFibre("QFibre_A->B", length=self.fiberLenth, loss_model=FibreLossModel( p_loss_length=self.fibre_loss_length, p_loss_init=self.fibre_loss_init), depolar_rate=0) # create classical fibre self.MyCfiber = DirectConnection( "CFibreConn_A->B", ClassicalFibre("CFibre_A->B", length=self.fiberLenth), ClassicalFibre("CFibre_B->A", length=self.fiberLenth)) self.node_A.connect_to(self.node_B, self.MyQfiber, local_port_name="portQA", remote_port_name="portQB") self.node_B.connect_to(self.node_A, self.MyCfiber, local_port_name="portCB", remote_port_name="portCA") #set callback functions=================================================== self.node_B.ports["portQB"].bind_input_handler( self.BB84_Bob_measure_send) self.node_A.ports["portCA"].bind_input_handler( self.BB84_Alice_measure_send_keygen) self.node_B.ports["portCB"].bind_input_handler(self.BB84_Bob_keygen) # Alice starts====================================================== self.BB84_Alice_sendQubits()
class QuantumCheque(Protocol): # QuantumCheque function ========================================================= def BA_BB84_keygen(self, num_bits=8, fibre_len=10**-6, fibre_loss_init=0, fibre_loss_length=0): loc_BB84 = BB84(num_bits=num_bits, fibre_len=fibre_len, fibre_loss_init=fibre_loss_init, fibre_loss_length=fibre_loss_init) # assign keys to A and B self.key_BB84_A = loc_BB84.key_A self.key_BB84_B = loc_BB84.key_B # trigger A's one way function self.node_A.ports["portC_A1"].tx_output('Alice') def B_send_GHZ(self, username): if username.items[0] == 'Alice': self.BChequeBook, tmpAChequeBook, tmpA2ChequeBook = Create_GHZ_triple_qubits_list( self.num_bits) tmpAChequeBook.extend(tmpA2ChequeBook) self.node_B.ports["portQ_B1"].tx_output(tmpAChequeBook) else: print("User name ERROR!") def A_req_USN(self, tmpAChequeBook): self.A1ChequeBook = tmpAChequeBook.items[:self.num_bits] self.A2ChequeBook = tmpAChequeBook.items[self.num_bits:] self.node_A.ports["portC_A2"].tx_output("USN_req") def B_USN_send_A(self, req): self.B_saveUSN = UniqueSerialNumGen( self.num_bits, 0, 1000) #min max value flexible (doesn't matter actually) self.node_B.ports["portC_B3"].tx_output(self.B_saveUSN) def A_owf_send_C(self, saveUSN): self.A_saveUSN = saveUSN.items if len(self.A1ChequeBook) < self.num_bits: print("No more cheque! Aborting!") return 0 else: for i in range(self.num_bits): # Alice write down the amound of money res_owf_qubit = OneWayFunction('Alice', self.key_BB84_A, self.A_saveUSN[i], self.Money) # Alice performs Bell state measurement operate([res_owf_qubit, self.A1ChequeBook[i]], ops.CNOT) H | res_owf_qubit mes_A1 = ns.qubits.qubitapi.measure(self.A1ChequeBook[i], observable=Z) mes_owf_qubit = ns.qubits.qubitapi.measure(res_owf_qubit, observable=Z) if mes_A1[0] == 0 and mes_owf_qubit[0] == 1: Z | self.A2ChequeBook[i] if mes_A1[0] == 1 and mes_owf_qubit[0] == 0: X | self.A2ChequeBook[i] if mes_A1[0] == 1 and mes_owf_qubit[0] == 1: Y | self.A2ChequeBook[i] self.node_A.ports["portQ_A2"].tx_output(self.A2ChequeBook) # C receives qubits from A then wait and send to B def C_rec_wait_send_B(self, chequeQList): chequeQList = chequeQList.items self.C_Qmemory.put(chequeQList) # wait for some time before summit to bank My_waitENVtype = EventType("WAIT_EVENT", "Wait for N nanoseconds") self._schedule_after(self.C_delay, My_waitENVtype) self._wait_once(ns.EventHandler(self.CpopMem), entity=self, event_type=My_waitENVtype) # can't add event_type # pop qubits from qMemory def CpopMem(self, event): # pop out from qmem sening_qList = [] sening_qList = self.C_Qmemory.pop(list(np.arange( self.num_bits))) #pop all self.node_C.ports["portQ_C2"].tx_output(sening_qList) # B varifies the list given by C def CB_Verify(self, chequeQList): chequeQList = chequeQList.items # error correction for i in range(self.num_bits): H | self.BChequeBook[i] bob_measurement = ns.qubits.qubitapi.measure(self.BChequeBook[i], observable=Z) if bob_measurement[0] == 1: Z | chequeQList[i] # For experiment test_var = 'n' # input("Use a fake check? (y/n)") # To use a fake cheque or not res_closeness = [] for i in range(self.num_bits): if test_var == 'n': owf_bank_state = OneWayFunction('Bob', self.key_BB84_B, self.B_saveUSN[i], self.Money) elif (test_var == 'y'): owf_bank_state = OneWayFunction('Bob', [1, 0, 1, 1, 0, 1], 0, 20) # try any value res_closeness.append(SwapTest(owf_bank_state, chequeQList[i])) sum = 0 for i in range(len(res_closeness)): if int(res_closeness[i][0]) == 0: sum += res_closeness[i][1] else: sum += 1 - res_closeness[i][1] self.chequeCloseness = sum / len( res_closeness) # get average closeness value if self.chequeCloseness > self.Threshold: # pass self.Varify = True #print('Verified! Cheque Accepted!') else: # fail self.Varify = False #print('FAILED! ABORTING!' ) # base function ======================================================== def __init__(self, num_bits=8, fibre_len_AC=10**-6, fibre_len_AB=10**-6, fibre_len_BC=10**-6, Money=1234, depolar_rate=0, time_independent=False, Threshold=0.99, C_delay=0): super().__init__() self.node_A = Node("A", ID=0, port_names=[ "portQ_A1", "portC_A1", "portC_A2", "portQ_A2", "portC_A3" ]) self.node_B = Node("B", ID=1, port_names=[ "portQ_B1", "portC_B1", "portC_B2", "portQ_B2", "portC_B3" ]) self.node_C = Node("C", ID=2, port_names=["portQ_C1", "portQ_C2"]) self.C_Qmemory = QuantumMemory( "C_QMemory", num_bits, ID=0, memory_noise_models=DepolarNoiseModel( depolar_rate=depolar_rate, time_independent=time_independent) ) #normally time_independent is False unless using probability self.C_delay = C_delay self.MyQfiber_AC = None self.MyQfiber_AB = None self.MyQfiber_BC = None self.MyCfiber_AB = None self.MyCfiber_BA = None self.fiberLenth_AC = fibre_len_AC self.fiberLenth_AB = fibre_len_AB self.fiberLenth_BC = fibre_len_BC self.num_bits = num_bits self.A1ChequeBook = [] self.A2ChequeBook = [] self.BChequeBook = [] self.A_saveUSN = [] #unique serial number stored in A self.B_saveUSN = [] #unique serial number stored in B self.key_BB84_A = [] self.key_BB84_B = [] self.Money = Money self.Threshold = Threshold #Threshold of closeness about whether to accept this cheque self.chequeCloseness = 0 self.Varify = False # True = cheque accepted, False = cheque denied self.start() def stop(self): super().stop() self._running = False def is_connected(): super().is_connected() pass def start(self): super().start() self.MyCfiber_AB = DirectConnection( "CFibreConn_AB", ClassicalFibre("CFibre_A->B", length=self.fiberLenth_AB), ClassicalFibre("CFibre_B->A", length=self.fiberLenth_AB)) self.MyCfiber_AB2 = DirectConnection( "CFibreConn_AB2", ClassicalFibre("CFibre_A->B", length=self.fiberLenth_AB), ClassicalFibre("CFibre_B->A", length=self.fiberLenth_AB)) self.MyCfiber_BA = DirectConnection( "CFibreConn_BA", ClassicalFibre("CFibre_A->B", length=self.fiberLenth_AB), ClassicalFibre("CFibre_B->A", length=self.fiberLenth_AB)) self.MyQfiber_AC = QuantumFibre("MyQFibre_AC", length=self.fiberLenth_AC, loss_model=None) #"default" self.MyQfiber_AB = QuantumFibre("MyQFibre_AB", length=self.fiberLenth_AB, loss_model=None) self.MyQfiber_BC = QuantumFibre("MyQFibre_BC", length=self.fiberLenth_BC, loss_model=None) self.node_A.connect_to(self.node_B, self.MyCfiber_AB, local_port_name="portC_A1", remote_port_name="portC_B1") self.node_B.connect_to(self.node_A, self.MyQfiber_AB, local_port_name="portQ_B1", remote_port_name="portQ_A1") self.node_A.connect_to(self.node_B, self.MyCfiber_AB2, local_port_name="portC_A2", remote_port_name="portC_B2") self.node_B.connect_to(self.node_A, self.MyCfiber_BA, local_port_name="portC_B3", remote_port_name="portC_A3") self.node_A.connect_to(self.node_C, self.MyQfiber_AC, local_port_name="portQ_A2", remote_port_name="portQ_C1") self.node_C.connect_to(self.node_B, self.MyQfiber_BC, local_port_name="portQ_C2", remote_port_name="portQ_B2") # set callback functions=================================================== self.node_B.ports["portC_B1"].bind_input_handler(self.B_send_GHZ) self.node_A.ports["portQ_A1"].bind_input_handler(self.A_req_USN) self.node_B.ports["portC_B2"].bind_input_handler(self.B_USN_send_A) self.node_A.ports["portC_A3"].bind_input_handler(self.A_owf_send_C) self.node_C.ports["portQ_C1"].bind_input_handler( self.C_rec_wait_send_B) self.node_B.ports["portQ_B2"].bind_input_handler(self.CB_Verify) # Start by BB84 self.BA_BB84_keygen()
def run_UBQC_sim(runtimes=1, fibre_len=10**-9, processorNoiseModel=None, memNoiseMmodel=None, loss_init=0.25, loss_len=0.2, QChV=3 * 10**2, CChV=3 * 10**2): resList = [] successCount = 0 set_qstate_formalism(QFormalism.DM) for i in range(runtimes): ns.sim_reset() # nodes==================================================================== nodeServer = Node("Server", port_names=["portQS_1", "portCS_1", "portCS_2"]) nodeClient = Node("Client", port_names=["portQC_1", "portCC_1", "portCC_2"]) # processors=============================================================== processorServer = QuantumProcessor( "processorServer", num_positions=10, mem_noise_models=memNoiseMmodel, phys_instructions=[ PhysicalInstruction(INSTR_X, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_CNOT, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_CZ, duration=10, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_MEASURE, duration=10, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=10, parallel=True), PhysicalInstruction(INSTR_R22, duration=1, parallel=True), PhysicalInstruction(INSTR_R45, duration=1, parallel=True), PhysicalInstruction(INSTR_R67, duration=1, parallel=True), PhysicalInstruction(INSTR_R90, duration=1, parallel=True), PhysicalInstruction(INSTR_R112, duration=1, parallel=True), PhysicalInstruction(INSTR_R135, duration=1, parallel=True), PhysicalInstruction(INSTR_R157, duration=1, parallel=True), PhysicalInstruction(INSTR_R180, duration=1, parallel=True), PhysicalInstruction(INSTR_R202, duration=1, parallel=True), PhysicalInstruction(INSTR_R225, duration=1, parallel=True), PhysicalInstruction(INSTR_R247, duration=1, parallel=True), PhysicalInstruction(INSTR_R270, duration=1, parallel=True), PhysicalInstruction(INSTR_R292, duration=1, parallel=True), PhysicalInstruction(INSTR_R315, duration=1, parallel=True), PhysicalInstruction(INSTR_R337, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv22, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv45, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv67, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv90, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv112, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv135, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv157, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv180, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv202, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv225, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv247, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv270, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv292, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv315, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv337, duration=1, parallel=True), PhysicalInstruction(INSTR_SWAP, duration=1, parallel=True) ]) processorClient = QuantumProcessor( "processorClient", num_positions=10, mem_noise_models=memNoiseMmodel, phys_instructions=[ PhysicalInstruction(INSTR_X, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_CNOT, duration=10, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_MEASURE, duration=10, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=10, parallel=True), PhysicalInstruction(INSTR_R22, duration=1, parallel=True), PhysicalInstruction(INSTR_R45, duration=1, parallel=True), PhysicalInstruction(INSTR_R67, duration=1, parallel=True), PhysicalInstruction(INSTR_R90, duration=1, parallel=True), PhysicalInstruction(INSTR_R112, duration=1, parallel=True), PhysicalInstruction(INSTR_R135, duration=1, parallel=True), PhysicalInstruction(INSTR_R157, duration=1, parallel=True), PhysicalInstruction(INSTR_R180, duration=1, parallel=True), PhysicalInstruction(INSTR_R202, duration=1, parallel=True), PhysicalInstruction(INSTR_R225, duration=1, parallel=True), PhysicalInstruction(INSTR_R247, duration=1, parallel=True), PhysicalInstruction(INSTR_R270, duration=1, parallel=True), PhysicalInstruction(INSTR_R292, duration=1, parallel=True), PhysicalInstruction(INSTR_R315, duration=1, parallel=True), PhysicalInstruction(INSTR_R337, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv22, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv45, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv67, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv90, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv112, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv135, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv157, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv180, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv202, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv225, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv247, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv270, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv292, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv315, duration=1, parallel=True), PhysicalInstruction(INSTR_Rv337, duration=1, parallel=True) ]) # channels================================================================== MyQChannel = QuantumChannel("QChannel_S->C", delay=0, length=fibre_len, models={ "quantum_loss_model": FibreLossModel(p_loss_init=loss_init, p_loss_length=loss_len, rng=None), "delay_model": FibreDelayModel(c=QChV) }) nodeServer.connect_to( nodeClient, MyQChannel, local_port_name=nodeServer.ports["portQS_1"].name, remote_port_name=nodeClient.ports["portQC_1"].name) MyCChannel = ClassicalChannel("CChannel_C->S", delay=0, length=fibre_len) MyCChannel2 = ClassicalChannel("CChannel_S->C", delay=0, length=fibre_len) nodeClient.connect_to(nodeServer, MyCChannel, local_port_name="portCC_1", remote_port_name="portCS_1") nodeServer.connect_to(nodeClient, MyCChannel2, local_port_name="portCS_2", remote_port_name="portCC_2") protocolServer = ProtocolServer(nodeServer, processorServer) protocolClient = ProtocolClient(nodeClient, processorClient, 1) protocolServer.start() protocolClient.start() #ns.logger.setLevel(1) stats = ns.sim_run() resList.append(protocolClient.verified) for i in resList: if i == True: successCount += 1 return successCount / runtimes
def run_QToken_sim(runTimes=1, num_bits=100, fibre_len=0, waitTime=1, processNoiseModel=None, memNoiseModel=None, loss_init=0, loss_len=0, threshold=0.854, fibreLoss_init=0.2, fibreLoss_len=0.25, QChDelay=1, CChDelay=0): resList = [] for i in range(runTimes): ns.sim_reset() # nodes==================================================================== nodeA = Node("Alice", port_names=["portQA_1", "portCA_1", "portCA_2"]) nodeB = Node("Bob", port_names=["portQB_1", "portCB_1", "portCB_2"]) # processors=============================================================== #noise_model=None Alice_processor = QuantumProcessor( "processor_A", num_positions=3 * 10**3, mem_noise_models=memNoiseModel, phys_instructions=[ PhysicalInstruction(INSTR_X, duration=1, q_noise_model=processNoiseModel), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=processNoiseModel), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=processNoiseModel), PhysicalInstruction(INSTR_CNOT, duration=10, q_noise_model=processNoiseModel), PhysicalInstruction(INSTR_MEASURE, duration=10, q_noise_model=processNoiseModel, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=10, q_noise_model=processNoiseModel, parallel=True) ]) Bob_processor = QuantumProcessor( "processor_B", num_positions=3 * 10**3, mem_noise_models=memNoiseModel, phys_instructions=[ PhysicalInstruction(INSTR_X, duration=1, q_noise_model=processNoiseModel), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=processNoiseModel), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=processNoiseModel), PhysicalInstruction(INSTR_CNOT, duration=10, q_noise_model=processNoiseModel), PhysicalInstruction(INSTR_MEASURE, duration=10, q_noise_model=processNoiseModel, parallel=True), PhysicalInstruction(INSTR_MEASURE_X, duration=10, q_noise_model=processNoiseModel, parallel=True) ]) # channels================================================================== MyQChannel = QuantumChannel("QChannel_B->A", delay=QChDelay, length=fibre_len, models={ "myFibreLossModel": FibreLossModel( p_loss_init=fibreLoss_init, p_loss_length=fibreLoss_len, rng=None) }) nodeB.connect_to(nodeA, MyQChannel, local_port_name=nodeB.ports["portQB_1"].name, remote_port_name=nodeA.ports["portQA_1"].name) MyCChannel = ClassicalChannel("CChannel_A->B", delay=CChDelay, length=fibre_len) MyCChannel2 = ClassicalChannel("CChannel_B->A", delay=CChDelay, length=fibre_len) nodeA.connect_to(nodeB, MyCChannel, local_port_name="portCA_1", remote_port_name="portCB_1") nodeB.connect_to(nodeA, MyCChannel2, local_port_name="portCB_2", remote_port_name="portCA_2") Alice_protocol = AliceProtocol(nodeA, Alice_processor, num_bits, waitTime=waitTime) Bob_protocol = BobProtocol(nodeB, Bob_processor, num_bits, threshold=threshold) Bob_protocol.start() Alice_protocol.start() #ns.logger.setLevel(1) stats = ns.sim_run() resList.append(Bob_protocol.successfulRate) #print("Bob_protocol.successfulRate:",Bob_protocol.successfulRate) return sum(resList) / len(resList)
def run_AT_sim(numNodes=4,fibre_len=10**-9,processorNoiseModel=None,memNoiseMmodel=None,loss_init=0,loss_len=0 ,QChV=3*10**-4): # initialize ns.sim_reset() sideProcessorList=[] sideNodeList=[] centerPortList=[] channelList=[] senderID=randint(0,numNodes-1) receiverID=randint(0,numNodes-1) while receiverID==senderID: receiverID=randint(0,numNodes-1) # build star network hardware components ## create side components for i in range(numNodes): ### processors================================================================ sideProcessorList.append(createProcessorAT(name="ProcessorSide_"+str(i))) ### nodes===================================================================== sideNodeList.append(Node("node_"+str(i), port_names=["portQside"])) ### channels================================================================== channelList.append(QuantumChannel("QChannel_Center->Side_"+str(i),delay=10,length=fibre_len ,models={"quantum_loss_model": FibreLossModel(p_loss_init=loss_init,p_loss_length=loss_len, rng=None), "delay_model": FibreDelayModel(c=QChV)})) ### record port list for center node centerPortList.append("PortQCenter_"+str(i)) ## create center component CenterNode=Node("CenterNode", port_names=centerPortList) CenterProcessor=createProcessorAT(name="ProcessorCenter") ## connect================================================================== for i in range(numNodes): CenterNode.connect_to(sideNodeList[i], channelList[i], local_port_name =CenterNode.ports["PortQCenter_"+str(i)].name, remote_port_name=sideNodeList[i].ports["portQside"].name) # create protocol object myProtocol_center = AT_Wstate_center(CenterNode,CenterProcessor,numNodes,portQlist=centerPortList) myProtocol_sideList=[] ## create side protocol for i in range(numNodes): if i==senderID: # create sender myProtocol_sideList.append(AT_Wstate_side(sideNodeList[i],sideProcessorList[i], sender=True)) elif i==receiverID: # create receiver myProtocol_sideList.append(AT_Wstate_side(sideNodeList[i],sideProcessorList[i],receiver=True)) else: # create normal side node myProtocol_sideList.append(AT_Wstate_side(sideNodeList[i],sideProcessorList[i])) for sideProtocols in myProtocol_sideList: sideProtocols.start() myProtocol_center.start() #ns.logger.setLevel(1) stats = ns.sim_run()
class E91(Protocol): def E91_A_sendHalf_EPR(self): qListA, qlistB = Create_multiEPR(self.num_bits) self.qListA = qListA self.node_A.ports["portQA"].tx_output(qlistB) def E91_B_randMeas(self, qListB): self.qListB = qListB.items #print(qListB) self.basisList_B, self.res_measure_B = Random_ZX_measure( self.num_bits, self.qListB) #print(self.basisList_B) self.node_B.ports["portCB_1"].tx_output(self.basisList_B) def E91_A_compare_keyGen(self, basisList_fromB): self.basisList_A, self.res_measure_A = Random_ZX_measure( self.num_bits, self.qListA) self.node_A.ports["portCA_2"].tx_output(self.basisList_A) #print(self.basisList_A) #print(basisList_fromB.items) self.key_A = Compare_basis(self.basisList_A, basisList_fromB.items, self.res_measure_A) self.key_A = ''.join(map(str, self.key_A)) print(self.key_A) # show results def E91_B_compare_keyGen(self, basisList_fromA): self.key_B = Compare_basis(self.basisList_B, basisList_fromA.items, self.res_measure_B) self.key_B = ''.join(map(str, self.key_B)) print(self.key_B) # show results #control functions=========================================================== def __init__(self, num_bits=8, fibre_len=10**-6): super().__init__() self.node_A = Node("A", ID=0, port_names=["portQA", "portCA_1", "portCA_2"]) self.node_B = Node("B", ID=1, port_names=["portQB", "portCB_1", "portCB_2"]) self.MyQfiber = None self.MyCfiber = None self.num_bits = num_bits self.qListA = [] self.qListB = [] self.basisList_A = [] self.basisList_B = [] self.res_measure_A = [] self.res_measure_B = [] self.key_A = [] self.key_B = [] self.fiberLenth = fibre_len self.start() def stop(self): super().stop() self._running = False def is_connected(): super().is_connected() pass def start(self): super().start() # connect and connect quantum fibres self.MyQfiber = QuantumFibre("QFibre_A->B", length=self.fiberLenth) # create classical fibre self.MyCfiber = DirectConnection( "CFibreConn_B->A", ClassicalFibre("CFibre_B->A", length=self.fiberLenth)) self.MyCfiber2 = DirectConnection( "CFibreConn_A->B", ClassicalFibre("CFibre_A->B", length=self.fiberLenth)) self.node_A.connect_to(self.node_B, self.MyQfiber, local_port_name="portQA", remote_port_name="portQB") self.node_B.connect_to(self.node_A, self.MyCfiber, local_port_name="portCB_1", remote_port_name="portCA_1") self.node_A.connect_to(self.node_B, self.MyCfiber2, local_port_name="portCA_2", remote_port_name="portCB_2") #set callback functions=================================================== self.node_B.ports["portQB"].bind_input_handler(self.E91_B_randMeas) self.node_A.ports["portCA_1"].bind_input_handler( self.E91_A_compare_keyGen) self.node_B.ports["portCB_2"].bind_input_handler( self.E91_B_compare_keyGen) # Alice starts====================================================== self.E91_A_sendHalf_EPR()
def run_E91_sim(runtimes=1, num_bits=20, fibre_len=10**-9, noise_model=None): MyE91List_A = [] # local protocol list A MyE91List_B = [] # local protocol list B for i in range(runtimes): ns.sim_reset() # Hardware configuration Alice_processor = sendableQProcessor( "processor_A", num_positions=100, mem_noise_models=None, phys_instructions=[ PhysicalInstruction(INSTR_INIT, duration=1, parallel=True), PhysicalInstruction(INSTR_X, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_CNOT, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_MEASURE, duration=1, parallel=True) ], topologies=[None, None, None, None, None, None]) #mem_noise_models=[DepolarNoiseModel(0)] * 100 Bob_processor = sendableQProcessor( "processor_B", num_positions=100, mem_noise_models=None, phys_instructions=[ PhysicalInstruction(INSTR_INIT, duration=1, parallel=True), PhysicalInstruction(INSTR_X, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_CNOT, duration=1, q_noise_model=noise_model), PhysicalInstruction(INSTR_MEASURE, duration=1, parallel=True) ], topologies=[None, None, None, None, None, None]) node_A = Node("A", ID=0, port_names=["portQA_1", "portCA_1", "portCA_2"]) node_B = Node("B", ID=1, port_names=["portQB_1", "portCB_1", "portCB_2"]) # connection MyQfiber = QuantumFibre("QFibre_A->B", length=fibre_len, quantum_loss_model=None) # default value: # p_loss_init=0.2, p_loss_length=0.25,depolar_rate=0, c=200000, models=None MyCfiber = DirectConnection( "CFibreConn_B->A", ClassicalFibre("CFibre_B->A", length=fibre_len)) MyCfiber2 = DirectConnection( "CFibreConn_A->B", ClassicalFibre("CFibre_A->B", length=fibre_len)) node_A.connect_to(node_B, MyQfiber, local_port_name=node_A.ports["portQA_1"].name, remote_port_name=node_B.ports["portQB_1"].name) node_B.connect_to(node_A, MyCfiber, local_port_name="portCB_1", remote_port_name="portCA_1") node_A.connect_to(node_B, MyCfiber2, local_port_name="portCA_2", remote_port_name="portCB_2") Alice = E91_A(node=node_A, processor=Alice_processor, num_bits=num_bits, portNameQ1="portQA_1", portNameC1="portCA_1", portNameC2="portCA_2") Bob = E91_B(node=node_B, processor=Bob_processor, num_bits=num_bits, portNameQ1="portQB_1", portNameC1="portCB_1", portNameC2="portCB_2") for i in Alice.A_prepare_qubits(): pass ns.sim_run() MyE91List_A.append(Alice.key) MyE91List_B.append(Bob.key) return MyE91List_A, MyE91List_B
def run_Teleport_sim(runtimes=1, fibre_len=10**-9, memNoiseMmodel=None, processorNoiseModel=None, loss_init=0, loss_len=0, QChV=3 * 10**-4, CChV=3 * 10**-4): for i in range(runtimes): ns.sim_reset() # nodes==================================================================== nodeSender = Node("SenderNode", port_names=["portC_Sender"]) nodeReceiver = Node("ReceiverNode", port_names=["portC_Receiver"]) # processors=============================================================== processorSender = QuantumProcessor( "processorSender", num_positions=10, mem_noise_models=memNoiseMmodel, phys_instructions=[ PhysicalInstruction(INSTR_X, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_CNOT, duration=10, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_MEASURE, duration=10, q_noise_model=processorNoiseModel, parallel=False) ]) processorReceiver = QuantumProcessor( "processorReceiver", num_positions=10, mem_noise_models=memNoiseMmodel, phys_instructions=[ PhysicalInstruction(INSTR_X, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_Z, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_H, duration=1, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_CNOT, duration=10, q_noise_model=processorNoiseModel), PhysicalInstruction(INSTR_MEASURE, duration=10, q_noise_model=processorNoiseModel, parallel=False) ]) # channels================================================================== MyCChannel = ClassicalChannel("CChannel_S->R", delay=0, length=fibre_len) nodeSender.connect_to(nodeReceiver, MyCChannel, local_port_name="portC_Sender", remote_port_name="portC_Receiver") # make an EPR pair and origin state oriQubit, epr1, epr2 = create_qubits(3) operate(epr1, H) operate([epr1, epr2], CNOT) # make oriQubit operate(oriQubit, H) myQT_Sender = QuantumTeleportationSender(node=nodeSender, processor=processorSender, SendQubit=oriQubit, EPR_1=epr1) myQT_Receiver = QuantumTeleportationReceiver( node=nodeReceiver, processor=processorReceiver, EPR_2=epr2) myQT_Receiver.start() myQT_Sender.start() #ns.logger.setLevel(1) stats = ns.sim_run() return 0