I am having difficulties with the calculations of qubits. I think I can do them, but it feels so massivly inefficient!

In this tutorial grover's algorithm for example, there's a simple oracle given for the binary bitcombination '110': (bit-order is as usual swapped, so most significant bit is q2 - at least I think so)

grover oracle for 110

what I've tried so far:

let $a = |110\rangle$

the first step I thought would be to apply the $X$ on the top bit and the Hadamard on the bottom bit so:

$a' = |111\rangle$

$a'' = \frac{1}{\sqrt{2}}(|011\rangle - |111\rangle)$

next I would think: only when the second and third bit are equal to 1 then the first bit swaps:

$a''' = \frac{1}{\sqrt{2}}(|111\rangle - |011\rangle)$

next I negate the top (third) bit:

$a^{4'} = \frac{1}{\sqrt{2}}(|110\rangle - |010\rangle)$

and finally I use Hadamard on the bottom (first) bit:

$\displaystyle a^{5'} = \frac{1}{2} \Big(|010\rangle - |110\rangle -\big(|010\rangle + |110\rangle\big)\Big)$

$= -|110\rangle$

and yes, thats exactly what I want. But is there a way to calculate this alternively with some matrices or tensors? where I can use the form of

$ |110\rangle = \begin{pmatrix} 0\\0\\0\\0\\0\\0\\1\\0 \end{pmatrix}$

How can I represent the layer with $X$ and $H$ mixed?


1 Answer 1


First of all: the bit-order that you use is not the conventional one in quantum computing. We usually use the convention that $$ |x_1 x_2 \dots x_n \rangle = |x_1\rangle \otimes |x_2\rangle \otimes \dots \otimes |x_n\rangle, $$ i.e. the "first" bit $x_1$ corresponds to the "first" qubit which is usually the top one in the circuit notation. I agree, this is completely arbitrary.

Now to your actual question: There is nothing wrong with your calculation. You could have noticed that conjugating by $H$ on the target qubit turns $CCX$ into $CCZ$. This would have saved two steps in your calculation.

Of course, you can instead express everything in terms of 3-qubit matrices (which are of size $2^3\times 2^3 = 8\times 8$). Then, the $X$ / $H$ layer would be represented by the matrix $X\otimes \mathbb{I}\otimes H$ where $\mathbb{I}$ is the identity matrix.

But please don't do this. This is highly inefficient, especially if you deal with an arbitrary number of qubits. The whole point of local gates is that they can be applied locally. This only requires updating the local states which can be done efficiently as long as the number of terms in your superposition is not too big. On the contrary, multiplying $n$-qubit matrices is always inefficient.

  • $\begingroup$ thanks for the answer. I was wondering if this is a weird way to enumerate the qubits. Am I right in thinking that the bit order in this example is given by the quantum circuit in combination with the code 110? It took me hours to get this sorted.. but if I would change the bit-order to 'your' convention, would the example still work with this circuit and the code 110? $\endgroup$
    – eli44
    Commented Apr 27, 2021 at 12:01
  • 1
    $\begingroup$ @eli44 It's only a matter of convention. First of all, we usually call the top-most qubit in a quantum circuit the "first" qubit, that's why it's called $q[0]$ in your picture. Next, and independent from circuits, we simply say that the leftmost bit is the first one and that it should correspond to the first qubit. Whether this first bit is the least or most significant bit is irrelevant at this level. It is only needed when data shall be encoded. I don't see the difference to how this is handled in classical computing ... $\endgroup$ Commented Apr 28, 2021 at 13:42

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.