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.

Qiskit 2.x Aer simulator, 4096 shots Python Alberto Aragón Jiménez · 2026
96%
Grover finds 1 of 16 states in 3 iterations (theory: 96.1%)
<10⁻⁵ Ha
VQE error vs. exact H₂ ground-state energy
25.4%
Teleported qubit measured as 1 (theory: 25%)
1 query
Deutsch–Jozsa separates constant from balanced
DemoWhat it showsMeasured
Bloch sphereHow single-qubit gates move a state9 states plotted
Bell statesTwo-qubit entanglementOnly correlated outcomes
GHZ stateEntanglement across 5 qubitsOnly 00000 and 11111, about 50/50
TeleportationSending a qubit with 2 classical bitsP(1) = 25.4% vs 25%
Deutsch–JozsaExponential query advantage0000 vs 1010, 100% of shots
GroverUnstructured search in √N~96% on the marked state
QFTFourier transform on amplitudesUniform amplitudes, linear phase
VQE for H₂Hybrid quantum–classical optimisation−1.857275 Ha (exact −1.857275)

Bloch sphere

1 qubit · bloch_gallery.py

A 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.

Bloch sphere for |0>
|0⟩, no gate
After H
H → equator
After X
X → |1⟩
Z after H
H then Z: rotated by π
S after H
H then S: rotated by π/2
T after H
H then T: rotated by π/4
Other gates in the gallery
After Y
Y
Rx(pi/3)
Rx(π/3)
Ry(pi/4)
Ry(π/4)

Bell states

2 qubits · bell_states.py

H 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.

Bell circuit
Circuit
Bell histogram
Measurements, 4096 shots
Bell density matrix
Density matrix (real and imaginary parts)
Bell Q-sphere
Q-sphere: amplitude and phase per basis state

GHZ state

5 qubits · ghz_state.py

The 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.

ResultOnly 00000 and 11111 appear, each about half of the 4096 shots.
GHZ circuit
Circuit
GHZ histogram
Measurements

Quantum teleportation

3 qubits · teleportation.py

Qubit 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.

CheckFor Ry(π/3), P(1) = sin²(π/6) = 25%. In the run shown, Bob's qubit read 1 in 1039 of 4096 shots (25.4%), and close to 25% for each combination of Alice's bits.
Teleportation circuit
Circuit with classically controlled corrections
Teleportation histogram
Outcomes as bob · alice₁ · alice₀

Deutsch–Jozsa

4 + 1 qubits · deutsch_jozsa.py

Is 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 |−⟩.

ResultConstant oracle: 0000 in all 2048 shots. Balanced oracle: 1010, the oracle's own pattern s, in all 2048 shots.
DJ constant
Constant oracle
DJ balanced
Balanced oracle
Circuits
DJ constant circuit
Constant
DJ balanced circuit
Balanced

Grover search

4 qubits, N = 16 · grover.py

The 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.

ResultThe marked state is measured about 96% of the time (3942 of 4096 in the run shown). Theory for 3 iterations: sin²(7·arcsin(1/4)) = 96.1%.
Grover histogram
Measurements after 3 iterations
Grover circuit
Full circuit: superposition, then 3 × (oracle + diffuser)
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

4 qubits · qft.py

H 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.

QFT Q-sphere
Output state on the Q-sphere
QFT density matrix
Output density matrix
QFT circuit
State preparation for |5⟩ followed by the QFT

VQE for the H₂ molecule

2 qubits · vqe_h2.py

The 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.

Result−1.857275 Ha after 64 evaluations. Exact diagonalisation of the 4×4 Hamiltonian: −1.857275 Ha. Electronic energy only, without nuclear repulsion.
VQE convergence
Energy per COBYLA evaluation; dashed line is the exact value
VQE ansatz
Ansatz with the optimal parameters
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})