How to Disentangle Qubits

I recently read a question asking how to disentangle qubits, and I realized that this isn’t explicitly explained in quantum computing tutorials. It’s implied, but it’s not explicit. So, if one person is asking this question, there are probably others out there with this same question.

First of all, let’s address why this is important. My most common use of disentanglement is probably gate construction, although I use it for other purposes, as well. But, for starters, let’s say you’re reading up on Grover’s algorithm and you see an oracle with a CCCCZ gate. If you use IBM Quantum (you should), you should realize that there is no such thing as a CCCCZ gate. Therefore, if we want to use a CCCCZ gate, we have to construct one first. That construction process requires disentanglement.

My target audience for this is anyone who has at least run the “hello, quantum world” experiment. Consequently, I am skipping the standard definitions of superposition and entanglement, as well as the instructions for implementing both. After all, if you don’t know how to entangle qubits, you’re probably not asking how to disentangle qubits.

Disentanglement = Reverse Entanglement

Reverse a CNOT with another CNOT.

Hopefully everyone recognizes the first two operations and the last two operations in the circuit above. We start with a Hadamard gate, which puts qubit0 into superposition. Then we use a CNOT gate to entangle qubit0 and qubit1. The last two operations are measurements. Together, these four operations constitute the “hello, quantum world” experiment which, I presume, is the first quantum computing experiment for most entrants to the field.

You’ll notice, however, that there’s one extra CNOT in there. While the tutorials usually mention reversibility, they don’t explicitly mention disentanglement. But, another way to think of disentanglement is reversing entanglement. The way you reverse anything in quantum computing is to just do it again. In this case, we applied a CNOT to entangle qubit0 and qubit1, and then we applied another CNOT to reverse that entanglement.

Qubit0 is in superposition, but qubit0 and qubit1 are not entangled.

If you recall from your “hello, quantum world” experiment, the telltale signature of entanglement is seeing 00 and 11 as your results. Plus noise, of course. But, that’s not what we see above. We see that qubit0, the right digit, is alternating 0 and 1, but qubit 1, the left digit, is remaining at 0. Plus noise. Therefore, we know that qubit0 has remained in superposition, while qubit1 is no longer entangled with it.

I’m assuming, by the way, that readers understand the basic concept of noise. I could’ve run this on a simulator, but playing with real hardware is always more fun. Consequently, the measurements of 00 and 01 are not quite equal, and the measurements of 10 and 11 are not both at zero. If we had perfect qubits, then we should see 00 and 01 exclusively, and in almost a 50/50 ratio.

So, let’s re-address the CCCCZ gate.

Practical Disentanglement

Constructing a CCCCZ gate requires three ancilla qubits.

Again, the CCCCZ gate is not native to IBM Quantum, which means that we have to construct one if we want to use one. Here, qubits0-3 are our control qubits and qubit7 is our target qubit. But, take a look at qubits4-6. Those are our ancilla qubits, aka “work” qubits. After the CCCCZ operation, those ancilla qubits are not needed anymore, but yet they are still entangled with the other five qubits. If we apply any operations to those ancilla qubits, we won’t see our desired results and residual entanglement will be the culprit.

Reverse three Toffoli gates (CCX) with three Toffoli gates in reverse order.

So, we’ll disentangle these ancilla qubits the same way we entangled them in the first place, but in reverse order. The control qubits return to whatever states the Hadamard gates tranformed them into. The ancilla qubits return to their ground states, disentangled and ready for re-use. And, the target qubit has maintained whatever state the CCCCZ operation put it in. You can trust you did everything correctly if you produce a mirror image, as shown above.

It’s worth emphasizing that the CZ gate at the bottom, between qubit6 and qubit7, appears only once. We’re not reversing that operation. We’re only disentangling the ancilla qubits by re-applying the Toffoli gates (CCX) in reverse order. The end result is that the four control qubits remain entangled with the target qubit, giving us the CCCCZ gate that we want.

The three ancilla qubits are entangled with the five other qubits.

Above are the states (simulated) of the ancilla qubits if you do not disentangle them. If you somehow don’t need them anymore, this might not be a big deal. But, if you do try to use them again, those 001, 011, and 111 measurements are foreshadowing unpredictable results. Because, keep in mind that these qubits remain entangled, so not only will they behave unpredictably, you might see some of the other qubits acting unpredictably.

The three ancilla qubits are disentangled and in their ground states.

Above, however, are the states (simulated) of the ancilla qubits after disentanglement. They are all in their ground states, measuring 0, as if we’re at the beginning of the circuit, as if we haven’t used them thus far, or as if we’ve applied reset gates. Qubit counts are limited these days, so go ahead and maximize their usefulness.

Disentangling Toffoli gates (CCX) have been applied, but not in reverse order.

So, what happens if you don’t disentangle in reverse order? After all, it’s easier to just copy the code of the three Toffoli gates (CCX) above and paste it after the CZ gate. This is still executing all the same operations a second time, right?

The ancilla qubits have not been completely disentangled.

Well, for this particular circuit, the ancilla qubits are not fully disentangled. For that matter, the remaining qubits aren’t entangled properly either. Any of that might become a problem later. Therefore, always reverse order for simplicity. You might get lucky with simpler constructions, but you’ll still want to verify that all the qubits, not just the ancilla qubits, are entangled and/or disentangled as desired.

Instead of reversing entangling operations, reset gates have been applied.

Another option you might consider using is reset gates. After all, we want to return the ancilla qubits to their disentangled ground states, and applying reset gates will definitely do that. However, even though you will achieve the desired effect with the ancilla qubits, you won’t be left with a true CCCCZ gate. You will disentangle more than you intended to. Therefore, this brute force approach only gives the brief illusion of a solution, but it’s not an ideal solution.

In conclusion, the best way to disentangle any-sized entangled system, is to repeat all the entangling operations in reverse.

A Note About Aesthetics

This alternate illustration of a CCCCZ gate keeps the control qubits and target qubit together.

One final word: you might see the CCCCZ shown like this, with the control qubits and the target qubit together and the ancilla qubits off to the side, but that’s just aesthetics for explanation or simulation purposes. After optimizing this circuit for a specific device, it probably won’t be aesthetic at all. But, this approach admittedly does show the CCCCZ a bit more clearly: qubits0-3 are still the control qubits and now qubit4 is the target qubit, so qubits0-4 are now a contiguous CCCCZ gate. But, again, circuit optimization will just undo that later anyway.