# My Quantum Spin: Qubit Ordering and Operations

# Overview

Picking up from [Learning Quantum Computing](https://hamacher.cloud/learning-quantum-computing), I continue chronicling my learning journey through quantum computing.

This post serves two purposes. First, I had a personal a-ha moment while manually working through the math to find the resulting statevector of a simple circuit. Namely, how to correctly and consistently compute tensor products of unitary matrices. Second, in association with that moment, I investigated the ramifications of how one may order the qubits in a circuit.

In this post, I will be working with the two similar but slightly different circuits shown here:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710589880654/48490986-9766-4b39-a8ce-d53610bdc2ec.png align="center")

The difference resides in the order of the qubits. The first qubit, q0, is on top in the first diagram and on bottom in the second. Why is this important? In all of my learning resources, except one, the first qubit is on top. The exception insisted on putting it at the bottom. As a beginner, the situation bothered me. How could I reconcile this in my mind? Stay tuned.

## First Circuit

Let's start by examining the first circuit. The circuit has no special significance (that I know of). It is simple enough to demonstrate what I learned.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710274754403/58ecf046-9fc4-436c-bada-2ff21e99bfc2.png align="center")

As stated, it is the qubit order I see in most learning resources and diagramming tools: top to bottom. In this diagram, q0 is the first qubit, q1 is the second, and q2 is the third. I want to find the resulting statevector of this circuit. The correct answer depends on correctly defining the intermediate unitary operations.

I placed a barrier down the middle of the diagram to cleanly separate the operations. The operations before the barrier may be considered to be executed simultaneously. They consist of a Pauli-Z gate on q0, a Hadamard gate in q1, and no operation on q2. The "no operation" can be represented mathematically by an identity gate. To consider the effect of these simultaneous operations on the circuit we need to take a tensor (Kronecker) product of including Z, H, and I (the identity operation). One might go ahead and do this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710285387459/92c303ff-099d-47b7-8d42-6f0b3492c272.png align="center")

This will eventually lead to the wrong statevector. The correct product is:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710590286706/dfceaf57-7fc3-4c23-9675-d2fa2488aadb.png align="center")

How do we keep this straight? Remember that the system's initial state is |000&gt; where the rightmost digit represents the first qubit (q0). The order of arguments in the tensor product is the same from right to left. This was my a-ha moment.

How about the effective operator to the right of the barrier? This time we have a multi-qubit operation, the CNOT gate. The operation on that side of the barrier is:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710286973366/9e994d3d-0fe1-44bb-9383-f63bdba2092e.png align="center")

Based on the previous IHZ operation this seems to make sense, the operation on q0 is the identity matrix and it is the righthand argument. Following the math through from the initial state, we get a final statevector of:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710287747299/e702a5ab-1cfa-4c6a-921b-ace63e65713c.png align="center")

This is the correct result. The result makes intuitive sense with the only possible states being |000&gt; and |110&gt;. Qubit q0 only had a phase flip and qubits q1 and q2 are entangled in a Bell state. All we had to do was remember the correct ordering of the operators.

## Second Circuit

Things get interesting when we reverse the numbered order of the qubits:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710288853746/c0797b7b-eb88-46b2-a608-849bdfbf3444.png align="center")

Now q0 is on the bottom. Again we start from the initial |000&gt; state. Let's look at the resulting statevector first before we look at the math that gets us there:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710289502069/609634f1-f629-47b9-b468-281c96665572.png align="center")

There might be momentary shock due to the different result but again it is easy to see why this makes sense. The possible states of the system are |000&gt; and |011. The first two qubits, q0 and q1, are in a Bell state while the third qubit q2 only experienced a phase flip. The result is different from the first circuit but consistent.

Evaluating the circuit, we start to the left of the barrier just as we did previously. This time, the ZHI tensor product is the correct operator:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710590503843/c561ff02-49c9-44b1-9a27-6646f1703356.png align="center")

Following the principle according to the ordering of qubits I discovered, we should expect the effective operator after the barrier to be:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710590954810/7325c373-43f4-421f-a2bb-15c941d8220e.png align="center")

This looks good but wait, that isn't the same CNOT matrix we used in the first circuit. Here are the two CNOT matrices used in the first and second circuits, respectively:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710591453597/c0d0d4e8-51e6-460f-b4b2-037131bd98e0.png align="center")

How am I going to remember which to use? The rule of thumb states that if the first qubit in CNOT is the control, use the first matrix. Otherwise, if the first qubit is the target, use the second matrix. Yuk. I wasn't satisfied. A formal mathematical derivation would be better. Moreover, what about the following situation?

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710593218253/fa299401-c7ca-486e-a38a-dcc046803642.png align="center")

The CNOT operator to be applied after the barrier now involves all three qubits! There is no tensor product to express the operator. Of course, we may imagine more complex situations with more qubits involved.

To answer my questions, I found the following Stack Exchange threads to be helpful:

[https://quantumcomputing.stackexchange.com/questions/5179/how-to-construct-matrix-of-regular-and-flipped-2-qubit-cnot](https://quantumcomputing.stackexchange.com/questions/5179/how-to-construct-matrix-of-regular-and-flipped-2-qubit-cnot)

[https://quantumcomputing.stackexchange.com/questions/4252/how-to-derive-the-cnot-matrix-for-a-3-qubit-system-where-the-control-target-qu](https://quantumcomputing.stackexchange.com/questions/4252/how-to-derive-the-cnot-matrix-for-a-3-qubit-system-where-the-control-target-qu)

Memorizing multi-qubit matrices will lead me astray. I need to formulate the correct matrix based on the situation.

# Conclusion

Strictly speaking, a practitioner may rely on simulators to derive statevectors and the operators needed to achieve them. I always want to know why something works so doing the math is valuable. I hope this post was thought-provoking.
