importloggingimportnumpyasnpimportnetsquid.qubits.ketstatesasksimportnetsquid.qubits.qubitapiasqapi# Flush all messages from a port
[docs]defflush_port(port):""" Receive and drop all messages in a port's queue Parameters ---------- port : netsquid.components.component.Port The port to be flushed """whileport.input_queue:_=port.rx_input()
[docs]defloss_prob(decibels):""" Convert signal loss in decibels (dB) to a probability. Parameters ---------- decibels : float Signal loss in decibels. Returns ------- float Probability of signal loss (value between 0 and 1). """return1-pow(10,-(decibels/10))
# ==== FIDELITY & DISTILLATION CALCULATION UTILITIES ====# Get two qubits at positions 0 for alice and bob and calculate their fidelities
[docs]defget_fidelities(alice,bob,qid_1=1,qid_2=1):""" Calculate the fidelities of entangled qubits for Alice and Bob. Parameters ---------- alice : QPUNode The QPU entity representing Alice. bob : QPUNode The QPU entity representing Bob. Returns ------- tuple A tuple containing: - status (bool): True if both Alice and Bob have valid qubits, False otherwise. - fidelity (float): Fidelity of the Bell state |B00>. """[qubit0]=alice.processor.peek(qid_1,skip_noise=True)[qubit1]=bob.processor.peek(qid_2,skip_noise=True)fidelity=(qapi.fidelity([qubit0,qubit1],ks.b00,squared=True),)logging.debug(f"[FIDELITY_STATS] Simulation output: {fidelity}")returnfidelity
# Function to calculate fidelity after entanglement distillation
[docs]defanalytical_distilled_fidelity(fidelity,n):""" Calculate the upper bound of fidelity after entanglement distillation. Parameters ---------- fidelity : float Initial fidelity of a single qubit. n : int Number of qubits used for entanglement distillation. Returns ------- float Fidelity after entanglement distillation. """ifn==1:returnfidelitynumerator=pow(fidelity,n)denominator=numerator+pow(1-fidelity,n)# Avoid division by zeroifdenominator==0:return0.0returnnumerator/denominator
[docs]deffind_minimum_ebits(fidelity,target_fidelity):""" Calculate the minimum number of ebits required to reach the target fidelity. Parameters ---------- fidelity : float Initial fidelity of a single qubit (must be > 0.5). target_fidelity : float Target fidelity to achieve (must be > fidelity). Returns ------- float Minimum number of ebits required. """iffidelity<=0.5:returnnp.inf# Impossible to reach target fidelityeliftarget_fidelity<=fidelity:return1forninrange(100):ifanalytical_distilled_fidelity(fidelity,n)>=target_fidelity:returnnreturnnp.inf
[docs]deftime_to_fidelity(success_probability,time_to_ebit,distillation_ebits):""" Calculate the time (in nanoseconds) needed to get the distillation ebit fidelity. This is based on analytical estimations of distillation efficiency. Parameters ---------- success_probability : float Probability of distillation success, between 0 and 1. time_to_ebit : float Average time (in nanoseconds) needed to establish an entangled bit distillation_ebits : int Number of entangled bits used to perform distillation in parallel Returns ------- float Time estimation in nanoseconds """return(distillation_ebits*time_to_ebit)/success_probability