Making Hybrid Images | Neural Networks and Backpropagation |
Elliptic Curves and Cryptography |

Bezier Curves and Picasso | Computing Homology | Probably Approximately Correct – A Formal Theory of Learning |

# A Proofless Introduction to Information Theory

There are two basic problems in information theory that are very easy to explain. Two people, Alice and Bob, want to communicate over a digital channel over some long period of time, and they know the probability that certain messages will be sent ahead of time. For example, English language sentences are more likely than gibberish, and “Hi” is much more likely than “asphyxiation.” The problems are:

- Say communication is very expensive. Then the problem is to come up with an encoding scheme for the messages which minimizes the expected length of an encoded message and guarantees the ability to unambiguously decode a message. This is called the
*noiseless coding problem.* - Say communication is not expensive, but error prone. In particular, each bit of your message is erroneously flipped with some known probably , and all the errors are independent. Then the question is, how can one encode their messages to as to guarantee (with high probability) the ability to decode any sent message? This is called the
*noisy coding problem.*

There are actually many models of “communication with noise” that generalize (2), such as models based on Markov chains. We are not going to cover them here.

Here is a simple example for the noiseless problem. Say you are just sending binary digits as your messages, and you know that the string “00000000” (eight zeros) occurs half the time, and all other eight-bit strings occur equally likely in the other half. It would make sense, then, to encode the “eight zeros” string as a 0, and prefix all other strings with a 1 to distinguish them from zero. You would save on average bits in every message.

One amazing thing about these two problems is that they were posed and solved in the same paper by Claude Shannon in 1948. One byproduct of his work was the notion of *entropy, *which in this context measures the “information content” of a message, or the expected “compressibility” of a single bit under the best encoding. For the extremely dedicated reader of this blog, note this differs from Kolmogorov complexity in that we’re not analyzing the compressibility of a string by itself, but rather when compared to a distribution. So really we should think of (the domain of) the distribution as being compressed, not the string.

## Entropy and noiseless encoding

Before we can state Shannon’s theorems we have to define entropy.

**Definition: **Suppose is a distribution on a finite set , and I’ll use to denote the probability of drawing from . The *entropy *of , denoted is defined as

It is strange to think about this sum in abstract, so let’s suppose is a biased coin flip with bias of landing heads. Then we can plot the entropy as follows

The horizontal axis is the bias , and the vertical axis is the value of , which with some algebra is . From the graph above we can see that the entropy is maximized when and minimized at . You can verify all of this with calculus, and you can prove that the uniform distribution maximizes entropy in general as well.

So what is this saying? A high entropy measures how *incompressible *something is, and low entropy gives us lots of compressibility. Indeed, if our message consisted of the results of 10 such coin flips, and was close to 1, we could be able to compress a lot by encoding strings with lots of 1’s using few bits. On the other hand, if we couldn’t get any compression at all. All strings would be equally likely.

Shannon’s famous theorem shows that the entropy of the distribution is actually all that matters. Some quick notation: is the set of all binary strings.

**Theorem (Noiseless Coding Theorem) [Shannon 1948]: **For every finite set and distribution over , there are encoding and decoding functions such that

- The encoding/decoding actually works, i.e. for all .
- The expected length of an encoded message is between and .

Moreover, *no encoding scheme can do better.*

Item 2 and the last sentence are the magical parts. In other words, if you know your distribution over messages, you *precisely* know how long to expect your messages to be. And you know that you can’t hope to do any better!

As the title of this post says, we aren’t going to give a proof here. Wikipedia has a proof if you’re really interested in the details.

## Noisy Coding

The noisy coding problem is more interesting because in a certain sense (that was not solved by Shannon) it is still being studied today in the field of coding theory. The interpretation of the noisy coding problem is that you want to be able to recover from white noise errors introduced during transmission. The concept is called *error correction*. To restate what we said earlier, we want to recover from error with probability asymptotically close to 1, where the probability is over the errors.

It should be intuitively clear that you can’t do so without your encoding “blowing up” the length of the messages. Indeed, if your encoding does not blow up the message length then a single error will confound you since many valid messages would differ by only a single bit. So the question is does such an encoding exist, and if so how much do we need to blow up the message length? Shannon’s second theorem answers both questions.

**Theorem (Noisy Coding Theorem) [Shannon 1948]: **For any constant noise rate , there is an encoding scheme with the following property. If is the message sent by Alice, and is the message received by Bob (i.e. with random noise), then as a function of . In addition, if we denote by the entropy of the distribution of an error on a single bit, then choosing any guarantees the existence of such an encoding scheme, and no scheme exists for any smaller .

This theorem formalizes a “yes” answer to the noisy coding problem, but moreover it characterizes the blowup needed for such a scheme to exist. The deep fact is that *it only depends on the noise rate*.

A word about the proof: it’s probabilistic. That is, Shannon proved such an encoding scheme exists by picking to be a random function (!). Then finds (nonconstructively) the string such that the number of bits different between and is minimized. This “number of bits that differ” measure is called the *Hamming distance. *Then he showed using relatively standard probability tools that this scheme has the needed properties with high probability, the implication being that some scheme has to exist for such a probability to even be positive. The sharp threshold for takes a bit more work. If you want the details, check out the first few lectures of Madhu Sudan’s MIT class.

The non-algorithmic nature of his solution is what opened the door to more research. The question has surpassed, “Are there any encodings that work?” to the more interesting, “What is the algorithmic cost of constructing such an encoding?” It became a question of complexity, not computability. Moreover, the guarantees people wanted were strengthened to worst case guarantees. In other words, if I can guarantee *at most 12 errors*, is there an encoding scheme that will allow me to always recover the original message, and not just with high probability? One can imagine that if your message contains nuclear codes or your bank balance, you’d definitely want to have 100% recovery ability.

Indeed, two years later Richard Hamming spawned the theory of *error correcting codes *and defined codes that can always correct a single error. This theory has expanded and grown over the last sixty years, and these days the algorithmic problems of coding theory have deep connections to most areas of computer science, including learning theory, cryptography, and quantum computing.

We’ll cover Hamming’s basic codes next time, and then move on to Reed-Solomon codes and others. Until then!

# Zero-One Laws for Random Graphs

Last time** **we saw a number of properties of graphs, such as connectivity, where the probability that an Erdős–Rényi random graph satisfies the property is asymptotically either zero or one. And this zero or one depends on whether the parameter is above or below a universal threshold (that depends only on and the property in question).

To remind the reader, the Erdős–Rényi random “graph” is a distribution over graphs that you draw from by including each edge independently with probability . Last time we saw that the existence of an isolated vertex has a sharp threshold at , meaning if is asymptotically smaller than the threshold there will certainly be isolated vertices, and if is larger there will certainly be *no* isolated vertices. We also gave a laundry list of other properties with such thresholds.

One might want to study this phenomenon in general. Even if we might not be able to find all the thresholds we want for a given property, can we classify which properties have thresholds and which do not?

The answer turns out to be mostly yes! For large classes of properties, there are proofs that say things like, “either this property holds with probability tending to one, or it holds with probability tending to zero.” These are called “zero-one laws,” and they’re sort of meta theorems. We’ll see one such theorem in this post relating to constant edge-probabilities in random graphs, and we’ll remark on another at the end.

## Sentences about graphs in first order logic

A zero-one law generally works by defining a class of properties, and then applying a generic first/second moment-type argument to every property in the class.

So first we define what kinds of properties we’ll discuss. We’ll pick a large class: **anything that can be expressed in first-order logic** in the language of graphs. That is, any finite logical statement that uses existential and universal quantifiers over variables, and whose only relation (test) is whether an edge exists between two vertices. We’ll call this test . So you write some sentence in this language, and you take a graph , and you can ask , whether the graph satisfies the sentence.

This seems like a *really* large class of properties, and it is, but let’s think carefully about what kinds of properties can be expressed this way. Clearly the existence of a triangle can be written this way, it’s just the sentence

I’m using for AND, and for OR, and for NOT. Similarly, one can express the existence of a clique of size , or the existence of an independent set of size , or a path of a fixed length, or whether there is a vertex of maximal degree .

Here’s a question: can we write a formula which will be true for a graph if and only if it’s connected? Well such a formula seems like it would have to know about how many vertices there are in the graph, so it could say something like “for all there is a path from to .” It seems like you’d need a family of such formulas that grows with to make anything work. But this isn’t a proof; the question remains whether there is some other tricky way to encode connectivity.

But as it turns out, connectivity is *not* a formula you can express in propositional logic. We won’t prove it here, but we will note at the end of the article that connectivity is in a different class of properties that you can prove has a similar zero-one law.

## The zero-one law for first order logic

So the theorem about first-order expressible sentences is as follows.

**Theorem:** Let be a property of graphs that can be expressed in the first order language of graphs (with the relation). Then for any constant , the probability that holds in has a limit of zero or one as .

*Proof. *We’ll prove the simpler case of , but the general case is analogous. Given such a graph drawn from , what we’ll do is define a countably infinite family of propositional formulas , and argue that they form a sort of “basis” for all first-order sentences about graphs.

First let’s describe the . For any , the sentence will assert that for every set of vertices and every set of vertices, there is some other vertex connected to the first but not the last .

.

In other words, these formulas encapsulate every possible incidence pattern for a single vertex. It is a strange set of formulas, but they have a very nice property we’re about to get to. So for a fixed , what is the probability that it’s false on vertices? We want to give an upper bound and hence show that the formula is true with probability approaching 1. That is, we want to show that *all* the are true with probability tending to 1.

Computing the probability: we have possibilities to choose these sets, and the probability that some other fixed vertex has the good connections is so the probability is not good is , and taking a product over all choices of gives the probability that *there is some bad vertex * with an exponent of . Combining all this together gives an upper bound of being false of:

And are constant, so the left two terms are polynomials while the rightmost term is an exponentially small function, and this implies that the whole expression tends to zero, as desired.

*Break from proof.*

## A bit of model theory

So what we’ve proved so far is that the probability of every formula of the form being satisfied in tends to 1.

Now look at the set of all such formulas

We ask: is there any graph which satisfies all of these formulas? Certainly it cannot be finite, because a finite graph would not be able to satisfy formulas with sufficiently large values of . But indeed, there is a *countably infinite* graph that works. It’s called the *Rado graph*, pictured below.

The Rado graph has some really interesting properties, such as that it contains *every finite and countably infinite graph* as induced subgraphs. Basically this means, as far as countably infinite graphs go, it’s the big momma of all graphs. It’s *the* graph in a very concrete sense of the word. It satisfies all of the formulas in , and in fact it’s uniquely determined by this, meaning that if any other countably infinite graph satisfies all the formulas in , then that graph is isomorphic to the Rado graph.

But for our purposes (proving a zero-one law), there’s a better perspective than graph theory on this object. In the logic perspective, the set is called a* theory*, meaning a set of statements that you consider “axioms” in some logical system. And we’re asking whether there any model realizing the theory. That is, is there some logical system with a semantic interpretation (some mathematical object based on numbers, or sets, or whatever) that satisfies all the axioms?

A good analogy comes from the rational numbers, because they satisfy a similar property among all ordered sets. In fact, the rational numbers are the unique countable, ordered set with the property that it has no biggest/smallest element and is dense. That is, in the ordering there is always another element between any two elements you want. So the theorem says if you have two countable sets with these properties, then they are actually isomorphic as ordered sets, and they are isomorphic to the rational numbers.

So, while we won’t prove that the Rado graph is a model for our theory , we will use that fact to great benefit. One consequence of having a theory with a model is that the theory is *consistent, *meaning it can’t imply any contradictions. Another fact is that this theory is *complete.* Completeness means that any formula or it’s negation is logically implied by the theory. Note these are syntactical implications (using standard rules of propositional logic), and have nothing to do with the model interpreting the theory.

The proof that is complete actually follows from the uniqueness of the Rado graph as the only countable model of . Suppose the contrary, that is not consistent, then there has to be some formula that is not provable, and it’s negation is also not provable, by starting from . Now extend in two ways: by adding and by adding . Both of the new theories are still countable, and by a theorem from logic this means they both still have countable models. But both of these new models are also countable models of , so they have to both be the Rado graph. But this is very embarrassing for them, because we assumed they disagree on the truth of .

So now we can go ahead and prove the zero-one law theorem.

*Return to proof.*

Given an arbitrary property . Now either or it’s negation can be derived from . Without loss of generality suppose it’s . Take all the formulas from the theory you need to derive , and note that since it is a proof in propositional logic you will only finitely many such . Now look at the probabilities of the : they are *all true *with probability tending to 1, so the implied statement of the proof of (i.e., itself) must also hold with probability tending to 1. And we’re done!

If you don’t like model theory, there is another “purely combinatorial” proof of the zero-one law using something called Ehrenfeucht–Fraïssé games. It is a bit longer, though.

## Other zero-one laws

One might naturally ask two questions: what if your probability is not constant, and what other kinds of properties have zero-one laws? Both great questions.

For the first, there are some extra theorems. I’ll just describe one that has always seemed very strange to me. If your probability is of the form but is *irrational*, then the zero-one law still holds! This is a theorem of Baldwin-Shelah-Spencer, and it really makes you wonder why irrational numbers would be so well behaved while rational numbers are not :)

For the second question, there is another theorem about *monotone *properties of graphs. Monotone properties come in two flavors, so called “increasing” and “decreasing.” I’ll describe increasing monotone properties and the decreasing counterpart should be obvious. A property is called *monotone increasing* if adding edges can never destroy the property. That is, with an empty graph you don’t have the property (or maybe you do), and as you start adding edges eventually you suddenly get the property, but then adding *more* edges can’t cause you to lose the property again. Good examples of this include connectivity, or the existence of a triangle.

So the theorem is that there is an identical zero-one law for monotone properties. Great!

It’s not so often that you get to see these neat applications of logic and model theory to graph theory and (by extension) computer science. But when you do get to apply them they seem very powerful and mysterious. I think it’s a good thing.

Until next time!

# The Giant Component and Explosive Percolation

Last time we left off with a tantalizing conjecture: a random graph with edge probability is almost surely a connected graph. We arrived at that conjecture from some ad-hoc data analysis, so let’s go back and treat it with some more rigorous mathematical techniques. As we do, we’ll discover some very interesting “threshold theorems” that essentially say a random graph will either certainly have a property, or it will certainly not have it.

## Big components

Recalling the basic definition: an Erdős-Rényi (ER) random graph with vertices and edge probability is a probability distribution over all graphs on vertices. Generatively, you draw from an ER distribution by flipping a -biased coin for each pair of vertices, and adding the edge if you flip heads. We call the random event of drawing a graph from this distribution a “random graph” even though it’s not a graph, and we denote an ER random graph by . When , the distribution is the uniform distribution over all graphs on vertices.

Now let’s get to some theorems. The main tools we’ll use are called the *first and second moment method*. Let’s illustrate them by example.

### The first moment method

Say we want to know what values of are likely to produce graphs with isolated vertices (vertices with no neighbors), and which are not. Of course, the value of will depend on in general, but we can already see by example that if then the probability of a fixed vertex being isolated is . We can use the union bound (sum this value over all vertices) to show that the probability of *any* vertex being isolated is at most which also tends to zero very quickly. This is not the first moment method, I’m just making the point that all of our results will be interpreted asymptotically as .

So now we can ask: what is the *expected number *of isolated vertices? If I call the random variable that counts the expected number of isolated* *vertices, then I’m asking about . Really what I’m doing is interpreting as a random variable depending on , and asking about the evolution of as .

Now the *first moment method *states, somewhat obviously, that if the expectation tends to zero then the value of itself also tends to zero. Indeed, this follows from Markov’s inequality, which states that the probability that is bounded by . In symbols,

.

In our case is counting something (it’s integer valued), so asking whether is equivalent to asking whether . The upper bound on the probability of being strictly positive is then just .

So let’s find out when the expected number of isolated vertices goes to zero. We’ll use the wondrous linearity of expectation to split into a sum of counts for each vertex. That is, if is 1 when vertex is isolated and 0 otherwise (this is called an *indicator variable*), then and linearity of expectation gives

Now the expectation of an indicator random variable is just the probability that the event occurs (it’s trivial to check). It’s easy to compute the probability that a vertex is isolated: it’s . So the sum above works out to be . It should really be but the extra factor of doesn’t change anything. The question is what’s the “smallest” way to set as a function of in order to make the above thing go to zero? Using the fact that for all , we get

And setting simplifies the right hand side to . This is almost what we want, so let’s set to be *anything that grows asymptotically faster than *. The notation for this is . Then using some slick asymptotic notation we can prove that the RHS of the inequality above goes to zero, and so the LHS must as well. Back to the big picture: we just showed that the expectation of (the expected number of isolated vertices) goes to zero, and so by the first moment method the value of (the *actual* number of isolated vertices) has to go to zero with probability tending to 1.

Some quick interpretations: when each vertex has neighbors in expectation. Moreover, having no isolated vertices is just a little bit short of the entire graph being connected (our ultimate goal is to figure out exactly when this happens). But already we can see that our conjecture from the beginning is probably false: we aren’t able to use this same method to show that when for some constant rules out isolated vertices as . We just got lucky in our data analysis that 5 is about the natural log of 100 (which is 4.6).

### The second moment method

Now what about the other side of the coin? If is asymptotically *less* than do we necessarily get isolated vertices? That would really put our conjecture to rest. In this case the answer is yes, but it might not be in general. Let’s discuss.

We said that in general if then the value of has to go to zero too (that’s the first moment method). The flip side of this is: if does necessarily the value of also tend to infinity? The answer is not always yes. Here is a gruesome example I originally heard from a book: say is the number of people that will die in the next decade due to an asteroid hitting the earth. The probability that the event happens is quite small, but if it does happen then the number of people that will die is quite large. It is perfectly reasonable for this to drag up the expectation (as the world population grows every decade), but at least we hope a growing population doesn’t by itself increase the *value* of .

Mathematics is on our side here. We’re asking under what conditions on does the following implication hold: implies .

With the first moment method we used Markov’s inequality (a statement about expectation, also called the first moment). With the second moment method we’ll use a statement about the second moment (variances), and the most common is Chebyshev’s inequality. Chebyshev’s inequality states that the probability deviates from its expectation by more than is bounded by . In symbols, for all we have

Now the opposite of , written in terms of deviation from expectation, is . In words, in order for any number to be zero, it has to have a distance of at least from any number . It’s such a stupidly simple statement it’s almost confusing. So then we’re saying that

.

In order to make this probability go to zero, it’s enough to have . Again, the little-o means “grows asymptotically slower than.” So the numerator of the fraction on the RHS will grow asymptotically slower than the denominator, meaning the whole fraction tends to zero. This condition and its implication are together called the “second moment method.”

Great! So we just need to compute and check what conditions on make it fit the theorem. Recall that , and we want to upper bound this in terms of . Let’s compute first.

Now the variance.

Expanding as a sum of indicator variables for each vertex, we can split the square into a sum over pairs. Note that since they are 0-1 valued indicator variables, and is the indicator variable for *both *events happening simultaneously.

By what we said about indicators, the last line is just

And we can compute each of these pieces quite easily. They are (asymptotically ignoring some constants):

Now combining the two terms together (subtracting off the square of the expectation),

Now we divide by to get . Since we’re trying to see if is a sharp threshold, the natural choice is to let . Indeed, using the upper bound and plugging in the little-o bounds the whole quantity by

i.e., the whole thing tends to zero, as desired.

## Other thresholds

So we just showed that the property of having no isolated vertices in a random graph has a *sharp* threshold at . Meaning at any larger probability the graph is almost surely devoid of isolated vertices, and at any lower probability the graph almost surely has some isolated vertices.

This might seem like a miracle theorem, but there turns out to be similar theorems for *lots* of properties. Most of them you can also prove using basically the same method we’ve been using here. I’ll list some below. Also note they are all sharp, two-sided thresholds in the same way that the isolated vertex boundary is.

- The existence of a component of size has a threshold of .
- for any is a threshold for the existence of a
*giant component*of linear size . Moreover, above this threshold no other components will have size . - In addition to being a threshold for having no isolated vertices, it is also a threshold for connectivity.
- is a sharp threshold for the existence of Hamiltonian cycles in the following sense: if then there will be a Hamilton cycle almost surely, if there will be no Hamiltonian cycle almost surely, and if the probability of a Hamiltonian cycle is . This was proved by Kolmos and Szemeredi in 1983. Moreover, there is an efficient algorithm to find Hamiltonian cycles in these random graphs when they exist with high probability.

## Explosive Percolation

So now we know that as the probability of an edge increases, at some point the graph will spontaneously become connected; at some time that is roughly before, the so-called “giant component” will emerge and quickly engulf the entire graph.

Here’s a different perspective on this situation originally set forth by Achlioptas, D’Souza, and Spencer in 2009. It has since become called an “Achlioptas process.”

The idea is that you are watching a random graph grow. Rather than think about random graphs as having a probability above or below some threshold, you can think of it as the number of edges growing (so the thresholds will all be multiplied by ). Then you can imagine that you start with an empty graph, and at every time step someone is adding a new random edge to your graph. Fine, eventually you’ll get so many edges that a giant component emerges and you can measure when that happens.

But now imagine that instead of being given a *single *random new edge, you are given a choice. Say God presents you with two random edges, and you must pick which to add to your graph. Obviously you will eventually still get a giant component, but the question is how long can you prevent it from occurring? That is, how far back can we push the threshold for connectedness by cleverly selecting the new edge?

What Achlioptas and company conjectured was that you can push it back (some), but that when you push it back as far as it can go, the threshold becomes discontinuous. That is, they believed there was a constant such that the size of the largest component jumps from to in steps.

This turned out to be false, and Riordan and Warnke proved it. Nevertheless, the idea has been interpreted in an interesting light. People have claimed it is a useful model of disaster in the following sense. If you imagine that an edge between two vertices is a “crisis” relating two entities. Then in every step God presents you with two crises and you only have the resources to fix one. The idea is that when the entire graph is connected, you have this one big disaster where all the problems are interacting with each other. The percolation process describes how long you can “survive” while avoiding the big disaster.

There are critiques of this interpretation, though, mainly about how simplistic it is. In particular, an Achlioptas process models a crisis as an exogenous force when in reality problems are usually endogenous. You don’t expect a meteor to hit the Earth, but you do expect humans to have an impact on the environment. Also, not everybody in the network is trying to avoid errors. Some companies thrive in economic downturns by managing your toxic assets, for example. So one could reasonably argue that Achlioptas processes aren’t complex enough to model the realistic types of disasters we face.

Either way, I find it fantastic that something like a random graph (which for decades was securely in pure combinatorics away from applications) is spurring such discussion.

Next time, we’ll take one more dive into the theory of Erdős-Rényi random graphs to prove a very “meta” theorem about sharp thresholds. Then we’ll turn our attention to other models of random graphs, hopefully more realistic ones :)

Until then!

# Multiple Qubits and the Quantum Circuit

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!