Search code examples
bitquantum-computingqscriptqubit

Can the difference between qubit and bit be explained with a simple code example?


The only places I know that you can play with quantum computing are the google quantum playground and the ibm's quantum experience. While the first one uses qscript and the second qasm languages (which are easy to learn) their usage still do not differ much from regular programming (besides the few specific functions). Here's the wikipedia explanation:

A qubit has a few similarities to a classical bit, but is overall very different. There are two possible outcomes for the measurement of a qubit—usually 0 and 1, like a bit. The difference is that whereas the state of a bit is either 0 or 1, the state of a qubit can also be a superposition of both.It is possible to fully encode one bit in one qubit. However, a qubit can hold even more information, e.g. up to two bits using superdense coding.

For a system of n components, a complete description of its state in classical physics requires only n bits, whereas in quantum physics it requires 2^n − 1 complex numbers.

Which more or less clear.But how this can shown with a code example?


Solution

  • Here is some classical code that flips coins and counts how many heads you get:

    def coin_count():
        bit = False
        counter = 0
        for _ in range(500):
            bit ^= random() < 0.5  # False → 50% False, 50% True
                                   #  True → 50% False, 50% True
            if bit:
                counter += 1
        return counter
    

    If you run this code many times, and make a histogram, the result will be approximately a Binomial distribution:

    Classical Binomial distribution

    Now here is some pseudo-code that does essentially the same thing, except the coin is replaced by a qubit. We "flip the qubit" by applying the Hadamard operation to it.

    def hadamard_coin_count():
        qubit = qalloc()
        counter = 0
        for _ in range(500):
            apply Hadamard to qubit # |0⟩ → √½|0⟩ + √½|1⟩
                                    # |1⟩ → √½|0⟩ - √½|1⟩
            if qubit:  # (not a measurement; controls nested operations)
                counter += 1  # (happens only in some parts of the superposition)
        return measure(counter)  # (note: counter was in superposition)
    

    Do this many times, plot out the distribution, and you get something very different:

    quantum walk distribution

    Clearly these code snippets are doing very different things despite their surface similarity. Quantum walks don't act the same as classical random walks. This difference is useful in some algorithms.