So by now we’ve understood that quantum circuits consist of a sequence of gates , where each is an 8-by-8 matrix that operates “locally” on some choice of three (or fewer) qubits. And in your head you imagine starting with some state vector and applying each locally to its three qubits until the end when you measure the state and get some classical output.

But the point I want to make is that actually changes the whole state vector , because the three qubits it acts “locally” on are part of the entire basis. Here’s an example. Suppose we have three qubits and they’re in the state

Recall we abbreviate basis states by subscripting them by binary strings, so , and a valid state is any unit vector over the possible basis elements. As a vector, this state is

Say we apply the gate that swaps the first and third qubits. “Locally” this gate has the following matrix:

where we index the rows and columns by the relevant strings in lexicographic order: 00, 01, 10, 11. So this operation leaves and the same while swapping the other two. However, as an operation on three qubits the operation looks quite different. And it’s sort of hard to describe a general way to write it down as a matrix because of the choice of indices. There are three different perspectives.

**Perspective 1:** if the qubits being operated on are sequential (like, the third, fourth, and fifth qubits), then we can write the matrix as where a tensor product of matrices is the Kronecker product and (the number of qubits adds up). Then the final operation looks like a “tiled product” of identity matrices by , but it’s a pain to write out. Let me hurt my self for your sake, dear reader.

And each copy of looks like

That’s a mess, but if you write it out for our example of swapping the first and third qubits of a three-qubit register you get the following:

And this makes sense: the gate changes any entry of the state vector that has values for the first and third qubit that are different. This is what happens to our state:

**Perspective 2:** just assume every operation works on the first three qubits, and wrap each operation in between an operation that swaps the first three qubits with the desired three. So like for a swap operation. Then the matrix form looks a bit simpler, and it just means we permute the columns of the matrix form we gave above so that it just has the form . This allows one to retain a shred of sanity when trying to envision the matrix for an operation that acts on three qubits that are not sequential. The downside is that to actually use this perspective in an analysis you have to carry around the extra baggage of these permutation matrices. So one might use this as a simplifying assumption (a “without loss of generality” statement).

**Perspective 3:** ignore matrices and write things down in a summation form. So if is the permutation that swaps 1 and 3 and leaves the other indices unchanged, we can write the general operation on a state as .

The third option is probably the nicest way to do things, but it’s important to keep the matrix view in mind for many reasons. Just one quick reason: “errors” in quantum gates (that are meant to approximately compute something) compound linearly in the number of gates because the operations are linear. This is a key reason that allows one to design quantum analogues of error correcting codes.

So we’ve established that the basic (atomic) quantum gates are “local” in the sense that they operate on a fixed number of qubits, but they are not local in the sense that they can screw up the entire state vector.

When I was chugging through learning this stuff (and I still have far to go), I wanted to come up with an alternate characterization of the word “local” so that I would feel better about using the word “local.” Mathematicians are as passionate about word choice as programmers are about text editors. In particular, for a long time I was ignorantly convinced that quantum gates that act on a small number of qubits don’t affect the *marginal distribution* of measurement outcomes for other qubits. That is, I thought that if acts on qubits 1,2,3, then and have the same probability of a measurement producing a 1 in index 4, 5, etc, *conditioned on fixing a measurement outcome for qubits 1,2,3*. In notation, if is a random variable whose values are binary strings and is a state vector, I’ll call the random process of measuring a state vector and getting a string , then my claim was that the following was true for every and every :

You could try to prove this, and you would fail because it’s false. In fact, it’s even false if acts on only *a single *qubit! Because it’s so tedious to write out all of the notation, I decided to write a program to illustrate the counterexample. (The most brazenly dedicated readers will try to prove this false fact and identify where the proof fails.)

import numpy H = (1/(2**0.5)) * numpy.array([[1,1], [1,-1]]) I = numpy.identity(4) A = numpy.kron(H,I)

Here is the 2 by 2 *Hadamard matrix*, which operates on a single qubit and maps , and . This matrix is famous for many reasons, but one simple use as a quantum gate is to generate uniform random coin flips. In particular, measuring outputs 1 and 0 with equal probability.

So in the code sample above, is the mapping which applies the Hadamard operation to the first qubit and leaves the other qubits alone.

Then we compute some arbitrary input state vector

def normalize(z): return (1.0 / (sum(abs(z)**2) ** 0.5)) * z v = numpy.arange(1,9) w = normalize(v)

And now we write a function to compute the probability of some query conditioned on some fixed bits. We simply sum up the square norms of all of the relevant indices in the state vector.

def condProb(state, query={}, fixed={}): num = 0 denom = 0 dim = int(math.log2(len(state))) for x in itertools.product([0,1], repeat=dim): if any(x[index] != b for (index,b) in fixed.items()): continue i = sum(d << i for (i,d) in enumerate(reversed(x))) denom += abs(state[i])**2 if all(x[index] == b for (index, b) in query.items()): num += abs(state[i]) ** 2 if num == 0: return 0 return num / denom

So if the query is `query = {1:0}`

and the fixed thing is `fixed = {0:0}`

, then this will compute the probability that the measurement results in the second qubit being zero conditioned on the first qubit also being zero.

And the result:

Aw = A.dot(w) query = {1:0} fixed = {0:0} print((condProb(w, query, fixed), condProb(Aw, query, fixed))) # (0.16666666666666666, 0.29069767441860467)

So they are not equal in general.

Also, in general we won’t work explicitly with full quantum gate matrices, since for qubits the have size which is big. But for finding counterexamples to guesses and false intuition, it’s a great tool.

Let’s close this post with concrete examples of quantum gates. Based on the above discussion, we can write out the 2 x 2 or 4 x 4 matrix form of the operation and understand that it can apply to any two qubits in the state of a quantum program. Gates are most interesting when they’re operating on entangled qubits, and that will come out when we visit our first quantum algorithm next time, but for now we will just discuss at a naive level how they operate on the basis vectors.

We introduced the Hadamard gate already, but I’ll reiterate it here.

Let be the following 2 by 2 matrix, which operates on a single qubit and maps , and .

One can use to generate uniform random coin flips. In particular, measuring outputs 1 and 0 with equal probability.

Let be the 2 x 2 matrix formed by swapping the columns of the identity matrix.

This gate is often called the “Pauli-X” gate by physicists. This matrix is far too simple to be named after a person, and I can only imagine it is still named after a person for the layer of obfuscation that so often makes people feel smarter (same goes for the Pauli-Y and Pauli-Z gates, but we’ll get to those when we need them).

If we’re thinking of as the boolean value “false” and as the boolean value “true”, then the quantum NOT gate simply swaps those two states. In particular, note that composing a Hadamard and a quantum NOT gate can have interesting effects: , but . In the second case, the minus sign is the culprit. Which brings us to…

Given an angle , we can “shift the phase” of one qubit by an angle of using the 2 x 2 matrix .

“Phase” is a term physicists like to use for angles. Since the coefficients of a quantum state vector are complex numbers, and since complex numbers can be thought of geometrically as vectors with direction and magnitude, it makes sense to “rotate” the coefficient of a single qubit. So does nothing to and it rotates the coefficient of by an angle of .

Continuing in our theme of concreteness, if I have the state vector and I apply a rotation of to the second qubit, then my operation is the matrix which maps and . That would map the state to .

If we instead used the rotation by we would get the output state .

In the last post in this series we gave the quantum AND gate and left the quantum OR gate as an exercise. Rather than write out the matrix again, let me remind you of this gate using a description of the effect on the basis where . Recall that we need three qubits in order to make the operation reversible (which is a consequence of all unitary gates being unitary matrices). Some notation: is the XOR of two bits, and is AND, is OR. The quantum AND gate maps

In words, the third coordinate is XORed with the AND of the first two coordinates. We think of the third coordinate as a “scratchwork” qubit which is maybe prepared ahead of time to be in state zero.

Simiarly, the quantum OR gate maps . As we saw last time these combined with the quantum NOT gate (and some modest number of scratchwork qubits) allows quantum circuits to simulate any classical circuit.

The last example in this post is a meta-gate that represents a conditional branching. If we’re given a gate acting on qubits, then we define the *controlled-A* to be an operation which acts on qubits. Let’s call the added qubit “qubit zero.” Then controlled-A does nothing if qubit zero is in state 0, and applies if qubit zero is in state 1. Qubit zero is generally called the “control qubit.”

The matrix representing this operation decomposes into blocks if the control qubit is actually the first qubit (or you rearrange).

A common example of this is the controlled-NOT gate, often abbreviated CNOT, and it has the matrix

Okay let’s take a step back and evaluate our life choices. So far we’ve spent a few hours of our time motivating quantum computing, explaining the details of qubits and quantum circuits, and seeing examples of concrete quantum gates and studying measurement. I’ve hopefully hammered into your head the notion that quantum states which aren’t pure tensors (i.e. entangled) are where the “weirdness” of quantum computing comes from. But we haven’t seen any examples of quantum algorithms yet!

Next time we’ll see our first example of an algorithm that is genuinely quantum. We won’t tackle factoring yet, but we will see quantum “weirdness” in action.

Until then!

]]>

It seems that Math ∩ Programming has become something of a resource. In a recent survey, I found out that my readers are as young as 15 and as old as 70. And they come from all over the world. It seems that already, even before graduating with my PhD, I’ve made a bigger dent in the world with my blog than my research papers likely ever will.

And I have no intention to stop. These last few months have been slow due to job searching, grant applications, and a horde of research projects. The graph isomorphism brouhaha didn’t help my productivity either, but it was worth it on a personal level :)

Indeed, the more I blog the more ideas I get. Here are just a few titles of unfinished drafts in my queue:

- The group theoretic view of quantum gates
- Singular value decomposition
- Matrix completion and recommender systems
- The unreasonable effectiveness of the Multiplicative Weights Update Algorithm
- Cryptographic hardness assumptions — a primer
- VC-dimension, Occam’s razor, and the geometry of hypotheses
- Big dimensions, and what you can do about it
- The quantum Fourier transform
- Linear programming and the most affordable healthy diet, part 2
- What’s up with graph Laplacians?
- Persistent homology
- Byzantine generals
- Support vector machines

And I’ve been meaning to spend some time on convex optimization techniques, and maybe some deep learning while I’m at it. Feel free to vote for your favorite topics and I’ll try to prioritize accordingly.

I have a few blog-related projects in mind for this year. The one that I want to publicly commit to, my new years resolution, is to finish and publish my book. This should be a surprise because I haven’t announced it anywhere. The tentative title is, **“A Programmer’s Introduction to the Culture of Mathematics.”** It’s intended to be a book introducing mathematics “from scratch,” but with the assumption that the reader is a competent programmer. So I’ll treat the reader like an experienced coder instead of a calculus student (or worse, a researcher). I’ll rely on explanations and analogies via code. In each chapter I’ll implement a full program illustrating what you learned in the chapter, with the code on github, too. I’ll explain every bit of notation, and discuss why the strange aspects of math are the way they are. This book isn’t intended to be a terse reference, but rather a book that you read from end to end. It’s also not meant to be a new “approach” to math. Instead this is more of a travel guide for the mathematical foreigner, with translations and tours and an explanation of mathematical tastes.

I’ve already written a hundred or so pages, and I’d estimate the book is around 1/3 to 1/2 done. I’m resolving to finish it this year. I plan to set up a mailing list within the next month or two for it so those interested can keep informed on my progress.

]]>

**Solution: **(in python)

import random def randomHash(modulus): a, b = random.randint(0,modulus-1), random.randint(0,modulus-1) def f(x): return (a*x + b) % modulus return f def average(L): return sum(L) / len(L) def numDistinctElements(stream, numParallelHashes=10): modulus = 2**20 hashes = [randomHash(modulus) for _ in range(numParallelHashes)] minima = [modulus] * numParallelHashes currentEstimate = 0 for i in stream: hashValues = [h(i) for h in hashes] for i, newValue in enumerate(hashValues): if newValue < minima[i]: minima[i] = newValue currentEstimate = modulus / average(minima) yield currentEstimate

**Discussion:** The technique used here is to use random hash functions. The central idea is the same as the general principle presented in our recent post on hashing for load balancing. In particular, if you have an algorithm that works under the assumption that the data is uniformly random, then the same algorithm will work (up to a good approximation) if you process the data through a randomly chosen hash function.

So if we assume the data in the stream consists of uniformly random real numbers between zero and one, what we would do is the following. Maintain a single number representing the minimum element in the list, and update it every time we encounter a smaller number in the stream. A simple probability calculation or an argument by symmetry shows that the expected value of the minimum is . So your estimate would be . (The extra +1 does not change much as we’ll see.) One can spend some time thinking about the variance of this estimate (indeed, our earlier post is great guidance for how such a calculation would work), but since the data is not random we need to do more work. If the elements are actually integers between zero and , then this estimate can be scaled by and everything basically works out the same.

Processing the data through a hash function chosen randomly from a 2-universal family (and we proved in the aforementioned post that this modulus thing is 2-universal) makes the outputs “essentially random” enough to have the above technique work with some small loss in accuracy. And to reduce variance, you can process the stream in parallel with many random hash functions. This rough sketch results in the code above. Indeed, before I state a formal theorem, let’s see the above code in action. First on truly random data:

S = [random.randint(1,2**20) for _ in range(10000)] for k in range(10,301,10): for est in numDistinctElements(S, k): pass print(abs(est)) # output 18299.75567190227 7940.7497160166595 12034.154552410098 12387.19432959244 15205.56844547564 8409.913113220158 8057.99978043693 9987.627098464103 10313.862295081966 9084.872639057356 10952.745228373375 10360.569781803211 11022.469475216301 9741.250165892501 11474.896038520465 10538.452261306533 10068.793492995934 10100.266495424627 9780.532155130093 8806.382800033594 10354.11482578643 10001.59202254498 10623.87031408308 9400.404915767062 10710.246772348424 10210.087633885101 9943.64709187974 10459.610972568578 10159.60175069326 9213.120899718839

As you can see the output is never off by more than a factor of 2. Now with “adversarial data.”

S = range(10000) #[random.randint(1,2**20) for _ in range(10000)] for k in range(10,301,10): for est in numDistinctElements(S, k): pass print(abs(est)) # output 12192.744186046511 15935.80547112462 10167.188106011634 12977.425742574258 6454.364151175674 7405.197740112994 11247.367453263867 4261.854392115023 8453.228233608026 7706.717624577393 7582.891328643745 5152.918628936483 1996.9365093316926 8319.20208545846 3259.0787592465967 6812.252720480753 4975.796789951151 8456.258064516129 8851.10133724288 7317.348220516398 10527.871485943775 3999.76974425661 3696.2999065091117 8308.843106180666 6740.999794281012 8468.603733730935 5728.532232608959 5822.072220349402 6382.349459544548 8734.008940222673

The estimates here are off by a factor of up to 5, and this estimate seems to get better as the number of hash functions used increases. The formal theorem is this:

**Theorem: **If is the set of distinct items in the stream and and , then with probability at least 2/3 the estimate is between and .

We omit the proof (see below for references and better methods). As a quick analysis, since we’re only storing a constant number of integers at any given step, the algorithm has space requirement , and each step takes time polynomial in to update in each step (since we have to compute multiplication and modulus of ).

This method is just the first ripple in a lake of research on this topic. The general area is called “streaming algorithms,” or “sublinear algorithms.” This particular problem, called *cardinality estimation*, is related to a family of problems called *estimating frequency moments. *The literature gets pretty involved in the various tradeoffs between space requirements and processing time per stream element.

As far as estimating cardinality goes, the first major results were due to Flajolet and Martin in 1983, where they provided a slightly more involved version of the above algorithm, which uses logarithmic space.

Later revisions to the algorithm (2003) got the space requirement down to , which is exponentially better than our solution. And further tweaks and analysis improved the variance bounds to something like a multiplicative factor of . This is called the HyperLogLog algorithm, and it has been tested in practice at Google.

Finally, a theoretically optimal algorithm (achieving an arbitrarily good estimate with logarithmic space) was presented and analyzed by Kane et al in 2010.

]]>

More succinctly put: Google loves hash tables.

As someone with a passion for math and theoretical CS, it’s kind of silly and reductionist. But if you actually work with terabytes of data that can’t fit on a single machine, it also makes sense.

But to understand why hash tables are so applicable, you should have at least a fuzzy understanding of the math that goes into it, which is surprisingly unrelated to the actual act of hashing. Instead it’s the guarantees that a “random enough” hash provides that makes it so useful. The basic intuition is that if you have an algorithm that works well assuming the input data is completely random, then you can probably get a good guarantee by preprocessing the input by hashing.

In this post I’ll explain the details, and show the application to an important problem that one often faces in dealing with huge amounts of data: how to allocate resources efficiently (load balancing). As usual, all of the code used in the making of this post is available on Github.

Next week, I’ll follow this post up with another application of hashing to estimating the number of distinct items in a set that’s too large to store in memory.

To emphasize which specific properties of hash functions are important for a given application, we start by introducing an abstraction: a hash function is just some computable function that accepts strings as input and produces numbers between 1 and as output. We call the set of allowed inputs (for “Universe”). A *family* of hash functions is just a set of possible hash functions to choose from. We’ll use a scripty for our family, and so every hash function in is a function .

You can use a single hash function to maintain an unordered set of objects in a computer. The reason this is a problem that needs solving is because if you were to store items sequentially in a list, and if you want to determine if a specific item is already in the list, you need to potentially check every item in the list (or do something fancier). In any event, without hashing you have to spend some non-negligible amount of time searching. With hashing, you can choose the location of an element based on the value of its hash . If you pick your hash function well, then you’ll have very few collisions and can deal with them efficiently. The relevant section on Wikipedia has more about the various techniques to deal with collisions in hash tables specifically, but we want to move beyond that in this post.

Here we have a family of random hash functions. So what’s the use of having many hash functions? You can pick a hash randomly from a “good” family of hash functions. While this doesn’t seem so magical, it has the informal property that it makes arbitrary data “random enough,” so that an algorithm which you designed to work with truly random data will also work with the hashes of arbitrary data. Moreover, even if an adversary knows and knows that you’re picking a hash function at random, there’s no way for the adversary to manufacture problems by feeding bad data. With overwhelming probability the worst-case scenario will not occur. Our first example of this is in load-balancing.

You can imagine load balancing in two ways, concretely and mathematically. In the concrete version you have a public-facing server that accepts requests from users, and forwards them to a back-end server which processes them and sends a response to the user. When you have a billion users and a million servers, you want to forward the requests in such a way that no server gets too many requests, or else the users will experience delays. Moreover, you’re worried that the League of Tanzanian Hackers is trying to take down your website by sending you requests in a carefully chosen order so as to screw up your load balancing algorithm.

The mathematical version of this problem usually goes with the metaphor of balls and bins. You have some collection of balls and bins in which to put the balls, and you want to put the balls into the bins. But there’s a twist: an adversary is throwing balls at you, and you have to put them into the bins before the next ball comes, so you don’t have time to remember (or count) how many balls are in each bin already. You only have time to do a small bit of mental arithmetic, sending ball to bin where is some simple function. Moreover, whatever rule you pick for distributing the balls in the bins, the adversary knows it and will throw balls at you in the worst order possible.

There is one obvious approach: why not just pick a uniformly random bin for each ball? The problem here is that we need the choice to be persistent. That is, if the adversary throws the same ball at us a second time, we need to put it in the same bin as the first time, and it doesn’t count toward the overall load. This is where the ball/bin metaphor breaks down. In the request/server picture, there is data specific to each user stored on the back-end server between requests (a session), and you need to make sure that data is not lost for some reasonable period of time. And if we were to save a uniform random choice after each request, we’d need to store a number for every request, which is too much. In short, we need the mapping to be persistent, but we also want it to be “like random” in effect.

So what do you do? The idea is to take a “good” family of hash functions , pick one uniformly at random for the whole game, and when you get a request/ball send it to server/bin . Note that in this case, the adversary knows your universal family ahead of time, *and* it knows your algorithm of committing to *some* single randomly chosen , but the adversary does not know which particular you chose.

The property of a family of hash functions that makes this strategy work is called *2-universality*.

**Definition: **A family of functions from some universe . is called *2-universal* if, for every two distinct , the probability over the random choice of a hash function from that is at most . In notation,

I’ll give an example of such a family shortly, but let’s apply this to our load balancing problem. Our load-balancing algorithm would fail if, with even some modest probability, there is some server that receives many more than its fair share () of the requests. If is 2-universal, then we can compute an upper bound on the expected load of a given server, say server 1. Specifically, pick any element which hashes to 1 under our randomly chosen . Then we can compute an upper bound on the expected number of other elements that hash to 1. In this computation we’ll only use the fact that expectation splits over sums, and the definition of 2-universal. Call the random variable which is zero when and one when , and call . In words, simply represents the number of inputs that hash to 1. Then

So in expectation we can expect server 1 gets its fair share of requests. And clearly this doesn’t depend on the output hash being 1; it works for any server. There are two obvious questions.

- How do we measure the risk that, despite the expectation we computed above,
*some*server is overloaded? - If it seems like (1) is on track to happen, what can you do?

For 1 we’re asking to compute, for a given deviation , the probability that . This makes more sense if we jump to multiplicative factors, since it’s usually okay for a server to bear twice or three times its usual load, but not like times more than it’s usual load. (Industry experts, please correct me if I’m wrong! I’m far from an expert on the practical details of load balancing.)

So we want to know what is the probability that for some small number , and we want this to get small quickly as grows. This is where the Chebyshev inequality becomes useful. For those who don’t want to click the link, for our sitauation Chebyshev’s inequality is the statement that, for any random variable

So all we need to do is compute the variance of the load of a server. It’s a bit of a hairy calculation to write down, but rest assured it doesn’t use anything fancier than the linearity of expectation and 2-universality. Let’s dive in. We start by writing the definition of variance as an expectation, and then we split up into its parts, expand the product and group the parts.

The easy part is , it’s just , and the hard part is . So let’s compute that

In order to continue (and get a reasonable bound) we need an additional property of our hash family which is not immediately spelled out by 2-universality. Specifically, we need that for every and , . In other words, each hash function should evenly split the inputs across servers.

The reason this helps is because we can split into . Using 2-universality to bound the left term, this quantity is at most , and since there are total terms in the double sum above, the whole thing is at most . Note that in our big-O analysis we’re assuming is much bigger than .

Sweeping some of the details inside the big-O, this means that our variance is , and so our bound on the deviation of from its expectation by a multiplicative factor of is at most .

Now we computed a bound on the probability that a *single* server is not overloaded, but if we want to extend that to the worst-case server, the typical probability technique is to take the *union bound* over all servers. This means we just add up all the individual bounds and ignore how they relate. So the probability that *none* of the servers has a load more than a multiplicative factor of is at most . This is only less than one when , so all we can say with this analysis is that (with some small constant probability) no server will have a load worse than times more than the expected load.

So we have this analysis that seems not so good. If we have a million servers then the worst load on one server could potentially be a thousand times higher than the expected load. This doesn’t scale, and the problem could be in any (or all) of three places:

- Our analysis is weak, and we should use tighter bounds because the true max load is actually much smaller.
- Our hash families don’t have strong enough properties, and we should beef those up to get tighter bounds.
- The whole algorithm sucks and needs to be improved.

It turns out all three are true. One heuristic solution is easy and avoids all math. Have some second server (which does not process requests) count hash collisions. When some server exceeds a factor of more than the expected load, send a message to the load balancer to randomly pick a new hash function from and for any requests that don’t have existing sessions (this is included in the request data), use the new hash function. Once the old sessions expire, switch any new incoming requests from those IPs over to the new hash function.

But there are much better solutions out there. Unfortunately their analyses are too long for a blog post (they fill multiple research papers). Fortunately their descriptions and guarantees are easy to describe, and they’re easy to program. The basic idea goes by the name “the power of two choices,” which we explored on this blog in a completely different context of random graphs.

In more detail, the idea is that you start by picking *two *random hash functions , and when you get a new request, you compute both hashes, inspect the load of the two servers indexed by those hashes, and send the request to the server with the smaller load.

This has the disadvantage of requiring bidirectional talk between the load balancer and the server, rather than obliviously forwarding requests. But the advantage is an *exponential* decrease in the worst-case maximum load. In particular, the following theorem holds for the case where the hashes are fully random.

**Theorem: **Suppose one places balls into bins in order according to the following procedure: for each ball pick two uniformly random and independent integers , and place the ball into the bin with the smallest current size. If there are ties pick the bin with the smaller index. Then with high probability the largest bin has no more than balls.

This theorem appears to have been proved in a few different forms, with the best analysis being by Berenbrink et al. You can improve the constant on the by computing more than 2 hashes. How does this relate to a good family of hash functions, which is not quite fully random? Let’s explore the answer by implementing the algorithm in python.

In order to implement the load balancer, we need to have some good hash functions under our belt. We’ll go with the simplest example of a hash function that’s easy to prove nice properties for. Specifically each hash in our family just performs some arithmetic modulo a random prime.

**Definition:** Pick any prime , and for any and define . Let .

This family of hash functions is 2-universal.

**Theorem: **For every ,

*Proof. *To say that is to say that for some integer . I.e., the two remainders of and are equivalent mod . The ‘s cancel and we can solve for

Since , there are possible choices for . Moreover, there is no point to pick bigger than since we’re working modulo . So there are possible values for the right hand side of the above equation. So if we chose them uniformly at random, (remember, is fixed ahead of time, so the only choice is ), then there is a out of chance that the equality holds, which is at most . (To be exact you should account for taking a floor of when does not evenly divide , but it only decreases the overall probability.)

If and were equal then this would be even more trivial: it’s just the fact that there is a unique line passing through any two distinct points. While that’s obviously true from standard geometry, it is also true when you work with arithmetic modulo a prime. In fact, it works using arithmetic over any field.

Implementing these hash functions is easier than shooting fish in a barrel.

import random def draw(p, m): a = random.randint(1, p-1) b = random.randint(0, p-1) return lambda x: ((a*x + b) % p) % m

To encapsulate the process a little bit we implemented a `UniversalHashFamily`

class which computes a random probable prime to use as the modulus and stores . The interested reader can see the Github repository for more.

If we try to run this and feed in a large range of inputs, we can see how the outputs are distributed. In this example is a hundred thousand and is a hundred (it’s not two terabytes, but give me some slack it’s a demo and I’ve only got my desktop!). So the expected bin size for any 2-universal family is just about 1,000.

>>> m = 100000 >>> n = 100 >>> H = UniversalHashFamily(numBins=n, primeBounds=[n, 2*n]) >>> results = [] >>> for simulation in range(100): ... bins = [0] * n ... h = H.draw() ... for i in range(m): ... bins[h(i)] += 1 ... results.append(max(bins)) ... >>> max(bins) # a single run 1228 >>> min(bins) 613 >>> max(results) # the max bin size over all runs 1228 >>> min(results) 1227

Indeed, the max is very close to the expected value.

But this example is misleading, because the point of this was that some adversary would try to screw us over by picking a worst-case input. If the adversary knew exactly which was chosen (which it doesn’t) then the worst case input would be the set of all inputs that have the given hash output value. Let’s see it happen live.

>>> h = H.draw() >>> badInputs = [i for i in range(m) if h(i) == 9] >>> len(badInputs) 1227 >>> testInputs(n,m,badInputs,hashFunction=h) [0, 0, 0, 0, 0, 0, 0, 0, 0, 1227, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

The expected size of a bin is 12, but as expected this is 100 times worse (linearly worse in ). But if we instead pick a random after the bad inputs are chosen, the result is much better.

>>> testInputs(n,m,badInputs) # randomly picks a hash [19, 20, 20, 19, 18, 18, 17, 16, 16, 16, 16, 17, 18, 18, 19, 20, 20, 19, 18, 17, 17, 16, 16, 16, 16, 17, 18, 18, 19, 20, 20, 19, 18, 17, 17, 16, 16, 16, 16, 8, 8, 9, 9, 10, 10, 10, 10, 9, 9, 8, 8, 8, 8, 8, 8, 9, 9, 10, 10, 10, 10, 9, 9, 8, 8, 8, 8, 8, 8, 9, 9, 10, 10, 10, 10, 9, 8, 8, 8, 8, 8, 8, 8, 9, 9, 10, 10, 10, 10, 9, 8, 8, 8, 8, 8, 8, 8, 9, 9, 10]

However, if you re-ran this test many times, you’d eventually get unlucky and draw the hash function for which this actually is the worst input, and get a single huge bin. Other times you can get a bad hash in which two or three bins have all the inputs.

An interesting question is, what is really the worst-case input for this algorithm? I suspect it’s characterized by some choice of hash output values, taking all inputs for the chosen outputs. If this is the case, then there’s a tradeoff between the number of inputs you pick and how egregious the worst bin is. As an exercise to the reader, empirically estimate this tradeoff and find the best worst-case input for the adversary. Also, for your choice of parameters, estimate by simulation the probability that the max bin is three times larger than the expected value.

Now that we’ve played around with the basic hashing algorithm and made a family of 2-universal hashes, let’s see the power of two choices. Recall, this algorithm picks *two* random hash functions and sends an input to the bin with the smallest size. This obviously generalizes to choices, although the theoretical guarantee only improves by a constant factor, so let’s implement the more generic version.

class ChoiceHashFamily(object): def __init__(self, hashFamily, queryBinSize, numChoices=2): self.queryBinSize = queryBinSize self.hashFamily = hashFamily self.numChoices = numChoices def draw(self): hashes = [self.hashFamily.draw() for _ in range(self.numChoices)] def h(x): indices = [h(x) for h in hashes] counts = [self.queryBinSize(i) for i in indices] count, index = min([(c,i) for (c,i) in zip(counts,indices)]) return index return h

And if we test this with the bad inputs (as used previously, all the inputs that hash to 9), as a typical output we get

>>> bins [15, 16, 15, 15, 16, 14, 16, 14, 16, 15, 16, 15, 15, 15, 17, 14, 16, 14, 16, 16, 15, 16, 15, 16, 15, 15, 17, 15, 16, 15, 15, 15, 15, 16, 15, 14, 16, 14, 16, 15, 15, 15, 14, 16, 15, 15, 15, 14, 17, 14, 15, 15, 14, 16, 13, 15, 14, 15, 15, 15, 14, 15, 13, 16, 14, 16, 15, 15, 15, 16, 15, 15, 13, 16, 14, 15, 15, 16, 14, 15, 15, 15, 11, 13, 11, 12, 13, 14, 13, 11, 11, 12, 14, 14, 13, 10, 16, 12, 14, 10]

And a typical list of bin maxima is

>>> results [16, 16, 16, 18, 17, 365, 18, 16, 16, 365, 18, 17, 17, 17, 17, 16, 16, 17, 18, 16, 17, 18, 17, 16, 17, 17, 18, 16, 18, 17, 17, 17, 17, 18, 18, 17, 17, 16, 17, 365, 17, 18, 16, 16, 18, 17, 16, 18, 365, 16, 17, 17, 16, 16, 18, 17, 17, 17, 17, 17, 18, 16, 18, 16, 16, 18, 17, 17, 365, 16, 17, 17, 17, 17, 16, 17, 16, 17, 16, 16, 17, 17, 16, 365, 18, 16, 17, 17, 17, 17, 17, 18, 17, 17, 16, 18, 18, 17, 17, 17]

Those big bumps are the times when we picked an unlucky hash function, which is scarily large, although this bad event would be proportionally less likely as you scale up. But in the good case the load is clearly more even than the previous example, and the max load would get linearly smaller as you pick between a larger set of randomly chosen hashes (obviously).

Coupling this with the technique of switching hash functions when you start to observe a large deviation, and you have yourself an elegant solution.

In addition to load balancing, hashing has a ton of applications. Remember, the main key that you may want to use hashing is when you have an algorithm that works well when the input data is random. This comes up in streaming and sublinear algorithms, in data structure design and analysis, and many other places. We’ll be covering those applications in future posts on this blog.

Until then!

]]>

This is The Inequality. I’ve been told on many occasions that the entire field of machine learning reduces to The Inequality combined with the Chernoff bound (which is proved using The Inequality).

Why does it show up so often in machine learning? Mostly because in analyzing an algorithm you want to bound the probability that some bad event happens. Bad events are usually phrased similarly to

And applying The Inequality we can bound this from above by

The point is that usually is the size of your dataset, which you get to choose, and by picking larger you make the probability of the bad event vanish exponentially quickly in . (Here is unrelated to how I am about to use as weights).

Of course, The Inequality has much deeper implications than bounds for the efficiency and correctness of machine learning algorithms. To convince you of the depth of this simple statement, let’s see its use in an elegant proof of the arithmetic geometric inequality.

**Theorem: **(The arithmetic-mean geometric-mean inequality, general version): For all non-negative real numbers and all positive such that , the following inequality holds:

Note that when all the this is the standard AM-GM inequality.

*Proof.* This proof is due to George Polya (in Hungarian, Pólya György).

We start by modifying The Inequality by a shift of variables , so that the inequality now reads . We can apply this to each giving , and in fact,

Now we have something quite curious: if we call the sum , the above shows that . Moreover, again because that shows that the right hand side of the inequality we’re trying to prove is also bounded by . So we know that *both* sides of our desired inequality (and in particular, the max) is bounded from above by . This seems like a conundrum until we introduce the following beautiful idea: normalize by the thing you think should be the larger of the two sides of the inequality.

Define new variables and notice that just by unraveling the definition. Call this sum . Now we know that

Now we unpack the pieces, and multiply through by , the result is exactly the AM-GM inequality.

Even deeper, there is only one case when The Inequality is tight, i.e. when , and that is . This allows us to use the proof above to come to a full characterization of the case of equality in the proof above. Indeed, the crucial step was that , which is only true when , i.e. when . Spending a few seconds thinking about this gives the characterization of equality if and only if .

So this is excellent: the arithmetic-geometric inequality is a deep theorem with applications all over mathematics and statistics. Adding another layer of indirection for impressiveness, one can use the AM-GM inequality to prove the Cauchy-Schwarz inequality rather directly. Sadly, the Wikipedia page for the Cauchy-Schwarz inequality hardly does it justice as far as the massive number of applications. For example, many novel techniques in geometry and number theory are proved directly from C-S. More, in fact, than I can hope to learn.

Of course, no article about The Inequality could be complete without a proof of The Inequality.

**Theorem: **For all , .

*Proof. *The proof starts by proving a simpler theorem, named after Bernoulli, that for every and every . This is relatively straightforward by induction. The base case is trivial, and

And because , we get Bernoulli’s inequality.

Now for any we can set , and get for every . Note that Bernoulli’s inequality is preserved for larger and larger because . So taking limits of both sides as we get the definition of on the right hand side of the inequality. We can prove a symmetrical inequality for when , and this proves the theorem.

What other insights can we glean about The Inequality? For one, it’s a truncated version of the Taylor series approximation

Indeed, the Taylor remainder theorem tells us that the first two terms approximate around zero with error depending on some constant times . In other words, is a lower bound on around zero. It is perhaps miraculous that this extends to a lower bound everywhere, until you realize that exponentials grow extremely quickly and lines do not.

One might wonder whether we can improve our approximation with higher order approximations. Indeed we can, but we have to be a bit careful. In particular, is only true for nonnegative (because the remainder theorem now applies to , but if we restrict to odd terms we win: is true for all .

What is really surprising about The Inequality is that, at least in the applications I work with, we rarely see higher order approximations used. For most applications, The difference between an error term which is quadratic and one which is cubic or quartic is often not worth the extra work in analyzing the result. You get the same theorem: that something vanishes exponentially quickly.

If you’re interested in learning more about the theory of inequalities, I wholeheartedly recommend The Cauchy-Schwarz Master Class. This book is wonderfully written, and chock full of fun exercises. I know because I do exercises from books like this one on planes and trains. It’s my kind of sudoku :)

]]>