# Multi-Qubit Gates

Unlike single qubit gates, multi-qubit gates are to complex to be described using the relatively simple Bloch sphere diagram. For these we'll just have their matrix to describe their behaviour.

Something important to note is that all gates must have the [*same number of inputs as there are outputs*](/learning-quantum/quantum-circuits/quantum-information.md#reversibility). This is because no information can be lost in quantum systems - whatever goes in must come out again.

## The Controlled Not Gate

This gate is also called the "*CX"* or the *"controlled X"* gate. You can guess what this means - it preforms the same operation as the [*Pauli X*](/learning-quantum/quantum-circuits/single-qubit-gates.md#pauli-x) gate but controlled by another qubit.

$$
\text{CNOT}=\begin{bmatrix}1&0&0&0\0&1&0&0\0&0&0&1\0&0&1&0\end{bmatrix}
$$

![Symbol for the CX gate](/files/-M41b7PFQVAVFFNEB_5j)

We pass in two bits - a *control* bit and a *target* bit. If the control bit is $$|1\rangle$$, the target bit will flip states. If the control bit is $$|0\rangle$$, nothing happens. The control bit is never affected by this gate.

### Toffoli - Double Control

The Toffoli gate (*"double control", "CCNOT", "CCX"* or *"TOFF"*) does basically the same thing, except this time we have *t*wo *control bits*. If both control bits are $$|1\rangle$$, we flip our target bit. If either of them are $$|0\rangle$$ nothing happens. The control bits are still never effected.

$$
\text{TOFF}=\begin{bmatrix}1&0&0&0&0&0&0&0\0&1&0&0&0&0&0&0\0&0&1&0&0&0&0&0\0&0&0&1&0&0&0&0\0&0&0&0&1&0&0&0\0&0&0&0&0&1&0&0\0&0&0&0&0&0&0&1\0&0&0&0&0&0&1&0\end{bmatrix}
$$

![Symbol for the TOFF gate](/files/-M41eOz95cVS59VTtNXK)

{% hint style="success" %}
As we operate on more quits, and end up with more distinguishable states, are matrices get larger. The same as a system with $$n$$ qubits have $$2^n$$ distinguishable states, or matrices will likewise have $$2^n$$ numbers of rows and columns.

So our TOFF gate, which works on 3 qubits, has $$2^3=8$$ distinguishable states, and there for 8 rows and 8 columns in it's matrix.

Notice that this works the same way it would when constructing classical truth tables to describe operations on classical bits.&#x20;
{% endhint %}

## The Controlled Z Gate

The controlled Z gate (*"CZ"*) works essentially just like the controlled X gate. It's the *Pauli Z* gate with an extra control bit. When the control bit is $$|1\rangle$$, we preform the Z gate operation. When the control bit is $$|0\rangle$$nothing changes. As before, the control bit is never affected by this operation.

$$
\text{CZ}=\begin{bmatrix}1&0&0&0\0&1&0&0\0&0&1&0\0&0&0&-1\end{bmatrix}
$$

![Symbol for the CZ gate](/files/-M4GDwT_bvjiVL63u1aN)

## The SWAP Gate

This gate does what is says - it swaps the state of the two bits included in the operation. So if the starting state is:

$$
|a\rangle=|0\rangle, |b\rangle=|1\rangle
$$

The resulting state will be:

$$
|a\rangle=|1\rangle, |b\rangle=|0\rangle
$$

And of course, vice versa.

$$
\text{SWAP}=\begin{bmatrix}1&0&0&0\0&0&1&0\0&1&0&0\0&0&0&1\end{bmatrix}
$$

![Symbol for the SWAP gate](/files/-M4GlKYpyeAtTs0Ma6hB)

## Universal Gate Sets

In the section about [*classical models of computation*](/learning-quantum/quantum-circuits/classical-models-of-computation.md), we talked a bit about constructing all possible classical logic gates with just one - [*the NAND gate*](/learning-quantum/quantum-circuits/classical-models-of-computation.md#universal-gates). The NAND gate is a *universal* gate because of this special property.

In quantum computing we have the same concept. All possible quantum gates can be a constructed with a selection of one and two qubit gates. Exactly which gates depends - there are multiple possible universal sets of quantum gates. You'll see different resources will rely on different universal sets. For this tutorial what most important is that you understand the concept.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lewisla.gitbook.io/learning-quantum/quantum-circuits/multi-qubit-gates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
