|q⟩ Bad Qubits

intermediate · Programming · Multi-Qubit Registers & State Simulation

Tensor Structure of Operations

When you have a register of nn qubits, applying a single-qubit gate GG to qubit kk means tensoring GG with identity on every other qubit. For a three-qubit register the full operator is

G0=GII,G1=IGI,G2=IIG.G_0 = G \otimes I \otimes I, \quad G_1 = I \otimes G \otimes I, \quad G_2 = I \otimes I \otimes G.

Writing out those 8×88 \times 8 matrices by hand would be tedious — so quantum programming frameworks handle the tensor product automatically. You simply call the gate and name the qubit.

The nn-qubit state space

An nn-qubit register lives in the tensor-product space (C2)n(\mathbb{C}^2)^{\otimes n}, which has dimension 2n2^n. The computational basis consists of all nn-bit strings bn1b1b0|b_{n-1} \cdots b_1 b_0\rangle. For three qubits the eight basis states are 000,001,,111|000\rangle, |001\rangle, \ldots, |111\rangle.

The state vector tracks one complex amplitude for each basis state, so a three-qubit register needs 23=82^3 = 8 amplitudes.

Applying a gate to one qubit

Suppose the register starts in 000|000\rangle and you apply HH only to qubit 0:

(HII)000=(H0)00=12(0+1)00.(H \otimes I \otimes I)|000\rangle = (H|0\rangle) \otimes |0\rangle \otimes |0\rangle = \tfrac{1}{\sqrt{2}}\bigl(|0\rangle + |1\rangle\bigr) \otimes |0\rangle \otimes |0\rangle.

Expanding into the eight-dimensional basis (qubit 0 is most significant in this platform):

=12(000+100).= \tfrac{1}{\sqrt{2}}\bigl(|000\rangle + |100\rangle\bigr).

Now add an XX gate on qubit 2 at the same time (independent gates commute when they act on different qubits):

X2H0000=12(001+101).X_2 H_0 |000\rangle = \tfrac{1}{\sqrt{2}}\bigl(|001\rangle + |101\rangle\bigr).

What the simulator does

Every time you call c.h(0), the simulator constructs the operator HI(n1)H \otimes I^{\otimes (n-1)} and multiplies it into the state vector. It then does the same for c.x(2) with I2XI^{\otimes 2} \otimes X. You never write the big matrix — the library builds it from the qubit index and the known 2×22 \times 2 gate.

This is the core idea behind all quantum programming frameworks: gate calls become tensor-product operators applied to the full state vector.

Try it

Start from 000|000\rangle (a three-qubit register). Apply HH to qubit 0 and XX to qubit 2, leaving qubit 1 untouched. The grader checks the final state vector.

Run your code to see the quantum state.

After running, you should see nonzero amplitudes only on 001|001\rangle and 101|101\rangle, each with magnitude 120.707\tfrac{1}{\sqrt{2}} \approx 0.707.

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