re-formatting
This commit is contained in:
parent
1a1c3307b0
commit
9b31296aed
59
04_qkd_2.py
59
04_qkd_2.py
@ -52,6 +52,32 @@ def measure(qbit):
|
|||||||
return random.choices([0, 1], measure_probability(qbit))[0]
|
return random.choices([0, 1], measure_probability(qbit))[0]
|
||||||
|
|
||||||
|
|
||||||
|
def run_qbit_tests():
|
||||||
|
# asserts are sets of tests to check if mathz workz
|
||||||
|
|
||||||
|
# Identity: verify that I|0> == |0> and I|1> == |0>
|
||||||
|
assert np.array_equal(I.dot(_0), _0)
|
||||||
|
assert np.array_equal(I.dot(_1), _1)
|
||||||
|
|
||||||
|
# Pauli X: verify that X|0> == |1> and X|1> == |0>
|
||||||
|
assert np.array_equal(X.dot(_0), _1)
|
||||||
|
assert np.array_equal(X.dot(_1), _0)
|
||||||
|
|
||||||
|
# measure probabilities in sigma_x of |0> and |1>
|
||||||
|
# using allclose since dealing with floats
|
||||||
|
assert np.allclose(measure_probability(_0), (1.0, 0.0))
|
||||||
|
assert np.allclose(measure_probability(_1), (0.0, 1.0))
|
||||||
|
|
||||||
|
# applying Hadamard puts the qbit in orthogonal +/- basis
|
||||||
|
assert np.array_equal(H.dot(_0), _p)
|
||||||
|
assert np.array_equal(H.dot(_1), _m)
|
||||||
|
|
||||||
|
# measure probabilities in sigma_x of |+> and |->
|
||||||
|
# using allclose since dealing with floats
|
||||||
|
assert np.allclose(measure_probability(_p), (0.5, 0.5))
|
||||||
|
assert np.allclose(measure_probability(_m), (0.5, 0.5))
|
||||||
|
|
||||||
|
|
||||||
def bases_to_classical(qbits):
|
def bases_to_classical(qbits):
|
||||||
"""
|
"""
|
||||||
Converts a list of qbits to classical bits.
|
Converts a list of qbits to classical bits.
|
||||||
@ -72,10 +98,8 @@ def unicode_message_to_binary(m):
|
|||||||
def binary_to_unicode_message(list_b):
|
def binary_to_unicode_message(list_b):
|
||||||
"""Converts a list of bits to Unicode message"""
|
"""Converts a list of bits to Unicode message"""
|
||||||
s = ''.join([str(a) for a in list_b])
|
s = ''.join([str(a) for a in list_b])
|
||||||
return "".join([chr(int(x, 2)) for x in [s[i:i + 8]
|
return "".join([chr(int(x, 2)) for x in
|
||||||
for i in range(0, len(s), 8)
|
[s[i:i + 8] for i in range(0, len(s), 8)]])
|
||||||
]
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
def encrypt_message(message, otp):
|
def encrypt_message(message, otp):
|
||||||
@ -97,6 +121,7 @@ def encrypt_message(message, otp):
|
|||||||
|
|
||||||
# XOR the message to be sent
|
# XOR the message to be sent
|
||||||
encrypted_message = [m_b ^ otp_b for m_b, otp_b in zip(binary_message, otp)]
|
encrypted_message = [m_b ^ otp_b for m_b, otp_b in zip(binary_message, otp)]
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
print("Bin msg: {}".format(binary_message))
|
print("Bin msg: {}".format(binary_message))
|
||||||
print("OTP: {}".format(otp))
|
print("OTP: {}".format(otp))
|
||||||
@ -127,32 +152,6 @@ def decrypt_message(encrypted_bits, otp):
|
|||||||
return _decrypted_msg
|
return _decrypted_msg
|
||||||
|
|
||||||
|
|
||||||
def run_qbit_tests():
|
|
||||||
# asserts are sets of tests to check if mathz workz
|
|
||||||
|
|
||||||
# Identity: verify that I|0> == |0> and I|1> == |0>
|
|
||||||
assert np.array_equal(I.dot(_0), _0)
|
|
||||||
assert np.array_equal(I.dot(_1), _1)
|
|
||||||
|
|
||||||
# Pauli X: verify that X|0> == |1> and X|1> == |0>
|
|
||||||
assert np.array_equal(X.dot(_0), _1)
|
|
||||||
assert np.array_equal(X.dot(_1), _0)
|
|
||||||
|
|
||||||
# measure probabilities in sigma_x of |0> and |1>
|
|
||||||
# using allclose since dealing with floats
|
|
||||||
assert np.allclose(measure_probability(_0), (1.0, 0.0))
|
|
||||||
assert np.allclose(measure_probability(_1), (0.0, 1.0))
|
|
||||||
|
|
||||||
# applying Hadamard puts the qbit in orthogonal +/- basis
|
|
||||||
assert np.array_equal(H.dot(_0), _p)
|
|
||||||
assert np.array_equal(H.dot(_1), _m)
|
|
||||||
|
|
||||||
# measure probabilities in sigma_x of |+> and |->
|
|
||||||
# using allclose since dealing with floats
|
|
||||||
assert np.allclose(measure_probability(_p), (0.5, 0.5))
|
|
||||||
assert np.allclose(measure_probability(_m), (0.5, 0.5))
|
|
||||||
|
|
||||||
|
|
||||||
def qkd():
|
def qkd():
|
||||||
"""
|
"""
|
||||||
# Quantum Key Distribution Algorithm (QKD)
|
# Quantum Key Distribution Algorithm (QKD)
|
||||||
|
Loading…
Reference in New Issue
Block a user