Code Snippets
Copy-paste, runnable examples of the circuits you learn about here. Where a circuit is visual, open it live in the Quantum Sandbox.
Superposition — a single Hadamard
One H gate puts a qubit into an equal 50/50 superposition. Measured many times, you get roughly half 0s and half 1s.
from qiskit import QuantumCircuit
from qiskit_aer import Aer
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0, 0)
sim = Aer.get_backend("aer_simulator")
print(sim.run(qc, shots=1000).result().get_counts())Bell state — entangle two qubits
H then CNOT creates (|00⟩+|11⟩)/√2. The two qubits are entangled: measuring one instantly determines the other, so you only ever see 00 or 11.
from qiskit import QuantumCircuit
from qiskit_aer import Aer
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])
sim = Aer.get_backend("aer_simulator")
print(sim.run(qc, shots=1000).result().get_counts())GHZ state — three-way entanglement
Extending the Bell recipe with a second CNOT entangles all three qubits into (|000⟩+|111⟩)/√2.
from qiskit import QuantumCircuit qc = QuantumCircuit(3, 3) qc.h(0) qc.cx(0, 1) qc.cx(1, 2) qc.measure(range(3), range(3))
Grover search (2 qubits, mark |11⟩)
One Grover iteration on 2 qubits finds the marked state with certainty: an oracle (CZ) flips the phase of |11⟩, then a diffusion operator amplifies it.
from qiskit import QuantumCircuit qc = QuantumCircuit(2, 2) qc.h([0, 1]) qc.cz(0, 1) # oracle: mark |11> qc.h([0, 1]); qc.z([0, 1]); qc.cz(0, 1); qc.h([0, 1]) # diffusion qc.measure([0, 1], [0, 1]) # -> 11 with prob 1
Bell state + expectation value (PennyLane)
PennyLane wraps circuits as differentiable functions. Here we build a Bell pair and read out ⟨Z⟩ on the first qubit (0 for a maximally-entangled qubit).
import pennylane as qml
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def bell():
qml.Hadamard(0)
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliZ(0))
print(bell()) # ~0.0Bell state (Cirq)
The same Bell circuit in Google's Cirq, simulated with its built-in state-vector simulator.
import cirq q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit([cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1)]) print(cirq.Simulator().run(circuit, repetitions=1000).histogram(key="0,1"))