|00⟩ and |11⟩ with equal probability. They are never in |01⟩ or |10⟩.
Creating a Bell state
Create the simulator and initialize two qubits
Instantiate a new simulator with Both
q.New(), then allocate two qubits in the |0⟩ state using qsim.Zero().q0 and q1 start in the computational basis state |0⟩. The combined system is |00⟩.Apply a Hadamard gate to q0
The Hadamard gate puts After this step, the system is
q0 into an equal superposition of |0⟩ and |1⟩:(|0⟩ + |1⟩)/√2 ⊗ |0⟩ = (|00⟩ + |10⟩)/√2.Apply CNOT with q0 as control and q1 as target
The CNOT gate flips After CNOT:
q1 only when q0 is |1⟩. This creates entanglement between the two qubits.|00⟩stays|00⟩(control is 0, target unchanged)|10⟩becomes|11⟩(control is 1, target flipped)
(|00⟩ + |11⟩)/√2 — a Bell state.Inspect the state
Iterate over Each line has the format:
qsim.State() to print each basis state with its amplitude and probability:[00]— basis state in binary (|00⟩)[ 0]— decimal index of the basis state( 0.7071 0.0000i)— complex amplitude;1/√2 ≈ 0.7071: 0.5000— measurement probability (|0.7071|² = 0.5)
|01⟩ and |10⟩ do not appear because their amplitudes are zero.Measure both qubits and verify correlation
Measuring collapses the superposition. Both qubits are measured independently, but because they are entangled they always yield the same outcome.After measurement the state collapses deterministically to either
|00⟩ or |11⟩, each with 50% probability. The amplitude becomes 1.0000 and the probability becomes 1.0000, confirming the wavefunction has fully collapsed.m0.Equal(m1) returns true every time — the two qubits always agree.