Last time we left off with the tantalizing question: how do you do a quantum “AND” operation on two qubits? In this post we’ll see why the tensor product is the natural mathematical way to represent the joint state of multiple qubits. Then we’ll define some basic quantum gates, and present the definition of a quantum circuit.

## Working with Multiple Qubits

In a classical system, if you have two bits with values , then the “joint state” of the two bits is given by the concatenated string . But if we have two qubits , which are vectors in , how do we represent their joint state?

There are seemingly infinitely many things we could try, but let’s entertain the simplest idea for the sake of exercising our linear algebra intuition. The simplest idea is to just “concatenate” the vectors as one does in linear algebra: represent the joint system as . Recall that the *direct sum* of two vector spaces is just what you’d want out of “concatenation” of vectors. It treats the two components as completely independent of each other, and there’s an easy way to take any vector in the sum and decompose it into two vectors in the pieces.

Why does this fail to meet our requirements of qubits? Here’s one reason: is not a unit vector when and are separately unit vectors. Indeed, . We could normalize everything, and that would work for a while, but we would still run into problems. A better reason is that direct sums screw up measurement. In particular, if you have two qubits (and they’re independent, in a sense we’ll make clear later), you should be able to measure one without affecting the other. But if we use the direct sum method for combining qubits, then measuring one qubit would collapse the other! There are times when we want this to happen, but we don’t *always* want it to happen. Alas, there should be better reasons out there (besides, “physics says so”) but I haven’t come across them yet.

So the nice mathematical alternative is to make the joint state of two qubits the *tensor product* . For a review of the basic properties of tensors and multilinear maps, see our post on the subject. Suffice it for now to remind the reader that the basis of the tensor space consists of all the tensors of the basis elements of the pieces and : . As such, the dimension of is the product of the dimensions .

As a consequence of this and the fact that all -vector spaces of the same dimension are the same (isomorphic), the state space of a set of qubits can be identified with . This is one way to see why quantum computing has the potential to be strictly more powerful than classical computing: qubits provide a state space with coefficients, each of which is a complex number. With classical probabilistic computing we only get “coefficients.” This isn’t a proof that quantum computing is more powerful, but a wink and a nudge that it could be.

While most of the time we’ll just write our states in terms of tensors (using the symbol), we could write out the vector representation of in terms of the vectors . It’s just , with the obvious generalization to vectors of any dimension. This already fixes our earlier problem with norms: the norm of a tensor of two vectors is the *product* of the two norms. So tensors of unit vectors are unit vectors. Moreover, if you measure the first qubit, that just sets the above to zero or one, leaving a joint state that is still a valid

Likewise, given two linear maps , we can describe the map on the tensor space both in terms of pure tensors () and in terms of a matrix. In the same vein as the representation for vectors, the matrix corresponding to is

This is called the Kronecker product.

One of the strange things about tensor products, which very visibly manifests itself in “strange quantum behavior,” is that not every vector in a tensor space can be represented as a single tensor product of some vectors. Let’s work with an example: , and denote by the computational basis vectors (the same letters are used for each copy of ). Sometimes you’ll get a vector like

And if you’re lucky you’ll notice that this can be factored and written as . Other times, though, you’ll get a vector like

And it’s a deep fact that this cannot be factored into a tensor product of two vectors (prove it as an exercise). If a vector in a tensor space *can* be written as a single tensor product of vectors, we call a *pure tensor**. *Otherwise, using some physics lingo, we call the state represented by *entangled. *So if you did the exercise you proved that not all tensors are pure tensors, or equivalently that there exist entangled quantum states. The latter sounds so much more impressive. We’ll see in a future post why these entangled states are so important in quantum computing.

Now we need to explain how to extend gates and qubit measurements to state spaces with multiple qubits. The first is easy: just as we often restrict our classical gates to a few bits (like the AND of two bits), we restrict multi-qubit quantum gates to operate on at most three qubits.

**Definition: **A quantum gate is a unitary map where is at most 3, (recall, is the state space for 3 qubits).

Now let’s see how to implement AND and OR for two qubits. You might be wondering why we need three qubits in the definition above, and, perhaps surprisingly, we’ll see that AND and OR require us to work with three qubits.

Because how would one compute an AND of two qubits? Taking a naive approach from how we did the quantum NOT, we would label as “false” and as “true,” and we’d want to map and all other possibilities to . The main problem is that this is not an invertible function! Remember, all quantum operations are unitary matrices and all unitary matrices have inverses, so we have to model AND and OR as an invertible operation. We also have a “type error,” since the output is not even in the same vector space as the input, but any way to fix that would still run into the invertibility problem.

The way to deal with this is to add an extra “scratch work” qubit that is used for nothing else except to make the operation invertible. So now say we have three qubits , and we want to compute AND in the sensible way described above. What we do is map

Here is the usual AND (where we interpret, e.g., ), and is the exclusive or operation on bits. It’s clear that this mapping makes sense for “bits” (the true/false interpretation of basis vectors) and so we can extend it to a linear map by writing down the matrix.

This gate is often called the *Toffoli gate* by physicists, but we’ll just call it the (quantum) AND gate. Note that the column represents the input , and the 1 in that column denotes the row whose label is the output. In particular, if we want to do an AND then we’ll ensure the “scratch work” qubit is , so we can ignore half the columns above where the third qubit is 1. The reader should write down the analogous construction for a quantum OR.

From now on, when we’re describing a basis state like , we’ll denote it as , and more generally when is a nonnegative integer or a binary string we’ll denote the basis state as . We’re taking advantage of the correspondence between the binary strings and the basis states, and it compactifies notation.

Once we define a quantum circuit, it will be easy to show that using quantum AND’s, OR’s and NOT’s, we can achieve any computation that a classical circuit can.

We have one more issue we’d like to bring up before we define quantum circuits. We’re being a bit too slick when we say we’re working with “at most three qubits.” If we have ten qubits, potentially all entangled up in a weird way, how can we apply a mapping to only some of those qubits? Indeed, we only defined AND for , so how can we extend that to an AND of three qubits sitting inside any we please? The answer is to apply the Kronecker product with the identity matrix appropriately. Let’s do a simple example of this to make everything stick.

Say I want to apply the quantum NOT gate to a qubit , and I have four other qubits so that they’re all in the joint state . I form the NOT gate, which I’ll call , and then I apply the gate to (since there are 4 of the ). This will compute the tensor , as desired.

In particular, you can represent a gate that depends on only 3 qubits by writing down the 3×3 matrix and the three indices it operates on. Note that this requires only 12 (possibly complex) numbers to write down, and so it takes “constant space” to represent a single gate.

## Quantum Circuits

Here we are at the definition of a quantum circuit.

**Definition: **A *quantum circuit* is a list of unitary matrices, such that each depends on at most 3 qubits.

We’ll write down what it means to “compute” something with a quantum circuit, but for now we can imagine drawing it like a usual circuit. We write the input state as some unit vector (which may or may not be a pure tensor), each qubit making up the vector is associated to a “wire,” and at each step we pick three of the wires, send them to the next quantum gate , and use the three output wires for further computations. The final output is the matrix product applied to the input . We imagine that each gate takes only one step to compute (recall, in our first post one “step” was a photon flying through a special material, so it’s not like we have to multiply these matrices by hand).

So now we have to say how a quantum circuit could solve a problem. At all levels of mathematical maturity we should have some idea how a regular circuit solves a problem: there is some distinguished output wire or set of wires containing the answer. For a quantum circuit it’s basically the same, except that at the end of the circuit we get a single quantum state (a tensor in this big vector space), and we just measure that state. Like the case of a single qubit, if the vector has coordinates , they must satisfy , and the probability of the measurement producing index is . The result of that measurement is an integer (some classical bits) that represent our answer. As a side effect, the vector is mutated into the basis state . As we’ve said we may need to repeat a quantum computation over and over to get a good answer with high probability, so we can imagine that a quantum circuit is used as some subroutine in a larger (otherwise classical) algorithm that allows for pre- and post-processing on the quantum part.

The final caveat is that we allow one to include as many scratchwork qubits as one needs in their circuit. This makes it possible already to simulate any classical circuit using a quantum circuit. Let’s prove it as a theorem.

**Theorem: **Given a classical circuit with a single output bit, there is a quantum circuit that computes the same function.

*Proof.* Let be a binary string input to , and suppose that has gates , each being either AND, OR, or NOT, and with being the output gate. To construct , we can replace every with their quantum counterparts . Recall that this takes . And so we need to add a single scratchwork qubit for each one (really we only need it for the ANDs and ORs, but who cares). This means that our start state is . Really, we need one of these gates for each wire *going out* of the classical gate , but with some extra tricks one can do it with a single quantum gate that uses multiple scratchwork qubits. The crucial thing to note is that the state vector is always a basis vector!

If we call the contents of all the scratchwork after the quantum circuit described above runs and the initial state of the scratchwork, then what we did was extend the function to a function . In particular, one of the bits in the part is the output of the last gate of , and everything is 0-1 valued. So we can measure the state vector, get the string and inspect the bit of which corresponds to the output wire of the final gate of the original circuit . This is your answer.

It should be clear that the single output bit extends to the general case easily. We can split a circuit with lots of output bits into a bunch of circuits with single output bits in the obvious way and combine the quantum versions together.

Next time we’ll finally look at our first quantum algorithms. And along the way we’ll see some more significant quantum operations that make use of the properties that make the quantum world interesting. Until then!