Skip to content

Two Qubits

You'll write a two-qubit quantum simulator from nothing but complex numbers and matrices, then run the same circuit on a real quantum computer and see the noise.

About

A quantum computer is a machine whose rules fit in about 200 lines, and writing a simulator for one is the quickest way to find out what the headlines are about. You keep a list of four complex numbers, multiply it by a few 4 by 4 matrices, and sample it to get bits out, and that's the whole model. The maths is a weekend of linear algebra (a 2 by 2 matrix and a tensor product), and the Math track next door has the longer version. At the time of writing IBM gives a few minutes of real machine time a month for free, and if that's gone when you read this, a library simulator with a noise model is the fallback for the last requirement.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Two weeks
Skill level
Advanced (years of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Coding to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Hold one qubit as two complex numbers and apply the X, H and Z gates as 2 by 2 matrices you typed in yourself. Use your language's complex number type, which Python ships with. Apply H twice and show that you get the starting state back, because that's the check that your matrices and your multiply are right. Take a screenshot of the state before and after each gate. Photo
  2. 2
    Build the two-qubit state as four amplitudes and apply a gate to one qubit with a tensor product you wrote by hand. The tensor product of a 2 by 2 gate and the 2 by 2 identity gives the 4 by 4 you need, and the point is to write the loops yourself rather than call a library's kron. Check your result against a library once, then put the library away. Take a screenshot of the 4 by 4 matrix for H on the first qubit next to the library's version. Photo
  3. 3
    Add CNOT and measurement by sampling, and make the Bell state come out 00 or 11 and never 01 or 10 over a thousand shots. H on the first qubit, then CNOT. Measurement is the squared magnitudes as probabilities, one random draw per shot, repeated. Around 500 and 500 is right, and exactly 500 and 500 is suspicious. Take a screenshot of the counts. Photo
  4. 4
    Run Deutsch's algorithm and show it tells a constant function from a balanced one in one query, where the classical version needs two. There are four possible one-bit functions, so build four oracles as 4 by 4 matrices. The first qubit reads 0 for the constant ones and 1 for the balanced ones, every time. Write the classical version that has to call the function twice as well, so the comparison is honest. Take a screenshot of the four results and the classical call count. Photo
  5. 5
    Check your simulator against Qiskit, Cirq or any library on five circuits, amplitude for amplitude. Run the same five circuits in your code and in the library's state-vector simulator and compare every amplitude to six decimal places. A disagreement is usually qubit ordering, because libraries don't agree on which qubit is written on the left. Take a screenshot of the five matched comparisons. Photo
  6. 6
    Run the Bell circuit on a real quantum computer, or a noisy simulator if the free tier is gone, and explain the 01 and 10 results. Sign up for the free tier, queue the job (it can sit for hours) and look at the counts. Real hardware gives a few percent of 01 and 10 that your simulator never did, and that gap is the whole engineering problem of the field. Write five lines on where the noise comes from. Take a screenshot of the counts with the machine's name and your note. Photo