I’m trying to calculate the probability amplitudes for this circuit: Quantum Circuit

My Octave code is:

sys = kron([1; 0], [1;0], [1;0])
h = 1/sqrt(2) * [1 1; 1 -1];
c = [1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0];
op1 = kron(h, eye(2), eye(2));
op2 = kron(c, eye(2));
op3 = kron(eye(2), c);
op4 = kron(h, eye(2), eye(2));
op4*op3*op2*op1 * sys

The output is:

ans =

This differs from the results the quantum circuit simulator gives me, where have I gone wrong?


2 Answers 2


You're getting the same output as Quirk, just with a different bit ordering convention for the kets.

Quirk considers the top qubit to be the "least significant" qubit (i.e. if you count 000, 001, 010, ... then it refers to the rightmost bit). So if you apply a Hadamard gate to the top qubit of a three-qubit circuit in Quirk you get the state |000> + |001>.

In your code you are using the opposite convention, and putting the H as the first argument to kron instead of the last argument, so you would get |000> + |100> instead.

To get comparable results you just need to vertically mirror the Quirk circuit, e.g. by throwing in a swap gate:

reversed circuit

  • $\begingroup$ Can I ask why quirk chooses that convention? I understand it is a convention and you’re free to choose it as you will, but this is in only instance that i’ve Ever seen which does not use the convention of top qubit being most significant bit. It’s so standard that people don’t bother specifying which convention they’re using when writing down a circuit. And it’s not the first question on this site where this ordering has been the issue. $\endgroup$
    – DaftWullie
    Aug 4, 2018 at 5:12
  • $\begingroup$ @DaftWullie It's because, with the other convention, the amplitude display would keep reordering as you grow the circuit. With this convention, the display at N qubits is a contiguous half of the display at N+1 qubits. Ironically I did used to use the opposite ket label ordering (but the same output display ordering). But people complained about that ket ordering so I switched it. It just generally seems like no one is happy with any ordering. $\endgroup$ Aug 4, 2018 at 17:09

The output you've stated there appears to be correct. The Hadamard produces $$ |000\rangle\mapsto\frac{1}{\sqrt{2}}(|000\rangle+|100\rangle). $$ Then, the two controlled-nots give $$ \mapsto\frac{1}{\sqrt{2}}(|000\rangle+|111\rangle). $$ The final Hadamard then yields $$ \mapsto\frac{1}{2}((|0\rangle+|1\rangle)|00\rangle+(|0\rangle-1|\rangle)|11\rangle). $$ This is the same as $\frac12(|000\rangle+|011\rangle+|100\rangle-|111\rangle)$.

The problem is presumably with the quantum simulator, or your interpretation of it, but since you don’t specify what simulator you’re using, it’s difficult to know...


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.