A Visual Guide to Quantum Gates: Hadamard, CNOT, and Beyond
A Visual Guide to Quantum Gates: Hadamard, CNOT, and Beyond
Every quantum circuit — including the ones that generated our Quantum Genesis NFTs — is built from quantum gates. Let's understand each one visually, with runnable Qiskit code.
Table of Contents
- What Are Quantum Gates?
- Pauli Gates: X, Y, Z
- The Hadamard Gate: Superposition Creator
- CNOT: The Entanglement Gate
- Rotation Gates: Rx, Ry, Rz
- Toffoli Gate: 3-Qubit Control
- Combining Gates Into Circuits
- How Quantum Genesis Uses These Gates
1. What Are Quantum Gates?
In classical computing, logic gates (AND, OR, NOT) transform bits. Quantum gates do the same for qubits — but with a crucial difference: they operate on probability amplitudes, not just 0s and 1s.
Mathematically, a quantum gate is a unitary transformation — a matrix U where U multiplied by its conjugate transpose equals the identity matrix. This guarantees that quantum information is preserved (no information lost, no probabilities exceeding 1).
Visually, think of a qubit's state as a point on a sphere (the Bloch sphere). Each gate rotates that point to a new position. Different gates rotate along different axes by different amounts.
Key insight: Every quantum computation is just a sequence of rotations on the Bloch sphere, followed by a measurement that collapses the sphere to a definite 0 or 1.
Gates are represented in circuit diagrams as boxes or symbols on horizontal lines (wires), where each wire represents one qubit. Time flows left to right.
2. Pauli Gates: X, Y, Z
The three Pauli gates are the fundamental single-qubit operations. Each corresponds to a 180-degree rotation around one axis of the Bloch sphere.
Pauli-X (Bit Flip)
The X gate is the quantum equivalent of a classical NOT gate. It flips |0⟩ to |1⟩ and |1⟩ to |0⟩. On the Bloch sphere, it's a 180-degree rotation around the X axis.
from qiskit import QuantumCircuit
qc = QuantumCircuit(1)
qc.x(0) # Apply X gate to qubit 0
qc.measure_all()
print(qc.draw())
# ┌───┐ ░ ┌─┐
# q_0: ┤ X ├─░─┤M├
# └───┘ ░ └╥┘
If the qubit starts in |0⟩, after X it's guaranteed to be |1⟩. Simple and deterministic.
Pauli-Y (Bit + Phase Flip)
The Y gate combines a bit flip with a phase flip. It rotates 180 degrees around the Y axis. Less commonly used alone, but essential in many algorithms.
qc = QuantumCircuit(1)
qc.y(0) # Apply Y gate
Pauli-Z (Phase Flip)
The Z gate leaves |0⟩ unchanged but flips the phase of |1⟩ (multiplies by -1). You can't observe this with a single measurement, but it affects interference in larger circuits.
qc = QuantumCircuit(1)
qc.z(0) # Apply Z gate — phase flip only
Think of Z as flipping the "hidden sign" of a qubit — invisible on its own, but critical when gates interact.
3. The Hadamard Gate: Superposition Creator
The Hadamard gate (H) is arguably the most important single-qubit gate in quantum computing. It puts a qubit into an equal superposition of |0⟩ and |1⟩.
Applied to |0⟩, H produces (|0⟩ + |1⟩)/√2 — meaning a 50/50 chance of measuring 0 or 1. Applied to |1⟩, it produces (|0⟩ - |1⟩)/√2 — still 50/50, but with a phase difference that matters for interference.
from qiskit import QuantumCircuit
from qiskit_ibm_runtime import SamplerV2 as Sampler
qc = QuantumCircuit(1)
qc.h(0) # Hadamard: creates superposition
qc.measure_all()
print(qc.draw())
# ┌───┐ ░ ┌─┐
# q_0: ┤ H ├─░─┤M├
# └───┘ ░ └╥┘
# Running on real hardware gives ~50% |0⟩, ~50% |1⟩
# But NOT exactly 50/50 — that's real quantum noise!
Fun fact: Applying H twice returns the qubit to its original state. H is its own inverse — it's like a quantum toggle switch.
On the Bloch sphere, H is a rotation that takes the "north pole" (|0⟩) to the "equator" — right in between 0 and 1.
The Hadamard gate is the starting point of nearly every quantum algorithm. Without it, qubits behave classically. With it, you unlock superposition — the foundation of quantum parallelism.
4. CNOT: The Entanglement Gate
The CNOT (Controlled-NOT) gate is the most important two-qubit gate. It takes a control qubit and a target qubit. If the control is |1⟩, it flips the target. If the control is |0⟩, it does nothing.
qc = QuantumCircuit(2)
qc.cx(0, 1) # CNOT: qubit 0 controls, qubit 1 is target
print(qc.draw())
# q_0: ──■──
# ┌─┴─┐
# q_1: ┤ X ├
# └───┘
On its own, CNOT isn't that exciting. The magic happens when you combine it with Hadamard:
qc = QuantumCircuit(2)
qc.h(0) # Put qubit 0 in superposition
qc.cx(0, 1) # Entangle qubit 0 and qubit 1
qc.measure_all()
# q_0: ┤ H ├──■──
# ┌─┴─┐
# q_1: ──────┤ X ├
# Results: ~50% "00", ~50% "11"
# NEVER "01" or "10" — they're entangled!
This H + CNOT combination creates a Bell state — the simplest form of quantum entanglement. The two qubits become correlated: measuring one instantly determines the other, no matter how far apart they are.
5. Rotation Gates: Rx, Ry, Rz
While Pauli gates are fixed 180-degree rotations, rotation gates let you specify any angle. This gives you fine-grained control over qubit states.
import math
qc = QuantumCircuit(1)
# Rotate by any angle (in radians)
qc.rx(math.pi / 4, 0) # Rotate around X axis by 45 degrees
qc.ry(math.pi / 3, 0) # Rotate around Y axis by 60 degrees
qc.rz(math.pi / 2, 0) # Rotate around Z axis by 90 degrees
Rx(θ) rotates around the X axis by angle θ. At θ=π, it equals the Pauli-X gate.
Ry(θ) rotates around the Y axis. Useful for preparing specific probability distributions — Ry(π/2) on |0⟩ gives equal superposition like H, but without the phase difference.
Rz(θ) rotates around the Z axis. A pure phase rotation — it changes the "hidden angle" without affecting measurement probabilities in the standard basis.
Universality: Any single-qubit gate can be decomposed into a sequence of Rz, Ry, and Rz rotations. Combined with CNOT, you can build ANY quantum circuit. This is called universal gate set.
6. Toffoli Gate: 3-Qubit Control
The Toffoli gate (CCX) extends CNOT to three qubits: two controls and one target. The target flips only when BOTH controls are |1⟩. It's the quantum equivalent of a classical AND gate (followed by XOR).
qc = QuantumCircuit(3)
qc.ccx(0, 1, 2) # Toffoli: qubits 0,1 control, qubit 2 is target
print(qc.draw())
# q_0: ──■──
# │
# q_1: ──■──
# ┌─┴─┐
# q_2: ┤ X ├
# └───┘
The Toffoli gate is important because it's universal for classical computation — any classical Boolean circuit can be built from Toffoli gates alone. It's also reversible, making it the bridge between classical and quantum computing.
In practice, Toffoli gates are expensive on real hardware — they decompose into about 6 CNOT gates. So quantum algorithms try to minimize their use.
7. Combining Gates Into Circuits
A quantum circuit is a sequence of gates applied to a register of qubits, followed by measurements. Here's a complete example that creates a 4-qubit entangled state:
from qiskit import QuantumCircuit
qc = QuantumCircuit(4, 4)
# Step 1: Superposition on all qubits
for i in range(4):
qc.h(i)
# Step 2: Entangle pairs
qc.cx(0, 1)
qc.cx(2, 3)
# Step 3: Cross-entangle
qc.cx(1, 2)
# Step 4: Add some rotation for variety
qc.rz(0.5, 0)
qc.ry(0.3, 3)
# Step 5: Measure
qc.measure(range(4), range(4))
print(qc.draw())
The output is a probability distribution over 16 possible states (0000 through 1111). On a real quantum computer, noise and decoherence add additional variation — which is a feature, not a bug, when generating art.
Gate Order Matters
Unlike classical logic, the order of quantum gates dramatically affects the outcome. H followed by Z is different from Z followed by H. This is because matrix multiplication is not commutative — the order of rotations on the Bloch sphere matters.
# These produce DIFFERENT states:
qc1 = QuantumCircuit(1)
qc1.h(0)
qc1.z(0) # H then Z
qc2 = QuantumCircuit(1)
qc2.z(0)
qc2.h(0) # Z then H — different result!
8. How Quantum Genesis Uses These Gates
For our Quantum Genesis collection, we designed circuits specifically for maximum entropy — unpredictable, irreproducible randomness:
# Simplified version of our NFT generation circuit
qc = QuantumCircuit(8, 8)
# Hadamard on all 8 qubits — maximum superposition
for i in range(8):
qc.h(i)
# CNOT chain — entangle all qubits
for i in range(7):
qc.cx(i, i + 1)
# Measure all
qc.measure(range(8), range(8))
# Run on REAL quantum hardware (IBM ibm_fez or Origin WK_C180)
# Each run produces genuinely random bits
The H + CNOT chain pattern ensures that all 8 qubits are entangled in a GHZ-like state. When measured, the quantum noise from real hardware produces bits that are fundamentally unpredictable — not pseudo-random, but physically random from quantum mechanics.
These raw quantum bits become seeds for color palettes, geometry, and composition in each NFT. NFTs #1-18 were generated on the Origin Quantum WK_C180 (180 qubits), and #19-100 on IBM Quantum's ibm_fez and ibm_torino processors.
Why Real Hardware Matters
A simulator would give you perfect 50/50 distributions from Hadamard gates. Real hardware has noise — gate errors, decoherence, crosstalk between qubits. This noise is genuinely random (quantum mechanical in origin) and makes each execution unique and irreproducible. For art, this is perfect.
Gate Summary Table
| Gate | Qubits | Symbol | Effect | Qiskit |
|---|---|---|---|---|
| Pauli-X | 1 | X | Bit flip (NOT) | qc.x(0) |
| Pauli-Y | 1 | Y | Bit + phase flip | qc.y(0) |
| Pauli-Z | 1 | Z | Phase flip | qc.z(0) |
| Hadamard | 1 | H | Superposition | qc.h(0) |
| CNOT | 2 | CX | Conditional flip | qc.cx(0,1) |
| Rx/Ry/Rz | 1 | R | Arbitrary rotation | qc.rx(θ,0) |
| Toffoli | 3 | CCX | Double-controlled flip | qc.ccx(0,1,2) |
Desenvolvo sistemas que usam hardware quântico real (IBM Quantum, Origin Quantum) para gerar arte e NFTs com proveniência verificável on-chain. Escrevo sobre computação quântica aplicada, criptografia, Web3 e arte generativa — tudo com código que roda em processadores quânticos de verdade.
Comentários
Postar um comentário