## Table of Contents

- Introduction
- Flipping and Rotating Qubits
- Rotating Phase with S and T Gates
- Rotating State with the R Gates
- Swapping Qubits with the Swap Gate
- Square Root of NOT Gate
- Swapping Half a Qubit's State with the √SWAP Gate
- Working with Controlled Gates
- Two-Qubit Controlled Gates
- Working with Doubly-Controlled Gates
- Universal Gates

- Conclusion
- References
- History

## Introduction

In the previous article, we explored several of the gates commonly used in quantum computation, and looked at using some of these gates to create various quantum states. In this article, we continue exploring gates and examine some of the more complex gates. You see how to rotate and swap qubits, and we delve into controlled gates, which enable you to apply a gate depending on its input.

If you haven't already, I highly encourage you to read the first and second parts in this series before continuing.

### Flipping and Rotating Qubits

We saw in Part 2 how the Pauli-X gate is used to flip a computational basis state. It does this by rotating the qubit to the opposite (antipodal) side of the Bloch sphere. Mutually orthogonal states correspond to antipodal points on the Bloch sphere.

There are two other Pauli gates that perform the same action on different axes. They are the Pauli-Y, and Pauli-Z gates.

NOTE:When the Pauli gates are presented as operators in state equations, they are sometimes written differently, as shown:

Each rotates a state by π radians (180°) around the indicated access of the Bloch sphere.

Along with rotation, each gate produces a particular and notable change to a quantum state. Previously we saw how the Pauli-X gate is used to flip a qubit from |0⟩ to |1⟩ and vice versa.

The Pauli Z gate causes a sign flip, so that if a qubit is the state:

Then applying a Pauli-Z gate produces the following:

To transform the |+⟩ state into |-⟩ or vice versa, we apply a Pauli-Z gate. The matrix for the Pauli-Z gate is shown below:

The result of applying the Pauli-Y gate is a qubit flip and a phase shift. σ_{y} = I The Pauli-Y gate maps the state |0⟩ to *i*|1⟩, and the state |1⟩ to -*i*|0⟩.

The matrix for the Pauli-Y gate is:

If we have two qubits in superposition, such as the \(\Phi^+\) state:

We can transform \(\ket{\Phi^+}\) to \(\ket{\Psi^-}\) just by using a Pauli-Y gate:

### Rotating Phase with S and T Gates

Like the Pauli-Z gate (a.k.a., Z gate), the S and T gates also change a qubit's phase by rotating around the z-access of the Bloch sphere. The rotation angles are as follows:

Since the Z, S, and T gates don't change the vertical position on the Bloch sphere, the probability distribution is unaffected.

The S gate (a.k.a., the Phase gate or Z90 gate or \(\sqrt{Z}\) gate) maps the state α |0⟩ + β |1⟩ to α |0⟩ + *i*β |1⟩. Since *i* becomes a coefficient of the |1⟩ component, and *i*^{2} = -1, the phase of |1⟩ is flipped on the Bloch sphere.

The matrix representation for the S gate is shown below:

The reason why the S gate is sometimes refereed to as the \(\sqrt{Z}\) gate is that S^{2} = Z, as shown:

The matrix representation of the T gate is shown below:

The S gate is related to the T gate in that S = T^{2}. This is because \(\left( e^{-i \frac{\pi}{4}} \right)^2 = i\).

### Rotating State with the R Gates

The Pauli matrices flip a state to the opposite side of the Bloch sphere, while the S and T gates perform a 90° and 45° rotation, respectively. There may be times, however, when you need to rotate a qubit by an arbitrary angle. For this we turn to the R family of gates.

R_{x}, R_{y}, and R_{z}, each rotate state by a specified angle around their respective axes.

The matrix representation of the R_{x} gate, is shown below:

The matrix representation of the R_{y} gate, is shown below:

Like the S, T, and Z gates, the R_{z} gate serves to change a state's phase.

The matrix representation of the R_{z} gate, is shown below:

It's worth noting that, as Williams (2011, p.83) points out, you can perform a rotation on the *X* axis without the use of the R_{x} gate by combining R_{y} gate and R_{z} gate rotations, as the following identities demonstrate:

There's another gate that could be used for rotating state. I say 'could be' because it has yet to be successfully realized on a real qubit. It's known as the Deutsch gate. The beauty of the gate is that it promises to allow rotation around a vector, projecting from the origin of the Bloch sphere. As Yanofsky & Mannucci (2010, p.182) describe, the Deutsch gate accepts three arguments: d_{x}, d_{y}, and d_{z}, describing the axis of the Bloch sphere where the rotation is to occur.

### Swapping Qubits with the Swap Gate

The purpose of the Swap gate is to swap the states of two qubits. So that, for example, |10⟩ becomes |01⟩.

There's a terrific post by Craig Gidney that covers the Quantum Swap gate in depth.

In it Craig Gidney shows how a classical swap operation is reversible.

In a programming language like C# or Java, you can swap the values of variables a and b using a temporary variable, as shown:

```
int temp = a;
a = b;
b = temp;
```

An alternative solution that doesn't require a temporary variable, is to XOR the variables three times, like so:

```
a ^= b;
b ^= a;
a ^= a;
```

It's not immediately obvious how this works, so let's work through an example. Say a = 8_{10} (which is 1000_{2}) and b = 2_{10} (or 0010_{2}). The subscript specifies the base. Let's look at how XOR performs the swap:

```
a ^= b; // a = 1000 ^ 0010 = 1010
b ^= a; // b = 0010 ^ 1010 = 1000
a ^= b; // a = 1010 ^ 1000 = 0010
```

The swap is made without a temporary variable.

The nice thing about this approach is that the operations are reversible. If we run through the steps again, the values are swapped back. Recall that reversibility is a requirement of quantum gates.

The swap gate can be implemented with three CNOT gates, because, if you recall from Part 2, a CNOT gate is analogous to a classical XOR.

Thus the implementation of the Swap gate, could look like that shown in Figure 1. The quantum circuit symbol is on the left; it's implementation is on the right.

**Figure 1.**Quantum Swap gate implementation

In fact, as Craig Gidney points out, the Swap gate can be implemented in a number of ways, including with Z gates and Y gates. I recommend taking a look at his blog post for more depth.

The matrix representing the Swap gate is shown below.

We can see that applying the Swap operator to the two qubit state |10⟩ produces the desired result: |01⟩:

### Square Root of NOT Gate

The 'Square Root of Not' (\(\sqrt{NOT}\)) gate, is so named because the square of its matrix representation equals that of a NOT gate.

The matrix representation of the \(\sqrt{NOT}\) gate is shown below:

The \(\sqrt{NOT}\) gate is a single qubit gate. It maps the basis state |0⟩ as shown:

\(\sqrt{NOT} \ket{1}\) is calculated in the same way:

To calculate the square of \(\sqrt{NOT}\) matrix, we use our knowledge of complex number multiplication from Part 1.

#### Squaring √NOT

Multiplying the elements of the \(\sqrt{NOT}\) matrix produces the following products:

We use these results to square the \(\sqrt{NOT}\) matrix representation:

As expected, it produces the NOT gate's matrix:

The circuit diagram symbol for the \(\sqrt{NOT}\) gate is shown in Figure 2.

**Figure 2.**Square Root Not Gate circuit symbol

### Swapping Half a Qubit's State with the √SWAP Gate

We saw earlier that the SWAP gate is used to interchange the bit values of two qubits. In particular:

The \(\sqrt{SWAP}\) gate is also a 2-qubit gate that swaps half the state of one qubit with another's. Its matrix representation is shown below:

The \(\sqrt{SWAP}\) gate places the input state |10⟩ half-way to the swapped state |01⟩, effectively creating a superposition at the midpoint between |10⟩ and |01⟩.

Two \(\sqrt{SWAP}\) gates in series is equivalent to a single SWAP gate. We can verify that by squaring the matrix representation:

No working was shown there for squaring the matrix. If you'd like to check for yourself, the following products are used to calculate the final matrix:

### Working with Controlled Gates

All controlled gates have an input that determines whether the gate has any affect on state. We saw this earlier when we looked at the CNOT gate. If the control qubit state is |1⟩ then the gate is applied to the target qubit; otherwise the target qubit is unchanged.

This is evident if we examine the structure of a controlled gate's matrix representation. First off, let's re-examine the CNOT matrix. It looks like this:

The bottom right 2 × 2 matrix is the Pauli-X matrix, and it is applied to the target qubit if the control qubit is 1. Otherwise, the top left 2 × 2 matrix is applied, which you'll notice is an Identity matrix; it has no effect on the target qubit.

This structure is the same for controlled versions of all 2 × 2 gates. We just switch out the bottom right (green) 2 × 2 values with the gate at hand.

For clarity, here's another example. Recall that the Pauli-Y matrix is:

So, the matrix representing the Controlled Y gate is:

By the way, the CNOT gate is actually the controlled version of the Pauli-X gate. They're the same thing.

Figure 3 shows the quantum circuit symbols for the Pauli-X (CNOT), Pauli-Y, and Pauli-Z gates.

The control qubit is connected to the target qubit via a vertical line.

**Figure 3.**Circuit symbols for CNOT gate, Controlled X gate, and Controlled Z gate

NOTE:You may sometimes see a CNOT depicted as a controlled X gate (a square with an X in it).

We can generalize this control mechanism to allow any single qubit gate to be enabled via a control input. This is known as the Controlled-U gate. It is used alongside an existing gate. It's an operator that transforms a 2 × 2 matrix into a controlled matrix.

Observe from its matrix below that a single qubit gate needs to be plugged into it.

The way this is employed depends on the environment you're working in. For example, the Q# programming language achieves this with a `Controlled`

statement, allowing it to be applied to any unitary operator that supports it.

### Two-Qubit Controlled Gates

So far we've looked at control inputs on single qubit gates. The Controlled-Swap (CSWAP) gate (a.k.a. the Fredkin gate) is a 3-bit gate that adds a control input to a 2-qubit Swap gate.

The CSWAP gate circuit diagram symbol is shown below:

The CSWAP's matrix is presented below:

Let's examine the effects of a CSWAP on the state |101⟩, as shown below. The CSWAP maps |101⟩ to |110⟩, as you would expect.

### Working with Doubly-Controlled Gates

In addition to the CNOT, we also have the CCNOT gate (a.k.a. the Toffoli gate) at our disposal. Comparing it to the CNOT gate, the CCNOT gate, has an extra control input; hence the extra 'C' in the name.

It's a three qubit gate that inverts the target qubit if both control qubits are |1⟩. If either of the control qubits are |0⟩, the gate does nothing.

Stated more concisely by Yanofsky & Mannucci (2008, p.172), the CCNOT gate maps |x,y,z⟩ to |x,y,z⊗(x∧y)⟩

The circuit symbol for the CCNOT gate is shown in Figure 4.

**Figure 4.**CCNOT Gate circuit symbol

The truth table for the CCNOT gate is shown below:

The matrix representation of the CCNOT gate is an 8 × 8 matrix.

The subject of this doubly-controlled gate is the Pauli-X matrix, seen down in the bottom right corner:

An example use of the CCNOT gate (demonstrated in Microsoft's Q# Katas), is to switch the probability amplitudes of a 3 qubit quantum state, which is in the following superposition:

If we apply the CCNOT, we effectively switch the probability amplitudes for |110⟩ and |111⟩, as shown:

### Universal Gates

In classical computing, some gates, such as the NOR gate, can be used to construct any other gate. You can use NOR to create AND, OR and NOT gates. The NAND gate is another with this property. NAND and NOR are said to be universal.

In quantum computing, while there is no single gate that is universal, there are sets of gates that are. One such set is:

- Hadamard
- Rz
- CNOT

The Deutsch gate was briefly mentioned earlier in the article. It has the potential to be a universal gate by itself. Though, it doesn't yet exist in practical form.

As Anita Ramanan points out, since any quantum state is able to be achieved using a set of universal gates, it means that "any transformation permitted by quantum physics can be implemented on a quantum computer."

Being able to simulate the quantum universe is something classical computers are not capable of, and it offers the potential of exciting new avenues of research in many fields. This is one of the main motivators for quantum computing.

## Conclusion

In this article, we continued our exploration of quantum gates and examined some of the more complex gates. You saw how to rotate and swap qubits, and we delved into controlled gates, which enable you to apply a gate depending on its input. Finally, we discussed the universality of quantum gates.

That concludes the final part in this series. I am, however, working on a new article that demonstrates how to implement a quantum algorithm and use it within a web application. In fact, it was that article that I had intended to write first until I realized that some introductory articles were in order. I'll update this page with a link when it is published.

Thanks for reading and I hope you found this article useful. If so, then I'd appreciate it if you would please rate it and/or leave feedback below.

## References

- Yanofsky, N., & Mannucci, M. (2008).
*Quantum Computing for Computer Scientists* - Anton, H., (2000)
*Elementary Linear Algebra*, 8th Edition, Wiley - Nielsen, M. & Chuang, I., (2010)
*Quantum Computation and Quantum Information*, 10th edition, Cambridge University Press, Cambridge UK - Wikipedia,
*Quantum logic gate*, accessed on 9 June 2019, retrieved from https://en.wikipedia.org/wiki/Quantum_logic_gate - Glendinning, I., (2010)
*Rotations on the Bloch Sphere*, accessed on 9 June 2019, retrieved from http://www.vcpc.univie.ac.at/~ian/hotlist/qc/talks/bloch-sphere-rotations.pdf - Williams, C., (2011),
*Explorations in Quantum Computing*, 2nd edition, Springer, https://books.google.ch/books?id=QE8S--WjIFwC - Ramanan, A.,
*Quantum Gates and Circuits: The Crash Course*, accessed on 9 June 2019, retrieved from https://blogs.msdn.microsoft.com/uk_faculty_connection/2018/02/26/quantum-gates-and-circuits-the-crash-course/ - Fraser, G.,
*The New Physics: For the Twenty-First Century*, p. 273, Cambridge University Press, https://books.google.ch/books?id=0idvEIXwfxsC

## History

- 27 June 2019
- Published