def test_decodeStateBB84_probabilistic(): # Test probabilistic measurement is roughly even numTrials = 10000 tolerance = 0.1 * numTrials q = qit.state('0') counts = [0, 0] for j in range(numTrials): counts[bb84.decodeState(q, 1)] += 1 assert abs(counts[0] - counts[1]) < tolerance q = q.u_propagate(qit.H) counts = [0, 0] for j in range(numTrials): counts[bb84.decodeState(q, 0)] += 1 assert (abs(counts[0] - counts[1]) < tolerance) q = qit.state('1') counts = [0, 0] for j in range(numTrials): counts[bb84.decodeState(q, 1)] += 1 assert (abs(counts[0] - counts[1]) < tolerance) q = q.u_propagate(qit.H) counts = [0, 0] for j in range(numTrials): counts[bb84.decodeState(q, 0)] += 1 assert (abs(counts[0] - counts[1]) < tolerance)
def test_encodeBitBB84(): # Only test the 4 cases in our encoding strategy assert (util.equivState(bb84.encodeBit(0, 0), qit.state('0'))) assert (util.equivState(bb84.encodeBit(1, 0), qit.state('1'))) assert (util.equivState(bb84.encodeBit(0, 1), qit.state('0').u_propagate(qit.H))) assert (util.equivState(bb84.encodeBit(1, 1), qit.state('1').u_propagate(qit.H)))
def flipState(state): """Perform the transformation corresponding to a bit flip on the given quantum state and return it. """ if util.equivState(state, qit.state('0')) or util.equivState( state, qit.state('1')): return state.u_propagate(qit.sx) else: return state.u_propagate(qit.sz)
def grover_u_s(n_qubits): plus_state = (qit.state('0') + qit.state('1')) / np.sqrt(2) state = (qit.state('0') + qit.state('1')) / np.sqrt(2) for i in range(1, n_qubits): state = qit.state.tensor(state, plus_state) I = qit.tensor(qit.lmap(np.eye(2))) for i in range(1, n_qubits): I = qit.tensor(I, qit.lmap(np.eye(2))) return -1 * I + 2 * qit.state.projector(state)
def flipState(state): """ |Z+> <-> |Z-> |X+> <-> |X-> """ if tools.equals(state, qit.state('0')) or tools.equals( state, qit.state('1')): return state.u_propagate(qit.sx) else: return state.u_propagate(qit.sz)
def test_decodeStateBB84_deterministic(): # Test deterministic cases q = qit.state('0') assert (bb84.decodeState(q, 0) == False) q = q.u_propagate(qit.H) assert (bb84.decodeState(q, 1) == False) q = qit.state('1') assert (bb84.decodeState(q, 0) == True) q = q.u_propagate(qit.H) assert (bb84.decodeState(q, 1) == True)
def test_decodeStateB92(): # Test probabilistic results match our expectations: # If sent == basis, measure the value of sent 50% of the time # If sent not measured, nothing comes through the filter numTrials = 50 numBits = 1024 sent = util.getRandomBits(numBits) bases = util.getRandomBits(numBits) tolerance = 0.05 for j in range(numTrials): seen = 0 for k in range(numBits): q = qit.state('0') if sent[k]: q = q.u_propagate(qit.H) result = b92.decodeState(q, bases[k]) if result != None: seen += 1 assert (result == sent[k]) # Expect to get ~1/4 of the original key material print(float(seen) / numBits) assert (abs(float(seen) / numBits - 0.25) < tolerance)
def calc_n_p_allequal(n_qbits, p_steps): n = n_qbits number_iterations = p_steps qaoa_approximation = [] grover_approximation = [] param_array = [] p_array = [] state = qit.state('000000000') params = 0.5 * np.pi for i in range(1, number_iterations): params = get_params_all(target_state=state, p=i, n_qubits=n, params_0=params) state_2 = F_function_all(target_state=state, params=params, p=i, n_qubits=n)[1] qaoa_approximation.append(qit.state.fidelity(state, state_2)**2) param_array.append(params) grover_approximation.append( GROVER(iterations=i, state=state, n_qubits=n)) p_array.append(i) print('Iteration ', i) print('angle', params) #params.extend([0,0]) np.save('qaoa_data', qaoa_approximation) np.save('grover_data', grover_approximation) np.save('p_data', p_array) np.save('angle_data_allequal', param_array)
def encodeBit(value): """Return the quantum state representing the B92 encoding of the given binary value.""" q = qit.state('0') if value: return q.u_propagate(qit.H) else: return q
def encodeBit(value, basis): """Return the quantum state representing the encoding of the given binary value in the given basis.""" q = qit.state('0') if value: q = q.u_propagate(qit.sx) # Apply Pauli X operator to flip the qubit if basis: q = q.u_propagate(qit.H) # Apply Hadamard operator to change basis return q
def encodeBit(bit, basis): q = qit.state('0') if bit == 1: #Pauli-X gate maps |0> to |1> and |1> to |0> q = q.u_propagate(qit.sx) if basis == 'X': #Hadamard gate maps |0> to 1/sqrt(2) [|0> + |1>] and |1> to 1/sqrt(2) [|0> - |1>] q = q.u_propagate(qit.H) return q
def Just_Grover(n_qbits, p_steps): n = n_qbits number_iterations = p_steps grover_approximation = [] p_array = [] string = '0' * n_qbits state = qit.state(string) for i in range(1, number_iterations): grover_approximation.append( GROVER(iterations=i, state=state, n_qubits=n)) print(grover_approximation[-1]) p_array.append(i) print('Iteration ', i) print('Grover probs') print(grover_approximation) print('n steps') print(p_array) plt.plot(p_array, grover_approximation, label='grover') plt.show()
def decodeState(state, basis): """Return a bool corresponding to the result of measuring the given state using one of two filters. If basis=0, the filter will pass antidiagonal photons and absorb diagonal photons. If basis=1, the filter will pass horizontal photons and absorb vertical photons. This corresponds to measuring the correct result 1/4 of the time, otherwise measuring nothing. """ # Save the original bit Alice sent aliceBit = True if util.equivState(state, qit.state('0')): aliceBit = False # Apply chosen filter if basis == 0: state = state.u_propagate(qit.H) _, result = state.measure() if result: return aliceBit else: return None
def initial_state(n_qubits): plus_state = (qit.state('0') + qit.state('1')) / np.sqrt(2) state = (qit.state('0') + qit.state('1')) / np.sqrt(2) for i in range(1, n_qubits): state = qit.state.tensor(state, plus_state) return state
def B_operator_all(n_qubits): plus_state = (qit.state('0') + qit.state('1')) / np.sqrt(2) state = (qit.state('0') + qit.state('1')) / np.sqrt(2) for i in range(1, n_qubits): state = qit.state.tensor(state, plus_state) return -1 * qit.state.projector(state)