Quantum Playground
Eight small quantum programs, from single-qubit gates to a variational eigensolver. Each one is run on a simulator and checked against the value theory predicts.
| Demo | What it shows | Measured |
|---|---|---|
| Bloch sphere | How single-qubit gates move a state | 9 states plotted |
| Bell states | Two-qubit entanglement | Only correlated outcomes |
| GHZ state | Entanglement across 5 qubits | Only 00000 and 11111, about 50/50 |
| Teleportation | Sending a qubit with 2 classical bits | P(1) = 25.4% vs 25% |
| Deutsch–Jozsa | Exponential query advantage | 0000 vs 1010, 100% of shots |
| Grover | Unstructured search in √N | ~96% on the marked state |
| QFT | Fourier transform on amplitudes | Uniform amplitudes, linear phase |
| VQE for H₂ | Hybrid quantum–classical optimisation | −1.857275 Ha (exact −1.857275) |
Bloch sphere
bloch_gallery.pyA single qubit is a point on a sphere. X and Y send |0⟩ to the south pole; H puts it on the equator. Z, S and T then rotate it around the vertical axis by π, π/2 and π/4. Those last three change only the phase, which a measurement histogram cannot show but the sphere does.






Other gates in the gallery



Bell states
bell_states.pyH on qubit 0 and a CNOT give |Φ+⟩ = (|00⟩ + |11⟩)/√2. An X before the H flips the sign; an X on qubit 1 swaps 00/11 for 01/10. Φ+ and Φ− give the same histogram. Only the density matrix shows the difference, in the sign of the off-diagonal terms.




GHZ state
ghz_state.pyThe Bell construction extended to five qubits: H on qubit 0 and a chain of CNOTs. Measuring all five always gives the same bit on every qubit.


Quantum teleportation
teleportation.pyQubit 0 holds the state to send, Ry(π/3) followed by Rz(π/5). Qubits 1 and 2 share a Bell pair. Alice entangles her qubits, measures them, and Bob applies X and Z depending on her two bits. The corrections run inside the circuit (if_test), not in post-processing.


Deutsch–Jozsa
deutsch_jozsa.pyIs f constant or balanced? Classically that can take up to 2ⁿ⁻¹ + 1 evaluations; here one oracle call is enough. The balanced oracle computes f(x) = x·s with CNOTs onto an ancilla in |−⟩.


Circuits


Grover search
grover.pyThe oracle flips the phase of 1010 (a multi-controlled Z wrapped in X gates where the target has a 0). The diffuser reflects the state about the mean. With N = 16 the optimal number of rounds is ⌊(π/4)√16⌋ = 3.


Oracle and diffuser code
def oracle(n, marked):
qc = QuantumCircuit(n)
for i, b in enumerate(reversed(marked)):
if b == "0": qc.x(i)
qc.h(n - 1); qc.mcx(list(range(n - 1)), n - 1); qc.h(n - 1)
for i, b in enumerate(reversed(marked)):
if b == "0": qc.x(i)
return qc
iters = int(np.floor((np.pi / 4) * np.sqrt(2 ** n))) # 3 for n = 4
Quantum Fourier Transform
qft.pyH gates and controlled phase rotations of π/2ᵏ, then swaps to reverse the qubit order. Applied to |5⟩ = |0101⟩, every basis state ends up with the same amplitude and a phase that grows linearly, 2π·5·y/16. The colours on the Q-sphere are those phases.



VQE for the H₂ molecule
vqe_h2.pyThe H₂ Hamiltonian at 0.735 Å (STO-3G basis, parity mapping reduced to 2 qubits) has five Pauli terms. A 4-parameter ansatz (Ry, CNOT, Ry) prepares a trial state, and COBYLA adjusts the angles to minimise the energy. Expectation values are computed exactly from the statevector, so there is no shot noise.


Cost function
def expectation(params, H):
sv = Statevector.from_instruction(ansatz(params))
return float(np.real(sv.expectation_value(H)))
res = minimize(cost, x0, method="COBYLA", options={"maxiter": 200, "rhobeg": 0.3})