In this and the following coding exercises, we'll look at the three-bit repetition algorithm.

In this exercise, you are given a function called encode that creates three copies of a bit : . Create a function called noisy_channel that flips each bit with an equal and independent probability . For example, if the repeated bit string is , then the flipped bit string could be, say, , where only the first bit was flipped and the other two are safe. This is going to be the message that Bob receives from Alice.

You have numpy — imported as np in the code — to use.

Hint.

Use np.random.uniform(0, 1) to see whether or not a bit will get flipped. This will generate a random number between and . If this number is greater than , then the bit doesn't get flipped.

Also, the ^ operator in Python might be useful. See here for more details.

def encode(b):
"""Returns three copes of Alice's bit b.
Args:
b (int): Alice's bit she wants to send to Bob. Can be 0 or 1.
Returns:
list(int): [b, b, b]
"""
return [b, b, b]

def noisy_channel(b, p):
"""Returns a three-bit codeword message that Bob receives through a noisy channel.
Args:
b (int): Alice's bit she wants to send to Bob. Can be 0 or 1.
p (float): The probability that a bit is flipped.
Returns:
list(int): The three-bit codeword that Bob receives. It might be different
than what Alice intended to send!
"""
alice_sends = encode(b) # Alice sends this to Bob
##################
# YOUR CODE HERE #
##################
return

or to submit your code

To interact with codercises, please switch to a larger screen size.

Learning Objectives:

  • Explain why error correction is important.
  • Explain and implement the three-bit classical repetition code.