Coin flipping yields heads or tails with equal probability. Even if you flip it again, it remains equal probability. Randomness stays random even when repeated.
This does not happen with qubits. Performing the same operation twice causes the randomness to vanish and return to its original state. This time, we actually run it to verify this.
Letting Generative AI Write the Code
As a rule for this series, we use generative AI to write the code. The only instruction this time was this.
Apply H once to 1 qubit and measure 1000 times to get the counts of 0 and 1. Apply H twice to the same qubit and measure 1000 times to get the counts similarly.
Here is what was returned.
from blueqat import Circuit
for n in (1,2):
c = Circuit(1)
for _ in range(n):
c.h[0]
result = c.m[:].run(shots=1000)
print(f"{n}H: 0={result[0]}, 1={result[1]}")Short and readable code. We ran it.
1H: 0=0, 1=0
2H: 0=0, 1=0⛔ No errors occurred. But all values are zero
⚠️ This is the most important point I want to convey this time. This code did not crash. It ran without exceptions or warnings, finished, and output numbers. Yet, the results from 1000 measurements are all zeros.
If the code produced an error, anyone would notice. ⛔ Code that runs without crashing but produces incorrect numbers is the most troublesome.
I investigated what was happening. I displayed exactly what blueqat returned.
Type: Counter
Content: Counter({'1': 7, '0': 3})
Keys: ['1', '0'] ['str', 'str']
r[0] = 0 / r['0'] = 3⭐ The keys for the result container were strings '0' and '1'. The generative AI was retrieving them using integers 0 and 1. When a Counter is queried with an unknown key, it returns 0 without raising an error. That is why it didn't crash and instead output a series of zeros.
⚠️ "Accessing measurement results by number" is a very natural way of writing. Therefore, it is hard to notice even when reading the code. It is the kind of mistake that can only be discovered by running it and noticing that the numbers are wrong.
After fixing, it looked like this
We simply changed the access method to r['0'].
from blueqat import Circuit
for n in (1, 2):
c = Circuit(1)
for _ in range(n):
c.h[0]
r = c.m[:].run(shots=1000)
print(f"H applied {n} times: 0 occurred {r['0']} times, 1 occurred {r['1']} times")H applied 1 times: 0 occurred 495 times, 1 occurred 505 times
H applied 2 times: 0 occurred 1000 times, 1 occurred 0 timesWhen H is applied once, it becomes almost half-half (495 vs 505). Just like flipping a coin.
However, when H is applied twice, all 1000 outcomes were 0. The randomness has cleanly disappeared.
It doesn't happen with coins
For comparison, we did the same thing with an ordinary coin.
import random
for n in (1, 2):
zero = 0
for _ in range(1000):
v = 0
for _ in range(n):
v = random.choice([0, 1])
zero += (v == 0)
print(f"Flip again {n} times: Heads {zero} times, Tails {1000-zero} times")Flip again 1 time: Heads 503 times, Tails 497 times
Flip again 2 times: Heads 492 times, Tails 508 times⭐ The coin remains half-half even if flipped twice (492 vs 508). Only the qubit returned to its original state on the second attempt.
| 1 time | 2 times | |
|---|---|---|
| Qubit (H) | 495 : 505 | 1000 : 0 |
| Coin (re-flip) | 503 : 497 | 492 : 508 |
Why does it return?
The moment you flip a coin, heads or tails is decided. If you re-flip something that is already decided, it becomes half-half again.
The state of a qubit after applying H is not yet decided. Both the 0 side and the 1 side remain in a form that can cancel each other out. When you apply H again to that state, the paths leading to 1 cancel each other out, and the paths leading to 0 reinforce each other. As a result, it always returns to 0.
⚠️ The crucial point is that it cannot be returned to once measured. If you measure after the first H, it becomes decided as 0 or 1 at that point. Even if you apply H after it is decided, it just scatters half-half, like a coin. It returned only because H was applied twice without measuring; the result does not hold if the order is swapped.
What we learned this time
- When the same operation is applied twice to a qubit, randomness disappears and it returns to the original state (all 1000 outcomes are 0)
- Doing the same with a coin does not return to the original state (492 vs 508)
- ⚠️ However, if measured in the middle, it does not return. Because it becomes decided at the moment of measurement
- ⛔ And, generative AI code can make mistakes without crashing. This time, the method of accessing results was wrong, causing all values to be zero
⭐ In this series, we make sure to only include output that has actually been run. This is because, as in this case, there are things you can only notice by running the code.
※ The code and outputs in this article are all actually executed on this site. Since random numbers are used, the numbers change slightly each time even with the same code.