Qiskit Fundamentals

Lesson 3 of 8

Quantum Gates

Quantum programs are built from gates, operations applied to one or more qubits. Unlike some classical logic gates (like AND, which throws information away), every quantum gate is reversible, you could always run it backwards to recover the input.

Common single-qubit gates

from qiskit import QuantumCircuit

qc = QuantumCircuit(1)
qc.x(0)  # NOT: flips |0> <-> |1>
qc.z(0)  # flips the sign of the |1> amplitude (no visible effect until combined with h)
qc.h(0)  # Hadamard: creates/undoes superposition

print(qc.draw())

x, z, and h are the gates you'll reach for constantly: x for a classical-style flip, h for superposition, z mostly matters when combined with other gates (its effect is invisible until you interfere it with something else, a recurring theme in quantum algorithms).

A two-qubit gate: CNOT

qc = QuantumCircuit(2)
qc.h(0)      # put qubit 0 into superposition
qc.x(1)      # flip qubit 1 to |1>
qc.cx(0, 1)  # CNOT: flip qubit 1 IF qubit 0 measures as |1>

print(qc.draw())

qc.cx(control, target) is the CNOT (controlled-NOT) gate: it flips the target qubit, but only conditioned on the control qubit's state. Applied to a qubit already in superposition, CNOT is what links two qubits' fates together, this is exactly how entanglement gets created, covered next.

Building bigger circuits

Gates chain together just like statements in any program:

qc = QuantumCircuit(3)
qc.h(0)
qc.cx(0, 1)
qc.cx(1, 2)
qc.x(2)

print(qc.draw())

Each line appends one more gate to the circuit, in order, exactly the sequence they'll be applied when the circuit runs.

NOTE

"Reversible" doesn't mean "does nothing", it means no information is thrown away, you could always build a circuit that undoes any sequence of gates. Measurement is the one operation in this course that is not reversible, once you measure, the superposition is gone for good.

📝 Quantum Gates Quiz

Passing score: 70%
  1. 1.What does qc.cx(0, 1) do?

  2. 2.Every quantum gate is reversible, no information is thrown away.

  3. 3.The CNOT gate is Qiskit's two-qubit gate, applied in code as qc.____(control, target).