Quantum logic gates
Quantum logic gates are the means of “getting work done” on a quantum computer. They perform computation by altering the values of quantum bits (“qubits”).
We’ll learn how quantum gates can act on a single qubit or multiple qubits in order to compute. While there’s no need to memorize the exact values that each gate uses to perform its operations, understanding the effect of some basic quantum gates is a solid foundation for understanding (and coding your own) quantum circuits.
Matrices, all the way down
A quantum computer is just a collection of qubits. These qubits hold probability values for resolving to either or . A quantum computer is able to calculate things by changing the value of its qubits over time. We tell the computer exactly how it should change the value of a qubit by instructing it to “walk through” a series of quantum gates. As the qubit “walks through” each gate its value is changed based on the type of gate it is passing through.
Contrary to what pop-science might tell you, there is nothing random or unpredictable about this process. Mathematically, a qubit is just a matrix. Similarly, a gate is also just a matrix. To apply a gate to a qubit is to multiply these matrices together. To demonstrate this, let’s begin with a “Horizontal” qubit—commonly thought of as representing “zero” or “off.” It has the following matrix form:
We would like to flip the value of this qubit from “off” to “on.” The result will be a “Vertical” qubit with the following matrix form:
By looking at the pair of numbers that represent each qubit, you can see that a Horizontal qubit is the inverse (or “flipped”) version of a Vertical qubit. In order to “flip” from one to the other we must apply a Pauli X gate to our qubit. Because Pauli X gates have the effect of “flipping” the value of a qubit, they are often thought of as the quantum equivalent of a classical “NOT” gate. Pauli X gates have the following matrix form:
We can now apply the Pauli X gate to the Horizontal qubit by multiplying their matrices together. It’s okay if your matrix multiplication is rusty—that’s what computers are for! The important part is just to recall that both qubits and quantum gates can be represented by matrices, and so can be multiplied together.
As anticipated, the resulting product matrix represents a Vertical qubit. Note how the gate’s matrix is the first factor (all the way to the left) and the qubit’s matrix is the second factor (second in from the left—with the resulting product to the right of the equals sign). This order matters because matrix multiplication is not commutative. With plain numbers (that is, numbers not contained within matrices), the order of the factors does not change the product outcome. But with matrices, a different order yields a different outcome. See matrix multiplication for an in-depth explanation.
Single-qubit gates
Indentity gate
An Identity gate has no effect on the value of the qubit it operates on; equivalent to multiplying a value by one. (Generally when a circuit is created from text or another source, any included identity gates are ignored. It is included here for completeness.)
Pauli X gate
The Pauli X gate represents a rotation on the Bloch sphere around the X-axis by radians. It is the quantum equivalent of the classical NOT gate in that it maps to and to .
Pauli Y gate
The Pauli Y gate represents a rotation on the Bloch sphere around the Y-axis by radians. It maps to and to .
Pauli Z gate
The Pauli Z gate represents a rotation on the Bloch sphere around the Z-axis by radians. It is a special case of a Phase shift gate where , and is therefore sometimes referred to as a “phase-flip” gate. It leaves the basis state unchanged and maps to .
Hadamard gate
Applies a Hadamard transform to a single qubit. For the basis qubit states of and this has the effect of putting a qubit into superposition. It represents a rotation on the Bloch sphere around the Z-axis by radians, followed by a rotation around the Y-axis by radians. This maps the basis state to (also referred to as or “ket plus”) and to (also referred to as or “ket minus”).
Phase shift gates
Phase gates are a family of quantum gates that employ the variable (phi) to represent tracing a horizontal arc (a line of latitude) of radians around the Bloch sphere. They leave the basis state unchanged and map to . The probability of measuring a or is unchanged after applying a phase shift gate, however modifying the phase of a quantum state (thankfully) does have implications within a quantum algorithm. The example form shown here represents a rotation on the Bloch sphere around the Z-axis of radians.
Remember that (phi) is a variable here. You can substitute whatever values you would like for without changing the gate’s properties as described above.
T gate
The T gate is also known as the “” gate and is a special case of a Phase shift gate where the (phi) variable is set to . (But why is it called “” when it actually divides by ? It’s a little mathy.) Like all phase shift gates, it represents a rotation on the Bloch sphere around the Z-axis.
Multi-qubit gates
Swap gate
The Swap gate swaps the value of two qubits. It is defined here with respect to the bases , , , and .
Squareroot swap gate
The √Swap gate performs half of a swap between two qubits. It is not maximally entangling. More than one application of it is required to produce a Bell state from its product states. As with the Swap gate, is defined here with respect to the bases , , , and .
Controlled gates
Controlled gates act on two or more qubits, where one qubit acts as the “target” of the operation and the remaining qubits act as “controls” determining if and how that target qubit is operated upon. In its most elementary form (a CNOT gate), a controlled gate operation acts as a sort of “if” statement, operating on the target qubit only when that “if” statement is satisfied—or to the degree with which it is satisfied. (Because qubits represent probabilities, they are not limited to strict Boolean values of `YES` or `NO`. You might imagine the “if” statement being partially satisifed, and thus partially operating on the target qubit.) Through this process, controlled gates have the ability to entangle and disentangle qubits.
Controlled NOT gate
The foundational example of a controlled gate is the Controlled-Not (CNOT) gate. The CNOT gate accepts two qubits as input, a control qubit and a target qubit—only operating on the target qubit according to the state of the control qubit. If the control qubit’s state is (“off”) the target qubit remains untouched. However, if the control cubit’s state is (“on”) then the target qubit’s state will be inverted by a Pauli X gate. Of course, a qubit’s state is not limited to or and therein lies the fun. Its matrix representation is akin to an identity matrix and an inversion matrix globbed together:
Controlled Swap (Fredkin) gate
The Controlled Swap (Fredkin) gate operates on three qubits, using one as a control and two as targets. True to its name, it swaps the states of the two target qubits according to the state of the control bit. As we can see here, more qubits means more matrix values.
Toffolli (CCNOT) gate
The Toffolli gate is also known as the CCNOT gate—a “controlled-controlled-not” gate. Like the Controlled-swap “Fredkin” gate, it operates on 3 qubits. Here we use two control qubits to apply a Pauli X gate to a single target qubit.
Ungated
Now that you’ve had a crash course in quantum gates, it’s probably a good time to step away from the screen, take a walk, and let some of this sink in.