Worked examples

Advanced QAOA by Hand for Max-Cut on the Square QAOA by Hand for Max-Cut on the Square Build a depth-1 QAOA circuit from R_ZZ and R_X and grid-search (γ, β); the best ⟨C⟩ is 3 out of the optimum 4. Advanced Quantum-Kernel SVM on Iris Quantum-Kernel SVM on Iris Train scikit-learn's SVC with a precomputed quantum kernel (zz_feature_map) on a binary Iris task and compare with a classical RBF kernel. Advanced VQE for a Two-Qubit Transverse-Field Ising Model VQE for a Two-Qubit Transverse-Field Ising Model Estimate the ground energy of H = −Z1Z0 − 0.5(X1 + X0) with an n_local ansatz, COBYLA and StatevectorEstimator; the result −√2 matches the exact value. Beginner Adding Gates Adding Gates Append one-, two- and three-qubit gates, count them, draw the circuit as text and compute the resulting probabilities from the statevector. Beginner Creating a QuantumCircuit Creating a QuantumCircuit QuantumCircuit is Qiskit's circuit container: specify qubits and classical bits, then append gates and measurements with methods. Advanced The Swap Test The Swap Test Estimate |⟨a|b⟩|² from the ancilla's measurement probability, 2P(0) − 1: 0.4196 vs the exact 0.4179. Intermediate Parameter-Shift Gradient in PennyLane Parameter-Shift Gradient in PennyLane Differentiate f(θ) = ⟨Z⟩ = cos θ after R_X(θ) with the parameter-shift rule; compare with a manual shift and the analytic −sin θ. Intermediate Superdense Coding Superdense Coding Send two classical bits with one qubit of a shared Bell pair; all four messages decode without error. Beginner Visualization: Statevector and Histogram Visualization: Statevector and Histogram Show the statevector as text, print probabilities, and save a histogram of counts to an image file without a GUI. Intermediate Algorithms: Two-Qubit Grover Algorithms: Two-Qubit Grover Implement Grover by hand on two qubits with |11⟩ marked: a CZ phase oracle and a diffuser, giving 11 in every shot. Beginner Parameterised Circuits Parameterised Circuits Define a symbolic Parameter, bind it with assign_parameters, and verify P(1) = sin²(θ/2) for R_Y. Beginner Measurement Measurement Measurement turns a qubit into a classical bit per the Born rule; learn to map qubits to bits, read bitstrings in the right order, and use measure_all. Beginner Measuring in the X, Y and Z Bases Measuring in the X, Y and Z Bases Measure |+⟩ and |i+⟩ in all three bases by adding basis-change gates before measurement. Beginner Qubits and Registers Qubits and Registers QuantumRegister and ClassicalRegister give meaningful names to groups of qubits and bits; each qubit still has a single index in the circuit. Intermediate Operators and Unitarity Checks Operators and Unitarity Checks Compute a circuit's matrix with Operator, and verify HXH = Z, CNOT unitarity and that three CNOTs equal SWAP. Beginner Simulation: AerSimulator and StatevectorSampler Simulation: AerSimulator and StatevectorSampler Two ways to run a circuit locally: AerSimulator after transpile, and the reference StatevectorSampler primitive (Sampler V2 interface). Advanced Teleportation with a Dynamic Circuit (if_test) Teleportation with a Dynamic Circuit (if_test) Teleportation with mid-circuit measurement and classically-conditioned corrections via if_test, verified by undoing the preparation on Bob's qubit. Intermediate Bernstein–Vazirani Bernstein–Vazirani Recover the secret s = 1011 with one query to f(x) = s·x. Advanced Building the QFT by Hand Building the QFT by Hand Build the QFT from H, controlled phases and SWAPs; check it equals QFTGate and the DFT matrix; apply it to |5⟩. Beginner Installing Qiskit and Checking the Version Installing Qiskit and Checking the Version Install Qiskit and Qiskit Aer in an isolated Python environment and verify the installed versions programmatically. Intermediate Analyzing Results Analyzing Results Turn counts into probabilities, extract a single-qubit marginal with marginal_counts, and estimate ⟨Z1Z0⟩ from samples, comparing with theory. Intermediate OpenQASM 2 Export and Import OpenQASM 2 Export and Import Convert a circuit to OpenQASM 2.0 with qasm2.dumps, rebuild it with qasm2.loads, and verify the operator matches. Intermediate OpenQASM 3 Export OpenQASM 3 Export Export a parameterised circuit to OpenQASM 3.0 with qasm3.dumps; an unbound parameter appears as input float[64]. Advanced Quantum Phase Estimation Quantum Phase Estimation Estimate the phase of P(2π·5/16) with four counting qubits and an inverse QFT; the result 0101 equals 5/16 exactly. Beginner The Four Bell States The Four Bell States Build all four Bell states with one parameterised construction (X before, Z after entangling) and print their amplitudes. Intermediate GHZ State and Reduced Purity GHZ State and Reduced Purity Prepare a 3-qubit GHZ state, verify that one qubit's reduced state is maximally mixed (purity 1/2), then sample it. Advanced Computing the CHSH Value with the Estimator Computing the CHSH Value with the Estimator Build the CHSH operator from Pauli terms and evaluate it on a Bell state: S = 2√2 ≈ 2.8284, above the classical bound 2. Advanced Simon's Algorithm Simon's Algorithm Simon's circuit for s = 110: every outcome y satisfies y·s = 0 mod 2, from which s is recovered classically. Intermediate Deutsch–Jozsa Deutsch–Jozsa Deutsch–Jozsa on three bits with a constant and a balanced oracle: constant always gives 000, balanced never does. Beginner Drawing Circuits Drawing Circuits Text drawing options: reversed qubit order, hiding barriers, and bundling classical registers. Advanced Shor: Order Finding for 7 mod 15 Shor: Order Finding for 7 mod 15 Order-finding circuit for a = 7, N = 15 with 8 counting qubits; peaks at 0, 64, 128, 192, and 64/192 yield r = 4 and factors 3, 5. Intermediate Depolarizing Noise in Aer Depolarizing Noise in Aer Compare a Bell state on an ideal simulator and on one with depolarizing noise on H and CNOT; 01 and 10 appear at about 5% with noise. Intermediate Expectation Values with SparsePauliOp and StatevectorEstimator Expectation Values with SparsePauliOp and StatevectorEstimator Compute ⟨ZZ⟩, ⟨XX⟩, ⟨YY⟩ for a Bell state and the expectation of a composite Hamiltonian with StatevectorEstimator. Intermediate A Tiny Variational Classifier in PennyLane A Tiny Variational Classifier in PennyLane A two-qubit variational classifier in PennyLane: angle embedding plus StronglyEntanglingLayers, trained by gradient descent for 15 steps on four points. Beginner Modeling Readout Error in Aer Modeling Readout Error in Aer Add a readout error with p01 = 0.02 and p10 = 0.05 and measure |0⟩ and |1⟩; wrong outcomes appear at roughly the expected rates. Intermediate Quantum Kernel with zz_feature_map Quantum Kernel with zz_feature_map Compute K_ij = |⟨φ(x_i)|φ(x_j)⟩|² for three points with zz_feature_map and Statevector, and check its properties.