Code exampleBeginnerQiskit labLesson 4

Adding Gates

Append one-, two- and three-qubit gates, count them, draw the circuit as text and compute the resulting probabilities from the statevector.

Every gate in Qiskit has a method named by its short name: x, y, z, h, s, sdg, t, tdg, sx for fixed single-qubit gates; rx, ry, rz, p, u for parameterised gates (angles in radians); cx, cy, cz, cp, swap, rzz for two-qubit gates; and ccx (Toffoli) and cswap (Fredkin) for three-qubit gates. In controlled gates the control qubit comes first, then the target.

Important conventions: $R_X(\theta)=e^{-i\theta X/2}$, $P(\lambda)=\mathrm{diag}(1,e^{i\lambda})$, $S=P(\pi/2)$ and $T=P(\pi/4)$. The count_ops() method counts operations by type, which is useful for estimating the cost of a circuit.

After building the circuit, Statevector.from_instruction(qc) computes the final state exactly (with no measurement or sampling), and probabilities_dict() returns the probability of each bit string in the order $q_2q_1q_0$. In the example four outcomes appear, each with probability $0.25$: H on $q_0$ and $R_X(\pi/2)$ on $q_2$ create two superpositions, the other gates (S, T and CZ) change only phases, while CNOT, SWAP and Toffoli permute the basis states.

In the text drawing, a SWAP gate appears as two connected × marks, CZ as two connected dots because it is symmetric, and Toffoli as two control dots and a ⊕ target.

In practice, hardware does not execute all these gates directly; the transpiler (transpile) rewrites them in the device's native gate set, so a single Toffoli gate may become several CNOT gates and single-qubit gates, increasing the depth and the exposure to noise.

Requirements: pip install qiskit qiskit-aer. The code runs on your machine or in Colab, not inside WordPress.

Code

Qiskit
from math import pi
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector

qc = QuantumCircuit(3)
qc.h(0)
qc.x(1)
qc.s(0)
qc.t(1)
qc.rx(pi / 2, 2)
qc.cx(0, 1)
qc.cz(1, 2)
qc.swap(0, 2)
qc.ccx(0, 1, 2)

print(dict(qc.count_ops()))
print(qc.draw("text"))
sv = Statevector.from_instruction(qc)
probs = sv.probabilities_dict()
print({str(k): round(float(v), 4) for k, v in probs.items()})

Line-by-line code explanation

LineExplanation
1Import π to specify angles in radians.
2Import the circuit class.
3Import Statevector to compute the state exactly.
5A three-qubit circuit with no classical bits.
6-7Hadamard on q0 and X on q1.
8-9Phase gates S on q0 and T on q1.
10RX rotation by π/2 on q2.
11CNOT: control q0, target q1.
12Controlled Z between q1 and q2.
13Swap the states of q0 and q2.
14Toffoli gate: controls q0 and q1, target q2.
16Count operations by type.
17Draw the circuit as text.
18Compute the circuit's final state vector.
19Dictionary of probabilities for each bit string.
20Print the probabilities rounded to four decimal places.

Expected output (from an actual run)

{'h': 1, 'x': 1, 's': 1, 't': 1, 'rx': 1, 'cx': 1, 'cz': 1, 'swap': 1, 'ccx': 1}
        ┌───┐   ┌───┐                
q_0: ───┤ H ├───┤ S ├──■──────X───■──
        ├───┤   ├───┤┌─┴─┐    │   │  
q_1: ───┤ X ├───┤ T ├┤ X ├─■──┼───■──
     ┌──┴───┴──┐└───┘└───┘ │  │ ┌─┴─┐
q_2: ┤ Rx(π/2) ├───────────■──X─┤ X ├
     └─────────┘                └───┘
{'010': 0.25, '100': 0.25, '101': 0.25, '111': 0.25}

The output is deterministic (exact state-vector computation, no sampling).

Try it yourself

Related topics

References

  1. IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
  2. Michael A. Nielsen; Isaac L. Chuang (2010). Quantum Computation and Quantum Information (10th Anniversary Edition). Cambridge University Press doi:10.1017/CBO9780511976667The standard reference for quantum computing; the Crossref record gives 2012 for the Cambridge Core online edition.
  3. A. Barenco; C. H. Bennett; R. Cleve; D. P. DiVincenzo; N. Margolus; P. Shor; T. Sleator; J. A. Smolin; H. Weinfurter (1995). Elementary gates for quantum computation. Phys. Rev. A 52(5):3457–3467 doi:10.1103/PhysRevA.52.3457