re-formatting

This commit is contained in:
Daniel Tsvetkov 2019-12-02 17:44:03 +01:00
parent 1a1c3307b0
commit 9b31296aed

View File

@ -52,6 +52,32 @@ def measure(qbit):
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):
"""
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):
"""Converts a list of bits to Unicode message"""
s = ''.join([str(a) for a in list_b])
return "".join([chr(int(x, 2)) for x in [s[i:i + 8]
for i in range(0, len(s), 8)
]
])
return "".join([chr(int(x, 2)) for x in
[s[i:i + 8] for i in range(0, len(s), 8)]])
def encrypt_message(message, otp):
@ -97,6 +121,7 @@ def encrypt_message(message, otp):
# XOR the message to be sent
encrypted_message = [m_b ^ otp_b for m_b, otp_b in zip(binary_message, otp)]
if DEBUG:
print("Bin msg: {}".format(binary_message))
print("OTP: {}".format(otp))
@ -127,32 +152,6 @@ def decrypt_message(encrypted_bits, otp):
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():
"""
# Quantum Key Distribution Algorithm (QKD)