Quantum Sandbox logo

Quantum Sandbox

An Interactive Quantum Circuit Playground

All Lessons Sandbox
Part 2: How They Work 12 min read

Gates and Circuits

Quantum gates are the operations of quantum computing - the building blocks that transform qubit states. A quantum circuit is a sequence of these gates applied in order. Understanding the common gates is the key to reading and writing quantum programs.

Gates as rotations

Every single-qubit gate corresponds to a rotation of the Bloch sphere. This is not an approximation - it is exact. Every valid single-qubit quantum operation rotates the Bloch vector by some angle around some axis. Different gates correspond to different rotations.

This geometric picture is powerful. It means you can understand what any gate does by asking: which direction does it rotate the state, and by how much? It also explains why H applied twice cancels: you rotate 180° and then 180° again, returning to the start. And it explains why gates are reversible - rotation in one direction can always be undone by rotation in the opposite direction. Think of it like turning a steering wheel: turn it 90° left, then 90° right, and the wheel is back where it started - no information about the trip is lost.

The essential single-qubit gates

Click any gate card to see a deeper explanation. Each gate is a precise rotation of the Bloch sphere.

X
Pauli-X
180° rotation around X axis. Flips |0⟩ to |1⟩.
The quantum NOT gate. Like a classical bit flip, but also works on superpositions. Applied twice it cancels, since two 180° rotations return to the start.
Y
Pauli-Y
180° rotation around Y axis. Flips bit AND phase.
Y flips the qubit like X but also injects an imaginary phase. The state |0⟩ becomes i|1⟩ and |1⟩ becomes -i|0⟩. Used in error correction and certain algorithm subroutines.
Z
Pauli-Z
180° rotation around Z axis. Phase flip only.
Z leaves |0⟩ unchanged and flips the sign of |1⟩. Probabilities don't change, but this phase difference matters when followed by interference. Applied after H on |0⟩ and then another H: you get |1⟩ instead of |0⟩.
H
Hadamard
Creates and destroys superposition.
The most important gate in quantum algorithms. H applied to |0⟩ gives (|0⟩+|1⟩)/√2 - perfect superposition. H applied twice cancels itself out. It is the switch that enters and exits the quantum regime.
S
Phase-S
90° phase rotation on |1⟩. Quarter-turn.
S rotates the phase of |1⟩ by 90° without touching |0⟩. Applied 4 times it completes a full rotation and returns to the start. S = T², where T is the T gate. Critical in the Quantum Fourier Transform.
T
Phase-T
45° phase rotation on |1⟩. Eighth-turn.
T is the finest standard phase gate. It rotates |1⟩ by 45°. Used for precise phase control in long circuits. Along with H and CNOT, T completes the universal gate set - any quantum computation can be built from these three types.

Two-qubit gates: creating entanglement

Single-qubit gates rotate one qubit. Two-qubit gates create correlations between two qubits - and entanglement. The most important two-qubit gate is the CNOT (Controlled-NOT). It has a control qubit and a target qubit: if the control is |1⟩, the target is flipped; if the control is |0⟩, nothing happens.

Other important two-qubit gates include the SWAP gate, which exchanges the states of two qubits. In the Sandbox's expert mode, you will also encounter the Toffoli gate (CCNOT) - a three-qubit gate that flips the target only when both controls are |1⟩, like a light switch that only turns on when two separate keys are both turned at once. The Toffoli gate makes the quantum gate set computationally universal, meaning it can simulate any classical logical operation.

Universal gate sets

Just as classical computing can implement any logic using only NAND gates, quantum computing can implement any quantum operation using a small set of universal gates. One universal set is {H, T, CNOT}. Every quantum algorithm can be decomposed into some sequence of these three gate types.

This matters practically: when a quantum chip only natively supports a few gate types, any arbitrary gate can be approximated to arbitrary precision using sequences of those native gates - similar to how a few standard LEGO bricks can be combined to approximate almost any shape, even curves they were never molded to make directly. The Solovay-Kitaev theorem guarantees that this approximation is efficient - you do not need exponentially many gates to approximate a complex rotation.

It's worth being precise about what "universal" means here: it does not mean these are the only gates that exist. Mathematically, any unitary matrix is a valid quantum gate, and rotation angles are continuous - so there are infinitely many possible gates, most of which never get a name. The named gates in this lesson (and in the Sandbox's Gate Library) are simply a small, practical set that real algorithms are commonly built from. A "universal" set is just powerful enough to reach any of those infinite possibilities through composition, the same way infinite shades of color can be mixed from a handful of primary pigments.

Putting it together: building a Bell state

Here is how gates compose into a real quantum circuit. This two-step sequence creates the simplest maximally entangled state - called a Bell state. Follow the flow:

Start
|00⟩
Both qubits initialized to |0⟩. This is the default ground state - fully classical, no superposition, no entanglement.
Step 1 - H gate on Q0
(|0⟩ + |1⟩)/√2 ⊗ |0⟩
H puts Q0 into perfect superposition. Q1 is still |0⟩. The two qubits are still independent of each other.
Step 2 - CNOT (Q0 control, Q1 target)
(|00⟩ + |11⟩)/√2
CNOT flips Q1 whenever Q0 is |1⟩. Since Q0 is in superposition, the flip applies to both branches simultaneously. The qubits are now entangled.
Result - Bell State |Φ+⟩
Maximal Entanglement
Measuring Q0 gives 50% |0⟩ or 50% |1⟩. But whichever it is, Q1 is instantly the same. Neither qubit has an independent state anymore.
Try it in the Sandbox
Build your own circuits and observe gate effects
  1. Start with preset |0⟩ in Basic mode. 8 circuit slots available.
  2. Apply X: qubit flips to |1⟩. Apply X again: back to |0⟩. X is its own inverse.
  3. Try H → Z → H. The qubit ends at 100% |1⟩ (phase flip becomes a visible bit flip).
  4. Stack four S gates: S→S = Z, and S→S→S→S returns to the start.
  5. Experiment freely. |α|² + |β|² always stays exactly 1, no matter what you build.

Reading a quantum circuit diagram

Quantum circuits are typically drawn as horizontal lines (one per qubit) with gate symbols placed on them. Time flows left to right. Single-qubit gates are boxes on one wire. Two-qubit gates span two wires and show a connection between them.

The circuit depth is the number of columns - the longest path through the circuit. The width is the number of qubits. Total gate count tells you roughly how many operations the quantum computer must perform. All three metrics matter for real hardware: more depth means more decoherence time, and more gates means more opportunities for error.

When you design a quantum circuit, you are balancing expressiveness (doing what you need computationally) against resource efficiency (keeping depth and gate count low enough to stay within the hardware's error budget). This optimization is one of the core skills of quantum programming.

Gate fidelity and circuit compilation

On real hardware, every gate is implemented as a carefully timed pulse of microwave radiation (for superconducting qubits) or a laser pulse (for trapped ions). These pulses are not perfect - they introduce small errors. Gate fidelity measures how close to perfect a gate operation is: a fidelity of 99.9% means the gate introduces a 0.1% error probability on average.

Quantum circuit compilation is the process of taking a high-level circuit description and translating it to the specific native gates of a particular hardware chip, while also mapping logical qubits to physical qubits (accounting for which qubits are connected to each other). It's a bit like translating a recipe written for a professional kitchen into one a home cook can follow with a smaller set of pots, pans, and ingredients - some steps combine, others expand, and the order may have to change. This compilation step can dramatically increase or decrease the final gate count, which is why quantum software tools like Qiskit and Cirq spend significant effort on circuit optimization.

Key takeaway

Quantum gates are rotations of the Bloch sphere. Single-qubit gates (X, Y, Z, H, S, T) manipulate one qubit's state. Two-qubit gates (CNOT, SWAP) create entanglement. The set {H, T, CNOT} is universal - any quantum computation can be built from these three types alone.

Knowledge check · Question 1 of 5

Why does applying the Hadamard (H) gate twice in a row to a qubit return it to its original state?

Every single-qubit gate is an exact rotation of the Bloch sphere. H rotates by 180°, so applying it twice is two 180° turns - a full circle back to where you started, the same way turning a steering wheel 180° twice returns it to center.
Knowledge check · Question 2 of 5

What is the effect of applying the Z gate to a qubit that is in perfect 50/50 superposition, then measuring it?

Z is a phase gate - it flips the sign of the |1⟩ amplitude but leaves |α|² and |β|² unchanged. A single measurement after Z gives the same statistics as before. The phase change only becomes visible through subsequent interference with other gates.
Knowledge check · Question 3 of 5

In a CNOT gate, what determines whether the target qubit gets flipped?

CNOT (Controlled-NOT) is the most important two-qubit gate: it flips the target qubit exactly when the control qubit is |1⟩, and leaves it unchanged when the control is |0⟩. This conditional flip is what creates entanglement when the control is in superposition.
Knowledge check · Question 4 of 5

Which combination of gates is commonly described as a "universal gate set" for quantum computing?

Just as classical computing can build any logic circuit from NAND gates alone, quantum computing can build any quantum operation from the universal set {H, T, CNOT}. This is what lets hardware with a limited native gate set still run arbitrary algorithms.
Knowledge check · Question 5 of 5

In a quantum circuit diagram, what does "circuit depth" refer to?

Circuit depth counts the columns, i.e. the longest sequential chain of gates a qubit passes through - not the total gate count or qubit count. Depth matters because more depth means more time for decoherence to creep in on real hardware.