能否用一个简单的代码示例来解释 qubit 和 bit 之间的区别?

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

据我所知,您可以使用量子计算的唯一地方是 google quantum playground and the ibm's quantum experience。虽然第一个使用 qscript 和第二个 qasm 语言(易于学习),但它们的用法仍然与常规编程没有太大区别(除了少数特定功能)。这是维基百科的解释:

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.

或多或少 clear.But 如何用代码示例显示?

下面是一些经典的代码,可以抛硬币并计算你得到了多少正面:

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

如果你运行这个代码很多次,然后做一个直方图,结果大约是Binomial distribution:

现在这里是一些伪代码,它们本质上做同样的事情,except the coin is replaced by a qubit。我们 "flip the qubit" 通过对其应用 Hadamard 操作。

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)

重复多次,绘制出分布,你得到 something very different:

很明显,尽管这些代码片段表面相似,但它们做的事情却截然不同。量子行走与经典随机行走不同。这种差异在某些算法中很有用。