|q⟩ Bad Qubits

beginner · Programming · Building & Reading Quantum Circuits

Circuit Depth and Width

Two simple numbers summarise the structure of any quantum circuit: width and depth.

Width

The width of a circuit is the number of qubits it uses. A circuit operating on nn qubits has width nn. Width directly sets the memory cost — a width-nn circuit manipulates a 2n2^n-dimensional state vector. A circuit that introduces an ancilla qubit to assist a calculation has larger width than one that avoids it.

Depth

The depth of a circuit counts the minimum number of time-steps required when gates acting on disjoint qubits are allowed to execute in parallel. Concretely, you assign each gate to the earliest layer that does not already contain a gate on any of the same qubits. The depth is the index of the last layer needed.

Consider this three-qubit example:

Layer 1:  H0H1Layer 2:  CNOT01Layer 3:  H0CNOT12\text{Layer 1:}\; H_0 \parallel H_1 \qquad \text{Layer 2:}\; \mathrm{CNOT}_{0 \to 1} \qquad \text{Layer 3:}\; H_0 \parallel \mathrm{CNOT}_{1 \to 2}

Here H0H_0 means the Hadamard on qubit 0 and CNOTct\mathrm{CNOT}_{c \to t} denotes a controlled-NOT with control cc and target tt. The symbol \parallel shows that the two operands run in the same layer because they act on distinct qubits. The circuit uses 3 qubits, so its width is 3, and it fills 3 layers, so its depth is 3.

Why depth matters

A naive implementation of an nn-qubit algorithm might place every gate sequentially, giving depth equal to the total gate count. Recognising which gates commute — because they act on different qubits — lets a compiler collapse them into the same layer, reducing depth without changing the computation. In the example above, the five gates collapse to depth 3 rather than 5.

Try it

The circuit described in the starter code has 3 qubits and the three layers shown above. Return its depth as an integer.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.