|q⟩ Bad Qubits

beginner · Programming · From Bits to Qubits

Your First Circuit

A quantum circuit is the basic unit of work in quantum programming. You describe a computation by choosing a set of qubits and then listing a sequence of gates — elementary operations applied one after another, from left to right.

Anatomy of a circuit

Every circuit starts with all qubits in the state 0|0\rangle. A gate transforms the current state into a new one. The simplest single-qubit gate is the X gate, which swaps the amplitudes of 0|0\rangle and 1|1\rangle:

X0=1,X1=0.X|0\rangle = |1\rangle, \qquad X|1\rangle = |0\rangle.

Its matrix confirms this directly:

X=(0110).X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}.

Multiplying by the column vector for 0=(10)|0\rangle = \binom{1}{0} gives (01)\binom{0}{1}, which is exactly 1|1\rangle. On the Bloch sphere, XX is a 180°180° rotation around the xx-axis, mapping the north pole to the south pole.

Creating a circuit in code

The simulator API makes building a circuit straightforward:

  1. Call circuit(n) to create a fresh nn-qubit circuit.
  2. Chain gate calls — for example, c.x(0) applies XX to qubit 0.
  3. Return the circuit object so the simulator can read its state vector.

This is the entire pattern you will use in every circuit exercise on this platform. The simulator initialises all qubits to 0|0\rangle automatically, so the code below starts from 0|0\rangle and ends in 1|1\rangle after one gate:

const c = circuit(1);
c.x(0);
return c;

Try it

Complete the circuit so the single qubit ends in 1|1\rangle. Press Run to see the state vector, then Check to grade your answer.

Run your code to see the quantum state.

After running, the state vector should show amplitude 11 for the 1|1\rangle basis state and amplitude 00 for 0|0\rangle. That is the signature of a definite 1|1\rangle — no superposition, just a clean flip.

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