BIP, High performance shrodingers cat
The most important concepts are:
Let's start completely from the beginning.
In a normal computer, a bit can be:
0
or
1
A qubit can be in a combination of both states at the same time.
We write:
|0⟩
|1⟩
for the two possible states.
A quantum circuit is just a sequence of operations applied to qubits.
This line creates a circuit with 2 qubits:
qc = QuantumCircuit(2)Initial state:
qubit 0 = |0⟩
qubit 1 = |0⟩
So:
|00⟩
means:
qubit0 = 0
qubit1 = 0
qc.h(0)applies a Hadamard gate to qubit 0.
Before:
|0⟩
After:
(|0⟩ + |1⟩)/√2
Meaning:
50% chance of measuring 0
50% chance of measuring 1
Visual idea:
Before H:
100% 0
After H:
50% 0
50% 1
So after:
qc.h(0)the first qubit is random.
Current state:
(|00⟩ + |10⟩)/√2
qc.cx(0, 1)means:
Control = qubit 0
Target = qubit 1
Rule:
If control = 1
flip target
Examples:
00 → 00
01 → 01
10 → 11
11 → 10
Before CNOT:
(|00⟩ + |10⟩)/√2
Apply CNOT:
|00⟩ stays |00⟩
|10⟩ becomes |11⟩
Result:
(|00⟩ + |11⟩)/√2
This is called an entangled state.
The qubits are now connected.
If you measure:
qubit0 = 0
you automatically know:
qubit1 = 0
If you measure:
qubit0 = 1
then:
qubit1 = 1
Possible outcomes:
00
11
Impossible outcomes:
01
10
qc.measure_all()Measurement converts quantum information into classical bits.
Before measurement:
(|00⟩ + |11⟩)/√2
After measurement:
50% → 00
50% → 11
sampler = StatevectorSampler()The sampler runs the circuit many times.
One run gives only:
00
or
11
So we run it thousands of times.
result = sampler.run([qc], shots=2200).result()Here:
shots=2200means:
Repeat the experiment 2200 times.
Example:
Run 1: 00
Run 2: 11
Run 3: 00
Run 4: 11
...
counts = result[0].data.meas.get_counts()Possible output:
{
'00': 1098,
'11': 1102
}Meaning:
00 appeared 1098 times
11 appeared 1102 times
which is approximately:
50%
50%
print(counts)Output:
{'00': 1098, '11': 1102}plot_histogram(counts)draws a graph.
Something like:
1100 |
| ████
1000 | ████
| ████
500 |
|
0 +----------------
00 11
Because only:
00
11
can occur.
from qiskit import QuantumCircuit from qiskit.primitives import StatevectorSampler from qiskit.visualization import plot_histogram
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure_all()
sampler = StatevectorSampler() result = sampler.run([qc], shots=2200).result()
counts = result[0].data.meas.get_counts() print() print("Measurement counts:", counts) print()
hist = plot_histogram(counts) hist.savefig("measurement_histogram.png") print("Histogram saved to measurement_histogram.png") print()