From 997188ad39fe1c254351a3149e5ee28f09339cff Mon Sep 17 00:00:00 2001 From: quantumjim Date: Mon, 28 Sep 2026 11:21:17 +0100 Subject: [PATCH] Add AI-generated transcript notes for lectures 1-4 Transcript-based markdown notes reconstructing the spoken narration for the single/multi-qubit states and gates lectures, and the Fun with Matrices / Circuits and Universality lectures. Co-Authored-By: Claude Sonnet 5 --- AI notes/1_Single_Qubit_States_and_Gates.md | 301 +++++++++++++ ...lti_Qubit_States_and_Circuit_Identities.md | 266 ++++++++++++ AI notes/3_Fun-with-matrices.md | 333 +++++++++++++++ AI notes/4_Circuits-and-universality.md | 400 ++++++++++++++++++ 4 files changed, 1300 insertions(+) create mode 100644 AI notes/1_Single_Qubit_States_and_Gates.md create mode 100644 AI notes/2_Multi_Qubit_States_and_Circuit_Identities.md create mode 100644 AI notes/3_Fun-with-matrices.md create mode 100644 AI notes/4_Circuits-and-universality.md diff --git a/AI notes/1_Single_Qubit_States_and_Gates.md b/AI notes/1_Single_Qubit_States_and_Gates.md new file mode 100644 index 0000000..5eb0be2 --- /dev/null +++ b/AI notes/1_Single_Qubit_States_and_Gates.md @@ -0,0 +1,301 @@ +# 3 — Representing Single Qubit States and Gates + +*Dr James Wootton, IBM Quantum* + +> **Transcription note:** transcript-based rewrite, using James's actual spoken narration (lightly cleaned up — filler words, mid-take corrections, and recording asides removed) matched against the whiteboard's equation order. + +--- + +## From experimentalist to theorist + +Hello, I'm Dr James Wootton of IBM Quantum, and this is the third lecture in this course on quantum computing. In the first two lectures we have been like experimentalists — at least, a toy model of experimentalists — where we've been getting our hands on qubits, poking them in various ways to see what happens, to get an understanding of what kind of states they have, how they're manipulated through gates, and how they can be used in computation. + +Now it's time to start being a theorist, and write down a mathematical representation of the kind of states and operations we've seen, so that we can better understand it and build up toward things like algorithms. + +--- + +## Reviewing the circuits we already know + +Let's first review a couple of simple circuits we've seen so far, and the behaviour we've seen, so we can start thinking about how to represent them with mathematics. + +The simplest thing we'll have seen so far is the circuit which does nothing — it just takes us from the default initial state to a measurement gate representing a $Z$ measurement, and the result we get is, with certainty, a zero. + +If we have something a little more complicated — where we go into an $X$ gate — this takes the qubit that's destined to give us a zero for a $Z$ measurement, and acts essentially as a NOT gate, giving us with certainty a one. + +We also see examples where we have some probability of getting a zero, and some probability of getting a one. For example, if we do a Hadamard gate: doing the Hadamard gives us exactly $50/50$ probability of getting a zero or a one — at least in theory; if you run that on a real device, noise will have an opinion as to whether you really get $50/50$. + +--- + +## Naming the states: $|0\rangle$ and $|1\rangle$ + +We need some way to represent these mathematically — a way to write down the state that's certain to give us a zero if we do a $Z$ measurement. So what are we going to call it? Let's call it "zero." How are we going to remind ourselves that this is a *qubit state* zero, and not the number zero, or a bit-string zero? When we do vectors we might write a little arrow on top — so what's our equivalent, to remind us this is a quantum state? It's to write a line on one side and an angle bracket on the other: + +$$|0\rangle$$ + +Similarly we define the state destined to give us a $1$ for a $Z$ measurement with certainty: + +$$|1\rangle$$ + +What mathematical object are we going to use to represent these? These are the only two possibilities, because this is a quantum bit — it has to be defined as something with only two possible states, otherwise it's not a quantum version of a bit. + +What we've found is that the best way is to represent these as vectors in a two-dimensional space, where each of these two states is one of two orthogonal vectors. The fact that these are two completely disjoint possibilities is represented by the fact that they're orthogonal. The fact that there are only two possibilities allowed for a qubit is represented by the fact that they sit in a two-dimensional vector space. + +We can use any basis vectors we like, as long as they're orthogonal — but since at this point we're defining things, let's use the simplest ones. One vector points along one particular direction — the $|0\rangle$ direction — and the other points along another. These aren't directions in physical space (that comes later, with the Bloch sphere) — these are just abstract vectors. + +More generally, we can write a qubit state as a general vector, written as an amplitude times the $|0\rangle$ state plus an amplitude times the $|1\rangle$ state — a linear superposition of the two: + +$$|\psi\rangle = a_0|0\rangle + a_1|1\rangle$$ + +--- + +## The inner product + +Let's talk about the notion of a scalar product, or dot product. With vectors it's often useful to have a product of two vectors that yields a scalar — in quantum computing we call this the **inner product**. + +Say we've got a state $|\psi\rangle$ as defined above, with amplitudes $a_0$ and $a_1$ represented as a column vector. We're going to define a **dual** of this — a row vector, with the same amplitudes $a_0$ and $a_1$, except that they're also complex conjugates. That's a bit of a spoiler for later, when we realize we'll need complex numbers — but when we do, this dual has to use the complex conjugate. For real numbers this makes no difference, but for complex numbers it means we reverse the sign of the imaginary part. + +We need some notation to flag that this is a quantum state, but not the same kind as the ket form — this different, dual form we call a **bra**. We flag it up by writing the notation in reverse: + +$$\langle\psi|$$ + +Now, when we have a bra and a ket, we can multiply them together using standard matrix multiplication to get a scalar. Let's multiply the bra form of $|\psi\rangle$ with the ket form: + +$$\langle\psi|\psi\rangle$$ + +This compact notation is really just shorthand for the matrix multiplication of a row vector by a column vector. And in fact, this is the origin of the entire notation — writing things in terms of these brackets came before writing the vectors themselves as "bras" and "kets." It's the word *bracket* that was split up to get *bra* and *ket*. So there you go — hopefully that makes a bit more sense now, and I can stop referring to it as "this weird notation." + +--- + +## Computing $\langle\psi|\psi\rangle$ + +If we do this matrix multiplication — a row vector times a column vector — we're multiplying rows by columns to get the entries of the result. Because we're multiplying a single row by a single column, we get a single scalar as the result, and that scalar is equal to the square of the magnitude of $a_0$ plus the square of the magnitude of $a_1$: + +$$\langle\psi|\psi\rangle = |a_0|^2 + |a_1|^2$$ + +Here I'm using the fact that for any complex number, the complex conjugate times the original number is the square of the magnitude. + +Now let's do something that's not multiplying the bra of a state by the ket of the same state. Let's do the inner product of $|0\rangle$ with $|\psi\rangle$. This is multiplying the basis vector $(1, 0)$ by our vector $(a_0, a_1)$ — well, technically $(1,0)$ is the complex conjugate of the basis vector, but that's just $(1,0)$, so who cares. Doing this multiplication: + +$$\langle0|\psi\rangle = 1\cdot a_0 + 0\cdot a_1 = a_0$$ + +I'm not going through each individual step of the matrix multiplication here since it's just matrix multiplication, and there are vast resources online if that doesn't make sense to you already. + +So we find that the inner product of $|0\rangle$ and $|\psi\rangle$ gives us $a_0$ — exactly the amplitude in front of the $|0\rangle$ state in the superposition. You get "how much zero-ness" there is in the superposition by doing this inner product. + +--- + +## Inner products of the basis states, and normalization + +Let's do a couple of inner products on states we already know well. $\langle0|0\rangle$: doing the matrix multiplication of $(1,0)$ with $(1,0)$, that's $1\times1 + 0\times0 = 1$. Similarly $\langle1|1\rangle$: $0\times0 + 1\times1 = 1$. And the off-diagonals — $\langle0|1\rangle$ and $\langle1|0\rangle$ — both come out as $1\times0 + 0\times1 = 0$. + +$$\langle0|0\rangle = 1, \qquad \langle1|1\rangle = 1, \qquad \langle0|1\rangle = \langle1|0\rangle = 0$$ + +So for a state with itself we get one, and between two orthogonal states we get zero — which is exactly what we should find. This gives us a **normalization condition** for our general state: if we take $|\psi\rangle$ and do the inner product of itself with itself, we get $a_0^* a_0 + a_1^* a_1$, and we want this to be one, for the state to be nicely normalized in the same way that $|0\rangle$ and $|1\rangle$ are: + +$$|a_0|^2 + |a_1|^2 = 1$$ + +--- + +## Working out the $|+\rangle$ state + +Now let's work out what state we get from doing a Hadamard. If we apply a Hadamard to $|0\rangle$, we know that doing a $Z$ measurement afterward, we're equally likely to get zero or one — but if we were to do an $X$ measurement, we'd definitely get zero. So what does this state look like? + +The fact that it's equally likely to give a zero or a one means the state shouldn't be biased toward either. The most even-handed thing is just $|0\rangle + |1\rangle$, since this is no more zero than it is one. But to make sure it's nicely normalized, we add a constant $C$ — let's call this state $|+\rangle$: + +$$|+\rangle = C\big(|0\rangle+|1\rangle\big)$$ + +Taking the inner product of $|+\rangle$ with itself: $\langle+|+\rangle = 2C^2$ (or more generally $2C^*C$ — let's just keep it real, sounds a bit nineties). We want this to equal one, so $2C^2=1$, which implies $C = 1/\sqrt2$: + +$$|+\rangle = \frac{1}{\sqrt2}\big(|0\rangle+|1\rangle\big)$$ + +Why does that give equal probabilities of zero or one? Well, what else would it give? It's basically an argument from symmetry — no reason for it to be more biased toward zero or one, since it's an equal superposition of the two. But in general we can't rely on symmetry arguments to work out probabilities. + +--- + +## The Born rule + +Let's look at the clues we already have for what corresponds to probability. We've got an equation where the magnitude of a complex number squared, plus another magnitude squared, equals one — and separately, we have two things that should equal a probability of a half: the outcomes for zero and for one, both with amplitude $1/\sqrt2$. + +If we look at what $a_0$ and $a_1$ are for the $|+\rangle$ state, we find that the squares of these amplitudes are equal to a half — they're actually equal to the probability. This could be a coincidence, but one thing that lends weight to it not being a coincidence is that in general, these two numbers have to add up to one, just like the probability of getting zero and the probability of getting one have to add up to one, because these are two disjoint possibilities that completely cover all possibilities. + +So it makes sense to say: the probability of getting a zero should be the square of the amplitude for the $|0\rangle$ state, and the probability of getting a one should be the square of the amplitude of the $|1\rangle$ state. You can either be convinced by the arguments so far, or just accept the rule either way — this is called the **Born rule**. + +--- + +## Working out the $|-\rangle$ state + +With this in mind, we can think about what would happen if we did $X$ then Hadamard. From Hello Qiskit or other experimentation, you can find that if you do a $Z$ measurement at the end, you get zero or one with equal probability — so the corresponding state should be some kind of equal superposition of $|0\rangle$ and $|1\rangle$, just like $|+\rangle$. However, if you were to do an $X$ measurement, you'd be certain to get a **one** — the complete opposite of what you get from $|+\rangle$, where you're certain to get a zero. + +So we need a state that's, like $|+\rangle$, an equal superposition of $|0\rangle$ and $|1\rangle$, but orthogonal to $|+\rangle$. The way to do this is simply to put a minus in the middle instead of a plus: + +$$|-\rangle = \frac{1}{\sqrt2}\big(|0\rangle - |1\rangle\big)$$ + +Here the amplitude $a_0 = 1/\sqrt2$, giving probability $P_0 = 1/2$. And for $a_1 = -1/\sqrt2$, this squares again to a half for probabilities — because we're squaring, the minus disappears, and we get the properties we need. You can verify for yourself that the inner product of this state with $|+\rangle$ is zero — they're orthogonal. + +So here we have a state we call $|-\rangle$, orthogonal to $|+\rangle$, just like $|0\rangle$ and $|1\rangle$ are orthogonal to each other. But if you measure either $|+\rangle$ or $|-\rangle$ in the $Z$ basis, you get random outcomes — and if you measure $|0\rangle$ or $|1\rangle$ in the $X$ basis, you'd also get random results. There's actually no way that $|0\rangle$ and $|1\rangle$ are more special than $|+\rangle$ and $|-\rangle$ — we could have started with $|+\rangle$ or $|-\rangle$ and taken the $X$-basis measurement as our favorite instead. There's no mathematical reason one is more important than the other; we just have a bias toward $|0\rangle$, $|1\rangle$, and the $Z$ basis when thinking about quantum computing. They're not in any way more fundamental states — they're just as valid as any other superposition. Everything is a superposition of everything else. + +--- + +## The $Y$ basis, and why we need complex numbers + +Because we're using complex numbers, we can define another pair of orthogonal states, mutually unbiased from the other two bases — the ones with $i$ in them: + +$$\frac{1}{\sqrt2}\big(|0\rangle + i|1\rangle\big)$$ + +Again normalized, with $1/\sqrt2$ — because of the possibility of having complex numbers, this is perfectly valid. If we take $a_1 = i/\sqrt2$, its complex conjugate $a_1^* = -i/\sqrt2$, and multiplying those together to get the probability of one, we find it's a half. + +This is the state that gives a definite result when measured in the $Y$ basis, and you can also define one orthogonal to it. This is just to make sure you know complex numbers are indeed required here — required to incorporate the $Y$ basis, which we already know exists because we covered it very lightly in the Hello Qiskit workbook. So, we'll be needing complex numbers in general as we go forward. + +--- + +## Global phase + +Now let's think about the general form of a quantum state. But before we can nail that down, we need to think about **global phase**. + +Consider the state $|0\rangle$ and the state $-|0\rangle$. How do these differ? $|0\rangle$ has amplitude $a_0=1$, so probability of getting zero is one. $-|0\rangle$ has amplitude $a_0=-1$, so the probability of getting zero is *also* one. If you measured in the $Z$ basis, you'd find no difference between the two states. + +What about the $X$ basis — are they distinguishable there? In the $X$ basis, we can write the $|0\rangle$ state as the superposition of $|+\rangle$ and $|-\rangle$ (I'll leave you to verify this yourself) — with amplitude $a_+ = 1/\sqrt2$ for $|+\rangle$ and $a_- = 1/\sqrt2$ for $|-\rangle$. The probability of getting the $|+\rangle$ outcome, $P_+$, is the square of $a_+$, which is a half for the $|0\rangle$ state. + +What happens for $-|0\rangle$? All those probabilities are the same, because the amplitudes are just multiplied by minus one — but that disappears when we take the square. So it turns out that in the $X$ basis too, these two states are completely indistinguishable. In fact, in *any* measurement we could ever possibly make, these two states are completely indistinguishable. There's no way to tell whether you have a $|0\rangle$ state or a $-|0\rangle$ state — or, more generally, whether you multiplied by any complex phase of magnitude one at all. It gives a perfectly normalized state that's completely indistinguishable, because when we take the magnitude, this phase disappears and doesn't show up in any probability. + +These probabilities for measurement results are the only thing that's actually physical when we're doing anything with qubits — and if there's no way in theory that any measurement could distinguish between two things, are they not the same? So it's true in this case too: when you multiply any state by a **global phase** — this could be a factor of minus one, or any factor of $e^{i\theta}$ for any real $\theta$ — the state is physically indistinguishable. So even though mathematically it might look different, physically it's indistinguishable. Therefore, when writing down the most general form of a quantum state, we don't need to include the ability for a general global phase, because global phases are irrelevant. + +--- + +## Building the general single-qubit state + +So let's write down our general state. We have $|\psi\rangle = a_0|0\rangle + a_1|1\rangle$, where $a_0$ has a real part and an imaginary part, which can be written as two real numbers, the imaginary part multiplied by $i$. We can think of either amplitude in terms of a real and imaginary part — but there's also a way to write complex numbers in **polar form**, again in terms of two real numbers, but differently: a phase and a magnitude. + +If the phase is zero, $e^{i\cdot0}=1$, so it's just a positive number; if the phase is $\pi$, we get $-1$, a negative number; anything else gives a more general complex number. Writing this out: + +$$|\psi\rangle = e^{i\alpha_0}a_0|0\rangle + e^{i\alpha_1}a_1|1\rangle$$ + +(here I'm using "nought" instead of "zero" when speaking, just because I'm inconsistent about what I call a particular number — I've tried for years to be more consistent and have yet to succeed, so please bear with me.) + +As a little trick, we can take $e^{i\alpha_0}$ as a **global phase** and remove it, since we don't care about it any more — it's physically equivalent without it. So this is what we'll call our state $|\psi\rangle$. And now, since it's only the *difference* between $\alpha_0$ and $\alpha_1$ — the relative phase — that's physically relevant, instead of having both, let's have a single parameter $\phi$. This gives us a simpler form, since we've removed one of the numbers and made it have a consistent global phase. + +One other thing to note: since $|a_0|^2 + |a_1|^2 = 1$, these amplitudes aren't really two independent parameters — they have a constraint over them, and we can replace them with a single parameter using a relationship like $\cos^2\theta + \sin^2\theta = 1$, whatever the value of $\theta$. So we can replace the magnitudes $|a_0|$ and $|a_1|$ with $\cos\theta$ and $\sin\theta$ for some $\theta$. There's another aspect here — for particular values of $\theta$, you can get one of them negative and the other positive, and that's a phase difference; so, although the phase is doing the work of that, we can get any balance we want using cos and sin with a single parameter $\theta$. + +I've drawn the $\theta$ slightly floating here because it's actually better for us to use $\theta/2$ rather than just $\theta$ — we'll see why in a moment. So our general form of a single-qubit superposition state is parametrized by two numbers, $\theta$ and $\phi$. + +--- + +## The Bloch sphere + +I have another example of something parametrized by two real numbers, which we might like to call $\theta$ and $\phi$: a point on the surface of a sphere, using polar coordinates — an angle down from the $z$-axis, and an angle around from the $x$-axis. The reason we use $\theta/2$ in our state is so that we get a one-to-one correspondence between states and points on the surface of this sphere. + +Let's look at some particular states. The $|0\rangle$ state has $\theta=0$, and $\phi$ doesn't matter, since $\phi$ is only relevant to the $|1\rangle$ amplitude, and $|1\rangle$ has no amplitude anyway — this is the state at the top of the sphere (with the polar bears). The $|1\rangle$ state has $\theta=\pi$, so $\cos(\pi/2)$ and $\sin(\pi/2)$ — and since $|1\rangle$ is the only state present, $e^{i\phi}$ becomes a global phase and we don't care about it either — this is the state among the penguins, at the bottom. + +The other states we've looked at, $|+\rangle$ and $|-\rangle$, are on the equator: $\theta=\pi/2$. For $\phi=0$ you're pointing along the $x$-axis — that's your $|+\rangle$ state — and for $\phi=\pi$, $e^{i\pi}=-1$, giving you a minus in the superposition instead of a plus, so that's your $|-\rangle$ state. The $Y$-basis states point along the $y$-axis. This is why we call these things $X$, $Y$, and $Z$ measurements — because the two states with a definite result for $Z$ measurement are associated with the $z$-axis of the sphere, and similarly for $X$ and $Y$. That gives us a nice visual understanding, and it's also nice that orthogonal states here are antipodes on this sphere. + +This is a two-dimensional — well, I say "real," meaning "true," not "real" in the sense of real numbers — description of what's going on in a qubit. It's the correspondence between a two-dimensional complex vector space and a three-dimensional real vector space, which lets us imagine this weird two-dimensional complex vector space as a nice, sensible three-dimensional real thing that we, as beings living in a three-dimensional real-valued world, can understand better. It's not the same vector space — that's why orthogonal states in the two-dimensional complex vector space correspond to antipodes in this three-dimensional version. But nevertheless it's a nice correspondence between different forms of things. In fact, we can define a measurement between any pair of antipodes on this sphere — any state anywhere, plus its antipode — and that measurement distinguishes between the two, giving outcome zero for one, and outcome one for the other. + +--- + +## Gates as rotations of the Bloch sphere + +This visualization is also good for thinking about what operations we can perform on single-qubit states. When we apply a gate, we're moving from one state to another — both points on the surface of the sphere (surface, because of our normalization condition — though we'll see examples later, with multiple qubits, where they don't live on the surface). + +The gates we have move things around like a **rotation**. We can have a rotation around the $x$-axis by some angle — for example, a rotation around the $x$-axis by an angle of $\pi$ is a 180-degree rotation, which would take a $|0\rangle$ state and turn it into a $|1\rangle$ state, and take $|1\rangle$ and rotate it round to $|0\rangle$. That's something we've seen before — that's the **$X$ gate**. This is why the $X$ gate is called the $X$ gate: it's a $\pi$ rotation around the $X$-axis. But we can have any angle of rotation, not just $\pi$. + +We can also rotate around the $Z$-axis — this is what's explicitly referenced in the textbook chapter this lecture is based on. This would take something like the $|+\rangle$ state and turn it into $|-\rangle$, and vice versa — that's the $Z$ we've seen before. There are also special names for other rotations around the $Z$-axis: $\pi/2$ we call $S$, $-\pi/2$ we call $S^\dagger$ (reasons we'll see in later lectures), $\pi/4$ we call $T$, and there's also $T^\dagger$ for $-\pi/4$. + +--- + +## Gates as matrices + +Because these are operations applied to vectors, we can also think of them as matrices. + +**Identity.** The identity gate is the one we've discussed already — the trivial gate that does nothing. Its matrix representation is the identity matrix: + +$$I = \begin{pmatrix}1&0\\0&1\end{pmatrix}$$ + +Applying it to a general vector $|\psi\rangle$ with amplitudes $a_0,a_1$ — taking the $1$ from the first row times $a_0$ from the column, plus the $0$ times $a_1$, gives $a_0$ back; likewise the second row gives $a_1$ back. So it does nothing — that's the identity matrix. + +**$X$.** The $X$ gate's matrix looks like this: + +$$X = \begin{pmatrix}0&1\\1&0\end{pmatrix}$$ + +Also multiplying things by ones and zeros — an easy answer, just with the ones and zeros the other way round, so you get the result the other way round: explicitly $(a_1, a_0)$. It has the effect of flipping a bit: $X|0\rangle = |1\rangle$, $X|1\rangle=|0\rangle$. + +What happens when we apply $X$ to $|+\rangle$? It's still got the $1/\sqrt2$ out front, and $|+\rangle = |0\rangle+|1\rangle$, so the $|0\rangle$ turns into $|1\rangle$ and the $|1\rangle$ turns into $|0\rangle$ — but $|0\rangle+|1\rangle$ is the same as $|1\rangle+|0\rangle$, so you get the same state out as you started with: + +$$X|+\rangle = |+\rangle$$ + +That's what we'd expect from Hello Qiskit. However, if you apply $X$ to $|-\rangle$, you get $|1\rangle - |0\rangle$, which isn't quite the same as $|-\rangle$ — but if you take a global phase of minus one, you get back to $|-\rangle$: + +$$X|-\rangle = -|-\rangle$$ + +Since this is just a global phase, it also effectively does nothing. So we can say $X$ does nothing to the $|-\rangle$ state on its own — but it does have an effect we need to account for when superpositions happen: it causes a phase difference between $|+\rangle$ and $|-\rangle$ within superpositions. + +**$Z$.** To see this, let's look at another gate: + +$$Z = \begin{pmatrix}1&0\\0&-1\end{pmatrix}$$ + +Rather like the identity, except with this phase difference between $|0\rangle$ and $|1\rangle$: $Z|0\rangle = |0\rangle$, $Z|1\rangle = -|1\rangle$. So $Z$'s action on $|0\rangle,|1\rangle$ is the same as $X$'s action on $|+\rangle,|-\rangle$. When we apply $Z$ to a general superposition, it keeps the $|0\rangle$ part as $|0\rangle$, but multiplies the $|1\rangle$ part by minus one — and because this phase difference isn't global, you can't pull the minus out and ignore it; it's actually relevant. + +Let's see this on $|+\rangle$ and $|-\rangle$. $|+\rangle$ is the superposition of $|0\rangle$ and $|1\rangle$ with amplitude $1/\sqrt2$ each — but because of $Z$'s effect, instead of adding $|0\rangle$ and $|1\rangle$, we get a minus in there, giving us $|-\rangle$: + +$$Z|+\rangle = |-\rangle, \qquad Z|-\rangle = |+\rangle$$ + +So $Z$ flips $|+\rangle$ to $|-\rangle$ and vice versa — the same effect on the $X$-basis states that $X$ has on the $Z$-basis states. + +**Hadamard.** Another important gate — this cannot be expressed purely as a rotation around the $X$, $Y$, or $Z$ axis of the Bloch sphere; it can be built out of a few of these combined, but another way to think of it is as a rotation around the axis that lies equally between the $X$ and $Z$ axes, by $\pi$. That's why it takes you from $|0\rangle$ round to $|+\rangle$ and vice versa, and also why it transforms between $|1\rangle$ and $|-\rangle$. + +$$H = \frac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix} = \frac{1}{\sqrt2}(X+Z)$$ + +If you do the matrix multiplication (I'll leave this as an exercise), $H|0\rangle$ gives $|+\rangle$, and $H|1\rangle$ gives $|-\rangle$ — transforming the $Z$-basis states into the $X$-basis states. + +--- + +## Gates as outer products + +Another important thing to note: these gates can also be written in terms of **outer products**. Let's take a ket and multiply it by a bra using matrix multiplication — a bra times a ket gives a scalar; a ket times a bra gives a matrix. + +$$|0\rangle\langle0| = \begin{pmatrix}1\\0\end{pmatrix}\begin{pmatrix}1&0\end{pmatrix} = \begin{pmatrix}1&0\\0&0\end{pmatrix}$$ + +Similarly $|1\rangle\langle1| = \begin{pmatrix}0&0\\0&1\end{pmatrix}$, and adding these together gives us back the identity matrix: + +$$I = |0\rangle\langle0| + |1\rangle\langle1|$$ + +Now let's try this for $X$. Taking the off-diagonal terms $|0\rangle\langle1|$ and $|1\rangle\langle0|$: $|0\rangle\langle1|$ gives a $1$ only in the first row, second column; $|1\rangle\langle0|$ gives a $1$ only in the second row, first column. Adding these: + +$$X = |0\rangle\langle1| + |1\rangle\langle0|$$ + +You might notice this isn't a coincidence with what $X$ actually does — it turns $|0\rangle$ into $|1\rangle$ and vice versa, and here we've got exactly that pairing. If you apply this to $|0\rangle$, you can reinterpret each term as an inner product: $|0\rangle\langle1|$ times $|0\rangle$ becomes $|0\rangle$ multiplied by the inner product $\langle1|0\rangle = 0$, giving zero; and $|1\rangle\langle0|$ times $|0\rangle$ becomes $|1\rangle$ multiplied by $\langle0|0\rangle=1$, giving $|1\rangle$. So one term was "looking for a $|1\rangle$" and does nothing when it doesn't find one, and the other was "looking for a $|0\rangle$," and once it slots in, outputs $|1\rangle$. + +We could equally have written $X$ in terms of its own eigenbasis — the gate that turns $|+\rangle$ into $|+\rangle$, and $|-\rangle$ into $-|-\rangle$: + +$$X = |+\rangle\langle+| - |-\rangle\langle-|$$ + +And similarly, $Z$ can be written $Z=|0\rangle\langle0|-|1\rangle\langle1|$, or, in terms of $|+\rangle$ and $|-\rangle$, as the gate that switches $|+\rangle$ to $|-\rangle$ and $|-\rangle$ to $|+\rangle$. These are some tricks we'll look into more in later lectures, but you can already start playing around with these outer products now. + +--- + +## General $Z$-rotations, and the $S$ gate as $\sqrt Z$ + +Now let's look at more general $Z$-rotations in matrix form. Taking $R_Z(\phi)$, there are two ways to write this down — we'll use the form with $e^{i\phi}$ in this lecture, though it could equivalently be written with $e^{-i\phi/2}$ and $e^{i\phi/2}$ (differing from the other form by a global phase; sometimes one form makes more sense than the other): + +$$R_Z(\phi) = \begin{pmatrix}1&0\\0&e^{i\phi}\end{pmatrix}$$ + +If you put $\pi$ in here, you get $e^{i\pi}=-1$, recovering the known result: + +$$R_Z(\pi) = Z = \begin{pmatrix}1&0\\0&-1\end{pmatrix}$$ + +And $R_Z(\pi/2)$ — what we're calling the $S$ gate, also called the **square root of $Z$**: + +$$R_Z(\pi/2) = S = \sqrt Z = \begin{pmatrix}1&0\\0&i\end{pmatrix}$$ + +We call it the square root of $Z$ because if you do two of these — $S$ followed by another $S$ — that's the same as a $Z$: + +$$SS = S^2 = Z$$ + +That relation will be useful later. And one more useful relation: $HZH = X$ — conjugating $Z$ by a Hadamard gives you $X$. + +$$HZH = X$$ + +--- + +## Closing + +So there you have some gates as matrices. Quantum computing is basically all about playing around with these matrices, playing around with the behaviour we get as we combine them — looking at things like what happens if you do a Hadamard, then a $Z$, then a Hadamard, and finding that this gives you the same as an $X$. Most of the time we'll be doing more complex things than that, but playing around with these sorts of relations is pretty much all that quantum computing is about — you just have to get to bigger and fancier implementations of it. That's what we'll be doing in the future lectures. But that's all for today. Thank you for listening, and I'll see you next time. diff --git a/AI notes/2_Multi_Qubit_States_and_Circuit_Identities.md b/AI notes/2_Multi_Qubit_States_and_Circuit_Identities.md new file mode 100644 index 0000000..fcd240c --- /dev/null +++ b/AI notes/2_Multi_Qubit_States_and_Circuit_Identities.md @@ -0,0 +1,266 @@ +# 4 — Multi Qubit States and Circuit Identities + +*Dr James Wootton, Moth Quantum* + +> **Transcription note:** transcript-based rewrite, using James's actual spoken narration (lightly cleaned up — filler words, mid-take corrections, and recording asides removed) matched against the whiteboard's equation order. + +--- + +## Introducing the tensor product + +Hello and welcome to another lecture in this course on quantum computing, based on the Qiskit textbook. In this lecture we're going to be moving from one-qubit states and gates, as we saw in the last lecture, to two-qubit states and gates. In doing so, we introduce a tool that lets us generalize to any number of qubits: the **tensor product**. + +The tensor product is used quite widely in linear algebra, so you may already know it — if not, let's look at a few examples. Take two $2\times2$ matrices, $A$ and $B$: + +$$A = \begin{pmatrix}a_{00}&a_{01}\\a_{10}&a_{11}\end{pmatrix}, \qquad B = \begin{pmatrix}b_{00}&b_{01}\\b_{10}&b_{11}\end{pmatrix}$$ + +The tensor product $A\otimes B$ gives us a $4\times4$ matrix. I kind of think of it as inserting the matrix on the right into the matrix on the left — we make a bigger, padded-out version of $A$, where the top-left element $a_{00}$ gets copied four times, and each copy multiplied by a different element of $B$: $a_{00}b_{00}$, $a_{00}b_{01}$, $a_{00}b_{10}$, $a_{00}b_{11}$. So here we've multiplied this element of $A$ by the whole $B$ matrix. We continue the same principle for $a_{01}$, $a_{10}$, $a_{11}$ (I won't fill in every entry — same principle, and I don't want to be saying "$a$, $b$, zero and one in various combinations" for the next 15 minutes): + +$$A\otimes B = \begin{pmatrix}a_{00}b_{00}&a_{00}b_{01}&a_{01}b_{00}&a_{01}b_{01}\\a_{00}b_{10}&a_{00}b_{11}&a_{01}b_{10}&a_{01}b_{11}\\a_{10}b_{00}&a_{10}b_{01}&a_{11}b_{00}&a_{11}b_{01}\\a_{10}b_{10}&a_{10}b_{11}&a_{11}b_{10}&a_{11}b_{11}\end{pmatrix}$$ + +That's how you do a tensor product of two $2\times2$ matrices, and hopefully that also gives you a flavor of how it works more generally. + +Now let's do it for vectors. Take vector $|a\rangle$ with amplitudes $a_0,a_1$, represented as the column vector $(a_0,a_1)$, and vector $|b\rangle = (b_0,b_1)$. The tensor product of these vectors works the same way — take the first element of $|a\rangle$, multiply it by the whole of $|b\rangle$; take the second element of $|a\rangle$, multiply it by the whole of $|b\rangle$ again: + +$$|a\rangle\otimes|b\rangle = \begin{pmatrix}a_0\begin{pmatrix}b_0\\b_1\end{pmatrix}\\a_1\begin{pmatrix}b_0\\b_1\end{pmatrix}\end{pmatrix} = \begin{pmatrix}a_0b_0\\a_0b_1\\a_1b_0\\a_1b_1\end{pmatrix}$$ + +This is going to be pretty important for what we're going to say today. + +--- + +## Recap: the single-qubit case + +Let's briefly review what happened in the single-qubit case. We found we can take a single measurement — the $Z$ measurement is our favorite — and there are two possible outcomes, $0$ and $1$. We define a state $|0\rangle$ that's definitely going to give us the outcome zero, and a state $|1\rangle$ that's definitely going to give us the outcome one. Any other state can be expressed as a linear superposition of these two, expressed as vectors: + +$$c_0|0\rangle + c_1|1\rangle = |\psi\rangle$$ + +--- + +## The two-qubit case + +What about the two-qubit case? If we measure both of our qubits in the $Z$ basis, we have four possible outcomes: $00, 01, 10, 11$. Similarly, we can define four vectors that correspond to those four cases — $|00\rangle$, representing the state where we definitely get outcome zero for both qubits; $|01\rangle$, meaning definitely zero for one qubit and definitely one for the other; and so on for $|10\rangle$ and $|11\rangle$. + +$$|00\rangle, \qquad |01\rangle, \qquad |10\rangle, \qquad |11\rangle$$ + +With these four basis states we can write any state as a superposition of them, with complex amplitudes in general, and the amplitudes have to satisfy a normalization condition — and global phase is again irrelevant. The probability of getting outcome $00$ again satisfies the Born rule: it's the square of the amplitude of $|00\rangle$. If we take a sum over all $j,k$ (each either zero or one) of $|c_{jk}|^2$, this has to equal one, since these individually are probabilities: + +$$c_{00}|00\rangle + c_{01}|01\rangle + c_{10}|10\rangle + c_{11}|11\rangle, \qquad \sum_{j,k\in\{0,1\}}|c_{jk}|^2 = 1$$ + +That's how you represent a two-qubit state. Put a three-bit string in the kets and this represents a three-qubit state; five, five qubits; a 72-bit string inside the kets, and it represents the state of 72 qubits. This is basically how you do it. But you might be wondering: what does $|00\rangle$ actually look like as a vector? + +--- + +## Basis vectors as tensor products + +We can represent these vectors as tensor products of vectors we already know: + +$$|00\rangle = |0\rangle\otimes|0\rangle = \begin{pmatrix}1\\0\\0\\0\end{pmatrix}$$ + +For $|01\rangle$: the tensor product of $|0\rangle=(1,0)$ with $|1\rangle=(0,1)$. The first two elements of this four-dimensional vector are a copy of $(0,1)$, and the next two elements are all multiplied by the zero in $|0\rangle$'s second entry, so they're zero too: + +$$|01\rangle = \begin{pmatrix}1\\0\end{pmatrix}\otimes\begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\1\\0\\0\end{pmatrix}$$ + +Each of these corresponds to a different possible bit string — that one's $00$, that one's $01$, the next is $10$, and the last is $11$. + +Also, if we have two single-qubit state vectors $|a\rangle$ (amplitudes $a_0,a_1$) and $|b\rangle$ (amplitudes $b_0,b_1$), their tensor product describes a two-qubit state in which one qubit is in state $|a\rangle$ and the other is in state $|b\rangle$: + +$$|a\rangle=a_0|0\rangle+a_1|1\rangle, \qquad |b\rangle=b_0|0\rangle+b_1|1\rangle, \qquad |a\rangle\otimes|b\rangle$$ + +So you can still do all your familiar single-qubit stuff on two-qubit states, but you have to use a tensor product to make it more general. + +--- + +## Applying single-qubit gates within a multi-qubit register + +If we were just talking about single-qubit states and wanted to apply an $X$ to the qubit in state $|a\rangle$, we'd just apply the $X$ matrix to that vector. But for two-qubit states, you can't just apply that $2\times2$ matrix to the four-dimensional vector describing both qubits — you need a $4\times4$ matrix that does an $X$ to the qubit you want, and nothing to the other. We already know how to do nothing — the identity matrix — and with these we do a tensor product again: $X\otimes I$. + +$$X = \begin{pmatrix}0&1\\1&0\end{pmatrix}, \qquad I = \begin{pmatrix}1&0\\0&1\end{pmatrix}$$ + +$$X\otimes I = \begin{pmatrix}0&0&1&0\\0&0&0&1\\1&0&0&0\\0&1&0&0\end{pmatrix}$$ + +This matrix applies an $X$ only to the qubit we want, and does nothing to the other — the top-left and bottom-right blocks are zero, and the off-diagonal blocks are identities. (This is what you signed up for, isn't it — listening to a guy talk in binary.) This matrix acts non-trivially on only a single qubit, but is phrased as a two-qubit matrix, so you can apply it to two-qubit states. + +Similarly, if you had three qubits and wanted to do an $X$ on just the middle one, that's $I\otimes X\otimes I$. (One other note: people often write the identity matrix with a blackboard-bold $\mathbb{1}$ — if I ever use that by accident instead of $I$, now you know what it means, since it's easy to slip between the two very common conventions.) + +--- + +## The controlled-NOT matrix + +Now we've phrased some of the single-qubit stuff from last time in this two-qubit context using the tensor product, we can look at some *truly* two-qubit operations — operations that cannot be expressed as the tensor product of two single-qubit gates. That's the **controlled-NOT**. + +The controlled-NOT has non-zero entries only in two blocks on the diagonal — one block looks like the identity, the other looks like an $X$; everything else is zero: + +$$C_x = \begin{pmatrix}1&0&0&0\\0&1&0&0\\0&0&0&1\\0&0&1&0\end{pmatrix}$$ + +(Often people can't be bothered to draw all the zeros in matrices like this — but it's good to do it the first time.) The identity block acts on the subspace spanned by $|00\rangle$ and $|01\rangle$ — the states where this qubit is $|0\rangle$; the $X$ block acts on the subspace spanned by $|10\rangle$ and $|11\rangle$ — where it's $|1\rangle$. So this matrix is telling us: when we have a $|0\rangle$, do nothing (identity); when we have a $|1\rangle$, do the same as an $X$, swapping the amplitudes for those two states. + +This is how you read off, from this matrix, a controlled-NOT where the control is the qubit on the *right* of these bit strings, and the target is the qubit on the *left*. If you want the matrix the other way round, it looks different — you can find it in the textbook, or calculate it yourself. + +--- + +## Applying CNOT: an entangled state appears + +Let's apply the controlled-NOT to a particular state, to see an interesting two-qubit state that can't be represented as a tensor product of two single-qubit states. We'll start with a state that *can* be — a $|+\rangle$ on one qubit and $|0\rangle$ on the other: + +$$|{+}\rangle\otimes|0\rangle = \frac{1}{\sqrt2}\big(|0\rangle+|1\rangle\big)\otimes|0\rangle = \frac{1}{\sqrt2}\big(|00\rangle+|10\rangle\big)$$ + +I'm sure you have years of experience with how addition and multiplication play together — it's exactly the same for addition of vectors and tensor products of vectors. + +When we apply the controlled-NOT to this state, we get the same interpretation as before: if the control qubit is $|0\rangle$, nothing happens; if it's $|1\rangle$, the target qubit gets flipped. So for the first term the control is $|0\rangle$, nothing happens; for the second term the control is $|1\rangle$, so the target's zero flips to a one: + +$$C_x\;|{+}\rangle\otimes|0\rangle = \frac{1}{\sqrt2}\big(|00\rangle+|11\rangle\big)$$ + +There is no tensor product of two single-qubit states that yields this result — this is a **truly two-qubit state**. Whenever we have a truly multi-qubit state that cannot be represented as a tensor product of single-qubit states, that's called an **entangled state**. Having an entangled state means we can't describe things as just lots of separate single-qubit things — it's become truly multi-party. + +This is why entangled states are key to quantum computing: if you could describe everything as lots of single-qubit states, you could *simulate* everything as lots of single-qubit states, which is very easy to simulate — and you'd never find anything a normal computer couldn't do. You need truly multi-qubit effects to find the advantage of quantum computing over conventional computing. That's why you need entanglement. + +--- + +## CNOT on $|{+}{-}\rangle$, and phase kickback + +Now let's look at applying the controlled-NOT to the tensor product of a $|+\rangle$ and a $|-\rangle$. This gives $\tfrac12$ (the two factors of $1/\sqrt2$ multiplied together), and a superposition of all the basis states, but with a few minus-one phases in there from the $|-\rangle$ state — a phase of minus one comes in whenever there's a one on that particular bit value, because of the phase coming from that side: + +$$|{+}\rangle\otimes|{-}\rangle = \frac{1}{2}\big(|00\rangle-|01\rangle+|10\rangle-|11\rangle\big)$$ + +Now let's look at cases where the controlled-NOT is controlled on the qubit written on the *left* and targeted on the qubit on the *right* — the opposite convention from the one in the textbook. The reason for taking the opposite convention is that controlled-NOTs do go both ways round, so it's important to see them both ways round — it's not that I'm choosing an "opposite" convention where only one should exist; both conventions exist simultaneously, they just differ by how you choose the control and target. (Also, I prefer it this way round!) + +Let's apply this controlled-NOT to $|{+}{-}\rangle$. Going through each basis state: for the first two ($|00\rangle,|01\rangle$) the control is $|0\rangle$, so nothing happens; for the other two ($|10\rangle,|11\rangle$) the control is $|1\rangle$, so the target flips — $|0\rangle\to|1\rangle$ and $|1\rangle\to|0\rangle$: + +$$C_x\;|{+}{-}\rangle = \frac{1}{2}\big(|00\rangle-|01\rangle+|11\rangle-|10\rangle\big) = |{-}\rangle\otimes|{-}\rangle$$ + +That's exactly what you'd get from the tensor product of $|-\rangle$ with $|-\rangle$ — I'll leave you to verify that yourself, but it's a quite interesting relation: take $|+\rangle$ on one qubit and $|-\rangle$ on the other, apply a CX, and you end up with $|-\rangle$ on *both* qubits. The qubit we're thinking of as the target is unchanged in this process; the qubit we're thinking of as the control *does* get changed. There's something interesting happening here. + +To see what, note that $|{-}\rangle\otimes|{-}\rangle$ can be expressed as a superposition of $|0\rangle$ on the control qubit tensored with $|{-}\rangle$, and $|1\rangle$ on the control qubit tensored with $|{-}\rangle$: + +$$|{+}\rangle\otimes|{-}\rangle = \frac{1}{\sqrt2}\Big(|0\rangle\otimes|{-}\rangle + |1\rangle\otimes|{-}\rangle\Big)$$ + +Look at these two terms individually. $C_x\;|0\rangle\otimes|{-}\rangle$: following the same steps as before, this can be expressed as $|0\rangle\otimes|{-}\rangle$ — no surprise, since the control qubit is $|0\rangle$, so nothing should happen. But apply it to the other term, $|1\rangle\otimes|{-}\rangle$: the control is $|1\rangle$, so we apply an $X$ to the $|-\rangle$ state. And when you apply an $X$ to $|-\rangle$, it doesn't affect that state, but does give a global phase of minus one: + +$$C_x\;|0\rangle\otimes|{-}\rangle = |0\rangle\otimes|{-}\rangle, \qquad C_x\;|1\rangle\otimes|{-}\rangle = -|1\rangle\otimes|{-}\rangle$$ + +$$X|{-}\rangle = -|{-}\rangle$$ + +But when we applied it to the superposition, that global phase of minus isn't global anymore — it changes *this* term's phase from a plus to a minus, and that's how the overall $|+\rangle$ on the control side becomes a $|-\rangle$. The global phase became a **relative** phase, and therefore becomes relevant. This effect is known as **phase kickback**, and it's a quite useful effect that shows up in many algorithms. + +--- + +## CNOT's truth table for $|\pm\rangle$ inputs + +If we look at a few more states, we find the CX has the effect of turning $|{+}{+}\rangle$ into $|{+}{+}\rangle$ (no change — only plus-one phases, so nothing to kick back), $|{+}{-}\rangle$ into $|{-}{-}\rangle$ (as we just found), $|{-}{+}\rangle$ into $|{-}{+}\rangle$ (no effect either, since the target's phase is just plus, multiplying by $+1$), and $|{-}{-}\rangle$ into $|{+}{-}\rangle$ (a phase to kick back that flips the initial phase): + +$$C_x: \quad |{+}{+}\rangle\to|{+}{+}\rangle,\quad |{+}{-}\rangle\to|{-}{-}\rangle,\quad |{-}{+}\rangle\to|{-}{+}\rangle,\quad |{-}{-}\rangle\to|{+}{-}\rangle$$ + +With this truth table you can come up with a completely different interpretation of what the controlled-NOT does: instead of looking at the control qubit, seeing if it's $|0\rangle$ or $|1\rangle$, and deciding whether to do an $X$ on the target — it also effectively looks at the *target* qubit, to see whether it's in state $|+\rangle$ or $|-\rangle$, and in the case where it's $|-\rangle$, applies a $Z$ to the *control* qubit, since $Z$ is the operation that flips $|+\rangle$s to $|-\rangle$s and vice versa: + +$$Z|{+}\rangle = |{-}\rangle$$ + +Don't get too stuck on the idea of the controlled-NOT being "the thing that looks at the control and decides whether to do an $X$ on the target" — there are multiple valid interpretations of what a controlled-NOT does. It's a truly quantum gate, and therefore has lots of lovely mysteries for us to ponder. + +--- + +## CNOT in Dirac notation + +To complete our meditation upon the controlled-NOT, let's look at it in Dirac notation form. First let's remind ourselves what it looks like as a matrix — the version with the control on the left, target on the right — as a block-diagonal form, where the first block is identity and the second is $X$: + +$$C_x = \begin{pmatrix}1&0&&\\0&1&&\\&&0&1\\&&1&0\end{pmatrix}$$ + +The first block pertains to any state where the control is $|0\rangle$, the second to any state where the control is $|1\rangle$. This means — and there's a bit of a leap between these two steps, so don't worry if you don't instantly see it, hopefully once we're there you'll see how it got there — we can write this down as a projector onto $|0\rangle$ (only affecting states where the control qubit is $|0\rangle$), tensor-producted with the identity, plus a projector onto $|1\rangle$, tensor-producted with an $X$: + +$$C_x = |0\rangle\langle0|\otimes\mathbb{1} + |1\rangle\langle1|\otimes X$$ + +Now let's use the expansions of these gates into their Dirac-notation outer-product form. The identity can be expressed as $|0\rangle\langle0|+|1\rangle\langle1|$, but equally as $|+\rangle\langle+|+|-\rangle\langle-|$. And $X$ can be represented as the gate that turns $|0\rangle$ into $|1\rangle$ and vice versa — but also as the gate that turns $|+\rangle$ into itself and gives $|-\rangle$ a phase of minus: + +$$\mathbb{1} = |0\rangle\langle0|+|1\rangle\langle1| = |{+}\rangle\langle{+}|+|{-}\rangle\langle{-}|$$ + +$$X = |0\rangle\langle1|+|1\rangle\langle0| = |{+}\rangle\langle{+}|-|{-}\rangle\langle{-}|$$ + +Substituting these identity expansions into our $C_x$ expression — first for the identity term, using $\mathbb{1}=|{+}\rangle\langle{+}|+|{-}\rangle\langle{-}|$, and then for the $X$ term, using $X=|{+}\rangle\langle{+}|-|{-}\rangle\langle{-}|$: + +$$C_x = |0\rangle\langle0|\otimes|{+}\rangle\langle{+}| + |0\rangle\langle0|\otimes|{-}\rangle\langle{-}| + |1\rangle\langle1|\otimes|{+}\rangle\langle{+}| - |1\rangle\langle1|\otimes|{-}\rangle\langle{-}|$$ + +Now we collect terms. Collecting the two terms sharing $|{+}\rangle\langle{+}|$ on the target qubit: on the control side we get $|0\rangle\langle0|+|1\rangle\langle1|$, which is just the identity. Collecting the two terms sharing $|{-}\rangle\langle{-}|$: on the control side we get $|0\rangle\langle0|-|1\rangle\langle1|$, which is $Z$: + +$$C_x = \mathbb{1}\otimes|{+}\rangle\langle{+}| + Z\otimes|{-}\rangle\langle{-}|$$ + +This completes the picture: we can see the CX in the normal way — the first matrix form we wrote down — or in a way where it looks to see whether the *target* qubit is $|+\rangle$ (and if so, does nothing) or $|-\rangle$ (and if so, does a $Z$ to the control). + +So there I've told you a lot about controlled-NOTs — we've really sort of meditated on them for a while, because they're really very key, and there's a lot of cool stuff to just thinking about controlled-NOTs. Forget Grover's algorithm, Shor's algorithm, all that nonsense — controlled-NOTs are more than enough coolness to keep you going for some time. But we're going to have to move on, unfortunately, to a few circuit identities: simple ways of combining gates to get useful effects that come up a lot in quantum computing. + +--- + +## The Hermitian conjugate, and Hadamard conjugation of Paulis + +We've already seen that if you have a ket, there's an equivalent representation of that state as a bra. Similarly, if you have a gate represented as a matrix $U$, there's an equivalent representation $U^\dagger$, known as its **Hermitian conjugate**. For now, the only thing we really need is: whatever effect $U$ has on a ket, $U^\dagger$ has the same effect on the corresponding bra. + +$$|\psi\rangle \xrightarrow{U} \qquad \langle\psi| \xrightarrow{U^\dagger}$$ + +There's a notion of a **Hermitian** matrix — one equal to its own Hermitian conjugate: + +$$U = U^\dagger$$ + +Most of the gates we've looked at so far are Hermitian: $X$, $Y$, $Z$, the Hadamard, the controlled-NOT — these are all Hermitian gates. The Hadamard, for example, turns $|0\rangle$ into $|+\rangle$ and $|+\rangle$ into $|0\rangle$ — and it has the same effect on bras: + +$$H|0\rangle=|{+}\rangle, \qquad H|{+}\rangle=|0\rangle$$ +$$\langle0|H=\langle{+}|, \qquad \langle{+}|H=\langle0|$$ + +(Also, $H$ applied to $|-\rangle$ gives $|1\rangle$, and $H$ applied to $|1\rangle$ gives $|-\rangle$ — I won't write those bra versions down, but they follow the same way.) + +This means we can use $H$ to transform different Paulis into each other. $X$ has the form $X = |0\rangle\langle1|+|1\rangle\langle0|$. If we apply a Hadamard on both sides — $HXH$ — the $H$ applied to that $|0\rangle$ turns it into $|+\rangle$, the $H$ applied to that $\langle1|$ turns it into $\langle{-}|$, and so on for the other term, giving us $|{+}\rangle\langle{-}|$ from the first term and $|{-}\rangle\langle{+}|$ from the second. A gate that turns $|+\rangle$ into $-|-\rangle$... — wait, let's write it as it comes out: a gate that turns $|+\rangle$'s component to a $-$ and $|-\rangle$'s component to a $+$ is exactly what the $Z$ gate does: + +$$HXH = Z$$ + +So taking an $X$ gate and putting a Hadamard before and after gives us the effect of a $Z$ gate — something that should be familiar from Hello Qiskit. And if you did an $H$ before and after a $Z$, you'd get an $X$ back: + +$$HZH = X$$ + +--- + +## Turning a controlled-$X$ into a controlled-$Z$ + +This gives us a way of turning a controlled-NOT into a controlled-$Z$, and vice versa. Recall the controlled-NOT expressed with outer products for zero (first term) and one (second term) on the control, and identity/$X$ on the target respectively. + +If we apply the gate $\mathbb{1}\otimes H$ before the controlled-NOT, and $\mathbb{1}\otimes H$ after it, the identities in the first term wouldn't do much — in fact nothing at all — but after, we'd get $H\,\mathbb{1}\,H$ for the first term, and $H\,X\,H$ for the other. What's $H\,\mathbb{1}\,H$? $H$ times identity is just $H$, so $H\,\mathbb{1}\,H$ is $H^2$ — and multiplying $H$ by itself gives you back the identity ($H^2=\mathbb{1}$): + +$$C_x = |0\rangle\langle0|\otimes\mathbb{1} + |1\rangle\langle1|\otimes X$$ + +$$(\mathbb{1}\otimes H)\;C_x\;(\mathbb{1}\otimes H) = |0\rangle\langle0|\otimes(H\mathbb{1}H) + |1\rangle\langle1|\otimes(HXH)$$ + +$$= |0\rangle\langle0|\otimes\mathbb{1} + |1\rangle\langle1|\otimes Z, \qquad H^2=\mathbb{1}$$ + +So the first term stays identity, while the second term becomes $Z$ for exactly the reasons we just saw — instead of a controlled-$X$, we've now got a controlled-$Z$. + +--- + +## $Z$-rotations conjugated by $X$ + +Another useful identity concerns rotations. Let's compare a $Z$-rotation on its own, $R_Z(\theta)$, to the same rotation sandwiched with an $X$ before and after. + +As an example, take a particular point on the Bloch sphere — let's use the $|+\rangle$ state, since it has a convenient property. Apply $R_Z(\theta)$ to it, bringing us to some new point on the equator. Now consider doing an $X$ first, then that same rotation, then another $X$. Since we're starting at $|+\rangle$, and $X$ applied to $|+\rangle$ is just $|+\rangle$, the first $X$ takes us nowhere; then we do the $Z$-rotation, starting from the same place, so we end at the same place; and then the last $X$ — since $X$ is a $\pi$ rotation around the $x$-axis — takes us to the *other* side, rather than back to where the plain rotation ended up. Doing more examples, or just multiplying the matrices, shows that the result is again a rotation around the $Z$-axis by angle $\theta$, but now in the **other direction**: + +$$X\;R_Z(\theta)\;X = R_Z(-\theta)$$ + +So sandwiching a $Z$-rotation between two $X$ gates flips its direction. (The exact same trick works for $Y$-rotations too: $X\;R_Y(\theta)\;X = R_Y(-\theta)$.) + +--- + +## Building a controlled rotation + +With that in mind, here's a circuit to think about: two rotations and two controlled-NOTs. Without any controlled-NOTs, on the bottom line you'd just have $R_Z(\theta)$ followed by $R_Z(-\theta)$ — these two combine to the identity, so nothing would happen: you'd rotate, then un-rotate, and get back exactly to where you were: + +$$R_Z(-\theta)\,R_Z(\theta) = I$$ + +However, if we insert some $X$s around that negative rotation, it gets flipped, as we've just seen, into a *positive* rotation, due to the effect of these flipping the sign of the rotation — so what we'd have instead is a combined rotation of $2\theta$: + +$$X\,R_Z(-\theta)\,X\,R_Z(\theta) = R_Z(2\theta)$$ + +So whether we get no rotation at all, or a rotation of $2\theta$, depends on whether or not we insert those $X$s — and that's exactly what a controlled-NOT does: whether or not we effectively have $X$s on this bottom line is *controlled* on what the top line is doing. So depending on whether the top qubit is $|0\rangle$ or $|1\rangle$, the bottom qubit either experiences no rotation at all, or a $Z$-rotation by angle $2\theta$ — and by choosing $\theta$, you can make this angle whatever you want: + +$$\begin{array}{c}\text{top: }|0\rangle \text{ or } |1\rangle \\ \text{bottom: } R_Z(\theta)\ \oplus\ R_Z(-\theta)\ \oplus\end{array} \quad\longrightarrow\quad \text{controlled-}R_Z(2\theta)$$ + +So what we have here is a **controlled rotation around the $Z$ axis**. And this trick works exactly the same way for $Y$-rotations — since, similarly, $X\;R_Y(\theta)\;X = R_Y(-\theta)$, sandwiching a $Y$-rotation between $X$s the same way gives you the controlled-rotation form you'll see in the textbook version of this lecture. + +--- + +## Closing + +If you look in the textbook section, you'll find more examples of circuit identities, and if you go through the entirety of Hello Qiskit, you'll go through multiple circuit identities there as well — things like how to make a SWAP gate, or how to construct a Toffoli out of single- and two-qubit gates. I won't go through them all explicitly now, but you now have the tools to start trying to understand how these things work for yourself. And there is no better exercise in knowing how quantum computing works than to do just that. diff --git a/AI notes/3_Fun-with-matrices.md b/AI notes/3_Fun-with-matrices.md new file mode 100644 index 0000000..a43a58a --- /dev/null +++ b/AI notes/3_Fun-with-matrices.md @@ -0,0 +1,333 @@ +# 5 — Fun with Matrices + +*Dr James Wootton, Moth Quantum* + +> **Transcription note:** this is a transcript-based rewrite, using James's actual spoken narration (lightly cleaned up — filler words, mid-take corrections, and recording asides removed) matched against the whiteboard's equation order. + +--- + +## Introduction + +Hello and welcome to this lecture, which I like to call "Fun with Matrices." It's basically a disjoint-seeming set of facts about the kind of matrices that are important to us in quantum computing, which we're going to use to help us analyze quantum gates and how they can be combined into quantum algorithms. + +--- + +## The Hermitian conjugate + +So let's begin with the Hermitian conjugate. Here we have an example of a two-by-two matrix — an arbitrary two-by-two matrix: + +$$M = \begin{pmatrix} a & b \\ c & d \end{pmatrix}$$ + +On this we're going to define the notion of a Hermitian conjugate. A Hermitian conjugate is defined in terms of two other properties. + +One is the **transpose**. The transpose of a matrix is where you basically flip the matrix along this axis: everything below goes above, everything above goes below, and everything on the diagonal stays where it is. So for a two-by-two matrix, the diagonal elements stay where they are, but the off-diagonal elements get swapped: + +$$M^T = \begin{pmatrix} a & c \\ b & d \end{pmatrix}$$ + +We also define the **complex conjugate**, which is simply the complex conjugate of all the elements: + +$$M^* = \begin{pmatrix} a^* & b^* \\ c^* & d^* \end{pmatrix}$$ + +Now, if we do both of these things — take the complex conjugate and then the transpose, or equivalently take the transpose and then the complex conjugate — we get the **Hermitian conjugate**: + +$$M^\dagger = (M^*)^T = (M^T)^* = \begin{pmatrix} a^* & c^* \\ b^* & d^* \end{pmatrix}$$ + +You'll be hearing more about that. + +--- + +## The Paulis + +Now let's move on to introduce the Paulis. The Paulis are the simplest, but also arguably the most important gates — ones that come up all of the time. They are $X$, $Y$, and $Z$. And in some contexts an honorary Pauli is the identity, $I$. + +Each of them has the property that if you square it, you get the identity — and this even holds for the honorary Pauli: + +$$X^2 = Y^2 = Z^2 = I^2 = I$$ + +And for any pair of them, we have anticommutation: + +$$XY = -YX, \qquad XZ = -ZX, \qquad ZY = -YZ$$ + +Also, if you multiply two of them together, you get the third one. What we find is that: + +$$ZX = iY, \qquad \text{while} \qquad XZ = -iY$$ + +I leave it as an exercise to the viewer to work out all the rest of them. But up to a global phase, multiply two of them together and you get the third one. So these are the Paulis. + +*(This confirms the whiteboard's second relation should read $XZ = -iY$, not $XZ = +iY$ as it appears written — a slip of the pen while talking, per the narration above.)* + +--- + +## Any matrix as outer products — and in terms of Paulis + +Now let's return to our general single-qubit matrix — although the properties we're going to look at also hold for any arbitrary matrix. So $a, b, c, d$: this can be written as $a$ times the matrix which has only a single element of one, and the rest of them are zero — and the one is exactly where we want this $a$ in our matrix — plus $b$ times the matrix which has a one only where we want the $b$, and so on for $c$ and $d$. + +The interesting thing is that all of these can be written out as an outer product. And they could also be written in terms of Paulis. + +So let's look at $\begin{pmatrix}1&0\\0&0\end{pmatrix}$. This can be written as the outer product of the column vector and the row vector — the outer product of $|0\rangle$ with itself. And the next one, for $b$, $\begin{pmatrix}0&1\\0&0\end{pmatrix}$, can be written as the outer product of $|0\rangle$ with $\langle1|$, and so on: + +$$M = a\,|0\rangle\langle0| + b\,|0\rangle\langle1| + c\,|1\rangle\langle0| + d\,|1\rangle\langle1|$$ + +This just proves something we've already really been using: that you can always write a matrix out in terms of outer products. Before, we've been using properties of the matrices we were looking at to know how to write down those outer products — but this is a way of doing it which holds in all times and in all places, for any matrix, regardless of what its properties are. Because we're just singling out each of the elements and writing down an outer product that corresponds to that particular element. + +And this holds for multi-qubit matrices too — you can write down the outer product of $|00\rangle$ with itself, for example, which is a four-by-four matrix with a one in the top corner and zeros everywhere else. + +--- + +## Writing those outer products in terms of Paulis + +We can also write down those exact outer products in terms of Paulis, which is going to show us that everything can be written in terms of Paulis as well. + +The outer product of $|0\rangle$ with itself, $\begin{pmatrix}1&0\\0&0\end{pmatrix}$, can be written as $(1,1) + (1,-1)$ along the diagonal — so that the element we don't want cancels. The first matrix in that sum is the identity, and the second is $Z$: + +$$|0\rangle\langle0| = I + Z$$ + +Similarly, we can write down the outer product of $|1\rangle$ with itself as: + +$$|1\rangle\langle1| = I - Z$$ + +*(Note: as spoken, these two don't quite add up dimensionally without a normalizing $\tfrac12$ — the standard identity is $|0\rangle\langle0|=\tfrac12(I+Z)$. It's dropped both on the board and in the narration here, so it may be intentional shorthand for this lecture; worth a quick gut-check before it goes to the slides.)* + +But how about something like the outer product of $|1\rangle$ with $\langle0|$? Well, this can be written as $X$ times the outer product of $|0\rangle$ with $\langle0|$, because of course that $X$ times $|0\rangle$ gives us the $|1\rangle$ we desire. And that means it's $X$ times $(I+Z)$: $X$ times identity is just $X$, and $X$ times $Z$ is $-iY$. So: + +$$|1\rangle\langle0| = X(I+Z) = X - iY$$ + +This gives us two more of these outer products written in terms of Paulis, and now we've even got a special guest star from $Y$. The outer product of $|0\rangle$ with $\langle1|$ comes out the same way, but with $+iY$: + +$$|0\rangle\langle1| = X + iY$$ + +Using these relations, we can write any general matrix down as: + +$$M = \alpha I + \beta X + \gamma Y + \delta Z$$ + +(I'm not using $a, b, c, d$ as before, because these aren't going to be the same numbers — these aren't the matrices that single out single elements. But there is a relationship between those numbers, which I'll leave for you to investigate yourself.) Hopefully it should be clear now that we can also write down matrices as sums of Pauli matrices. + +--- + +## The same trick, tensored up + +Similarly, if you have a term like $|00\rangle\langle00|$, this is the tensor product of $|0\rangle\langle0|$ with itself — and each of those can be written as $I + Z$. Expand this out, and: + +$$|00\rangle\langle00| = (I+Z)\otimes(I+Z) = I\otimes I + I\otimes Z + Z\otimes I + Z\otimes Z$$ + +You can do this for all other possible two-qubit outer products, and also multi-qubit outer products. So it's also true that multi-qubit matrices can be written as sums of Paulis — but the notion of a Pauli gets extended to include tensor products of Paulis like these, plus, as the honorary Pauli, the identity. + +So that's how we can write down matrices in terms of outer products, but also in terms of Paulis. + +--- + +## Unitary matrices + +Now let's talk about two specific classes of matrix which are defined in relation to the Hermitian conjugate. + +One class of matrices is a class for which multiplying the matrix by its Hermitian conjugate gives us the identity — and it doesn't matter which way around you do it. If a matrix satisfies this property, we call it **unitary**: + +$$UU^\dagger = \mathbb{1} = U^\dagger U$$ + +There are a few fun facts about unitaries. Suppose you've got a state $|\psi\rangle$ that's normalized, so $\langle\psi|\psi\rangle = 1$. Now someone comes along and applies $U$ to $|\psi\rangle$, giving you some new state $|\varphi\rangle$. Is $|\varphi\rangle$ still normalized? + +The inner product of $|\varphi\rangle$ with itself — and we already noted before that the effect $U$ has on the ket form of the state, $U^\dagger$ has the same effect on the bra form — is the inner product $\langle\psi|U^\dagger U|\psi\rangle$. But because $U^\dagger U = \mathbb{1}$, putting an identity in the middle of that just means we come out with the inner product of $|\psi\rangle$ with itself, which is one: + +$$\langle\varphi|\varphi\rangle = \langle\psi|U^\dagger U|\psi\rangle = \langle\psi|\psi\rangle = 1$$ + +So if you apply a unitary to a normalized state, you get a normalized state back. But actually the property is more general than this. + +If we have a couple of states, $|\psi_0\rangle$ and $|\psi_1\rangle$, and we know their inner product — and act on both of these with $U$, turning $|\psi_0\rangle$ into $|\varphi_0\rangle$ and $|\psi_1\rangle$ into $|\varphi_1\rangle$ — then the inner product of $|\varphi_0\rangle$ and $|\varphi_1\rangle$ will be exactly the same as that of $|\psi_0\rangle$ and $|\psi_1\rangle$, with exactly the same proof as before: + +$$\langle\varphi_0|\varphi_1\rangle = \langle\psi_0|\psi_1\rangle$$ + +What this means is that if $|\psi_0\rangle$ and $|\psi_1\rangle$ are, for example, orthogonal — if they represent a basis we can use to describe our system — then the new states $|\varphi_0\rangle$ and $|\varphi_1\rangle$ will also represent a basis. So we can think of unitaries as being transformations between different orthogonal bases: whatever basis we care about, we can apply the unitary to it and find out what basis it turns that into. + +--- + +## Worked example: the $X$ gate as a basis transformation + +Let's look at a few examples of this. One is $X$. We can apply this to our favorite basis, the $|0\rangle/|1\rangle$ basis. What we find is it turns $|0\rangle$ into $|1\rangle$, and $|1\rangle$ into $|0\rangle$: + +$$X: |0\rangle \to |1\rangle, \qquad |1\rangle \to |0\rangle$$ + +We have started off with a basis, and we have ended up with a basis — all we've actually done is changed the ordering of the states, but it's still a valid effect. + +We could also try $X$ on another basis: the $X$-basis states $|+\rangle$ and $|-\rangle$. Then we find $X$ turns $|+\rangle$ into $|+\rangle$, and gives $|-\rangle$ a phase of $-1$: + +$$X: |+\rangle \to |+\rangle, \qquad |-\rangle \to -|-\rangle$$ + +That's another valid basis — these basis states only differ by global phases, but it's still a valid effect. + +And of course we've been using exactly these basis transformations as a way of writing down the effects of $X$: we've been writing $X = |0\rangle\langle1| + |1\rangle\langle0|$, because this pair of outer products describes exactly this effect. And similarly, we can write $X$ in its own eigenbasis: + +$$X = |+\rangle\langle+| - |-\rangle\langle-|$$ + +--- + +## The general recipe for a unitary + +With any unitary, if it's turning some states $|\psi_0\rangle$ and $|\psi_1\rangle$ — which form a basis, so they're orthogonal — into some states $|\varphi_0\rangle$ and $|\varphi_1\rangle$, then this is a way of writing down that unitary: + +$$U = |\varphi_0\rangle\langle\psi_0| + |\varphi_1\rangle\langle\psi_1|$$ + +When we have multi-qubit states, we just have more states in our basis — it's become bigger, but it's all equally valid. + +--- + +## Eigenbasis and spectral form + +Now one particular basis of interest for any unitary is the **eigenbasis** — the basis for which the unitary has no effect but to give global phases. And unitaries always have an eigenbasis. + +So for any unitary $U$, we can find a set of states, which I'm going to call $|h_j\rangle$ (the reason for using $h$ will become clear later), such that $U$ acting on these $|h_j\rangle$s gives us just a global phase. It's very limited in what it can do — it can only multiply by a global phase, just like $X$ applied to $|-\rangle$ gives a phase of $-1$. And global phases can always be written as $e^{i}$ times some real number — our eigenvalue, parametrized by that real number: + +$$U|h_j\rangle = e^{ih_j}|h_j\rangle$$ + +This also shows us how $U^\dagger U$ comes out as the identity: for $U^\dagger$, the effect is to give us the complex conjugate of the phase, and everything else is the same eigenbasis: + +$$U^\dagger|h_j\rangle = e^{-ih_j}|h_j\rangle$$ + +And we can write this all in what we call **spectral form**. When we're using the eigenbasis, the mapping of the original basis to the new basis is all nice and diagonal, because we've got the same state on both sides, differing only by the eigenvalue — summed over all of our basis states: + +$$U = \sum_j e^{ih_j}\,|h_j\rangle\langle h_j|$$ + +That's going to come in handy. + +--- + +## Hermitian matrices + +Now let's move on to the other special kind of matrix defined in relation to the Hermitian conjugate: **Hermitian matrices**, which are matrices equal to their own Hermitian conjugate: + +$$H = H^\dagger$$ + +Hermitian matrices can also be written in spectral form — they also have eigenstates — but in this case, applying a Hermitian matrix to some state does not necessarily give us a valid state. One example: $\begin{pmatrix}1&0\\0&0\end{pmatrix}$ is Hermitian (transpose it, conjugate it, you get the same thing back), but if you apply this matrix to the $|1\rangle$ state, you get the two-dimensional zero vector — not a normalized state at all. + +So we don't necessarily get normalized states out of these. For the eigenstates — call them $|h_j\rangle$ again, which makes more sense here since $h$ is for "Hermitian" — applying $H$ gives us some number times $|h_j\rangle$. But in spectral form, one thing we'll note is that these numbers have to be **real**: + +$$H|h_j\rangle = h_j|h_j\rangle, \qquad h_j = h_j^*$$ + +Why do they have to be real? The Hermitian conjugate is a basis-independent quantity. The transpose depends on what basis you perform it in, and so does the complex conjugate — but the combination of these two things isn't basis-dependent. It doesn't matter which basis you write your matrix down in; the Hermitian conjugate you get out is exactly the same. + +So if we do the transpose in the eigenbasis — and for outer products, a transpose just means swapping the two states in each term — using the spectral form, the two states are already the same, so the transpose has no effect. What's left is to do the complex conjugate, and in the eigenbasis that means taking the complex conjugate of the eigenvalues. For the Hermitian matrix to equal its own Hermitian conjugate, the complex conjugates of the eigenvalues have to equal the eigenvalues themselves — which means these values have to be real. So: + +$$H = \sum_j h_j\,|h_j\rangle\langle h_j|$$ + +--- + +## $U = e^{iH}$ + +So we've got two kinds of matrices: unitary matrices, which in spectral form can be written as $e^i$ times a real number for each eigenstate, and Hermitian matrices, which can be written as a real number times the eigenstate projector — and this is why I've used $|h_j\rangle$ for the eigenstates of both, because we can see a correspondence between these two things. Both are parametrized by a set of real numbers — in one case used directly as eigenvalues, in the other used to define complex numbers of magnitude one — but in both cases they exist. + +This means that for any unitary we can define a corresponding Hermitian, and for any Hermitian we can define a corresponding unitary. The way we write this down is to say we can get a unitary which is $e$ to the $i$ times a Hermitian: + +$$\boxed{U = e^{iH}}$$ + +— where the exponential here is defined the way we always define the exponential of a matrix, via its Taylor series. If you apply that to something in spectral form, when you square the outer products you get the original outer product back, and when you multiply terms from different eigenstates you get zero because they're orthogonal. What it means is you just get the exponentials of the eigenvalues — exactly what we want: you take the complex exponential of $H$, and you get the matrix with exactly the same eigenstates, whose eigenvalues are exponentials of the original eigenvalues. + +--- + +## Cliffords + +Now let's talk about Cliffords. There are different families of gates useful for different things in quantum computation. We've already talked about the Paulis — $X, Y, Z$, and the identity as an honorary Pauli. + +Another set of gates are the **Cliffords**, which map Paulis to Paulis by conjugation. If we conjugate a Pauli by a Clifford — applying the gate on one side and its Hermitian conjugate on the other — what we get is a Pauli, up to some phase. + +The unitary that does this for $X$, up to sign, is the **Hadamard**: + +$$HXH = Z, \qquad HZH = X, \qquad HYH = -Y$$ + +That's not the only example of a Clifford gate. Another is $S$: + +$$SXS^\dagger = Y, \qquad SYS^\dagger = -X, \qquad SZS^\dagger = Z$$ + +$S$ and $Z$ commute, so conjugating $Z$ by $S$ just gives you plain old $Z$ back. So with Cliffords we can transform Paulis into each other. + +A Pauli on its own is pretty boring and cheap, so you might wonder why we'd want to do this — but we'll see some good uses of this later. I should note that the notion of a Pauli also exists for multi-qubit systems: a tensor product of Paulis. So here's a two-qubit example. We can have the equivalent of what we saw before, which just turns $X\otimes I$ into $Z\otimes I$. But we can have something more interesting: a different unitary that takes $X\otimes I$ and turns it into $X\otimes X$: + +$$U(X\otimes I)U^\dagger = X\otimes X$$ + +And in fact, you already know the gate that does that — it's the **controlled-NOT**. + +Most of the gates we've looked at so far are Cliffords, in fact. One more example of Cliffords: the Paulis themselves are Cliffords too. If you do $XZX^\dagger$, say — and Paulis are Hermitian, so this is the same as $XZX$ — commuting the $X$'s through gives us $-Z X^2$, and $X^2$ is the identity, so that gives us $-Z$: + +$$XZX^\dagger = XZX = -ZX^2 = -Z$$ + +So Paulis transform Paulis to Paulis too, just in a rather boring way — giving phases due to the anticommutation. + +So we have Paulis, and then we have Cliffords, which include the Paulis but also include other gates. And then we have the next set of gates: **non-Cliffords** — everything else. + +--- + +## Non-Cliffords: rotation gates + +An example of a non-Clifford gate is a rotation around the $x$-axis by some angle. If it's $\pi$, you get $X$, which is a Clifford. If it's $\pi/2$, you also get a Clifford. Everything else is non-Clifford. + +Recall that any unitary can be represented as the matrix exponential of a Hermitian, $e^{iH}$. What's the Hermitian matrix corresponding to rotation around the $x$-axis by some angle? It's $X$ times $\theta/2$: + +$$R_x(\theta) = e^{iX\theta/2}$$ + +And similarly: + +$$R_y(\theta) = e^{iY\theta/2}, \qquad R_z(\theta) = e^{iZ\theta/2}$$ + +--- + +## Using a Clifford to trade one rotation for another + +If we have the ability to do one of these rotations, but want another, and we also have Clifford gates, this is one place Cliffords become useful. Say we want to do $R_z(\theta)$. This equals $e^{iZ\theta/2}$ — but we can also implement it with a Hadamard conjugating $e^{iX\theta/2}$: + +$$R_z(\theta) = e^{iZ\theta/2} = H\,e^{iX\theta/2}\,H$$ + +The reason: the only difference between $e^{iZ\theta/2}$ and $e^{iX\theta/2}$ is their eigenstates — the unitary and its Hermitian generator share an eigenbasis, so the eigenstates of $e^{iZ\theta/2}$ are the $|0\rangle,|1\rangle$ eigenstates of $Z$, and the eigenstates of $e^{iX\theta/2}$ are the eigenstates of $X$. All we need to do is transform the state from the eigenstates of $X$ to the eigenstates of $Z$, and that basis transformation is exactly what a Hadamard does. + +So we can take the ability to do a rotation around the $x$-axis, plus the ability to do a Hadamard, and turn that into the ability to do a rotation around the $z$-axis. We can also bring the Clifford conjugation up into the exponent (writing the Hadamard's Hermitian conjugate explicitly, even though $H = H^\dagger$ here for clarity): + +$$R_z(\theta) = H\,e^{iX\theta/2}\,H^\dagger = e^{i(HXH^\dagger)\theta/2}$$ + +That's fine to do because all it's doing is a basis transformation — changing the eigenbasis — and we're using the fact that the unitary has the same eigenbasis as the Hermitian we're describing it in terms of. So we can apply the conjugation to the Hermitian just as well as to the unitary. That's a trick we'll use again shortly. + +--- + +## Spreading a rotation across a multi-qubit register + +With Cliffords, we can turn a rotation around $X$, $Y$, or $Z$ on a single qubit into one of the others. But using multi-qubit Cliffords, this gets even more interesting. + +Say we have $e^{i\theta/2\, X}$ applied to a single qubit. To apply it across many qubits, we tensor it up — say, over four qubits, with our chosen qubit in one slot and identities elsewhere: + +$$e^{i\theta/2\; X\otimes\mathbb{1}\otimes\mathbb{1}\otimes\mathbb{1}}$$ + +(Same trick as before — moving everything up into the exponent, since the unitary and the Hermitian share an eigenbasis.) + +What happens if we conjugate this with a controlled-NOT — labeling the qubits $0,1,2,3$, controlled on qubit $3$ and targeted on qubit $2$? + +$$C_x^{3,2}\; e^{i\theta/2\; X\otimes\mathbb{1}\otimes\mathbb{1}\otimes\mathbb{1}}\; C_x^{3,2}$$ + +We can write this out again, this time moving the CNOTs up into the exponent: + +$$e^{i\theta/2\; C_x^{3,2}\big(X\otimes\mathbb{1}\otimes\mathbb{1}\otimes\mathbb{1}\big)C_x^{3,2}}$$ + +And CNOTs have exactly the effect we discussed earlier — conjugating $X\otimes I$ by a CNOT gives $X\otimes X$. So this becomes: + +$$e^{i\theta/2\; X\otimes X\otimes\mathbb{1}\otimes\mathbb{1}}$$ + +This is *not* the same as doing this rotation independently on both qubits — in that case you'd get lots of different terms. Instead, this is an **entangling operation** — it entangles the first two qubits involved. + +We can similarly conjugate by even more CNOTs and get: + +$$e^{i\theta/2\; X\otimes X\otimes X\otimes X}$$ + +by the magic of controlled-NOTs. Then we can apply some other Clifford operation and use it, by conjugation, to change one of those Paulis — say a Hadamard on qubit $2$, either side of the operation. That Hadamard conjugates that $X$, turning it into a $Z$: + +$$H_2\; e^{i\theta/2\; X\otimes X\otimes X\otimes X}\; H_2^\dagger = e^{i\theta/2\; X\otimes Z\otimes X\otimes X}$$ + +--- + +## The punchline + +So by just using the humble rotation around the $x$-axis, combined with all of the Clifford gates — notably the controlled-NOT, and the single-qubit Cliffords which can transform $X$ into $Z$, $Z$ into $X$, $X$ into $Y$, $Y$ into $X$ — we can write down any arbitrary unitary of the form: + +$$e^{i\theta/2\; P_{n-1}\otimes P_{n-2}\otimes\cdots\otimes P_1\otimes P_0}$$ + +where each $P_j$ is drawn from the set of Paulis, including the honorary Pauli, the identity. + +Any gate of this form — big, multi-qubit, messy gates — we can get from the gates we started with. And this is a pretty powerful gate set. We don't usually think about designing algorithms specifically in terms of gates of this form, but we can prove that using gates of this form, we can do *anything*. + +So if we can do anything with gates of this form, and we can make gates of that form by combining simple, single-qubit non-Cliffords with Cliffords, then just with these gates we know so far — rotation around the $x$-axis, the Hadamard, $S$, and the controlled-NOT — we can do quite a lot. + +But we'll leave it till next week to use this to prove that we can do anything: we'll define what it means for a quantum computer to do *anything* a quantum computer can do — the notion of **universality** — and show that using this gate set, we can achieve it. Then we'll link that to a specific application showing that quantum computers can also do things that are intractable for classical computers. Once we have that, we can start looking at specific applications. diff --git a/AI notes/4_Circuits-and-universality.md b/AI notes/4_Circuits-and-universality.md new file mode 100644 index 0000000..aadfbdc --- /dev/null +++ b/AI notes/4_Circuits-and-universality.md @@ -0,0 +1,400 @@ +# 6 — Circuits and Universality + +*Dr James Wootton, Moth Quantum* + +> **Transcription note:** this is a transcript-based rewrite, using James's actual spoken narration (lightly cleaned up — filler words, mid-take corrections, and recording asides removed) matched against the whiteboard's equation order. + +--- + +## What is computing? + +Hello, I'm again Dr James Wootton. This is again a lecture on quantum computing. But what is computing? + +A very simple model for what a computer does is: it takes an input, let's call it $x$, and then it computes a function, let's call it $f(x)$, to give us an output which we can call $y$: + +$$x \qquad f(x) = y$$ + +In a digital computer, all inputs and outputs can be expressed as binary strings. So this is a function which takes in binary strings and outputs binary strings. In general those binary strings can be different lengths: an input bit string with some $n_i$ bits, an output bit string with some $n_o$ bits. And in the middle we can think of the computer as basically just being a black box which takes the input in and spits the output out: + +$$n_i \text{ bit} \longrightarrow \boxed{\phantom{xxxx}} \longrightarrow n_o \text{ bit}$$ + +It's not literally a black box, but you get the idea. + +If we can implement a general mapping from inputs to outputs for a general function in this way, then whatever it is we're using to do that, we can call **universal**. So if we can use, for example, NAND gates to perform any general mapping of this kind, we can say NAND gates are universal for computation — they can do any of these general computations, any function, any number of bits for inputs and outputs. You can find some configuration of NAND gates which does that. + +--- + +## Why a lookup table is a terrible program + +It's easiest to prove this if we think of implementing the computation through a lookup table — basically writing down the program as: if the input is some particular input, then output the corresponding output. + +$$\text{NAND} \qquad \text{if } x==x' : \ \text{return } y$$ + +This is a terrible way to write a program, because (A) you need as many lines in your code as there are possible inputs — you have to go through all $2^{n_i}$ possible inputs, which goes up exponentially with the number of input bits — and (B) you need to know all of the answers already. Nevertheless, it's quite easy to show that if you have building-block logic gates, you can do the process of checking all the values to see if these conditions are met, and if they are, writing down the correct output. So we can show that Boolean logic gates are universal, because they can reproduce any of these functions. + +$$2^{n_i}$$ + +Before we move on, let's look at a couple of possible functions just to be more concrete. We could have an input which is something like three times five: the binary representation of the numbers to be multiplied. + +$$3 \times 5 \longrightarrow 15$$ + +If your function is just multiplication, that's all you need. If you have a more general computation that could do any kind of maths, you might also want a binary representation telling your computer *that* you want it to do multiplication. And of course the output here would be $15$. + +But you also might have something where your input is an image — say, a picture of a cat — and your output is a simple binary yes/no of whether the image contains a cat: + +$$\boxed{\text{🐱}} \longrightarrow 1$$ + +We'd hope a picture with a cat in it gives us a $1$, and one without gives a $0$. These are just a few examples of the many possible kinds of computation. And in this latter case, it's obvious that you do *not* want to do it by a lookup table — you don't want to go through every possible image that could ever exist and decide by hand whether there's a cat in it, then write a lookup table that checks against that list. You want to actually *process* the information. And once you do that, you get into the weeds of what is actually computable — we're not going into that right now. We're just thinking about the notion of universality: computation as a function, and when do we have the ability to implement those functions. + +--- + +## Reversible computation + +Now let's start talking more specifically about quantum computing. In quantum computing we do computations by unitary gates, and unitary gates are reversible. So we're going to need a slightly different definition of computation to suit this reversibility condition. + +Specifically, we want to define computation as something that takes an $n$-bit input and gives an $n$-bit output, where the $n$ is the same in both cases: + +$$n \text{ bit} \longrightarrow \boxed{\phantom{xxxx}} \longrightarrow n \text{ bit}$$ + +This looks like a more restrictive version of computation than before, but it isn't necessarily so. We also want each unique input to give a unique output, and vice versa — again seemingly more restrictive, but again not necessarily so, because we can just choose $n$ to be large enough to forgive all the sins. + +Specifically, take $n$ to be our previous $n_i + n_o$: + +$$n = n_i + n_o$$ + +Then our computation takes in not just $x$, but $x$ together with a bunch of zeros — a blank variable where we're going to write our output — and the output is a copy of $x$ with the output written on the blank variables: + +$$x, 00\ldots \longrightarrow x, f(x)$$ + +So every unique input does have a unique output, enforced in a very simple way, just by keeping a copy of the input alongside the output. + +If we take reversible gates and implement a function like this — say, a computation that takes $x$ and turns it into $f(x)$ — you could compile that down to NAND gates, then implement those with Toffolis (the reversible form of the NAND gate). Implementing the function this way, keeping a copy of the output, you'll find it's also reversible, in that each unique output has a unique input. That happens automatically because you're building it out of reversible gates — that condition is enforced naturally. + +--- + +## Writing this in quantum form + +Let's now write this in a more quantum form. What we want from our computation is the ability to take an input expressed as a bit string, but written in qubits. If it's an $n_i$-bit string, we use $n_i$ qubits — so that's a tensor product of $n_i$ qubits hiding inside the simple-looking $|x\rangle$. We tensor that with a bunch of zeros, specifically $n_o$ of them: + +$$|x\rangle \otimes |0\rangle^{\otimes n_o}$$ + +We want our computation to take such a state and rotate it into a state which still has the input expressed as a bit string, but also has the output expressed as a bit string. And since it's a reversible process, this can be implemented by a unitary: + +$$|x\rangle \otimes |0\rangle^{\otimes n_o} \longrightarrow |x\rangle \otimes |f(x)\rangle$$ + +So we want to find unitaries that provide this sort of rotation. If universality for classical computers means the ability to implement any function $y = f(x)$ for a given function, universality for quantum computers means the ability to implement any unitary $U_f$ — an $n$-qubit unitary that reproduces this function, taking a bit string expressed in qubits and writing down the corresponding output: + +$$U_f : |x\rangle \otimes |0\rangle^{\otimes n_o} \longrightarrow |x\rangle \otimes |f(x)\rangle$$ + +For any possible function, for any possible number of qubits, if we can create these corresponding unitaries, we can reproduce computation in exactly the same way a classical computer does — and we are universal. + +However, restricting ourselves to unitaries that specifically reproduce this binary-function form is an unnecessary complication — it doesn't actually make anything simpler or make more sense. We can be much more general and much simpler in our definition of universality for quantum computers: + +$$y = f(x) \qquad\qquad U_f$$ + +**Universality means we can reproduce any arbitrary unitary acting on $n$ qubits.** + +$$U_f \; (n \text{ qubits})$$ + +For any number of qubits, if we can find some way to implement any arbitrary unitary, we have what we'd call a universal quantum computer. It's just a consequence of that that we could use these unitaries to implement classical computations, and therefore, in theory, run everything from Excel to Mario Kart. But because we're defining it in terms of unitaries, we can also do more general things. + +In this lecture we want to prove that a gate set we've already seen is sufficient for universality — sufficient for reproducing any unitary, and therefore any quantum computation, any quantum algorithm, or indeed any classical algorithm. So let's get on with doing that. + +--- + +## The gate set: $R_x(\theta)$ and the Cliffords + +I'll remind you of the gate set we looked at last week: rotations around the $x$-axis (we could use rotations around any axis, but we were looking specifically at $R_x(\theta)$ for some angle $\theta$), plus the Clifford gates — the Hadamard, $S$, and $S^\dagger$ (even without $S^\dagger$ explicitly, you can make it out of $S$'s, since it's $S^3$), and the controlled-NOT: + +$$R_x(\theta),\quad H,\ S,\ S^\dagger = S^3,\ CX$$ + +There are different ways to express generators for the Clifford group, but this is a good one. + +The first step towards "everything" is to note that $R_x$ can be expressed as $e^{i(\theta/2)X}$. Using the single-qubit Cliffords, we can transform this into $e^{i(\theta/2)Y}$, $e^{-i(\theta/2)X}$, and so on. And by using the two-qubit Clifford — the controlled-NOT — we can turn this into a two-qubit gate such as $e^{i(\theta/2)X\otimes X}$, and from there into $X\otimes Z$, $Y\otimes X$, and so on: + +$$e^{i\frac{\theta}{2}X} \qquad e^{i\frac{\theta}{2}X\otimes X}$$ + +We can get to the point of expressing this as $e^{i(\theta/2)P}$, where $P$ is some general $n$-qubit Pauli — a tensor product of Paulis on qubits $n-1$ down to qubit $0$, each Pauli being $X$, $Y$, $Z$, or the identity: + +$$e^{i\frac{\theta}{2}P}, \qquad P = P_{n-1}\otimes\cdots\otimes P_0, \qquad P_j \in \{X,Y,Z,I\}$$ + +So we have the ability to create a wide set of multi-qubit Paulis this way — but not every unitary can be expressed in this form. We need the ability to represent unitaries expressed in *other* forms too. The simplest such case is where we take $e^{i}$ times $(\theta/2)P$ plus $(\theta'/2)P'$, for some other Pauli $P'$ that isn't of the same tensor-string form: + +$$e^{i\left(\frac{\theta}{2}P + \frac{\theta'}{2}P'\right)}$$ + +How can we use gates of the first kind to create gates of this second kind? + +--- + +## Why the naive approach fails + +As a naive approach, it doesn't work — but we can adapt it into something that does. The naive approach is to remember from school that $e^xe^y = e^{x+y}$. One way to prove this is via the Taylor series of each term: + +$$e^xe^y = e^{x+y}$$ +$$\big(1+x+O(x^2)\big)\big(1+y+O(y^2)\big) = 1 + x + y + xy + yx + \cdots$$ + +And you compare that to the Taylor series of $e^{x+y}$ directly. Why have I written this as $xy$ *plus* $yx$, when I could more easily have written $2xy$? + +$$+\,2xy$$ + +Because they commute — and that's exactly the property we need to be careful of. If we continued this proof, we'd explicitly be using the fact that $x$ and $y$ commute: + +$$xy = yx$$ + +When we're given this relation in school, we're given it for numbers, and the fact that multiplication commutes for ordinary numbers is so ingrained in us we hardly think about it. But when we have matrices, we have to be more careful — matrices do not always commute. So this relation only holds when $A$ and $B$ commute: + +$$e^Ae^B = e^{A+B} \qquad \text{only if} \qquad AB = BA$$ + +And this is not true of Paulis in general — sometimes they commute, sometimes they anticommute. $X$ and $Y$ anticommute, for example, and therefore $e^Xe^Y \neq e^{X+Y}$. That's why the naive approach doesn't work. + +--- + +## Why the Trotter–Suzuki approach *does* work + +Now let's see why it does work. Explicitly, what we want is for + +$$U = e^{i\left(\frac{\theta}{2}P + \frac{\theta'}{2}P'\right)}$$ + +to equal + +$$U = e^{i\frac{\theta}{2}P}\,e^{i\frac{\theta'}{2}P'}$$ + +since we have the ability to do each of the two gates on the right separately, and just doing them both would give us this unitary. However, it doesn't work exactly, because they don't commute in general. + +But what we *can* do is take the $N$th root of both sides, for some sufficiently large $N$: + +$$U^{1/N} \approx e^{i\frac{\theta}{2N}P}\,e^{i\frac{\theta'}{2N}P'}$$ + +That's equivalent to just making the angle much smaller — $\theta/2N$ instead of $\theta/2$: + +$$\frac{\theta}{2N}$$ + +In that case, these become approximately equal. Although on its own this isn't a very strong statement — if you take some unitary to the power of $1/N$ for very large $N$, as $N\to\infty$ it basically becomes $U^0$, and $U^0$ is the identity: + +$$U^0 = I$$ + +So in that limit, we're really just saying something approximately equal to the identity is approximately equal to something approximately equal to the identity, times something approximately equal to the identity — not very impressive. + +What's a *stronger* statement is that if we take the $N$th **power** of both sides, the approximate relation still holds: + +$$U \approx \left(e^{i\frac{\theta}{2N}P}\,e^{i\frac{\theta'}{2N}P'}\right)^{N}$$ + +because taking the $N$th power of $U^{1/N}$ just gets you back to $U$. More explicitly, if we want this written as equal up to some error term $\delta$, the value of $N$ needed to get an arbitrarily small $\delta$ increases only *polynomially* in $1/\delta$: + +$$U = \left(e^{i\frac{\theta}{2N}P}\,e^{i\frac{\theta'}{2N}P'}\right)^{N} + \delta, \qquad N = \mathrm{poly}\!\left(\frac{1}{\delta}\right)$$ + +So it's not just approximate — you can get as close as you like just by choosing the right number of repetitions, and that number doesn't scale horribly; it doesn't ask for too many repetitions. This is a way of taking gates of the first form and combining them into gates of the second form, and it's known as the **Trotter–Suzuki method**. We basically take very tiny slices of those gates, put them in order, and repeat many, many times. The more repetitions, and the tinier the slices, the better the approximation. + +That's sufficient to explain how to do it for two Paulis — but more generally we'll have more than two terms. + +--- + +## Any unitary is reachable: $U = e^{iH}$, $H$ decomposed into Paulis + +Let's go towards the final goal: showing how to reproduce *any* unitary. Any unitary can be expressed as $e^{iH}$ for some Hermitian matrix $H$. And a Hermitian matrix, like any matrix, can be decomposed into a linear combination of Paulis with corresponding coefficients: + +$$U = e^{iH}, \qquad H = \sum_j J_j P_j$$ + +One thing to note: if you take the Hermitian conjugate of a linear combination of matrices, you get the linear combination of the Hermitian conjugates of each — so we take the Hermitian conjugate of these Paulis, and of the coefficients, which for numbers just means the complex conjugate: + +$$H^\dagger = \sum_j J_j^*\, P_j^\dagger$$ + +Since $H$ is Hermitian (equal to its own Hermitian conjugate), and the Paulis are Hermitian too, this relation only holds if the couplings are also "Hermitian" — which for numbers just means they have to be **real**: + +$$J_j = J_j^*, \qquad H = H^\dagger, \qquad P_j = P_j^\dagger$$ + +So a general unitary can be written as $e^{iH}$ for some $H$ expressed as a linear combination of $n$-qubit Paulis with real coefficients. This means we can decompose each term as one of these $e^{i(\theta/2)P_j}$ gates, just using $\theta/2$ as the corresponding coupling $J_j$ for that particular $j$: + +$$e^{i\frac{\theta}{2}P_j}, \qquad \frac{\theta}{2} = J_j$$ + +So by setting these angles to the right values, we can come up with a gate corresponding to each term using the Trotter–Suzuki method — which we just looked at for two terms, but it works for any number of terms. Stitching together all of these terms, we can come up with any arbitrary unitary. + +So there's our result: using single-qubit rotations like $R_x(\theta)$ for an arbitrary angle, together with the Cliffords, we can make these big multi-qubit $e^{i(\theta/2)P}$ gates — and using the fact that a unitary can be written as $e^{i(\text{Hermitian})}$, and that a Hermitian matrix can be written as a linear combination of Paulis, then with the Trotter–Suzuki method and these gates we've created, we can recreate any unitary. + +This is not necessarily the *best* way to build any given unitary in practice — you shouldn't, as your first tactic in writing a quantum algorithm, come up with the target unitary and then try to reproduce it via Trotter–Suzuki. That's a bit like the bad method of programming I mentioned earlier — writing an exponential number of conditional clauses. It's not necessarily the best way, but it *does* prove that the gate set is universal. So we can proceed with gates like $R_x(\theta)$ and the Cliffords, knowing that anything a quantum computer can do, can be done using those gates. If we build our algorithms out of those gates, we're not missing out on any fancy quantum features, because any quantum computation is a unitary, and any unitary can be made out of those gates — they are a universal gate set. + +--- + +## Other universal gate sets + +There are also other universal gate sets. For example, the Cliffords plus *anything* non-Clifford. $R_x(\theta)$ is one example of something non-Clifford — but it's really an infinite number of gates, since it can take any angle. We could instead take, say, the $T$ gate, which is rotation around the $z$-axis by an angle of $\pi/4$: + +$$\text{Clifford} + T = R_z(\pi/4)$$ + +If we have the $T$ gate as well as all the Cliffords, we can also prove this is universal. It's a bit of a horrible and awkward proof, so we won't do it here — but you can. + +One I find even more surprising: another universal gate set is the **Toffoli**, which (as you may remember, since it can do AND and NAND) is essentially universal for classical computation on its own already, given bits initialised to $1$s to unlock its NAND power. The only thing you need to add to make it universal for quantum computation is the **Hadamard**: + +$$\text{Toffoli} + H$$ + +Using these two together, you can do any arbitrary single-qubit rotation — which is kind of mind-blowing. It's not necessarily how you'd want to actually *build* a quantum computer, but the very fact this can be proven is an interesting fact. + +The "Clifford plus anything non-Clifford" result is actually used quite a lot in fault-tolerant quantum computing plans, because — for reasons we'll look at more later in the course — it's quite easy to build versions of Clifford gates that are hardly affected by noise, that we can easily identify and correct imperfections in. For non-Clifford gates, especially a continuous rotation like $R_x(\theta)$ by some angle, this is much harder — a rotation by $\theta$ is a valid gate, but so is a rotation by $\theta$ plus some infinitesimal amount. How do you distinguish the correct gate from a slight over-rotation, when the two are continuously related to each other? It's a very difficult thing to distinguish. But because we can just use a single non-Clifford gate, all we need to do is hone the ability to do that *one* thing perfectly — and if we're slightly over-rotated from that gate, we know that's incorrect and needs correcting. We then use that one, perfectly-honed gate to bring in all the non-Cliffordness the computation needs. + +That's enough about universality. We've proven that our quantum computer can do everything. What remains is to prove it can do specific things, and that it can do them better than a classical computer. + +--- + +## A motivating example: simulating spin-1/2 systems + +One motivating example of a quantum computer doing something better than a classical computer actually uses exactly the same example we've used to prove universality. + +In physics there are various physical systems we might be interested in. One is spin-1/2 particles, like electrons — these have a property called spin, described by a two-dimensional quantum system with states of spin up and spin down in each of the normal real-world directions. Another is photons — described in terms of position by a continuous quantum variable, but they also have polarisation, described by a discrete quantum variable: vertical or horizontal, or (an equally valid basis) $45°$-rotated polarisations, or (another valid basis) circularly polarised states. So that's another two-dimensional quantum system. And of course our favourite two-dimensional quantum system is an abstract one that isn't physical at all: the qubit, with basis states $|0\rangle$ and $|1\rangle$ (the $Z$ basis), the $X$ basis $|+\rangle,|-\rangle$, and the $Y$ basis: + +$$\text{Spin-}\tfrac12: \ |{+}\rangle,|{-}\rangle \qquad \text{Photon}: \ |{\circlearrowleft}\rangle,|{\circlearrowright}\rangle \qquad \text{Qubit}: \ |0\rangle,|1\rangle$$ + +What a coincidence they're all written here — this is just to impress upon you that the mathematics, the theory, behind qubits is exactly the same as the theory behind spin-1/2 particles. That's one reason some approaches to building quantum computers use spin-1/2 particles as qubits. It's also the same as the theory behind photon polarisation, which is why some approaches use that instead. + +So, if you want to do the physics of a bunch of spin-1/2 particles sitting on, say, some square lattice, interacting with each other according to their spin values, that's something we can seek to represent on a classical computer, or on a quantum computer. Which is going to be best? + +The length of the state vector for $n$ spin-1/2 particles — the number of numbers you have to write down — is $2^n$: it scales exponentially with the number of particles, just to write down the state. + +$$2^n$$ + +But how many *qubits* do you need to write down the state of $n$ spin-1/2 particles? Just $n$: + +$$n$$ + +That's an exponential advantage, simply because you're encoding the information in a system that's more naturally expressed that way, instead of trying to write it down in terms of classical variables. + +And if you want to simulate the time evolution on a classical computer, you still have to keep track of that horrible number of numbers. But in quantum computing, time evolution corresponds to a unitary, and that unitary can be expressed as $e^{iH}$, where $H$ here is indeed Hermitian, but it's also called $H$ because it's the **Hamiltonian** — the operator describing the interactions between these spin-1/2 particles, naturally written as a set of interaction terms expressed in terms of Paulis and corresponding couplings: + +$$U = e^{iH}, \qquad H = \sum_j J_j P_j$$ + +So the description of our problem is exactly one of these Hamiltonians. What we want is that time evolution, and we know we have a polynomial number of gates that can simulate it to arbitrary accuracy. So simulating the time evolution of a quantum system is a computational problem that would take exponential resources in general for a classical computer — assuming no special symmetries let you simplify things — but for a quantum computer, because it naturally speaks the language of spin-1/2 particles, it can do it in a much more efficient way. + +So we've shown that quantum computing is universal, how to get universality, and found a motivating example for a problem where a quantum computer could do better than a classical one. Now we can study more generally what quantum computing is, how it approaches problems, and what advantages it can have over classical computing — both for problems relevant to physics and chemistry, where the quantum-ness of the computer is doing the quantum-ness in the problem, and also more broadly for problems that have nothing to do with physics. + +--- + +## Oracles: Boolean and phase + +As we go into some of these examples — especially the early, simple examples of quantum advantage we'll go through — we'll learn the concept of an **oracle**. An oracle is just a fancy way of saying "a time when we need a quantum computer to do classical computation within, as part of, its quantum computation." The classic forms are **phase oracles** and **Boolean oracles**. + +The Boolean oracle is exactly what we saw earlier — call it $U_f^b$. It takes an input encoded in a bunch of qubits, and a bunch of blank qubits (to the power $n_o$), and turns this into the input unchanged, but with the output written on the other register: + +$$U_f^b \; |x\rangle\otimes|0\rangle^{\otimes n_o} = |x\rangle\otimes|f(x)\rangle$$ + +We've got two quantum registers — $n_i$ qubits and $n_o$ qubits — and the oracle effectively doesn't do anything to the input register, but does act on the output register. The reason we need to represent this quantum-mechanically is that there are times in computations where we want to act not just on some single $x$, but on a *superposition* of $x$'s, in order to get interference effects — so we start with a superposition and want a superposition of different outputs out: + +$$U_f^b \sum_x c_x\, |x\rangle\otimes|0\rangle^{\otimes n_o} = \sum_x c_x\, |x\rangle\otimes|f(x)\rangle$$ + +That's a Boolean oracle. + +The other kind is a **phase oracle**. It acts on a register of qubits encoding the input, and gives back the same register in the same state, but with a phase. Applied to a single basis state, this is just a global phase and has no effect — but applied to a superposition, it becomes a *relative* phase, and becomes relevant. The phase is $(-1)^{f(x)}$ — here we assume $f$ gives a binary result out (like our cat picture): + +$$U_f^p\,|x\rangle = (-1)^{f(x)}\,|x\rangle, \qquad f(x)\in\{0,1\}$$ + +If $f(x)=0$ the phase is $(-1)^0=+1$; if $f(x)=1$ the phase is $(-1)^1=-1$. + +--- + +## Phase kickback: building a phase oracle from a Boolean one + +Even though these two kinds of oracle look quite different, they're actually closely related, and we can use Boolean oracles to implement phase oracles. The trick is to take a Boolean oracle for a given function and apply it to the input state tensored with the $|-\rangle$ state: + +$$U_f^b \; |x\rangle\otimes|-\rangle$$ + +Why does this work? The action of the Boolean oracle takes $|x\rangle\otimes|0\rangle$ to $|x\rangle\otimes|0\rangle$ if $f(x)=0$, and to $|x\rangle\otimes|1\rangle$ if $f(x)=1$: + +$$|x\rangle\otimes|0\rangle \longrightarrow |x\rangle\otimes|0\rangle, \ f(x)=0 \qquad\qquad |x\rangle\otimes|1\rangle, \ f(x)=1$$ + +It's like a generalised form of the controlled-NOT — but instead of just looking at a control qubit and checking if it's $0$ or $1$, it looks at the control *register* and asks: is this an $x$ that gives $f(x)=0$, or an $x$ that gives $f(x)=1$? In the case $f(x)=1$, it applies an $X$ to flip the target qubit from $0$ to $1$. + +Now, we know that applying $X$ to the $|-\rangle$ state gives $|-\rangle$ back with a phase of $-1$: + +$$X|{-}\rangle = -|{-}\rangle$$ + +So, doing this "controlled-NOT"-like operation, the control either does nothing to the $|-\rangle$ (no phase induced) or applies an $X$ to it (picking up a phase of $-1$). What we find is: + +$$U_f^b\;|x\rangle\otimes|{-}\rangle = (-1)^{f(x)}\,|x\rangle\otimes|{-}\rangle$$ + +— depending on whether the phase has been induced or not — and the $|-\rangle$ remains completely unaffected. Which means if the $|x\rangle$ part was in a superposition, it stays in exactly that superposition, unentangled from $|-\rangle$. So we can effectively ignore the $|-\rangle$ register entirely, and we find that this **phase kickback** method means the Boolean oracle has the effect of a phase oracle. So wherever we need phase oracles, if we can implement a Boolean oracle, we get ourselves a phase oracle too. + +--- + +## Implementing a Boolean oracle without garbage + +So — how do we implement a Boolean oracle? I implied earlier that the ability to do a classical computation which takes a given $x$ and gives back $f(x)$ is sufficient to also do a quantum version, because you can take the program implementing that, compile it down to NAND gates, replace the NANDs with Toffolis, and get something that takes $x$ and a bunch of zeros and gives you $|x\rangle\otimes|f(x)\rangle$: + +$$x \longrightarrow f(x) \qquad\qquad |x\rangle\otimes|0\ldots\rangle \longrightarrow |x\rangle\otimes|f(x)\rangle$$ + +However, this isn't quite true, because one thing we often forget is that in the course of a classical computation, we typically use intermediate variables that get thrown away over the course of the program. If we account for these, they show up as what we could call **garbage** — the undeleted remnants of a computation: + +$$|x\rangle\otimes|0\ldots\rangle\otimes|0\ldots\rangle \longrightarrow |x\rangle\otimes|f(x)\rangle\otimes|g(x)\rangle$$ + +In classical computation it's perfectly fine to do a *deleting* operation — something that, expressed in quantum terms, would keep a $0$ as $0$ but turn a $1$ into a $0$: + +$$|0\rangle\langle0| + |0\rangle\langle1|$$ + +Such an operation isn't Hermitian — it can't be realised by a *unitary* gate. If we wanted to do this in a computation, we'd have to use something like measurement — measure a qubit, and if it's $1$, flip it down to $0$. Or, in a case where the $0$ and $1$ states have different physical energies, let the qubit interact with a thermal bath and cool down to the $0$ state. But that too is physically similar to a measurement — it's almost as if the thermal bath is measuring the qubit and then deciding whether to take the energy out. This will not sit well with any interference effects: lots of quantum algorithms rely on delicate interference, and using non-unitary gates will mess that up. + +So we need some way to get rid of this garbage, without wrecking the interference effects the algorithm needs. + +--- + +## Uncomputation + +Let's assume we have a unitary $U$ that does this computation, with garbage: + +$$U : |x\rangle\otimes|0\ldots\rangle\otimes|0\ldots\rangle \longrightarrow |x\rangle\otimes|f(x)\rangle\otimes|g(x)\rangle$$ + +When we have a unitary, we typically also have the ability to do its Hermitian conjugate — because whatever sequence of gates makes up $U$ (say $U = AB$, so $U^\dagger = B^\dagger A^\dagger$), as long as we can do the Hermitian conjugate of each individual gate, we can just reverse the order and conjugate each one to get $U^\dagger$: + +$$U = AB \qquad U^\dagger = B^\dagger A^\dagger$$ + +So usually if we can do a gate, we can also do its Hermitian conjugate. This means we can take something with garbage and delete the garbage *unitarily*, because applying $U^\dagger$ to $|x\rangle\otimes|f(x)\rangle\otimes|g(x)\rangle$ gets us back to $x$ and a bunch of zeros, both for the register that held the output and the register that held the garbage: + +$$U^\dagger\;|x\rangle\otimes|f(x)\rangle\otimes|g(x)\rangle = |x\rangle\otimes|0\ldots\rangle\otimes|0\ldots\rangle$$ + +So we've got rid of the garbage — but at the cost of also getting rid of the output, which is a problem, because we need the output. + +--- + +## Copying the output first (and why the no-cloning theorem doesn't block this) + +So the idea is: before we "uncompute" to get rid of the garbage, why don't we just copy-paste our output first? For this we need to be careful, because of the **no-cloning theorem**. + +Does there exist a unitary that can be applied to a state — say a single-qubit state $|\psi\rangle$ — plus another qubit in a well-known state like $|0\rangle$, that creates two copies of $|\psi\rangle$? + +$$U\;|\psi\rangle\otimes|0\rangle = |\psi\rangle\otimes|\psi\rangle$$ + +The answer depends on what $|\psi\rangle$ is. If we completely and utterly know what the state is, this is fine — we know a unitary that creates that one specific state, and we apply it to the other qubit and we have two of them. However, if we have *no* idea what the state $|\psi\rangle$ is, and we want *one single* unitary that can be given any arbitrary state and copy it, that is *not* possible — no such unitary exists. This is the no-cloning theorem. + +So that's two extremes: if you know exactly what the state is, designing a copier for that one specific state is possible; if you want one unitary that copies *any* arbitrary unknown state, that's not possible. + +An in-between case: designing a unitary that copies a *partially* unknown state — say, we know it's going to be either $|0\rangle$ or $|1\rangle$, and we want a single unitary that can copy whichever one it is, without needing to design a specific unitary for each: + +$$|\psi\rangle\otimes|0\rangle = |\psi\rangle\otimes|\psi\rangle, \qquad |\psi\rangle \in \{|0\rangle,|1\rangle\}$$ + +When we know the state is drawn from a set of *orthogonal* states, we *can* do this — there is a way to copy an unknown state from a set of orthogonal states. The unitary we need for this is one we already know well: the **controlled-NOT**. It takes $|00\rangle \to |00\rangle$ and $|10\rangle \to |11\rangle$: + +$$C_x: \ |00\rangle \to |00\rangle, \qquad |10\rangle \to |11\rangle$$ + +Whatever is on the control gets copied onto the target, assuming the target starts in state $|0\rangle$. So we can copy *bits*, and that's perfectly valid. + +--- + +## Putting it together: compute, copy, uncompute + +So we can do a process where we initially have a register of qubits with our input $x$, then a register of zeros (for the output), then another register of zeros (for the garbage), then another register of zeros (where we'll park a clean copy of the output). We apply our computation $U$, which gives back $x$ again, overwrites the second register of zeros with the output $f(x)$, overwrites the third with the garbage $g(x)$, and leaves the last register untouched, since $U$ doesn't act on it: + +$$|x\rangle\otimes|0\ldots\rangle\otimes|0\ldots\rangle\otimes|0\ldots\rangle \ \xrightarrow{\ U\ }\ |x\rangle\otimes|f(x)\rangle\otimes|g(x)\rangle\otimes|0\ldots\rangle$$ + +Then we apply a bunch of controlled-NOTs, one for each qubit in the output register, targeted onto the corresponding qubit of that final, blank register — copying $f(x)$ bit by bit until we have $x$, $f(x)$, $g(x)$, and a second copy of $f(x)$: + +$$\xrightarrow{\ \otimes^{n_o}C_x\ }\ |x\rangle\otimes|f(x)\rangle\otimes|g(x)\rangle\otimes|f(x)\rangle$$ + +Once we've safely got our copy of the output in the bag, we apply the **uncomputation** — $U^\dagger$ — to undo the original computation, going back to just $x$ and two blocks of zeros, getting rid of the unwanted garbage, and leaving our safe copy of the output exactly where it was: + +$$\xrightarrow{\ U^\dagger\ }\ |x\rangle\otimes|0\ldots\rangle\otimes|0\ldots\rangle\otimes|f(x)\rangle$$ + +The entire effect of this — if you just recheck the registers — is exactly the Boolean oracle we wanted, $U_f^b$, sitting on the input register and the final output register: + +$$U_f^b : |x\rangle\otimes|0\ldots\rangle \longrightarrow |x\rangle\otimes|f(x)\rangle$$ + +So: with the ability to do any classical computation, and compile it down to, say, NAND gates, we can use this **compute–copy–uncompute** trick to build the corresponding Boolean oracle, and hence the corresponding phase oracle. So when we come across quantum algorithms that need oracles in the next few sessions, you'll know exactly how those are built — if you want the details of actually implementing them, we usually leave that as an exercise to the reader, or to whoever's actually implementing it once we have fault-tolerant quantum computers. + +You've also seen in this lecture that we can build universal gate sets from just a few simple gates — rotations around the $x$-axis for a single qubit, plus the Clifford gates — and with those, we can do any computation. So it now just remains to look at specific forms of computation, which is what we'll be doing in the next few sessions. + +That's all for this time. Thank you for listening.