Lecture 5 - Leveraging Superposition, Entanglement, and Error Correction
Lecture 5 - Leveraging Superposition, Entanglement, and Error Correction
Updated 27 Nov 2026
Lecture 5 - Leveraging Superposition, Entanglement, and Error Correction
Chapter map
1. Bernstein-Vazirani Algorithm (slides 3–14): crack a "padlock" (oracle s) in one query with superposition + phase kickback → Final Q2 (slides 3–12)
2. Deutsch-Jozsa Algorithm (slides 15–22): test if a hash function is constant or balanced in one query → Final Q2 (slides 15–20)
3. Quantum Teleportation (slides 23–33): send an unknown qubit using a shared Bell pair + 2 classical bits → Final Q4 (slides 23–26, "bartender vs bandit")
4. Superdense Coding (slides 34–39): send 2 classical bits by sending 1 qubit (the reverse trade)
5. Quantum Noisy Theorem (slides 40–49): density matrix, noisy channels, purity, fidelity → Final Q3 (slides 41, 45–46)
Skipped in class / not on the exam: Quantum gate teleportation (slides 29–32), Partial trace (43), Purification (44), Fidelity "Case 2" by hand (45–46 computation)
Final Q2 — Phase Kickback Technique (Ch5 slides 3–12 and 15–20)
Expect "why" questions: why do we use the phase kickback, what does it mean that the phase is "kicked back" from the ancillary qubit to the input qubits, why the ancilla is ∣−⟩, why Hadamard at the end.
He said: no math proof in the exam — know the input (∣+⟩⊗N⊗∣−⟩) and the output (H∣s1⟩⊗⋯⊗H∣sN⟩⊗H∣1⟩) and the reasoning.
"The main challenge is that you may have to design your Uf" (the oracle operator).
🎙️ In class (Oct 6, 17:06–23:43): He calls BV the "ATM cracking algorithm" (in the UK an ATM is "a hole in the wall"). A padlock with 3 decimal digits has 103 codes; with 1,024 digits it is 101024 → intractable classically (you need as many nested for-loops as digits). With quantum computing it is O(1) — "one single execution". Oracle = the password; we want to separate the oracle from the padlock.
🎙️ In class (Oct 6, 18:29): "Every time you learn quantum computing it's a love–hate relationship: conceptually it's a lovely idea, but mathematically it's disgusting."
💡 Analogy: A suitcase lock at Suvarnabhumi. Classically you spin through 000, 001, 002… one by one. BV is like typing every code into the lock at the same time and having the lock "echo" the right code back to you.
Quantum Parallelism {Exam Q2}
In quantum computing, we enable parallelism by:
Converting a bit string into a sequence of binary distributions
Manipulating these distributions with complex matrix operations
Sharing data structures via quantum entanglement and lazy evaluation
Quantum state for a binary distribution:
∣ψ⟩=α(0)∣0⟩+α(1)∣1⟩
where α,β are complex coefficients, and
P(0∣ψ)=α(0)2P(1∣ψ)=α(1)2
We can represent the set of all possible N-bit input strings with one superposition:
∣ψ⟩=2N1j=0∑2N−1βj∣BN(j)⟩
where each BN(j) is the N-bit binary string of decimal number j, and βj=∏k=1Nαj(xk) is its coefficient
The probability for each binary string BN(j)=∣x1,…,xN⟩ is
P(x1,…,xN∣ψ)=∣βj∣2
🎙️ In class (Oct 6, 24:41–25:52):BN(j) just means "bit string of j with length N", e.g. B3(6)=110. "Quantum parallelism is actually the superposition of all possible quantum states" — for 3 qubits, the superposition of 000, 001, …, 111.
❓ Why? Why is this "parallel"? Because one state ∣+⟩⊗N carries all 2N inputs at once with equal amplitude, so a single application of an operator U acts on all 2N inputs simultaneously (linearity).
Bernstein-Vazirani Problem (BV Problem) {Exam Q2}
Suppose we have a black-box function f that matches an input binary with a secret strings1:N (i.e. the oracle) [% is modulo]
fs(x1:N)=x1:N⋅s1:N=(j=1∑Nxjsj)%2
N.B. fs(x1:N) returns the number of matched 1's modulo 2 (the binary dot product)
We can recover the oracle s from the function fs with bitwise measurement:
sk=fs(BN(2k))
where BN(j) is the N-bit binary of decimal number j — e.g. B6(15)=001111
Figure 2: inputs 00000000 … 11111111 → black-box fs (oracle s = 11001010 hidden inside) → 0 OR 1
📝 Instructor's annotation: Next to the fs formula he wrote "one-hot vector ∣2k⟩" — i.e. to read bit k of the secret you feed an input with a single 1 in position k.
Example
Let oracle s=110
fs(100)=[(1⋅1)+(0⋅1)+(0⋅0)]%2=1
🎙️ In class (Oct 6, 29:57–33:09): Worked out all three one-hot probes for s=110: fs(100)=1, fs(010)=(0+1+0)%2=1, fs(001)=(0+0+0)%2=0 → reading them in order gives 110 = the oracle. "It only works if you use these one-hot vectors." Classically that is still N queries; the quantum version does all probes in one query.
💡 Analogy: Asking a friend "is the 1st digit of your PIN a 1?", then "is the 2nd?", … One-hot probes ask about one digit at a time.
Bernstein-Vazirani Problem {Exam Q2}
This problem is a prime example of the complexity class BQP (bounded-error quantum polynomial problems)
We can leverage the quantum parallelism to separate the oracle s1:N from the function f
Figure 3 (Problem space): nested sets — PSPACE ⊃ NP ⊃ {NP-complete, P}; a dashed oval BQP contains P and pokes outside NP's P region (BQP overlaps NP but is not known to contain NP-complete)
Let's define a quantum operator Ufs in response to the function f:
U_{f_s} &= |x_{1:N}\rangle\langle x_{1:N}| \otimes |y\oplus f_s(x_{1:N})\rangle\langle y| \\
&= |x_{1:N}\rangle\langle x_{1:N}| \otimes \big(|0\oplus f_s(x_{1:N})\rangle\langle 0| + |1\oplus f_s(x_{1:N})\rangle\langle 1|\big)
\end{aligned}$$
$$\boxed{U_{f_s} = I_N \otimes \begin{bmatrix} 0\oplus f_s(x_{1:N}) & 0 \\ 0 & 1\oplus f_s(x_{1:N}) \end{bmatrix}}$$
- The **ancillary qubit $y$** works as a **carry flag** in the phase kickback technique
> 🎙️ **In class (Oct 6, 35:12–41:29):** The operator has **two parts**: the first $N$ qubits $x$ = **your guess** (identity — the guess passes through unchanged); the last qubit $y$ = a flag that **flips if your guess matches** ($f_s = 1$). Wrong guess: $0\oplus0=0$, $1\oplus0=1$ → no flip. Right guess: $0\oplus1=1$, $1\oplus1=0$ → flip.
> 🎙️ **In class (Oct 6, 45:32–47:18):** "**Ancilla**" is Latin for a maid (originally a slave woman in the Roman Empire) — the ancillary qubit "just exists there, no importance, but you need it to complete the circuit."
> 🎙️ **In class (Oct 6, 55:07–59:49):** **BQP** = **B**ounded-error **Q**uantum **P**olynomial: **Q P** = solvable by a quantum algorithm in polynomial time; **B**ounded error = the output is a **probability distribution** — you may sample a wrong answer, but only with low probability.
> 💡 **Analogy:** The ancilla is the little LED on the padlock that blinks green when the code is right.
### Phase Kickback Technique {Exam Q2}
> 📝 **Instructor's annotation:** Handwritten next to the title: **"The change of phase in one qubit ripples to the others when measured."**
- In the **phase kickback technique**, a phase applied on the ancillary qubit gets **kicked-back** (i.e. taking a repercussion) due to **interference**
- Suppose we have an operator $U$ that applies a phase onto the input qubit $|\psi\rangle$:
$$U|\psi\rangle = \operatorname{cis}\theta\,|\psi\rangle$$
- Let's define a **multiple-control operator** $C^N U$, where $|x_{1:N}\rangle \neq |1\rangle^{\otimes N}$ and
$$C^N U = |x_{1:N},y\rangle\langle x_{1:N},y| + \big(|1\rangle^{\otimes N}\otimes\operatorname{cis}\theta\,|y\rangle\big)\big(\langle 1|^{\otimes N}\otimes\langle y|\big)$$
- Note that the phase is **kicked-back to $|1\rangle^{\otimes N}$** when $|x_{1:N}\rangle$ is a superposition:
$$\begin{aligned}
C^N U\big(|+\rangle^{\otimes N}\otimes|\psi\rangle\big)
&= \frac{1}{2^{N/2}}\sum_{j=0}^{2^N-1} U\big(|\mathbb{B}_N(j)\rangle\otimes|\psi\rangle\big) \\
&= \frac{1}{2^{(N+1)/2}}\left(\sum_{j=0}^{2^N-2}|\mathbb{B}_N(j)\rangle\otimes|\psi\rangle\right) + \frac{1}{2^{(N+1)/2}}\big(|1\rangle^{\otimes N}\otimes\operatorname{cis}\theta\,|\psi\rangle\big) \\
&= \frac{\left(\sum_{j=0}^{2^N-2}|\mathbb{B}_N(j)\rangle\right) + \operatorname{cis}\theta\,|1\rangle^{\otimes N}}{2^{(N+1)/2}} \otimes |\psi\rangle
\end{aligned}$$
> 🎙️ **In class (Oct 6 part 2, 4:22–13:18):** Two cases: (orange) the control part is **not** all-1 → indices $0\ldots 2^N-2$, nothing happens; (second case) control = $11\ldots1$ → $\operatorname{cis}\theta$ is applied to the last qubit. Because $\operatorname{cis}\theta$ is **just a constant complex number**, it can be moved from the last qubit to the front: "**relocation of the phase** … this phenomenon is called phase kickback."
> 🎙️ **In class (Oct 6 part 2, 6:03–6:30):** "You don't have to be serious about the math proof — it's **not going to show up in the final**. The majority of the final will be **why, why, why and apply, apply, apply**. Some mathematical questions, but **no proof**."
> ❓ **Why does the phase end up on the control qubits?** Because a phase is a **scalar**, and in a tensor product a scalar can sit on either factor: $|x\rangle\otimes(\operatorname{cis}\theta|\psi\rangle) = (\operatorname{cis}\theta|x\rangle)\otimes|\psi\rangle$. If $|\psi\rangle$ is an **eigenvector** of $U$, the target qubit does not change at all — only a number appears — so the target stays the same and the phase is "booked" to the control branch that triggered it.
> ❓ **Why is this useful (why not just a global phase)?** On a single basis state the phase would be a global phase (invisible). But the controls are in a **superposition**, so only *some* branches get $\operatorname{cis}\theta$ → it becomes a **relative phase** between branches, which **interference** (a later Hadamard) turns into a measurable change.
> 💡 **Analogy:** A horse kicks back when you poke it — you act on the ancilla, but the "kick" lands on the input register. (He: "kickback is from a horse.")
### Phase Kickback Technique (generalized by an oracle) {Exam Q2}
- Instead of a single oracle $|1\rangle^{\otimes N}$, let's generalize $C^N U$ by a function $f$ with an oracle $|s_{1:N}\rangle$:
$$\begin{aligned}
U_{f_s} &= |x_{1:N},y\rangle\langle x_{1:N},y| + |s_{1:N},y\operatorname{cis}\theta\rangle\langle s_{1:N},y| \\
&= |x_{1:N},y\rangle\langle x_{1:N},y| + \operatorname{cis}\theta\,|s_{1:N},y\rangle\langle s_{1:N},y|
\end{aligned}$$
- where all $x_{1:N}\neq s_{1:N}$
- Let $f_{sk}(x_{1:N})$ be a **probing function** of each $k$-th bit of the oracle $s$:
$$\boxed{f_{sk}(x_{1:N}) = x_{1:N}\cdot\mathbb{B}_N(s_k 2^k)}$$
- **Kickback property:**
$$\begin{aligned}
U_{f_s}\big(|+\rangle^{\otimes N}\otimes|y\rangle\big)
&= U_{f_s}\left(\sum_{j=0}^{2^N-1}|\mathbb{B}_N(j),y\rangle\right) \\
&= \sum_{k=1}^{N}\sum_{j=0}^{2^N-1}(\operatorname{cis}\theta)^{\mathbb{B}_N(j)\cdot\mathbb{B}_N(2^k)}\,|\mathbb{B}_N(j),y\rangle \\
&= \sum_{k=1}^{N}\sum_{j=0}^{2^N-1}(\operatorname{cis}\theta)^{f_{sk}(\mathbb{B}_N(j))}\,|\mathbb{B}_N(j),y\rangle
\end{aligned}$$
- Note that **each $k$-th matched bit gets kicked-back by $\operatorname{cis}\theta$**
> 📝 **Instructor's annotation:** He added a blue subscript **$s$** to every $U_f$ on this slide → $U_{f_s}$ (the operator depends on the hidden secret $s$).
> 🎙️ **In class (Oct 6 part 2, 16:27–23:28):** $f_{sk}$ "probes the $k$-th qubit": take bit $s_k$, multiply by $2^k$, convert to a bit string — that is the probe. The exponent is 1 if the probe matches (phase changes) and 0 if not (exponent 0 → no change): "change a phase, change a phase… if they do not match, keep it, keep it." He admitted he omitted the $1/\sqrt{2^N}$ normalization "for ease of math".
### Generalized Phase Kickback Technique {Exam Q2}
- For an oracle function $f$ with an oracle $|s_{1:N}\rangle$, we can construct its **kickback operator** of $f$ as follows:
$$\begin{aligned}
U_{f_s} &= |x_{1:N},\,y\oplus f(x_{1:N})\rangle\langle x_{1:N},y| \\
&= |x_{1:N},\,0\oplus f(x_{1:N})\rangle\langle x_{1:N},0| + |x_{1:N},\,1\oplus f(x_{1:N})\rangle\langle x_{1:N},1|
\end{aligned}$$
$$\boxed{U_{f_s} = I_N\otimes\begin{bmatrix}0\oplus f_s(x_{1:N}) & 0\\ 0 & 1\oplus f_s(x_{1:N})\end{bmatrix}}$$
- Recall that each $k$-th bit of the oracle can be recovered by $s_k = f_{sk}(2^k)$
- Feed the ancilla with $|-\rangle$:
$$\begin{aligned}
U_{f_s}\big(|+\rangle^{\otimes N}\otimes|-\rangle\big)
&= \frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}U_{f_s}\left(|\mathbb{B}_N(j)\rangle\otimes\frac{|0\rangle-|1\rangle}{\sqrt2}\right) \\
&= \frac{1}{\sqrt{2^{N+1}}}\sum_{j=0}^{2^N-1}\sum_{k=1}^{N}|\mathbb{B}_N(j)\rangle\otimes|0\oplus f_{sk}(j)\rangle - \frac{1}{\sqrt{2^{N+1}}}\sum_{j=0}^{2^N-1}\sum_{k=1}^{N}|\mathbb{B}_N(j)\rangle\otimes|1\oplus f_{sk}(j)\rangle \\
&= \left(\bigotimes_{k=1}^{N}\frac{|0\rangle + (-1)^{f_{sk}(2^k)}|1\rangle}{\sqrt2}\right)\otimes|-\rangle \\
&= H|s_1\rangle\otimes\dots\otimes H|s_N\rangle\otimes H|1\rangle
\end{aligned}$$
$$\boxed{U_{f_s}\big(|+\rangle^{\otimes N}\otimes|-\rangle\big) = H|s_1\rangle\otimes\dots\otimes H|s_N\rangle\otimes H|1\rangle}$$
> 📝 **Instructor's annotation:** He wrote **"cis π"** above the $(-1)$ in the last-but-one line (i.e. $-1 = \operatorname{cis}\pi$: fixing $\theta = \pi$ turns the kickback phase into a sign), and wrote **$s$** over the $x$'s and as subscripts of $f$ in the operator definition (it is $U_{f_s}$, $f_s$).
> 🎙️ **In class (Oct 6 part 2, 24:59–33:56):** Physicists asked "what if I fix the phase to $\pi$?" $\operatorname{cis}\pi = -1$, and they **fix the ancilla to $|-\rangle$**. The derivation "takes about one A4 page" — **you don't need to remember it**. What to remember: the **input is always $|+\rangle^{\otimes N}\otimes|-\rangle$**, and the result: if $s_k = 1$ the $k$-th qubit becomes $|0\rangle-|1\rangle$ ($=|-\rangle$); if $s_k = 0$ it becomes $|0\rangle+|1\rangle$ ($=|+\rangle$). Apply Hadamard: $H|+\rangle = |0\rangle$, $H|-\rangle = |1\rangle$ → you read $s_k$ directly. "Nobody thought of that. It's so obvious in plain sight."
> ❓ **Why the ancilla must be $|-\rangle$:** The oracle does $|y\rangle\mapsto|y\oplus f(x)\rangle$, i.e. applies X to the ancilla when $f(x)=1$. $|-\rangle$ is an **eigenvector of X with eigenvalue $-1$**: $X|-\rangle = -|-\rangle$. So $U_f|x\rangle|-\rangle = (-1)^{f(x)}|x\rangle|-\rangle$ — the ancilla is unchanged and the bit flip becomes a **sign** $(-1)^{f(x)}$ on the input register. With $|0\rangle$ or $|1\rangle$ the flip would just change the ancilla (no kickback); with $|+\rangle$ nothing would happen at all ($X|+\rangle = |+\rangle$).
> ❓ **Why the Hadamard at the end:** After kickback, qubit $k$ is $|+\rangle$ or $|-\rangle$ — the information is in the **phase (sign)**, which a Z-measurement cannot see (both give 50/50). H converts sign into bit: $|+\rangle\to|0\rangle$, $|-\rangle\to|1\rangle$.
> 💡 **Analogy:** Each input qubit is a coin. The oracle doesn't flip the coins; it just paints a hidden mark on the coins where the secret bit is 1. The final Hadamard is the UV lamp that makes the hidden mark show up as heads/tails.
### Implementation of Oracle Operator {Exam Q2}
![[ch5-p12.jpg]]
- The oracle operator $U_{f_s}$ can be implemented as a **series of CCX (Toffoli)** for each bit of $|s_{1:N}\rangle$
- Figure 4 (Oracle operator $U_{f_s}$): the hidden oracle qubits $|s_1\rangle\ldots|s_N\rangle$ (inside the box) and the input qubits $|x_1\rangle\ldots|x_N\rangle$; for each $k$, a CCX with controls $s_k, x_k$ and target the last qubit $|x_{N+1}\rangle$ → $|y\rangle$
> 🎙️ **In class (Oct 6, 42:51–44:52; part 2, 34:41–36:19):** The secret $s$ is **fixed inside the box** — "nobody sees that" — not an input. Compare bit by bit with a Toffoli (= AND), flipping the ancilla each time: the ancilla ends up as $\sum_k s_k x_k \bmod 2$ = $s\cdot x$. "In practice we don't care about the math; just remember this scheme: **Toffoli for each bit**." (He also referenced the **last midterm question**, which intended Toffoli gates for AND.)
> ❓ **Why Toffoli?** CCX flips the target only when **both** controls are 1, i.e. when $s_k x_k = 1$. XOR-ing $N$ such flips into one qubit computes exactly $\big(\sum_k s_k x_k\big)\%2 = f_s(x)$.
### Bernstein-Vazirani Algorithm (BV Algorithm) {Exam Q2}
![[ch5-p13.jpg]]
- If we have an oracle function $f$ whose oracle is $|s_{1:N}\rangle$, we construct its kickback operator
$$U_{f_s} = I_N\otimes\begin{bmatrix}0\oplus f_s(x_{1:N}) & 0\\ 0 & 1\oplus f_s(x_{1:N})\end{bmatrix}$$
- The Bernstein-Vazirani algorithm for $f$ (Figure 5):
- Top register: $|0\rangle^{\otimes N}$ → $H^{\otimes N}$ → $U_{f_s}$ → $H^{\otimes N}$ → **measure**
- Ancilla: $|1\rangle$ → $H$ → $U_{f_s}$ → $H$ → $|1\rangle$
- Read out $s_1 = \langle Z^{(1)}\rangle,\ s_2 = \langle Z^{(2)}\rangle,\ \dots,\ s_N = \langle Z^{(N)}\rangle$
> 🎙️ **In class (Oct 6 part 2, 37:08–39:56):** Prepare $|+\rangle^{\otimes N}$ (H on $|0\rangle$s) and $|-\rangle$ (H on $|1\rangle$). After the oracle, H on the ancilla gives back $|1\rangle$; H on the top register then measure each qubit's **Z expectation** (north pole = 0, south pole = 1) → you get 0/1 per bit = the oracle. "**In the exam it's going to look like this. No math proof.** We care about the input and output."
> ❓ **Why $O(1)$?** The oracle $U_{f_s}$ is called **once**; the superposition makes that single call evaluate all $2^N$ inputs, and interference collects the answer into one basis state $|s\rangle$ (probability 1). Classically you need $N$ queries (one per one-hot probe), and a brute-force padlock attack needs up to $2^N$ tries.
### Summary {Exam Q2}
- BV Algorithm demonstrates the efficiency of **exponential queries via superposition**
- One multi-qubit query $|+\rangle^N$ in quantum computing represents $2^N$ binary strings in classical computing:
$$\boxed{|+\rangle^{N} = \frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}|\mathbb{B}_N(j)\rangle}$$
- State superposition allows **parallel evaluation** of independent queries:
$$U|+\rangle^{N} = \sum_{j=0}^{2^N-1}U|\mathbb{B}_N(j)\rangle$$
- For any blackbox function $f$ guaranteed to have a **single oracle**, we construct
$$U_{f_s} = I_N\otimes\begin{bmatrix}0\oplus f_s(x_{1:N}) & 0\\ 0 & 1\oplus f_s(x_{1:N})\end{bmatrix}$$
- The **last qubit is dedicated for the phase kickback technique**
- BV Algorithm **separates the oracle** $|s_{1:N}\rangle$ from an unseen quantum operator $U_f$ using quantum parallelism and the phase kickback technique:
$$\boxed{|s_{1:N}\rangle\otimes|1\rangle = H\,U_f\,H\big(|0\rangle^{\otimes N}\otimes|1\rangle\big)}$$
> 🎙️ **In class (Oct 6 part 2, 40:28–40:48):** "The idea is very simple. The **input is $|+\rangle^{\otimes N}$ and then $|-\rangle$**. The **output** is the Hadamard of each qubit [of the oracle] and then Hadamard of 1 for the last qubit."
### Bernstein-Vazirani Algorithm in PennyLane
```python
secret_code = [0,1,0,0,1,1,0,1]
no_bits = len(secret_code)
dev = qml.device('default.qubit', wires=no_bits + 1)
def oracle(secret_code, wires):
for i in range(len(secret_code)): # CCX for each bit of the oracle
if secret_code[i] == 1:
qml.CNOT(wires=[wires[i], wires[-1]])
@qml.qnode(dev, shots=500)
def bernstein_vazirani(secret_code):
qml.X(wires=no_bits) # Prepare state |+>^N (*) |-> from |0>^(N+1)
qml.Hadamard(wires=no_bits)
for i in range(no_bits):
qml.Hadamard(wires=[i])
oracle(secret_code, range(no_bits + 1)) # Apply the oracle operator U_f
for i in range(no_bits): # Extract the oracle from qubits[1:N]
qml.Hadamard(wires=[i])
return qml.counts(wires=range(no_bits)) # Return the oracle
bernstein_vazirani(secret_code)
# Expected result: {'01001101': 500}
```
- Note: since the secret bits are known classically in code, the CCX with control $s_k$ reduces to a plain **CNOT** from $x_k$ to the ancilla only where $s_k = 1$
- `wires[-1]` = the **last** wire (Python negative index) = the ancilla
> 🎙️ **In class (Oct 6 part 2, 41:04–43:21):** A student asked about `wires[-1]`: "Minus one is the last element, minus two the second last… it's a Python trick, not widely taught." Code is on his GitHub, "everything is runnable".
### Exercise 5.1: Bernstein-Vazirani Algorithm
- Answer the following questions:
1. Suggest how to modify the oracle operator $U_f$ to possess **multiple oracles**.
2. Show that $H^{\otimes N} = \frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}|\mathbb{B}_N(j)\rangle$ (applied to $|0\rangle^{\otimes N}$)
3. Show that for any oracle $s$ of length $N$: $s = \sum_{k=0}^{N-1}\big[2^k f_s(\mathbb{B}_N(2^k))\big]$, where $f_s(x_{1:N}) = x_{1:N}\cdot s_{1:N}$
4. Regarding the implementation of the oracle operator $U_f$, show that
$$\mathrm{CCX}^{(N+1)}_{s_k,x_k,x_{N+1}}\big[|\mathbb{B}_N(2^k)\rangle\otimes|1\rangle\big] = I_N\otimes\begin{bmatrix}0\oplus x_k s_k & 0\\ 0 & 1\oplus x_k s_k\end{bmatrix}$$
5. In connection with the previous question, show that
$$\prod_{k=1}^{N}\mathrm{CCX}^{(N+1)}_{s_k,x_k,x_{N+1}}\big[|+\rangle^{\otimes N}\otimes|-\rangle\big] = H|s_1\rangle\otimes\dots\otimes H|s_N\rangle\otimes H|1\rangle$$
---
## 2. Deutsch-Jozsa Algorithm
> [!exam] Final Q2 — Phase Kickback Technique (Ch5 slides 15–20)
> - Same technique as BV, now used to measure the **balance** of a function. Expect: why the balance score shows up as an amplitude, why we measure $|0\rangle^{\otimes N}$, difference between BV and DJ, why it is $O(1)$.
> - "Focus on the theoretical part: **measure on the all-zero** state" (not his ancilla-probability shortcut from the code).
> - See [[Final - Instructor Hints]]
### Balance Testing {Exam Q2}
- In cryptography, a **hash function** converts an input string into a binary representation
- A good hash function should be **balanced**, where all outputs are equally distributed
- David **Deutsch** and Richard **Jozsa** introduced a quantum algorithm for testing all input combinations (1992) in $O(1)$ time complexity
- Figure 6 (Balance testing problem): all inputs `00000000 … 11111111` → cryptographic hash function → scale icon "Balance or Imbalance"
> 🎙️ **In class (Oct 6 part 2, 45:26–50:36):** A crypto system = encryption + decryption + **hash function** (content → binary signature; if the content changes the signature changes). A good hash function must distribute signatures **uniformly (balanced)**. Classically you must try all contents — "intractable… exactly the same idea as cracking the ATM". Simplification for DJ: the hash outputs just **0 or 1**; balanced = equal numbers of 0s and 1s.
> 💡 **Analogy:** Checking whether a เซียมซี (fortune stick) cup is fair: classically you shake out every stick and count; DJ shakes them all at once and reads "fair / always the same" in one go.
### Deutsch-Jozsa Problem {Exam Q2}
- Suppose we have a black-box function $f$ that takes an input binary string and returns either 0 or 1:
$$f:\{0,1\}^N\mapsto\{0,1\}$$
- We are **guaranteed** that this function is **either constant or balanced**:
- **Constant**: it always returns either 0's or 1's for all inputs
- **Balanced**: it returns equal amounts of 0's and 1's
- **Best case**: we can classify the function in the first two samples — $[0,0]$ and $[1,1]$ for constant, while $[0,1]$ and $[1,0]$ for balanced (strictly, two different outputs prove *balanced*; two equal outputs are only a hint of constant)
- **Worst case**: we have to enumerate at most half of all inputs (+1) and count the amounts of 0's and 1's $\Rightarrow O(2^N)$
- This problem also belongs to the **BQP** complexity class
- Figure 7: inputs → black-box $f$ → 0 OR 1
### Deutsch-Jozsa Algorithm (balance score) {Exam Q2}
- Let's define a quantum operator $U_f$ in response to the function $f$:
$$\begin{aligned}
U_f &= \big(|x_{1:N}\rangle\langle x_{1:N}|\big)\otimes\big(|y\oplus f(x_{1:N})\rangle\langle y|\big) \\
&= I^{\otimes N}\otimes\big(|0\oplus f(x_{1:N})\rangle\langle0| + |1\oplus f(x_{1:N})\rangle\langle1|\big) \\
&= I^{\otimes N}\otimes\begin{bmatrix}0\oplus f(x_{1:N}) & 0\\ 0 & 1\oplus f(x_{1:N})\end{bmatrix}
\end{aligned}$$
- Notice the **phase kickback technique** (same form as BV)
- We set up a **balance score** of $f$ as follows:
$$\boxed{b(f) = \frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}(-1)^{f(\mathbb{B}_N(j))}}$$
- Apply $U_f$ to $|+\rangle^{\otimes N}\otimes|-\rangle$:
$$\begin{aligned}
U_f\big(|+\rangle^{\otimes N}\otimes|-\rangle\big)
&= \frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}|\mathbb{B}_N(j)\rangle\otimes\left(\frac{0\oplus f(\mathbb{B}_N(j))}{\sqrt2}|0\rangle\right) - |\mathbb{B}_N(j)\rangle\otimes\left(\frac{1\oplus f(\mathbb{B}_N(j))}{\sqrt2}|1\rangle\right) \\
&= \frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}(-1)^{f(\mathbb{B}_N(j))}|\mathbb{B}_N(j)\rangle\otimes\frac{|0\rangle-|1\rangle}{\sqrt2} \\
&= \left(\frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}(-1)^{f(\mathbb{B}_N(j))}|\mathbb{B}_N(j)\rangle\right)\otimes\frac{|0\rangle-|1\rangle}{\sqrt2} \\
&= b(f)\left(\sum_{j=0}^{2^N-1}|\mathbb{B}_N(j)\rangle\right)\otimes\frac{|0\rangle-|1\rangle}{\sqrt2}
\end{aligned}$$
> 📝 **Instructor's annotation:** He labelled the sum $\sum_j(-1)^{f(\mathbb{B}_N(j))}$ in the 4th line as **"balance score"**.
> 🎙️ **In class (Oct 6 part 2, 51:00–59:28):** $U_f$ "flips the ancillary qubit if the output of $f$ is 1, otherwise keeps it." Each output becomes $(-1)^0 = +1$ or $(-1)^1 = -1$; **sum (and average)** them: equal numbers of $+1$ and $-1$ → **0 = balanced**; otherwise imbalanced. "The phase kickback technique focuses on two things: the **input $|+\rangle^{\otimes N}|-\rangle$ (fixed)** and the **operator in this form** (flip the last qubit)."
> ❓ **Why does the balance appear as a phase?** Same reason as BV: with the ancilla in $|-\rangle$, "flip the ancilla when $f(x)=1$" becomes "multiply branch $|x\rangle$ by $(-1)^{f(x)}$". Each input string now carries its own $\pm1$ — the whole truth table of $f$ is written into the signs of one quantum state.
> ⚠️ **Precision note (mine):** The rigorous amplitude of $|0\rangle^{\otimes N}$ after the final $H^{\otimes N}$ is $\frac{1}{2^N}\sum_x(-1)^{f(x)}$, which is $\pm1$ for constant $f$ and $0$ for balanced $f$. The slide's $1/\sqrt{2^N}$ normalization is a simplification; the conclusions (1 vs 0) are the same.
### Deutsch-Jozsa Algorithm (measurement) {Exam Q2}
- **Algorithm:**
$$\begin{aligned}
&H^{\otimes N+1}U_fH^{\otimes N+1}\big(|0\rangle^{\otimes N}\otimes|1\rangle\big) \\
&= H^{\otimes N+1}U_f\big(|+\rangle^{\otimes N}\otimes|-\rangle\big) \\
&= H^{\otimes N+1}\left[\left(b(f)\sum_{j=0}^{2^N-1}|\mathbb{B}_N(j)\rangle\right)\otimes|-\rangle\right] \\
&= \big(b(f)|0\rangle^{\otimes N}\big)\otimes|1\rangle \\
&= b(f)\big(|0\rangle^{\otimes N}\otimes|1\rangle\big)
\end{aligned}$$
- **Difference from BV Algorithm**: black-box function $f$ is either constant or balanced, implying there might be **multiple oracles**
- We can now solve the Deutsch-Jozsa problem by **measuring the amplitude of the pure state $|0\rangle^{\otimes N}$**:
$$\boxed{\big[b(f)\big]^2 = \left|\frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}(-1)^{f(\mathbb{B}_N(j))}\right|^2}$$
- **Evaluation**:
- $b(f)$ evaluates to **1 if $f(x_{1:N})$ is constant**
- $b(f)$ evaluates to **0 if it is balanced**
- **Advantage**: we have evaluated all possible inputs at the same time via quantum parallelism — just apply the Hadamard gate to $|0\rangle^{\otimes N}$
> ❓ **Why measure only $|0\rangle^{\otimes N}$?** $H^{\otimes N}$ maps the uniform superposition back to $|0\cdots0\rangle$. If all signs are equal (constant), all the amplitude **constructively interferes** into $|0\cdots0\rangle$ (prob 1). If half the signs are $-$ (balanced), the contributions to $|0\cdots0\rangle$ **cancel exactly** (prob 0) and the amplitude moves to other strings. So "probability of all-zeros" = $|b(f)|^2$ = the verdict.
> ❓ **Why is the constant case "1" even for $f\equiv1$?** Then every sign is $-1$, so $b(f) = -1$; that is a **global phase**, and the measured probability $|b(f)|^2 = 1$ — still "constant".
### Deutsch-Jozsa Algorithm (circuit) {Exam Q2}
![[ch5-p22.jpg]]
- If we have an oracle function $f$ whose oracle is $|s_{1:N}\rangle$, we construct its kickback operator
$$U_f = I_N\otimes\begin{bmatrix}0\oplus f(x_{1:N}) & 0\\ 0 & 1\oplus f(x_{1:N})\end{bmatrix}$$
- The Deutsch-Jozsa algorithm for $f$ (Figure 8):
- Top register: $|0\rangle^{\otimes N}$ → $H^{\otimes N}$ → $U_f$ → $H^{\otimes N}$ → measure → look for $|0\rangle^{\otimes N}$
- Ancilla: $|1\rangle$ → $H$ → $U_f$ → $H$
> 🎙️ **In class (Oct 6 part 3, 0:36–1:28):** "What's the difference between BV and DJ? **Look at the output**. The measurement is done only on the quantum state $|00\ldots0\rangle$." The circuits are otherwise identical.
> 🎙️ **In class (Oct 6 part 3, 1:43–2:03):** DJ as taught only works for a hash function with output **0 or 1** (binary).
### Summary {Exam Q2}
- DJ Algorithm speeds up the **balance testing** problem by exponential queries via superposition
- The balance score of $f$ is the sum between 0's and 1's yielded from $f$:
$$b(f) = \frac{1}{\sqrt{2^N}}\sum_{j=0}^{2^N-1}(-1)^{f(\mathbb{B}_N(j))}$$
- **Evaluation**:
- If $b(f) = 1$, then $f$ is **constant**
- If $b(f) = 0$, then $f$ is **balanced**
- Otherwise, $f$ is **neither** of them
- For any blackbox function $f$ that yields either 0 or 1, we construct
$$U_f = I_N\otimes\begin{bmatrix}0\oplus f(x_{1:N}) & 0\\ 0 & 1\oplus f(x_{1:N})\end{bmatrix}$$
- DJ Algorithm evaluates the balance score $b(f)$ from an unseen quantum operator $U_f$ using **quantum parallelism and the phase kickback technique**:
$$\boxed{b(f)\,|0\rangle^{\otimes N}\otimes|1\rangle = H\,U_f\,H\big(|0\rangle^{\otimes N}\otimes|1\rangle\big)}$$
- We obtain $b(f)$ by **measuring the amplitude of $|0\rangle^{\otimes N}$**
| | Bernstein-Vazirani | Deutsch-Jozsa |
|---|---|---|
| Problem | find hidden string $s$ in $f(x) = s\cdot x \bmod 2$ | is $f$ constant or balanced? |
| Real-world story | crack a padlock / ATM | test a hash function |
| Promise | exactly one oracle $s$ | constant **or** balanced (may have many "oracles") |
| Input | $\lvert 0\rangle^{\otimes N}\otimes\lvert 1\rangle$ | $\lvert 0\rangle^{\otimes N}\otimes\lvert 1\rangle$ |
| Circuit | $H$ – $U_f$ – $H$ | $H$ – $U_f$ – $H$ (same) |
| Read-out | each qubit's $\langle Z\rangle$ → $s_1\ldots s_N$ | amplitude/probability of $\lvert 0\rangle^{\otimes N}$ |
| Queries | 1 (classical: $N$) | 1 (classical worst: $2^{N-1}+1$) |
> [!info] Related
> - Both use the same kickback idea → reused in Grover / Shor's period-finding: [[Lecture 6 - Searching and Cryptography]]
> - Parameterized oracles ($R_X(\theta)$) are a preview of [[Lecture 8 - Quantum Artificial Intelligence]]
### Deutsch-Jozsa Algorithm in PennyLane
```python
no_bits = 4 # Length of the hash code
dev = qml.device('default.qubit', wires=no_bits + 1)
def oracle(theta1, theta2, theta3):
# Compute a hash code from the input wires
qml.RX(theta1, wires=[0]); qml.CNOT(wires=[0, 1])
qml.RZ(theta2, wires=[2]); qml.CNOT(wires=[2, 3])
qml.RY(theta3, wires=4)
qml.Toffoli(wires=[0, 2, no_bits]) # Last bit is the type of this hash code
@qml.qnode(dev, shots=500)
def deutsch_jozsa():
qml.X(wires=no_bits) # Prepare state |+>^N (*) |-> from |0>^(N+1)
qml.Hadamard(wires=no_bits)
for i in range(no_bits):
qml.Hadamard(wires=[i])
oracle(np.pi / 4, np.pi / 4, np.pi / 4) # Apply the oracle operator U_f
for i in range(no_bits): # Extract the balance from the last qubit
qml.Hadamard(wires=[i])
return qml.probs(wires=[no_bits]) # Return the balance
deutsch_jozsa()
# Expected result: [0.65, 0.35]
```
> 🎙️ **In class (Oct 6 part 3, 2:28–5:57):** His code differs from the paper: it measures the **probability of the ancilla** (0 vs 1) instead of the all-zero state — it "saves memory" on a simulator (no need to look at all $2^{10}$ outcomes). "**In the exam, focus on the theoretical part: measure on the all-zero.**"
### Exercise 5.2: Deutsch-Jozsa Algorithm
- Let's define the following operators:
$$\mathrm{MCX}^{(N+1)} = \prod_{k=1}^{N}\mathrm{CNOT}^{(N+1)}_{k,N+1} \qquad \mathrm{MCZ}^{(N+1)} = \prod_{k=1}^{N}\mathrm{CZ}^{(N+1)}_{k,N+1}$$
- Draw quantum circuits for the following oracle operators. Then implement them in PennyLane to check if each of them is balanced or not:
1. $\mathrm{MCX}^{(5)}$
2. $\mathrm{MCZ}^{(5)}$
3. $\mathrm{MCX}^{(5)}\,\mathrm{MCZ}^{(5)}$
4. $\mathrm{MCX}^{(5)}\,\mathrm{SWAP}^{(5)}_{1,5}$
5. $\mathrm{CCX}^{(5)}_{3,4,5}\,\mathrm{CCX}^{(5)}_{1,2,3}$
6. $\mathrm{MCX}^{(5)}\,X^{(5)}_3\,X^{(5)}_1$
7. $\mathrm{MCZ}^{(5)}\,Z^{(5)}_4\,Z^{(5)}_2$
8. Let $U_f(\theta)$ be this quantum circuit: $R_X(\theta)$ on $x_1$, $R_Z(\theta)$ on $x_3$, $R_Y(\theta)$ on $x_5$, CNOT $x_1\to x_2$, CNOT $x_3\to x_4$, then an MCX block on all 5 wires. **Vary $\theta$ to make it balanced.**
> 🎙️ **In class (Oct 6 part 3, 6:24–8:11):** "This is an example of **your $U_f$** — read the definition." These need code (5 qubits by hand "will take a while"). Q8 is taken from **quantum machine learning**: the $\theta$'s act as model parameters (preview of Ch8).
---
## 3. Quantum Teleportation
> [!exam] Final Q4 — Quantum Teleportation (Ch5 slides 23–26)
> - Instructor: "Like a cowboy movie, **you will be a bartender talking to a bandit**. Be cautious for everything the bandit says." → expect a dialogue full of **plausible-but-wrong claims** that you must catch and explain.
> - Be exact about: **which measured bit is which** ($p$ = message qubit after H, $q$ = sender's half of the Bell pair after CNOT), **which correction goes with which bit** ($q\to X$, $p\to Z$), the **order** (X first, then Z), and **why** it is **not faster than light** and **not cloning**.
> - He also said (Oct 20): a question on **combining superdense coding and teleportation** "will show up for sure".
> - See the **Bandit claims checklist** at the end of this section and [[Final - Instructor Hints]]
### Quantum Teleportation {Exam Q4}
![[ch5-p27.jpg]]
- **Quantum teleportation** is a **communication technique**, where information is transferred over a long distance via **entangled states**
- In traditional communication, information is transferred by **state cloning** (copying)
- **No-cloning theorem**: it is **impossible to create an independent and identical copy of an unknown quantum state**
- We measure it by collapsing onto the axes, and construct a new state accordingly
- However, we have **completely destroyed the original state by measurement**, resulting in **no perfect clone**
- **Spoiler**: this is **NOT** the teleportation in your favorite sci-fi movies
- Figure 9 (No-cloning theorem): Original state → measure → "Original state is annihilated"; from the measurement → **Recreate** → "Copied state" (not identical)
$$\boxed{\text{No-cloning: there is no } U \text{ with } U\big(|\psi\rangle\otimes|0\rangle\big) = |\psi\rangle\otimes|\psi\rangle \text{ for all unknown } |\psi\rangle}$$
> 🎙️ **In class (Oct 6, 16:07–16:59):** "This is not sci-fi… It's **not faster than light. It's just as fast as light. But it's very secure.** You no longer rely on cryptography; you rely on quantum mechanical phenomena."
> 🎙️ **In class (Oct 6 part 3, 8:22–14:20):** Teleportation is "a fancy term for a **secure communication protocol**", not mass transfer. Classical crypto relies on mathematical hardness; quantum communication uses a very simple technique yet "you cannot even eavesdrop without the other partner knowing". To copy a qubit you must read it → it collapses → "the original one… **dies**". Even **non-destructive (weak) measurement** only lets you *guess* (~90%), so there is still **no perfect clone**.
> 🎙️ **In class (Oct 20, 8:49–10:04):** No-cloning ⇒ if someone eavesdrops on a qubit carried by a particle, it collapses immediately and "you'll be alerted right away".
> ❓ **Why does no-cloning make it secure?** An eavesdropper cannot copy the quantum part and keep a perfect duplicate; any attempt to read it disturbs it (detectable). And the classical part ($p,q$) carries no information about $a,b$ by itself (see below).
> 💡 **Analogy:** Trying to photocopy a soap bubble — the moment you press it on the glass, the original pops.
### Secure Communication via Entanglement — Sender {Exam Q4}
- State cloning can be **emulated with entangled states**, offering a secure means of communication
- **Ingredients:**
- An unknown qubit $|x\rangle = a|0\rangle + b|1\rangle$
- Entanglement $|\Phi^+\rangle$ distributed over quantum networks, e.g. fiber optics
- A **classical communication channel**
- **Setup:**
- Sender and receiver share an entangled pair $|\Phi^+\rangle = \frac{|00\rangle+|11\rangle}{\sqrt2}$: $|\Phi^+_A\rangle$ for **sender** and $|\Phi^+_B\rangle$ for **receiver** (slide prints the denominator as 2; it is $\sqrt2$)
- Note that $|\Phi^+_A\rangle$ and $|\Phi^+_B\rangle$ are **either both 0's or both 1's at the same time**
- We have an **unseen** qubit $|x\rangle$
- **Sender**: if we collapse $|y_1,y_2\rangle$, we will get $|y_3\rangle$ (normalization $\tfrac{1}{\sqrt2}$, later $\tfrac12$, omitted as on the slide)
$$\begin{aligned}
|y_1,y_2,y_3\rangle
&= H^{(3)}_1\,\mathrm{CNOT}^{(3)}_{1,2}\big(|x\rangle\otimes|\Phi^+\rangle\big) \\
&= H^{(3)}_1\,\mathrm{CNOT}^{(3)}_{1,2}\big((a|0\rangle+b|1\rangle)\otimes(|00\rangle+|11\rangle)\big) \\
&= H^{(3)}_1\,\mathrm{CNOT}^{(3)}_{1,2}\big(a|000\rangle + b|100\rangle + a|011\rangle + b|111\rangle\big) \\
&= H^{(3)}_1\big(a|000\rangle + b|110\rangle + a|011\rangle + b|101\rangle\big) \\
&= a|000\rangle + a|100\rangle + b|010\rangle - b|110\rangle + a|011\rangle + a|111\rangle + b|001\rangle - b|101\rangle \\
&= |00\rangle\otimes(a|0\rangle+b|1\rangle) + |01\rangle\otimes(b|0\rangle+a|1\rangle) + |10\rangle\otimes(a|0\rangle-b|1\rangle) + |11\rangle\otimes(-b|0\rangle+a|1\rangle)
\end{aligned}$$
$$\boxed{|y_1y_2y_3\rangle = \tfrac12\Big[|00\rangle(a|0\rangle+b|1\rangle) + |01\rangle(b|0\rangle+a|1\rangle) + |10\rangle(a|0\rangle-b|1\rangle) + |11\rangle(-b|0\rangle+a|1\rangle)\Big]}$$
- Qubit roles: $y_1$ = the message $|x\rangle$, $y_2$ = sender's key $|\Phi^+_A\rangle$, $y_3$ = receiver's key $|\Phi^+_B\rangle$
> 📝 **Instructor's annotation (slide colouring):** the expanded $|x\rangle$ is in green; the four groups by the first two qubits are colour-coded: $|00\rangle$ (blue) → $a|0\rangle+b|1\rangle$, $|01\rangle$ → $b|0\rangle+a|1\rangle$, $|10\rangle$ → $a|0\rangle-b|1\rangle$, $|11\rangle$ → $-b|0\rangle+a|1\rangle$ (red).
> 🎙️ **In class (Oct 6 part 3, 27:44–28:26):** Common mistake (a student, Feel, in the midterm): **forgetting to distribute the tensor product** — $a|0\rangle$ must go to both $|00\rangle$ and $|11\rangle$, and so must $b|1\rangle$ — "that's why you got the wrong CNOT."
> 🎙️ **In class (Oct 6 part 3, 34:15–40:18):** Group the 8 terms by the first two qubits and look at the last qubit: $|00\rangle$ → exactly $|x\rangle$; $|01\rangle$ → $a,b$ swapped = **X**$|x\rangle$; $|10\rangle$ → sign of $|1\rangle$ flipped = **Z**$|x\rangle$; $|11\rangle$ → swapped **and** sign flipped = "X and then Z". "These first two qubits are the **signature** for the operations" — "**if the second qubit is 1, I should do X; if the first qubit is 1, I should do Z**." He first wrote "QP", then corrected: "**PQ** — my own notation is PQ."
> 🎙️ **In class (Oct 20, 17:19–19:44):** Recap: 00 → don't touch; 01 → **bit flip**; 10 → **sign flip**; 11 → **flip the bit and flip the sign**. "**P means the first qubit, Q means the second qubit.**"
> ❓ **Why does the 2nd bit ($q$) decide the X correction?** $q$ comes from $\Phi^+_A$ after the **CNOT** controlled by $x$. The CNOT copies the "is it 0 or 1" (bit) correlation of $x$ into the key, and the key is perfectly correlated with Bob's half. If $q=1$, Bob's half ended up **bit-flipped** relative to $x$ → undo with X.
> ❓ **Why does the 1st bit ($p$) decide the Z correction?** $p$ comes from $x$ after the **Hadamard**. H turns the bit value of $x$ into a **sign**: $H|1\rangle = (|0\rangle-|1\rangle)/\sqrt2$. When $p = 1$, the $|1\rangle$-branch of the $b$ term picked up a minus sign → Bob's half has a **sign flip** → undo with Z.
> ❓ **Why is each outcome equally likely (1/4)?** Each group has amplitude $\tfrac12$ times a normalized state ($|a|^2+|b|^2 = 1$), so $P(pq) = \tfrac14$ for all four — **independent of $a,b$**. That is why $p,q$ alone reveal nothing about the message.
### Secure Communication via Entanglement — Receiver {Exam Q4}
- **Receiver**: if we know $|y_1,y_2\rangle$, let's decode $|y_3\rangle$:
$$\begin{aligned}
|z_1,z_2,z_3\rangle &= \mathrm{CZ}^{(3)}_{1,3}\,\mathrm{CNOT}^{(3)}_{2,3}\,|y_1,y_2,y_3\rangle \\
&= \mathrm{CZ}^{(3)}_{1,3}\,\mathrm{CNOT}^{(3)}_{2,3}\Big(|00\rangle(a|0\rangle+b|1\rangle) + |01\rangle(b|0\rangle+a|1\rangle) + |10\rangle(a|0\rangle-b|1\rangle) + |11\rangle(-b|0\rangle+a|1\rangle)\Big) \\
&= \mathrm{CZ}^{(3)}_{1,3}\Big(|00\rangle(a|0\rangle+b|1\rangle) + |01\rangle(a|0\rangle+b|1\rangle) + |10\rangle(a|0\rangle-b|1\rangle) + |11\rangle(a|0\rangle-b|1\rangle)\Big) \\
&= |00\rangle(a|0\rangle+b|1\rangle) + |01\rangle(a|0\rangle+b|1\rangle) + |10\rangle(a|0\rangle+b|1\rangle) + |11\rangle(a|0\rangle+b|1\rangle)
\end{aligned}$$
- Step 1 — $\mathrm{CNOT}_{2,3}$ (control = $y_2$ = $q$): un-swaps $a,b$ in the $|01\rangle$ and $|11\rangle$ groups
- Step 2 — $\mathrm{CZ}_{1,3}$ (control = $y_1$ = $p$): fixes the minus sign in the $|10\rangle$ and $|11\rangle$ groups
- Result: **the third qubit is $|x\rangle = a|0\rangle+b|1\rangle$ in every branch**
$$\boxed{|x\rangle = Z^{\,p}\,X^{\,q}\,|y_3\rangle \quad\text{(apply } X \text{ if } q=1 \text{ first, then } Z \text{ if } p=1\text{)}}$$
| Measured $p\,q$ | Bob's qubit before correction | Correction | After |
|---|---|---|---|
| 0 0 | $a\lvert 0\rangle + b\lvert 1\rangle$ | none ($I$) | $a\lvert 0\rangle + b\lvert 1\rangle$ |
| 0 1 | $b\lvert 0\rangle + a\lvert 1\rangle$ | $X$ | $a\lvert 0\rangle + b\lvert 1\rangle$ |
| 1 0 | $a\lvert 0\rangle - b\lvert 1\rangle$ | $Z$ | $a\lvert 0\rangle + b\lvert 1\rangle$ |
| 1 1 | $-b\lvert 0\rangle + a\lvert 1\rangle$ | $X$ then $Z$ ($ZX$) | $a\lvert 0\rangle + b\lvert 1\rangle$ |
> 🎙️ **In class (Oct 6 part 3, 41:04–48:11):** Worked the receiver line by line: CNOT on 2nd→3rd ("if the second qubit is 0 you don't touch the third; if 1 you flip"), then CZ on 1st→3rd ("if the first qubit is 1, negate $|1\rangle$"). "What do we have in the last qubit? **$|x\rangle$, $|x\rangle$, $|x\rangle$, $|x\rangle$** — you have decrypted the message."
> 🎙️ **In class (Oct 20, 14:48–15:14):** "You start from $q$. $q$ is produced by applying the CNOT, so **you apply the CNOT first** [conditioned on $q$]. Then you flip the sign with $p$."
> ❓ **Why X before Z (and does order matter)?** In the 11 branch: $X(-b|0\rangle+a|1\rangle) = a|0\rangle - b|1\rangle$, then $Z \to a|0\rangle + b|1\rangle$ ✓. Doing $Z$ first then $X$ gives $-(a|0\rangle+b|1\rangle)$ — the same state up to a **global phase** (unobservable), so both orders work physically, but the slide's/instructor's prescribed order is **CNOT($q$) then CZ($p$)** — write it that way.
> 💡 **Analogy:** Bob holds a locked box that is in one of four "scrambled" versions of the message. Alice's two bits are not the message — they are the **instructions** "which of the 4 keys to turn". Without the instructions, Bob can only guess (25% each); with them he un-scrambles perfectly.
### Quantum Teleportation Algorithm for Secure Communication {Exam Q4}
![[ch5-p30.jpg]]
1. Sender and receiver are far apart, but they share a pair of **secure keys** $(|\Phi^+_A\rangle, |\Phi^+_B\rangle)$
2. We apply **CNOT on $|\Phi^+_A\rangle$ conditioned on the message $|x\rangle$**, and then apply **H on $|x\rangle$**
3. **Encoding**: we **measure the message and the sender's secure key as $(p,q)$**, and send them over through the **classical channel** ⇒ measurement causes **immediate collapse** $|\Phi^+_B\rangle := |\Phi^+_A\rangle$
4. **Decoding**: on $|\Phi^+_B\rangle$, we apply **CNOT conditioned on $q$** and **CZ conditioned on $p$**
- Figure 10 (circuit):
- SENDER: $|x\rangle$ —●— H — measure → $p$; $|\Phi^+_A\rangle$ — ⊕ (target of CNOT from $x$) — measure → $q$
- RECEIVER: $|\Phi^+_B\rangle$ — X (classically controlled by $q$, double line) — Z (classically controlled by $p$) → $|y\rangle = |x\rangle$
- Dotted arrow between $\Phi^+_A$ and $\Phi^+_B$ = they are entangled
$$\boxed{p = \text{measure}(H\text{ on } x) \to \text{controls } Z \qquad q = \text{measure}(\Phi^+_A \text{ after CNOT}) \to \text{controls } X}$$
> 🎙️ **In class (Oct 6 part 3, 18:30–25:09):** Setup = 3 things: the qubit $x$ to send, the entangled pair $\Phi_A,\Phi_B$, and a **classical channel** ("your internet, your Wi-Fi"). $\Phi_A,\Phi_B$ are like **keys** (public/private). Sender: CNOT($x\to\Phi_A$), H($x$), measure $p$ (from $x$) and $q$ (from $\Phi_A$); "because you measure $\Phi_A$, $\Phi_B$ also collapses at the same time." Send $p,q$ as the **encrypted message**. Receiver: CNOT controlled by classical $q$ (double line), CZ controlled by $p$ → $y$ = exactly $x$. "Up here is **encryption**, down here is **decryption**; $\Phi_A$ and $\Phi_B$ are **secure keys** (shared decryption keys)."
> 🎙️ **In class (Oct 6 part 3, 49:28–50:17):** "It doesn't mean you transport the actual qubit faster than light. **You need to send $P$ and $Q$ through a classical channel** — radio, Wi-Fi, cable. Therefore it's **not faster than light**. Quantum teleportation is a secure protocol. **It's just secure. It's not fast.**"
> 🎙️ **In class (Oct 6 part 3, 50:28–52:25):** "Although $P$ and $Q$ are eavesdropped along the line, the eavesdropper **still cannot decode the message because he does not have $\Phi_B$**. That's the key." Both sides need many entangled pairs and must **use them one by one** (one pair per qubit sent).
> 🎙️ **In class (Oct 6 part 3, 48:18–48:42):** In cyber security the encrypted message is a **"crypt"** — which also means a graveyard: "you dig the original body from the crypt."
> ❓ **Why is it NOT faster than light?** The instant Alice measures, Bob's qubit collapses to one of 4 states — but each with probability 1/4, so on average Bob holds $\tfrac14\sum(\text{4 states}) = \tfrac12 I$ — a **maximally mixed state with zero information** about $a,b$. Nothing Bob can measure changes when Alice acts. He must wait for $p,q$, which travel at **≤ the speed of light** over a classical channel.
> ❓ **Why is it NOT cloning?** At the end Alice's qubit $x$ has been **measured** (collapsed to $|p\rangle$) — the original is **destroyed**. Only **one** copy of $a|0\rangle+b|1\rangle$ exists at any time (now at Bob's). No-cloning is respected; the state was **moved**, not copied.
> ❓ **Why is the input qubit destroyed after encrypting it into $(p,q)$?** (Ex 5.3 Q3) Because extracting $p$ requires **measuring** $x$, which collapses it to $|0\rangle$ or $|1\rangle$; $a,b$ are no longer in Alice's lab.
> ❓ **Why do we still need the entangled pair if we send 2 bits anyway?** Two classical bits cannot describe the continuous amplitudes $a,b$ (infinitely many states). The amplitudes travel through the **entanglement**; the bits only say **which Pauli fix** to apply.
> ❓ **Why is physical transmission of qubits insecure (Ex 5.3 Q1) but teleportation secure (Q2)?** Sending the particle itself means it can be intercepted on the line (and an attacker can try to measure/guess it). In teleportation, **no quantum carrier of the message travels**; only $p,q$ travel, which are uniformly random without $\Phi_B$.
### Cartoon from JPL, NASA
![[ch5-p31.jpg]]
- Figure 11: Quantum teleportation as a cartoon ("What happens to A will affect B")
- Alice has a **yellow** photon; Charlie has a pair of **blue** entangled photons; sends one to Alice, one to Bob (stored in his crystal "memory bank")
- Alice and Bob's relationship is cordial, but somewhat distant (a long way)
- Alice's blue photon collides with her yellow photon; she **measures** the event and learns that the state of her photon has been teleported to Bob's (both photons annihilated)
- Alice's measuring affects Bob's far-away photon, changing its state — **however, Bob can't determine that his photon has changed**
- … until Alice sends him **two bits of information over an optical fiber**
- Bob learns his photon has changed from blue to yellow, too
- Source: https://www.jpl.nasa.gov/news/researchers-advance-quantum-teleportation/
> 🎙️ **In class (Oct 6 part 3, 52:37–55:13):** In physics the endpoints are **Alice and Bob** (third party = Charlie). Key point in the cartoon: Bob's photon collapses, but **Bob can't read it yet — he must wait for $P$ and $Q$**. "There's no joke here… NASA nerdy joke, where can I laugh? No laugh."
### Quantum Teleportation in PennyLane
```python
dev = qml.device('default.qubit', wires=['S', 'A', 'B'])
def prepare_bellstate():
qml.Hadamard(wires='A')
qml.CNOT(wires=['A', 'B'])
def sender(info):
qml.cond(info==1, qml.X)(wires=['S'])
qml.CNOT(wires=['S', 'A'])
qml.Hadamard(wires='S')
def receiver(p, q):
qml.cond(q, qml.X)(wires='B')
qml.cond(p, qml.Z)(wires='B')
@qml.qnode(dev, shots=500)
def quantum_teleportation(info):
prepare_bellstate() # Bell state |Phi+> as carrier
sender(info) # Encrypt the message
(p, q) = (qml.measure('S'), qml.measure('A')) # Measure (p,q) and send them
receiver(p, q) # via classical communication
return qml.probs(wires='B') # Receiver extracts the message
quantum_teleportation(info=1)
# Expected result: [0.0, 1.0]
```
- Note `receiver`: **X conditioned on `q`** (from wire `A`), **then Z conditioned on `p`** (from wire `S`) — exactly the slide-26 rule
- `qml.measure` = mid-circuit measurement; `qml.cond` = classically controlled gate (see [[Lecture 4 - Computation, Measurement, and Dynamic Circuits]])
### Quantum Gate Teleportation (Skipped)
- **Quantum gate teleportation** is an extension, where an **entangled state** is transferred over a long distance
- We **cannot separate counterparts of an entangled state** in teleportation because that will **destroy the entanglement**
- **Ingredients:**
- An entangled state $|x_A,x_B\rangle$
- 4-qubit entanglement $|\chi\rangle$, where
$$|\chi_A,\chi_B,\chi_C,\chi_D\rangle = \frac12\big(|0000\rangle + |0111\rangle + |1100\rangle + |1011\rangle\big)$$
- Classical communication channel
- **Setup:**
- Sender and receiver share a 4-qubit entangled state: $|\chi_A,\chi_D\rangle$ for sender, and $|\chi_B,\chi_C\rangle$ for receiver ⇒ **secure keys**
- $|\chi_A,\chi_D\rangle$ corresponds with $|\chi_B,\chi_C\rangle$:
| $\lvert\chi_A,\chi_D\rangle$ | $\lvert\chi_B,\chi_C\rangle$ |
|---|---|
| $\lvert 00\rangle$ | $\lvert 00\rangle$ |
| $\lvert 01\rangle$ | $\lvert 11\rangle$ |
| $\lvert 10\rangle$ | $\lvert 10\rangle$ |
| $\lvert 11\rangle$ | $\lvert 01\rangle$ |
- We have an unseen entanglement $|x_A,x_B\rangle$
> 🎙️ **In class (Oct 6 part 3, 55:35–56:25; Oct 20, 23:46–24:15):** "Quantum gate teleportation — a little too advanced, so we're going to **skip** … In my book it's not skipped. **It's skipped from the exam.** It's just two quantum teleportations attached together."
#### Circuit (Figure 12)
![[ch5-p34.jpg]]
- Prepare $|\chi\rangle$: H + CNOTs on four $|0\rangle$ qubits; Sender: CNOT $x_A\to$ wire 2, H on $x_A$, measure $p_1,q_1$; CNOT $x_B\to$ wire 5, H on wire 5, measure $q_2,p_2$; Receiver: classically controlled Z's and X's on the two middle wires, final CNOT → $|y_A\rangle,|y_B\rangle$
#### Quantum Gate Teleportation in PennyLane/1
```python
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('default.qubit', wires=['S1', 'S2', 'X1', 'X2', 'X3', 'X4'])
def prepare_bellstate(wire1, wire2, minus=False):
qml.Hadamard(wires=wire1)
if minus:
qml.PauliX(wires=wire2)
qml.CNOT(wires=[wire1, wire2])
def prepare_chistate(wire1, wire2, wire3, wire4):
prepare_bellstate(wire1, wire2, minus=False)
prepare_bellstate(wire3, wire4, minus=False)
qml.CNOT(wires=[wire3, wire2])
def sender(input1, input2, x1, x2, x3, x4):
qml.CNOT(wires=[input1, x1])
qml.CNOT(wires=[x4, input2])
qml.Hadamard(wires=input1)
qml.Hadamard(wires=x4)
```
#### Quantum Gate Teleportation in PennyLane/2
```python
def receiver(p1, q1, p2, q2, x2, x3):
qml.cond(p1, qml.Z)(wires=x2)
qml.cond(p1, qml.Z)(wires=x3)
qml.cond(p2, qml.X)(wires=x2)
qml.cond(p2, qml.X)(wires=x3)
qml.cond(q1, qml.X)(wires=x2)
qml.cond(q2, qml.Z)(wires=x3)
qml.CNOT(wires=[x3, x2])
@qml.qnode(dev, shots=500)
def quantum_gate_teleportation(info):
prepare_bellstate('S1', 'S2', minus=info) # Input entangled state
prepare_chistate('X1', 'X2', 'X3', 'X4') # Prepare the chi state
sender('S1', 'S2', 'X1', 'X2', 'X3', 'X4') # Sender part
p1 = qml.measure('S1') # Measure four qubits
p2 = qml.measure('S2')
q1 = qml.measure('X1')
q2 = qml.measure('X4')
receiver(p1, p2, q1, q2, 'X2', 'X3') # Receiver part
return qml.counts(wires=['X2', 'X3'])
quantum_gate_teleportation(info=True) # False = Bell-plus, True = Bell-minus
# Expected result: {'01': 250, '10': 250}
```
### Exercise 5.3: Quantum Teleportation
- Answer the following questions:
1. Explain why **physical transmission of qubits** over quantum networks is **insecure**.
2. Explain why quantum teleportation addresses the problem of **eavesdropping** in physical transmission with the **no-cloning theorem**.
3. Explain why the **input qubit is destroyed** after encrypting it into two classical bits $(p,q)$.
4. Explain how we **reconstruct** the encrypted qubit from two bits $(p,q)$ received from a classical communication channel.
5. Modify the quantum teleportation algorithm so that it uses $|\Psi^-\rangle$ as secure keys. Implement a PennyLane code to validate your idea.
6. Explain why we **cannot teleport separate qubits of the entangled state**.
7. Experiment with the PennyLane code for quantum gate teleportation with variations of Bell states $|\Psi^+\rangle, |\Psi^-\rangle, |\Phi^+\rangle, |\Phi^-\rangle$. Observe the algorithm's behaviors and outputs.
- Q1–Q4 are answered by the ❓ boxes above; Q4 = the correction table (CNOT on $q$, CZ on $p$)
- Q6 hint (slide 29): teleporting each half separately would **break the entanglement** between them — the entangled pair must be teleported as a whole (gate teleportation)
> [!exam] Bandit claims checklist (Final Q4 practice — my compilation)
> | Bandit says… | Bartender's reply |
> |---|---|
> | "I teleported the qubit faster than light." | ✗ Bob needs $p,q$ via a **classical channel** (≤ c). Before they arrive his qubit is $\tfrac12 I$ (no info). |
> | "Now Alice and Bob both have the qubit — I cloned it." | ✗ Alice's $x$ was **measured and destroyed**; only Bob has it. No-cloning holds. |
> | "If $p=1$ Bob applies X; if $q=1$ he applies Z." | ✗ Reversed. **$q$ (from $\Phi_A$, after CNOT) → X**; **$p$ (from $x$, after H) → Z**. |
> | "Bob applies Z first, then X." | Prescribed order is **CNOT($q$) then CZ($p$)** ($\lvert x\rangle = Z^pX^q\lvert y_3\rangle$); reversed order differs only by a global phase on 11, but state the slide's order. |
> | "I stole $p,q$ on the wire, so I know the message." | ✗ $p,q$ are **uniformly random** (1/4 each), independent of $a,b$; useless without $\Phi_B$. |
> | "Bob can read the message the moment Alice measures." | ✗ Collapse is instant but **unreadable**; he must wait for $p,q$ (NASA cartoon: "Bob can't determine his photon has changed"). |
> | "Bob now knows $a$ and $b$." | ✗ Bob **has** the state, he does not **know** $a,b$; measuring it would collapse it. |
> | "Two bits are enough to describe a qubit." | ✗ $a,b$ are continuous; they travel via **entanglement**; bits only choose among 4 fixes. |
> | "We can reuse the same Bell pair for the next qubit." | ✗ The pair is **consumed** (measured); one fresh pair per qubit, used **one by one**. |
> | "Teleportation moves matter / the particle." | ✗ It moves **quantum information (state)**, not mass — it's a **secure communication protocol**. |
> | "We measured $y_3$ (Bob's qubit) to get $q$." | ✗ $p,q$ come from **Alice's** two qubits ($y_1 = x$, $y_2 = \Phi_A$); Bob does not measure before correcting. |
> | "The protocol works with no shared entanglement." | ✗ The Bell pair $\Phi^+$ is the **secure key**; without it there is nothing to collapse into $x$. |
---
## 4. Superdense Coding
> [!info] Related
> - Same Bell-pair ingredients as teleportation; "reverse" trade: teleportation = 1 ebit + **2 classical bits → 1 qubit**; superdense = 1 ebit + **1 qubit → 2 classical bits**
### Superdense Coding
![[ch5-p39.jpg]]
- **Superdense coding** is a **compression** method whereby **two classical bits** can be transmitted with **a single quantum state**
- **Ingredients:**
- Two classical bits $b_1b_2$
- Entanglement $|\Phi^+\rangle$ distributed over quantum networks, e.g. fiber optics
- **Quantum** communication channel
- **Setup:**
- Sender and receiver share an entangled pair $|\Phi^+\rangle = \frac{|00\rangle+|11\rangle}{\sqrt2}$: $|\Phi^+_A\rangle$ for sender and $|\Phi^+_B\rangle$ for receiver
- Sender wants to send two classical bits $b_1b_2$ to the receiver
- Figure 13 (Overview): Alice ($b_1b_2$) → sends the **modified entangled pair** (her half) → Bob → measurement → $b_1b_2$
> 🎙️ **In class (Oct 6 part 3, 56:42–57:06; Oct 20, 23:29–26:04):** "Superdense coding — this is **WinZip**": a **2:1 ratio**. "Can we transmit two classical bits on just a single qubit? … We compress two classical bits in one qubit: the **bit flipping and the sign flipping**."
> 💡 **Analogy:** A single postcard that can be rotated/flipped 4 ways; Bob, who holds the matching half, can tell which of the 4 ways it was sent — 2 bits from 1 card.
### Superdense Coding — Sender
- **Sender**: we encode **$b_1$ with sign-flipping** and **$b_2$ as bit-flipping**:
$$\begin{aligned}
Q(b_1,b_2) &= \mathrm{CZ}^{(2)}_{b_1,1}\,\mathrm{CNOT}^{(2)}_{b_2,1}\,\frac{1}{\sqrt2}\big(|00\rangle+|11\rangle\big) \\
&= \mathrm{CZ}^{(2)}_{b_1,1}\begin{cases}\frac{1}{\sqrt2}(|00\rangle+|11\rangle) & [00]\\ \frac{1}{\sqrt2}(|10\rangle+|01\rangle) & [01]\\ \frac{1}{\sqrt2}(|00\rangle+|11\rangle) & [10]\\ \frac{1}{\sqrt2}(|10\rangle+|01\rangle) & [11]\end{cases}
= \begin{cases}\frac{1}{\sqrt2}(|00\rangle+|11\rangle) & [00]\\ \frac{1}{\sqrt2}(|10\rangle+|01\rangle) & [01]\\ \frac{1}{\sqrt2}(|00\rangle-|11\rangle) & [10]\\ \frac{1}{\sqrt2}(|10\rangle-|01\rangle) & [11]\end{cases}
\end{aligned}$$
- Table 1: Superdense coding
| Message $b_1,b_2$ | State $Q(b_1,b_2)$ |
|---|---|
| 00 | $\frac{1}{\sqrt2}(\lvert 00\rangle + \lvert 11\rangle)$ = $\lvert\Phi^+\rangle$ |
| 01 | $\frac{1}{\sqrt2}(\lvert 10\rangle + \lvert 01\rangle)$ = $\lvert\Psi^+\rangle$ |
| 10 | $\frac{1}{\sqrt2}(\lvert 00\rangle - \lvert 11\rangle)$ = $\lvert\Phi^-\rangle$ |
| 11 | $\frac{1}{\sqrt2}(\lvert 10\rangle - \lvert 01\rangle)$ = $\lvert\Psi^-\rangle$ (up to sign) |
$$\boxed{Q(b_1,b_2) = Z^{b_1}X^{b_2}\otimes I\,|\Phi^+\rangle \quad (b_1 \to Z \text{ sign flip},\ b_2 \to X \text{ bit flip})}$$
> 🎙️ **In class (Oct 20, 28:02–33:24):** The CNOT/CZ here are **controlled by classical values** (double lines). "We use **$b_1$ for sign flipping and $b_2$ for bit flipping**." Apply X (from $b_2$) first, then Z (from $b_1$), both on the **first** (Alice's) qubit — even though it's part of an entangled state.
> ❓ **Why can a local operation on one qubit produce 4 distinguishable states?** The 4 results are the 4 **Bell states**, which are mutually **orthogonal** — perfectly distinguishable by a joint measurement. Alice only touches her half, but because the pair is entangled the *global* state changes.
### Superdense Coding — Receiver
- **Receiver**: we decode sign-flipping and bit-flipping into $b_1,b_2$:
$$\begin{aligned}
|b_1,b_2\rangle &= H^{(2)}_1\,\mathrm{CNOT}^{(2)}_{1,2}\begin{cases}\frac{1}{\sqrt2}(|00\rangle+|11\rangle) & [00]\\ \frac{1}{\sqrt2}(|10\rangle+|01\rangle) & [01]\\ \frac{1}{\sqrt2}(|00\rangle-|11\rangle) & [10]\\ \frac{1}{\sqrt2}(|10\rangle-|01\rangle) & [11]\end{cases}
= H^{(2)}_1\begin{cases}\frac{1}{\sqrt2}(|00\rangle+|10\rangle) & [00]\\ \frac{1}{\sqrt2}(|11\rangle+|01\rangle) & [01]\\ \frac{1}{\sqrt2}(|00\rangle-|10\rangle) & [10]\\ \frac{1}{\sqrt2}(|11\rangle-|01\rangle) & [11]\end{cases} \\
&= H^{(2)}_1\begin{cases}\frac{1}{\sqrt2}(|0\rangle+|1\rangle)\otimes|0\rangle & [00]\\ \frac{1}{\sqrt2}(|1\rangle+|0\rangle)\otimes|1\rangle & [01]\\ \frac{1}{\sqrt2}(|0\rangle-|1\rangle)\otimes|0\rangle & [10]\\ \frac{1}{\sqrt2}(|1\rangle-|0\rangle)\otimes|1\rangle & [11]\end{cases}
= \begin{cases}|0\rangle\otimes|0\rangle & [00]\\ |0\rangle\otimes|1\rangle & [01]\\ |1\rangle\otimes|0\rangle & [10]\\ |1\rangle\otimes|1\rangle & [11]\end{cases}
\end{aligned}$$
- (The [11] case comes out as $-|1\rangle|1\rangle$ — a global phase, measured as 11)
> ❓ **Why CNOT then H?** It is the **Bell circuit run backwards** (Bell state = H then CNOT), so it maps each Bell state back to a computational basis state: CNOT moves the "bit-flip" information into qubit 2, H converts the "sign" ($\pm$) of qubit 1 into a bit.
### Superdense Coding (circuit)
![[ch5-p42.jpg]]
- We can transmit **two classical bits using only one qubit** over physical networks
- Figure 14: Sender: $|\Phi^+_A\rangle$ — ⊕ (classically controlled by $b_2$) — Z (controlled by $b_1$); then Alice's qubit is **sent** to Bob. Receiver: CNOT ($A\to B$), H on $A$, measure $A \to b_1$, measure $B \to b_2$
> 🎙️ **In class (Oct 20, 29:40–36:16):** "You send this qubit … to the receiver. He has $B$ and he got the encoded message. He applies CNOT based on $A$ on $B$, then Hadamard afterwards to decode it back to $b_1$ and $b_2$." "Dense means **double the compression rate**."
### Superdense Coding in PennyLane
```python
dev = qml.device('default.qubit', wires=['A', 'B'])
def prepare_bellstate():
qml.Hadamard(wires='A')
qml.CNOT(wires=['A', 'B'])
@qml.qnode(dev, shots=500)
def superdense_coding(bit1, bit2):
prepare_bellstate()
# Sender
qml.cond(bit2==1, qml.X)(wires='A')
qml.cond(bit1==1, qml.Z)(wires='A')
# Receiver
qml.CNOT(wires=['A', 'B'])
qml.Hadamard(wires='A')
return qml.counts(wires=['A', 'B'])
superdense_coding(1, 0)
# Expected result: {'10': 500}
```
> 🎙️ **In class (Oct 20, 36:31–37:50):** "Do you think I'm going to ask you a question on this? No, that's not me. I would ask further questions like **how could you apply superdense coding and quantum teleportation in one go** … **It will show up for sure. I'm drafting the final paper.** … In the final I'm asking more **reason** questions: give me some application and reasons why you do this. Some math may show up — maybe one question. But **no more state preparation**."
### Exercise 5.4: Superdense Coding
- Answer the following questions:
1. Explain why superdense coding is **tolerant to eavesdropping**, where the transmitted qubit $|\Phi^+_A\rangle$ is intercepted before the receiver receives it.
2. Modify the quantum teleportation [superdense coding] algorithm so that it uses $|\Psi^-\rangle$ as entanglement. Implement a PennyLane code to validate your idea.
3. Since there are three axes in a quantum state, it is tempting to incorporate the **third classical bit (as CY)** into superdense coding. Explain why doing so would **perplex the decryption** process.
4. To enhance security, we can integrate the quantum teleportation algorithm with the superdense coding. Implement with PennyLane the **ultra-secure quantum communication**. On the sender side, a qubit is encrypted as two classical bits and then transmitted via superdense coding. On the receiver side, the received qubit is decrypted into two classical bits and then decoded to the original quantum state. Design this algorithm and implement it with PennyLane to validate your idea.
- Hints (mine):
- Q1: Alice's qubit alone is **maximally mixed** ($\tfrac12 I$) in every one of the 4 encodings → an interceptor learns nothing without Bob's half; and intercepting disturbs it (no-cloning).
- Q3: $Y = iXZ$ — applying Y is the same as applying X and Z (up to phase), so a third bit via CY would map onto one of the **same 4 Bell states** — only $2^2 = 4$ orthogonal states exist for 2 qubits, so 3 bits (8 messages) cannot be decoded uniquely.
- Q4 (the "will show up" question): Alice teleport-encodes her qubit $x$ → gets $(p,q)$; she then **superdense-encodes $(p,q)$** into her half of a **second** Bell pair ($b_1 = p$ → Z, $b_2 = q$ → X) and sends that one qubit; Bob superdense-decodes (CNOT, H, measure) → $(p,q)$, then applies the teleportation fix to his half of the **first** Bell pair (X if $q$, then Z if $p$) → $x$. Needs **2 Bell pairs** and a quantum channel, no classical channel.
> 🎙️ **In class (Oct 20, 36:50–37:05):** see above — combining both protocols "will show up for sure".
---
## 5. Quantum Noisy Theorem
> [!exam] Final Q3 — Quantum Noisy Channel (Ch5 slides 41, 45–46)
> - Know each **channel formula** (bit-flip, sign-flip, bit-sign-flip, Pauli, depolarization, amplitude damping, phase damping), what physical loss it models, which are "most common" and which is the **worst case**, and **why**.
> - Know **fidelity**: definition, the easy pure-state case $|\langle\rho|\sigma\rangle|^2$ (he said "if I ask you this question it's going to be like this"), what it is used for (quality of a quantum computer/register), and the **steps** for the noisy case (he will **not** make you compute Case 2 by hand).
> - See [[Final - Instructor Hints]]
### Density Matrix
- Quantum states constantly interact with the environment, collectively a.k.a. **noise**
- State vector $|\psi\rangle = \sum_{k=1}^N a_k|\psi_k\rangle$, where each $|\psi_k\rangle$ is a pure state, is extended to the **density matrix** to accommodate noise:
$$\boxed{\rho^{(\psi)} = \sum_{k=1}^{N}a_k^2\,|\psi_k\rangle\langle\psi_k|}$$
- **Density matrix** of mixed states is
$$\boxed{\rho = \sum_{j=0}^{2^N-1}\sum_{k=0}^{2^N-1}q_{j,k}\,|\mathbb{B}_N(j)\rangle\langle\mathbb{B}_N(k)| \quad\text{where } \sum_j\sum_k q_{j,k} = 1}$$
- **Example**: the density matrix of $|+\rangle$ is (as on the slide)
$$\rho^{(+)} = \frac12|0\rangle\langle0| + \frac12|1\rangle\langle1| = \frac12\begin{bmatrix}1&0\\0&1\end{bmatrix}$$
- PennyLane: `qml.math.dm_from_state_vector`, `qml.density_matrix(wires=...)`
- **Noise**: interaction with the environment makes the **off-diagonal non-zero**, e.g.
$$\tilde\rho^{(+)} = \begin{bmatrix}0.4 & 0.2\\ 0.1 & 0.3\end{bmatrix}$$
> ⚠️ **Precision note (mine):** The slide's formula keeps only the diagonal $|\psi_k\rangle\langle\psi_k|$ terms. The full outer product of the pure state $|+\rangle$ is $|+\rangle\langle+| = \frac12\begin{bmatrix}1&1\\1&1\end{bmatrix}$ (off-diagonals $= \tfrac12$). $\frac12 I$ is the density matrix of a **50/50 mixture** of $|0\rangle$ and $|1\rangle$ (maximally mixed). If an exam asks "density matrix of a state vector", use $\rho = |\psi\rangle\langle\psi|$ (`qml.math.dm_from_state_vector` does this).
> 🎙️ **In class (Oct 20, 39:37–48:00):** "The density matrix is a mathematical representation of a **noisy** quantum state … in reality all quantum states are represented as a matrix." Compute it as **ket × bra** of each pure state and add them — "it's just another kind of quantum operator". Example done in class: $\frac{1}{\sqrt2}(|00\rangle+|11\rangle)$. With noise there is **"energy leakage"** between entries (e.g. 1 and 0 become 0.8 and 0.2) → off-diagonals become non-zero. Current single-qubit noise is "very, very small, but it happens".
> 💡 **Analogy:** A state vector is a sharp photo; a density matrix is that photo plus the blur from a shaky hand. The blur (noise) shows up in the extra entries.
> [!info] Related
> - Density matrix first appeared under simulation methods: [[Lecture 4 - Computation, Measurement, and Dynamic Circuits]]
> - Outer product (ket-bra): [[Lecture 1 - Complex Algebra#Outer Product and Wave Interaction]]
### Common Types of Quantum Noisy Channel {Exam Q3}
- **Bit-flip channel (X-noise)** — with probability $q$ the qubit is X-flipped:
$$\boxed{\tilde\rho = (1-q)\rho + q\,X\rho X}$$
- **Sign-flip channel (Z-noise)**: **most common**
$$\boxed{\tilde\rho = (1-q)\rho + q\,Z\rho Z}$$
- **Bit-sign-flip channel (Y-noise)**
$$\boxed{\tilde\rho = (1-q)\rho + q\,Y\rho Y}$$
- **Pauli channel** (all three at once):
$$\boxed{\tilde\rho = (1-q_x-q_y-q_z)\rho + q_x X\rho X + q_y Y\rho Y + q_z Z\rho Z}$$
- **Depolarization (information loss)** ⇒ **worst-case scenario!**
$$\boxed{\tilde\rho = (1-q)\rho + \frac{q}{2}I}$$
- **Amplitude damping channel (energy loss)** ⇒ in superconducting and optical qubits
$$\boxed{|0\rangle\mapsto|0\rangle \qquad |1\rangle\mapsto|0\rangle\sqrt{q} + |1\rangle\sqrt{1-q}}$$
- **Phase damping channel (coherence loss)** ⇒ **also most common**
$$\boxed{\tilde\rho = \begin{bmatrix}\rho_{11} & (1-q)\rho_{12}\\ (1-q)\rho_{21} & \rho_{22}\end{bmatrix}}$$
#### What each channel does to $\rho = \begin{bmatrix}\rho_{11}&\rho_{12}\\ \rho_{21}&\rho_{22}\end{bmatrix}$ (worked by me, the same "do the math" he showed)
- $X\rho X = \begin{bmatrix}\rho_{22}&\rho_{21}\\ \rho_{12}&\rho_{11}\end{bmatrix}$ → bit-flip **swaps** populations and coherences with prob. $q$
- $Z\rho Z = \begin{bmatrix}\rho_{11}&-\rho_{12}\\ -\rho_{21}&\rho_{22}\end{bmatrix}$ → sign-flip keeps the diagonal; off-diagonals shrink to $(1-2q)\rho_{12}$
- $Y\rho Y = \begin{bmatrix}\rho_{22}&-\rho_{21}\\ -\rho_{12}&\rho_{11}\end{bmatrix}$ → both swap and sign
- Depolarization: everything is pulled toward $\tfrac12 I$ (the maximally mixed state)
| Channel | Formula | Physical meaning | Note on slide / in class |
|---|---|---|---|
| Bit-flip (X) | $(1-q)\rho + qX\rho X$ | $\lvert 0\rangle\leftrightarrow\lvert 1\rangle$ | in class: seen on IBM systems |
| Sign-flip (Z) | $(1-q)\rho + qZ\rho Z$ | phase of $\lvert 1\rangle$ flips | **most common** |
| Bit-sign-flip (Y) | $(1-q)\rho + qY\rho Y$ | both at once | |
| Pauli | mix of X, Y, Z | general flip noise | 3 parameters $q_x,q_y,q_z$ |
| Depolarization | $(1-q)\rho + \frac q2 I$ | information loss | **worst case** |
| Amplitude damping | $\lvert 1\rangle\to\sqrt q\lvert 0\rangle+\sqrt{1-q}\lvert 1\rangle$ | energy loss ($\lvert 1\rangle$ decays to $\lvert 0\rangle$) | superconducting & optical qubits |
| Phase damping | off-diagonals $\times(1-q)$ | coherence loss | **also most common** |
> 🎙️ **In class (Oct 20, 48:00–56:26):** He derived $X\rho X$ "for you" — "1−q probability of retaining the good old density matrix and q probability of flipping". Sign flip: $a|0\rangle+b|1\rangle \to a|0\rangle-b|1\rangle$. All of these are caused by **unexpected interaction with the environment** that affects the rotation of a particle. Pauli channel = all three combined ($q_x$ bit, $q_z$ sign, $q_y$ "phase"). "All of these four types **can be managed** — you can recover the qubits out of the noisy state. But the ones on the right-hand side are the **worst-case scenario** … **Depolarization** … when that happens, **make a prayer to your god**." Amplitude **dampening** = "lower": $|0\rangle$ preserved, $|1\rangle$ becomes a superposition of $|0\rangle$ and $|1\rangle$ with probability $q$ (he said common in **trapped-ion** machines; the slide says superconducting & optical). Phase dampening acts on the **off-diagonal** elements, "also very common". "These are **seven types of noise** — they show up in all modern quantum computers. The noise is very, very small, but it shows up."
> ❓ **Why is depolarization the worst case?** It replaces the state (with prob. $q$) by $\tfrac12 I$ — the **maximally mixed state**, equally far from every direction on the Bloch sphere. All three axes are damaged at once and nothing about the original is left in that part → information is **lost**, not merely rotated. A bit/sign flip is a *specific* reversible Pauli error that a code can detect and undo.
> ❓ **Why are sign-flip / phase damping the most common?** Phase (the off-diagonal "coherence") is the most fragile part of a qubit — any tiny random energy fluctuation from the environment shifts the relative phase, while flipping $|0\rangle\leftrightarrow|1\rangle$ needs an energy exchange. So **coherence loss** happens much faster than population changes.
> ❓ **Why does noise show up in the off-diagonal entries?** Diagonal = probabilities of $|0\rangle,|1\rangle$; off-diagonal = **coherence** (the quantum "superposition-ness"). Losing coherence turns a superposition into a classical coin flip → off-diagonals shrink (phase damping) or get mixed.
> 💡 **Analogy:** A Bloch-sphere arrow on a bumpy tuk-tuk ride: bit-flip = it gets knocked upside-down; sign-flip = it gets spun around the vertical axis; amplitude damping = it slowly droops toward the north pole (losing energy); depolarization = the arrow **shrinks toward the centre** — the message fades away entirely.
#### Example (Ex 5.5 Q10, worked by me): sign-flip on $|-\rangle$ with $q = 0.25$
- $\rho^{(-)} = |-\rangle\langle-| = \frac12\begin{bmatrix}1&-1\\-1&1\end{bmatrix}$
- $Z\rho^{(-)}Z = \frac12\begin{bmatrix}1&1\\1&1\end{bmatrix} = \rho^{(+)}$
- $\tilde\rho^{(-)} = 0.75\cdot\frac12\begin{bmatrix}1&-1\\-1&1\end{bmatrix} + 0.25\cdot\frac12\begin{bmatrix}1&1\\1&1\end{bmatrix} = \begin{bmatrix}0.5&-0.25\\-0.25&0.5\end{bmatrix}$
- Purity: $\tilde\rho^2 = \begin{bmatrix}0.3125&-0.25\\-0.25&0.3125\end{bmatrix}$ → $\gamma = \operatorname{tr}(\tilde\rho^2) = 0.625 < 1$ (noisy)
### Purity
- Properties of density matrix $\rho$:
- It is **Hermitian**: $\rho = \rho^*$
- It has a **unit trace**: $\operatorname{tr}(\rho) = 1$
- It is **positive-semidefinite**: for all quantum states $|\psi\rangle$, we have $\langle\psi|\rho|\psi\rangle \geq 0$
- **Purity** of density matrix:
$$\boxed{\gamma(\rho) = \operatorname{tr}(\rho^2)}$$
- PennyLane: `qml.math.purity`
- If a quantum state $|\psi\rangle$ is pure, we then have $\gamma(\rho^{(\psi)}) = 1$; otherwise $\gamma(\rho^{(\psi)}) < 1$
$$\gamma(\rho^{(0)}) = \operatorname{tr}(|0\rangle\langle0|0\rangle\langle0|) = 1 \qquad \gamma(\rho^{(1)}) = \operatorname{tr}(|1\rangle\langle1|1\rangle\langle1|) = 1$$
- **Maximally mixed state** is a quantum state with maximal noise:
$$\rho_{\max} = \sum_{j=0}^{2^N-1}\sum_{k=0}^{2^N-1}\frac{1}{2^N}|\mathbb{B}_N(j)\rangle\langle\mathbb{B}_N(k)|$$
- (standard form: $\rho_{\max} = \frac{1}{2^N}I$, purity $= \frac{1}{2^N}$ — e.g. $\tfrac12$ for one qubit)
- **Expectation**: expectation measured with a projector $U = \sum_{k=1}^{N}\lambda_k|k\rangle\langle k|$ is
$$\boxed{\langle U^{(\rho)}\rangle = \sum_{k=1}^{N}\lambda_k q_{k,k} = \sum_{k=1}^{N}\lambda_k\operatorname{tr}(\rho|k\rangle\langle k|) = \operatorname{tr}(\rho U)}$$
> 🎙️ **In class (Oct 20, 56:34–59:54):** Purity = "multiply the density matrix against itself and compute the **trace** (sum of diagonal elements)". 1 → pure, no noise; between 0 and 1 → noisy, "still usable".
> 🎙️ **In class (Oct 20 part 2, 0:00–1:36):** Expectation from a density matrix: "multiply the density matrix and the gate X, Y or Z and compute the trace — that's it … **I gave you a hint about the mathematical question. You know what to do now.**" (→ this is the Final Q1 skill, see [[Lecture 4 - Computation, Measurement, and Dynamic Circuits]])
> ❓ **Why does $\operatorname{tr}(\rho^2)$ measure purity?** For a pure state $\rho = |\psi\rangle\langle\psi|$, $\rho^2 = \rho$ (it's a projector) so the trace stays 1. Mixing spreads the probability over several states; squaring the weights (each $<1$) makes the sum smaller than 1.
### Partial Trace (Skipped)
> 📝 **Instructor's annotation:** He wrote **"(Skip)"** next to the title. In the examples he **crossed out** the qubit being traced out in each ket (e.g. $|\cancel{0}0\rangle$).
- **Partial trace** of a multi-qubit density matrix:
$$\operatorname{tr}_j(\rho) = \sum_{k\in\{0,1\}}\big(U_{j,k,N}\big)^*\rho\big(U_{j,k,N}\big) \quad\text{where } U_{j,k,N} = I_{j-1}\otimes|k\rangle\otimes I_{N-j}$$
- **Reduced density matrix**: $\operatorname{tr}_j(\rho)$ **removes the $j$-th qubit** from $\rho$ and sums the remaining reduced states
- This concept imitates **quantum state transmission via a quantum channel**
- **Example**: our density matrix is $\rho = \frac12|00\rangle + \frac14|01\rangle + \frac16|10\rangle + \frac18|11\rangle$ (shorthand for the diagonal weights)
- Reduced density matrices of $\rho$:
$$\operatorname{tr}_1(\rho) = \frac12|\cancel{0}0\rangle + \frac14|\cancel{0}1\rangle + \frac16|\cancel{1}0\rangle + \frac18|\cancel{1}1\rangle = \left(\frac12+\frac16\right)|0\rangle + \left(\frac14+\frac18\right)|1\rangle$$
$$\operatorname{tr}_2(\rho) = \frac12|0\cancel{0}\rangle + \frac14|0\cancel{1}\rangle + \frac16|1\cancel{0}\rangle + \frac18|1\cancel{1}\rangle = \left(\frac12+\frac14\right)|0\rangle + \left(\frac16+\frac18\right)|1\rangle$$
> 🎙️ **In class (Oct 20 part 2, 1:43–2:14):** "I spill the beans all the time … **this is skip. Purification skip.** It's more like a computational part; you need a computer."
### Purification (Skipped)
> 📝 **Instructor's annotation:** **"(Skip)"** written next to the title.
- **Purification** of quantum state $|\psi\rangle$ can be done by finding partial traces:
$$|\psi_1\rangle = \operatorname{tr}_2(|\psi\rangle\langle\psi|) \qquad |\psi_2\rangle = \operatorname{tr}_1(|\psi\rangle\langle\psi|)$$
- If a density matrix $\rho$ has $K$ eigen-pairs $(\lambda_k, |e_k\rangle)$, i.e. $\rho = \sum_{k=1}^K\lambda_k|e_k\rangle\langle e_k|$, we can purify $\rho$ into each pure state $|i_k\rangle$:
$$|\psi\rangle = \sum_{k=1}^{K}\sqrt{\lambda_k}\big(|e_k\rangle\otimes|i_k\rangle\big)$$
- **Example**: a noisy density matrix $\rho = \begin{bmatrix}0.4&0.2\\0.1&0.3\end{bmatrix}$
- Decompose $\rho$ into two eigen-pairs: $\left(0.5, \frac{1}{\sqrt5}[2\ 1]^\top\right)$ and $\left(0.2, \frac{1}{\sqrt2}[1\ {-1}]^\top\right)$
- Purify $\rho$ into pure states $|0\rangle$ and $|1\rangle$:
$$|\psi\rangle = \sqrt{0.5}\times\left(\frac{1}{\sqrt5}[2\ 1]^\top\otimes|0\rangle\right) + \sqrt{0.2}\times\left(\frac{1}{\sqrt2}[1\ {-1}]^\top\otimes|1\rangle\right)$$
> 💡 **Analogy:** Purification = explaining a blurry photo as a sharp photo of a *bigger* scene of which you only see part.
### Fidelity {Exam Q3}
- **Fidelity**: **similarity** between two density matrices $\rho$ and $\sigma$
$$\boxed{F(\rho,\sigma) = \left(\operatorname{tr}\sqrt{\sqrt\rho\,\sigma\sqrt\rho}\right)^2}$$
- **Case 1**: both $\rho$ and $\sigma$ are **pure** states
$$\boxed{F(\rho,\sigma) = |\langle\rho|\sigma\rangle|^2}$$
- **Case 2**: $\rho$ and/or $\sigma$ are **noisy**
1. Diagonalize $\rho$ into $(V, D)$
2. Compute $\sqrt\rho = VD^{1/2}V^*$
3. Compute $M = \sqrt\rho\,\sigma\sqrt\rho$
4. Diagonalize $M$ into $(W,\Lambda)$
5. Compute $\sqrt M = W\Lambda^{1/2}W^*$
6. Compute $F(\rho,\sigma) = \operatorname{tr}\sqrt M$ (then square, per the boxed definition)
- **Diagonalization**:
1. Decompose $\rho$ into $K$ eigen-pairs $(\lambda_k, v_k)$
2. Construct singular [eigenvector] matrix $V = [v_1|\dots|v_K]$ and diagonal matrix $D = \operatorname{diag}(\lambda_1,\dots,\lambda_K)$
3. Return $(V, D)$
#### Example: $F(|+\rangle, |0\rangle)$
$$\begin{aligned}
F(|+\rangle,|0\rangle) &= |\langle+|0\rangle|^2 = \left|\left(\frac{1}{\sqrt2}\langle0| + \frac{1}{\sqrt2}\langle1|\right)|0\rangle\right|^2 \\
&= \left|\frac{1}{\sqrt2}\langle0|0\rangle + \frac{1}{\sqrt2}\langle1|0\rangle\right|^2 = \left|\frac{1}{\sqrt2} + 0\right|^2 = \frac12
\end{aligned}$$
> 🎙️ **In class (Oct 20 part 2, 2:20–5:46):** "Fidelity is a synonym for **quality** … but in quantum computing fidelity means **similarity**." For two noiseless states: compute the bra–ket product, take the absolute value and square. $\langle0|0\rangle = 1$, $\langle1|0\rangle = 0$ (perpendicular). "**If I want to ask you this question, it's going to be like this. Easy, right?**"
> 🎙️ **In class (Oct 20 part 2, 5:54–8:48):** Case 2 (noisy): square root of a matrix → diagonalize, square-root the diagonal, multiply back, … "Oh my god, that's hectic. **We're not going to do the case two** … **I'm not going to do this to you.**"
> 🎙️ **In class (Oct 20 part 2, 8:48–12:12):** **Why fidelity?** "Fidelity is how we **measure the quality of a quantum computer**." Put $|+\rangle$ into a quantum register $R$, read it out (a possibly noisy density matrix), and measure the fidelity between what you **want** and what you **get**. Today ≈ **99.5%** — sounds great, but for reliable computation you need an error rate of about **$10^{-10}$**. → this is why we need **error correction**.
> ❓ **Why is the pure-state formula $|\langle\rho|\sigma\rangle|^2$?** The inner product measures how much the two state vectors **overlap** (cosine of the angle between them); squaring its magnitude gives a probability — the chance that $\sigma$ would "pass" as $\rho$ in a measurement. Same state → 1, orthogonal → 0, $|+\rangle$ vs $|0\rangle$ (45° apart in Hilbert space) → ½.
> 💡 **Analogy:** Fidelity is like a "similarity score" between the song you sent on LINE and the compressed one your friend hears — 100% means identical, 0% means a totally different song.
### Fidelity (noisy example) {Exam Q3}
- **Example**: for noisy states
$$\rho = \begin{bmatrix}0.4&0.2\\0.1&0.3\end{bmatrix} \qquad \sigma = \begin{bmatrix}1/\sqrt2&0\\0&1/\sqrt2\end{bmatrix}$$
- Let's compute the fidelity $F(\rho,\sigma)$:
- We diagonalize $\rho$ into
$$V = \begin{bmatrix}2/\sqrt5 & 1/\sqrt2\\ 1/\sqrt5 & -1/\sqrt2\end{bmatrix} \qquad D = \begin{bmatrix}0.5&0\\0&0.2\end{bmatrix}$$
- We compute $\sqrt\rho = VD^{1/2}V^*$:
$$\sqrt\rho = \begin{bmatrix}0.79&0.06\\0.06&0.37\end{bmatrix}$$
- We compute $M = \sqrt\rho\,\sigma\sqrt\rho$:
$$M = \begin{bmatrix}0.44&0.05\\0.05&0.10\end{bmatrix}$$
- We diagonalize $M$ into
$$W = \begin{bmatrix}0.99&-0.14\\0.14&0.99\end{bmatrix} \qquad \Lambda = \begin{bmatrix}0.45&0\\0&0.09\end{bmatrix}$$
- We compute $\sqrt M = W\Lambda^{1/2}W^*$:
$$\sqrt M = \begin{bmatrix}0.66&0.05\\0.05&0.31\end{bmatrix}$$
- Fidelity $F(\rho,\sigma) = \operatorname{tr}\sqrt M = 0.66 + 0.31 = 0.97$ (slide stops at $\operatorname{tr}\sqrt M$; with the boxed squared definition, $0.97^2 \approx 0.94$)
> 🎙️ **In class (Oct 20 part 2, 7:51–8:48):** He showed these steps only to explain the procedure; "typically we need a computer for that." Know the **steps and why** (you need $\sqrt\rho$, and a matrix square root requires diagonalization), not the arithmetic.
> ❓ **Why diagonalize to take $\sqrt\rho$?** A matrix square root is only easy for a diagonal matrix (square-root each eigenvalue). $\rho = VDV^*$ ⇒ $\sqrt\rho = V\sqrt D V^*$, because $(V\sqrt DV^*)^2 = V D V^* = \rho$.
> [!info] Related
> - Eigen-decomposition: [[Lecture 1 - Complex Algebra#Eigen-Decomposition]]
> - Fidelity as a loss function in QML (Ex 5.5 Q14): [[Lecture 8 - Quantum Artificial Intelligence]]
### Fidelity of Density Matrices in PennyLane
```python
import pennylane as qml
from pennylane import numpy as np
from pennylane import math as qmath # We need this qmath, too
# Depolarization channel
def noisy_dm(mat, p_noise):
noise = p_noise / 2 * np.eye(2)
new_mat = (1 - p_noise) * mat + noise
return new_mat
def main():
# Convert a state vector into a density matrix (dm)
mat = qmath.dm_from_state_vector([0.9, 0.1])
mat = mat / qmath.norm(mat)
for p_noise in range(10):
noisy_mat = noisy_dm(mat, p_noise * 0.1) # Noisy matrix with noise parameter
f = qmath.fidelity(mat, noisy_mat) # Fidelity of quantum states
print(f'p_noise = {p_noise * 0.1:4.2f}: fidelity = {f:6.4f}')
main()
```
- `noisy_dm` implements **depolarization** $(1-q)\rho + \frac q2 I$; fidelity drops as $q$ grows
### Fidelity of State Vectors in PennyLane
```python
import pennylane as qml
from pennylane import numpy as np
from pennylane import math as qmath
# Phase damping channel
def noisy_vec(vec, p_noise):
noise = np.array([ [0.0, 1 - p_noise],
[1 - p_noise, 0.0] ])
new_vec = noise @ vec
return new_vec
def main():
vec = np.array([1.0, 1.0])
vec = vec / qmath.norm(vec)
for p_noise in range(10):
new_vec = noisy_vec(vec, p_noise * 0.1) # Noisy vector with noise parameter
f = qmath.fidelity_statevector(vec, new_vec) # Fidelity of quantum states
print(f'p_noise = {p_noise * 0.1:4.2f}: fidelity = {f:6.4f}')
main()
```
### Exercise 5.5: Quantum Noisy Theorem
- Convert the following state vectors into equivalent density matrices. Then separately compute their X-, Z-, and Y-noises with the parameter $q = 0.25$:
1. $|0\rangle$ 2. $|1\rangle$ 3. $|+\rangle$ 4. $|-\rangle$ 5. $|\Psi^+\rangle$ 6. $|\Phi^-\rangle$ 7. $|{+}{+}\rangle$ 8. $|{+}{-}\rangle$ 9. $|{+}{-}{+}\rangle$
- Implement PennyLane code.
10. Let $\rho^{(-)}$ be the density matrix of $|-\rangle$. Let $\tilde\rho^{(-)}$ be the sign-flip channel of $\rho^{(-)}$ with $q = 0.25$. Then compute the purity $\gamma(\tilde\rho^{(-)})$. (worked above: **0.625**)
11. Regarding the previous question, purify $\tilde\rho^{(-)}$. [Hint: we can decompose a matrix with `numpy.linalg.eig`.]
- Answer the following questions:
12. Explain why a **noisy density matrix cannot be converted to an equivalent state vector**.
13. For any two pure states $|\rho\rangle$ and $|\sigma\rangle$, where $|\sigma\rangle = |\rho\rangle\operatorname{cis}\alpha$ and $\langle\rho|\sigma\rangle = \cos\theta$, show that their fidelity is $F(\rho,\sigma) = \cos^2\theta$
14. Regarding the previous question, discuss how the fidelity could be used as a **loss function** in machine learning.
- Hint Q12 (mine): a state vector can only describe a **pure** state (purity 1). A noisy $\rho$ has purity $<1$ — it is a probabilistic **mixture**, which no single vector reproduces.
- Hint Q14 (mine): $1 - F(\text{target},\text{output})$ is 0 when the circuit's output equals the target and grows as they differ — a differentiable loss to minimize over circuit parameters $\theta$.
---
## 6. Quantum Error Correction
### Noisy Intermediate-Scale Quantum (NISQ) Computers
- In current technology (mid-2020s), quantum computers are **not fully noise-tolerant** due to noisy hardware implementation of quantum gates
- **Quantum decoherence** hinders the reliability of multi-qubit computers, where more qubits may **exponentially** introduce noise to the quantum gates
- State-of-the-art milestone is the **noisy intermediate-scale quantum (NISQ)** computers equipped with efficient quantum error correction
- Current NISQ devices contain **50–1,000 physical qubits** and they are noise-intolerant because of noisy hardware implementation and quantum decoherence
- **Gate fidelities**: one-qubit operations **99.5%**, two-qubit operations **95–99%**, while the error rate is **0.1%/gate**
- For a quantum algorithm, we prefer **fewer quantum gates** due to this exponential increase of noise from the per-gate error rate
> 🎙️ **In class (Oct 20 part 2, 14:56–20:25):** **Logical qubits** = the qubits your algorithm uses; **physical qubits** = the hardware ones. Each logical qubit is supported by many physical qubits for error correction. Example: breaking **RSA** needs ≥ **1,400 logical qubits ≈ 280,000 physical qubits** → about **200 physical per logical** (2026); "in the future we may need just 10" with better fidelity. Early classical computers (1940s) were also noisy and needed redundant bits — "it's happening again in quantum computers." Fidelity is measured **per gate**.
> 🎙️ **In class (Oct 20 part 3, 22:01–24:25):** Why 200 physical per logical? The codes here only fix bit-flip and sign-flip; there are **five more noise types** (e.g. decoherence, "the most severe of all"), so many more qubits are needed for "enough" fidelity. When a vendor says "1,000 qubits" they mean **logical** — "behind the scene there are millions of physical qubits."
> ❓ **Why prefer fewer gates?** Each gate has ~0.1% error and errors compound: success ≈ $(1-0.001)^{G}$, which decays exponentially with the gate count $G$.
### Classical Error Correction (Encoding)
- We use a **redundant correcting code** to detect errors:
$$\boxed{\text{Encode: } y = \big[I_N\,|\,A^\top\big]^\top x \qquad \text{Decode: } p = \big[A\,|\,I_L\big]\hat y}$$
- where $x$ is a binary-string block of length $N$ and $[A]_{L\times N}$ is a **parity-check matrix**
- **Hamming code**: for a 4-bit block $b_1b_2b_3b_4$, there are **3 parity check bits** $p_1p_2p_3$:
$$\boxed{A = \begin{bmatrix}1&1&0&1\\1&0&1&1\\0&1&1&1\end{bmatrix}_{3\times4}}$$
- This is called the **Hamming(7,4)** code (7 bits total, 4 data bits)
- i.e. $p_1 = b_1\oplus b_2\oplus b_4$, $p_2 = b_1\oplus b_3\oplus b_4$, $p_3 = b_2\oplus b_3\oplus b_4$
#### Example: encode `1011`
$$\big[I_4\,|\,A^\top\big]^\top\big[1\ 0\ 1\ 1\big]^\top = \begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&1\\1&1&0&1\\1&0&1&1\\0&1&1&1\end{bmatrix}\times\begin{bmatrix}1\\0\\1\\1\end{bmatrix} = \big[1\ 0\ 1\ 1\,|\,0\ 1\ 0\big]^\top$$
- The parity-check bits are **010**
- Check: $p_1 = 1\oplus0\oplus1 = 0$, $p_2 = 1\oplus1\oplus1 = 1$, $p_3 = 0\oplus1\oplus1 = 0$ ✓
> 🎙️ **In class (Oct 20 part 2, 20:57–30:07):** Recap from 2nd-year computer architecture. **Parity = even number**: each circle must contain an even number of 1s; add a 1 to $p$ if the count is odd. Mnemonic: "**$b_1b_2b_4$ → $p_1$; $b_1b_3b_4$ → $p_2$; $b_2b_3b_4$ → $p_3$** — each row is one parity bit." Problem: the parity bits themselves can be corrupted — that is why the Hamming code (matrix form) was invented.
> 🎙️ **In class (Oct 20 part 2, 34:26–35:08):** A student asked if it is normal matrix multiplication: "It's **binary matrix multiplication** — $1+1 = 0$, i.e. **XOR**. People back home, listen to me."
### Classical Error Correction (Decoding / Syndrome)
![[ch5-p59.jpg]]
- **Decoding**: via a noisy channel, we receive a **corrupted** block `1010010` (the 4th bit is underlined as the corrupted one)
$$\big[A\,|\,I_3\big]\hat y = \begin{bmatrix}1&1&0&1&1&0&0\\1&0&1&1&0&1&0\\0&1&1&1&0&0&1\end{bmatrix}\times\begin{bmatrix}1\\0\\1\\0\\0\\1\\0\end{bmatrix} = \big[1\ 1\ 1\big]^\top \triangleq \big[p_1\ p_2\ p_3\big]^\top$$
- **Syndrome**: regarding $p_1p_2p_3$, we consult the Hamming(7,4) **parity-check diagram**
- With $p_1p_2p_3 = 111$, we identify the corrupted position by the **intersection of three circles** $p_1,p_2,p_3$, resulting in **$b_4$**
- Figure 15 (Venn diagram): three circles $p_1$ (top), $p_2$ (bottom-left), $p_3$ (bottom-right); $b_1 = p_1\cap p_2$, $b_2 = p_1\cap p_3$, $b_3 = p_2\cap p_3$, $b_4$ = centre (all three)
$$\boxed{\text{syndrome} = [A\,|\,I_3]\,\hat y \ (\bmod 2); \quad 000 = \text{no error}}$$
| Syndrome $p_1p_2p_3$ | Corrupted bit | Reason |
|---|---|---|
| 000 | none | all circles even |
| 111 | $b_4$ | in all three circles |
| 110 | $b_1$ | in $p_1,p_2$ only |
| 101 | $b_2$ | in $p_1,p_3$ only |
| 011 | $b_3$ | in $p_2,p_3$ only |
| 100 / 010 / 001 | $p_1$ / $p_2$ / $p_3$ | the parity bit itself |
> 🎙️ **In class (Oct 20 part 2, 30:34–36:32):** Drew the circles for `1010|010`: all three circles have an odd number of 1s → "intersect all of them → **$b_4$ is corrupted**." For a syndrome like 110: intersect the 1-circles and **subtract** the 0-circle → $b_1$. "**1 = inclusion, 0 = exclusion.**"
> 🎙️ **In class (Oct 20 part 2, 39:41–41:33):** The vector $p$ is called the **syndrome**. **Limitation: the Hamming code detects/corrects just one erroneous bit (out of 7). With two erroneous bits it just fails.**
> 💡 **Analogy:** Three overlapping security cameras. If cameras 1 and 2 both report "something odd" but camera 3 doesn't, the culprit must be standing where only cameras 1 and 2 overlap.
### Quantum Error Correction
![[ch5-p60.jpg]]
- **Quantum error correction (QEC)** is a technique for protecting quantum information from **decoherence and noise**
- Sources of noise: faulty state storage, faulty gates, faulty state preparation, and faulty measurement ⇒ **low fidelity**
- **Syndrome**: types of quantum error
- **Random bit-flipping** via $R_X$, e.g. $a|0\rangle + b|1\rangle \Rightarrow b|0\rangle + a|1\rangle$
- **Random sign-flipping** via $R_Z$, e.g. $a|0\rangle + b|1\rangle \Rightarrow a|0\rangle - b|1\rangle$
- Both types may happen at the same time
- **Bit-flipping noise** with probability $q$: $\mathbf{E}_{\text{bit}}\rho = (1-q)\rho + qX\rho X$
- **Sign-flipping noise** with probability $q$: $\mathbf{E}_{\text{sign}}\rho = (1-q)\rho + qZ\rho Z$
- Figure 16 (Implementation of noisy channels): a "noise" qubit $\sqrt{1-p}|0\rangle + \sqrt p|1\rangle$ controls a **CNOT** on $|\psi\rangle$ → $\mathbf{E}_{\text{bit}}|\psi\rangle$; or controls a **CZ** → $\mathbf{E}_{\text{sign}}|\psi\rangle$
> 🎙️ **In class (Oct 20 part 2, 41:38–45:24):** "We have seven types of noise, but let's focus on only two: **bit flip and sign flip**." The noise is modelled as CNOT / CZ whose **control is uncertain** — a superposition that fires with probability $p$. "Up here is not a real qubit; it's **noise** — an uncontrollable variable, not in the system, just there in the channel."
### Correction for Bit-Flipping
![[ch5-p61.jpg]]
- We can apply the **3-repetition code** for bit-flipping correction:
$$\begin{aligned}
&\mathrm{CNOT}^{(3)}_{1,3}\,\mathrm{CNOT}^{(3)}_{1,2}\big(a|0\rangle+b|1\rangle\big)\otimes|00\rangle \\
&= \mathrm{CNOT}^{(3)}_{1,3}\big(a|00\rangle+b|11\rangle\big)\otimes|0\rangle \\
&= a|000\rangle + b|111\rangle
\end{aligned}$$
$$\boxed{a|0\rangle+b|1\rangle \xrightarrow{\text{encode}} a|000\rangle + b|111\rangle}$$
- When receiving a block $|b_1,b_2,b_3\rangle$ via a noisy channel, we determine the **syndrome** by
$$\boxed{|s_1,s_2\rangle = |b_1\oplus b_2,\ b_1\oplus b_3\rangle}$$
- where each bit determines if $b_1 = b_2$ and $b_1 = b_3$, respectively
- Table 2: Correction table for bit-flipping
| Syndromes | Corrections |
|---|---|
| 00 | No correction |
| 01 | Bit-flip $b_3$ |
| 10 | Bit-flip $b_2$ |
| 11 | Bit-flip $b_1$ |
- Figure 17 (Encoder for bit-flipping): CNOT from qubit 1 to qubit 2, CNOT from qubit 1 to qubit 3
> 🎙️ **In class (Oct 20 part 2, 46:06–51:05):** "To correct the bit flipping we add **two more qubits** — now you know why physical qubits are much more than logical qubits." **Repetition code**: all three must be the same; "you use CNOT to do repetition." Syndrome: XOR = CNOT again; if all equal, both XORs are 0 → no correction; "$b_1\oplus b_2 = 0$ but $b_1\oplus b_3 = 1$ → $b_3$ is erroneous."
> ❓ **Why not just copy the qubit 3 times ($|\psi\rangle|\psi\rangle|\psi\rangle$)?** No-cloning forbids it. $a|000\rangle + b|111\rangle$ is **not** three copies — it is one entangled state that spreads the *bit value* (not the amplitudes) across three qubits.
> ❓ **Why measure parities instead of the qubits?** The syndrome $b_1\oplus b_2,\ b_1\oplus b_3$ reveals **where** the error is without revealing **$a,b$** — so the superposition survives (measuring a qubit directly would collapse it).
### Correction for Bit-Flipping (decoder)
![[ch5-p62.jpg]]
- We can address the correction table by the **decoder circuit** below
- Figure 18 (Encoder-decoder for bit-flipping correction): Encoder (2 CNOTs) → $\mathbf{E}_{\text{bit}}$ → Decoder: CNOT $1\to2$, CNOT $1\to3$, then **Toffoli** with controls 2,3 and target 1 → $|\psi\rangle$ on qubit 1
> 🎙️ **In class (Oct 20 part 2, 53:12–57:10):** Decoder: CNOT on qubit 2, CNOT on qubit 3, then **Toffoli flips qubit 1** (if both syndromes are 1). "This is **majority vote**": e.g. 0,0,1 → the zeros win → original was 0. "**I was about to ask this question in the final, but you spilled the beans** … I'm going to add some more details into the questions." (A student caught a mix-up of $b_1/b_2$ in his handwritten table: "Oh, it's the middle one, I'm sorry" → the printed table is the one to trust; "just draw the picture.")
> 🎙️ **In class (Oct 20 part 2, 54:15–54:33):** "Do you think I'm going to ask you to **write down this diagram**? Nah. You know me."
> 🎙️ **In class (Oct 20 part 3, 1:14–4:06):** Q: what if two qubits are wrong? "It can detect **one erroneous qubit at a time** … with more than one erroneous qubit there's a chance of errors." After decoding we **only use the first qubit**; the extra qubits are just for the majority vote.
> 💡 **Analogy:** Asking three friends what time the movie starts; if one says 8 and two say 7, you go with 7.
### Correction for Sign-Flipping
![[ch5-p63.jpg]]
- **Sign-flipping is equivalent to bit-flipping when applied with the Hadamard gate**:
$$H\big(a|+\rangle + b|-\rangle\big) \Rightarrow H\big(a|-\rangle + b|+\rangle\big)$$
$$\frac{a+b}{\sqrt2}|0\rangle + \frac{a-b}{\sqrt2}|1\rangle \Rightarrow \frac{a+b}{\sqrt2}|0\rangle - \frac{a-b}{\sqrt2}|1\rangle$$
- Figure 19 (Encoder for sign-flipping): the bit-flip encoder followed by **H on all three qubits** → $a|{+}{+}{+}\rangle + b|{-}{-}{-}\rangle$
- When receiving a block $|b_1,b_2,b_3\rangle$ via a noisy channel, we determine the syndrome by $|s_1,s_2\rangle = |b_1\oplus b_2, b_1\oplus b_3\rangle$
- Table 3: Correction table for sign-flipping
| Syndromes | Corrections |
|---|---|
| $++$ | No correction |
| $+-$ | Sign-flip $b_3$ |
| $-+$ | Sign-flip $b_2$ |
| $--$ | Sign-flip $b_1$ |
$$\boxed{a|0\rangle+b|1\rangle \xrightarrow{\text{encode}} a|{+}{+}{+}\rangle + b|{-}{-}{-}\rangle}$$
> 🎙️ **In class (Oct 20 part 2, 57:47–59:45; part 3, 0:01–1:12):** "Apply the Hadamard after the repetition code: $H|0\rangle = |+\rangle$, $H|1\rangle = |-\rangle$ … it's still majority vote." In the sign basis a Z error looks like an X error ($Z|+\rangle = |-\rangle$). He apologised for **mistakes on this slide** ("the slides were finished a year before the book; I corrected the mistakes in the book") — e.g. Figure 20's caption says "bit-flipping" but it is the **sign-flipping** circuit.
> ❓ **Why does Hadamard turn sign-flip into bit-flip?** $HZH = X$. A Z error between two H's acts like an X error, which the repetition code already corrects.
### Correction for Sign-Flipping (decoder)
![[ch5-p64.jpg]]
- We can address the correction table by the decoder circuit below
- Figure 20 (Encoder-decoder for **sign**-flipping correction): Encoder = CNOT $1\to2$, CNOT $1\to3$, H on all 3 → $\mathbf{E}_{\text{sign}}$ → Decoder = H on all 3, CNOT $1\to2$, CNOT $1\to3$, Toffoli (controls 2,3 → target 1) → $|\psi\rangle$
- "If you add the Hadamard here, you add the Hadamard there as well. That's the only difference. The mechanism is still the same — majority vote."
### Shor Code
![[ch5-p65.jpg]]
- **Shor code** is a **9-repetitive code** that **interleaves bit-flipping and sign-flipping** to accommodate any arbitrary error
$$\boxed{E = I + c_xX + c_yY + c_zZ}$$
- Figure 21: 9 qubits ($|\psi\rangle$ + eight $|0\rangle$); Encoder: sign-flip level (CNOT from qubit 1 to 4 and 7, H on 1, 4, 7) combined with three bit-flip blocks (each of 1, 4, 7 copies to its two neighbours); → $E$ → Decoder: the reverse (block decoders with Toffolis, H on 1, 4, 7, then the outer majority vote with a Toffoli on qubit 1) → $|\psi\rangle$
> 🎙️ **In class (Oct 20 part 3, 4:16–6:23):** "Shor code is an extension of repetition codes: we combine bit flipping and sign flipping into one circuit. We need **eight extra qubits**, so **nine in total** — we use only the first one. Previously 3 qubits for bit flipping and 3 for sign flipping; combine them: **3 × 3 = 9**." Easy to remember: the **inner grouping = bit flipping**, the **outside = sign flipping**; decoding is the reverse (sign first, then bit).
> 🎙️ **In class (Oct 20 part 3, 6:49–8:01):** (fun) The circuit "looks like me in high school … a **bad boy** — one side tucked in, one side left out … juvenile delinquent." "Not related to quantum bit flipping at all — it's just me flipping."
> 🎙️ **In class (Oct 20 part 3, 8:40–9:14):** Repetition/Shor code ratio is **1 : 9** — "that's really bad."
> ❓ **Why does correcting only X and Z fix any error $E$?** Any single-qubit error is a combination $I + c_xX + c_yY + c_zZ$, and $Y \propto XZ$. Measuring the syndrome **collapses** the error onto one of these Pauli cases, each of which the bit-flip layer (X) and sign-flip layer (Z) can undo.
### Surface Code (Dennis et al., 2002)
![[ch5-p66.jpg]]
- **State-of-the-art** error correction algorithm for **large-scale** quantum computing (i.e. **high data-per-parity ratio**)
- **Data qubits** are **open circles (∘)**, while **Z- and X-measurements** are **filled circles (•)**
- Both types of measurement qubits are computed from **surrounding data qubits**:
$$\boxed{Z_{abcd} = \begin{cases}+1 & \text{even number of 1's}\\ -1 & \text{otherwise}\end{cases} \qquad X_{abcd} = \begin{cases}+1 & \text{even number of 1's}\\ -1 & \text{otherwise}\end{cases}}$$
- These qubits are measured via **projection**
- Figure 22 (Surface code, Fowler et al., 2012): a checkerboard lattice of data qubits with green Z-measure and yellow X-measure crosses; each measure qubit touches 4 data qubits $a,b,c,d$; measurement circuits repeat every cycle → $+1$ ($Z+$/$X+$) or $-1$
> 🎙️ **In class (Oct 20 part 3, 9:14–14:34):** "In 2002 they came up with a new algorithm — a **hardware implementation**, the real implementation for modern quantum computers." White circles = data qubits; black circles = measurements, done by "guessing" (weak, not full-blown laser). Each yellow/green cross measures the X/Z expectation of the **4 surrounding qubits** — "the same idea as **parity check**". **Data qubits live in the quantum system (QPU); the measurement flags are stored in the classical system** (like registers). In 3-D layouts each measure has 6 neighbours ("you can't have four dimensions — that's physics").
### Syndrome Detection in Surface Code
![[ch5-p67.jpg]]
- **Syndrome**: error is detected in **each cycle** throughout the network, where the **current** Z- and X-measures are **compared with those from the previous timestep** (stored in a classical computer)
- **Single qubit error** always causes a **mismatched pair of adjacent measures**:
- $c$ has a **bit-flip** error: $Z^{(t+1)}_{abcd}\neq Z^{(t)}_{abcd}$ and $Z^{(t+1)}_{ecfg}\neq Z^{(t)}_{ecfg}$
- $c$ has a **sign-flip** error: $X^{(t+1)}_{abcd}\neq X^{(t)}_{abcd}$ and $X^{(t+1)}_{ecfg}\neq X^{(t)}_{ecfg}$
- Both errors may happen at the same time
- **Multiple qubit errors** result in a mismatched pair of **separate** measures, and we have to find the **shortest path** between them with the **minimum-weight perfect-matching** algorithm (Edmonds, 1965)
- Figure 23: Error detection as **Minesweeper**
> 🎙️ **In class (Oct 20 part 3, 14:59–15:20):** "Have you ever played **Minesweeper**? We are playing Minesweeper on quantum computers. If there is a misalignment … you can pinpoint the mine right away."
> ⚠️ **Precision note:** Follow the **slide**: a **bit-flip** shows up as **Z-measure** mismatches, a **sign-flip** as **X-measure** mismatches. (In class he once said it the other way round — "if the parity check of X misaligns, flip the bit" — the slide version is the standard one: Z-type checks detect X errors and vice versa.)
### Error Detection in Surface Code (isolated error)
![[ch5-p68.jpg]]
- **Isolated qubit error** results in a **square of measure mismatches**
- Once a qubit error is detected, we can **bit-flip it (Z-measure)** or **sign-flip it (X-measure)**
- Figure 24: data grid (0/1) with ±1 parity flags at timestep $t$ and $t+1$; at $t+1$ three data bits changed (highlighted red) and the flags around each changed bit flipped sign (boxed)
### Error Detection in Surface Code (consecutive errors)
![[ch5-p69.jpg]]
- **Consecutive qubit errors** result in measure mismatches **at the corners**
- We try to **draw a rectangle** that covers these qubit errors
- Some of these rectangles may also **overlap** each other
- Figure 25: a vertical chain of flipped data bits at $t+1$; mismatched flags appear only at the corners of the chain; a rectangle drawn from the corners covers the erroneous bits
> 🎙️ **In class (Oct 20 part 3, 15:41–21:01):** Compare timestep $t$ and $t+1$; where flags misalign "draw the box … on the inside all of these bits are erroneous — flip them." "**Do you know what I want you to do? I'll give you two time steps. Highlight the errors and flip it** … You've got three hours in the final; you may spend like **five minutes on this question**. Just easy." Overlapping: "just draw the box from the corners; you may have multiple boxes."
> 🎙️ **In class (Oct 20 part 3, 18:08–18:39):** Surface code does what the Shor code does "with **much fewer qubits**, but you **need the classical computer** to store the previous state and compare."
> ❓ **Why does the surface code beat the Shor code?** Shor protects 1 logical qubit with 9 physical ones (fixed 1:9) and needs deep encode/decode circuits. The surface code reuses each measure qubit for 4 neighbours (local, 2-D/3-D hardware-friendly), only measures parities each cycle, and lets a **classical** decoder (matching) find error chains — a much better data-per-parity ratio and it scales.
> 💡 **Analogy:** A Minesweeper board: you never look at the mines (data qubits) directly; the numbers around them (parity flags) tell you where they are.
### Exercise 5.6: Quantum Error Correction
- Encode the following codes with the Hamming(7,4) code:
1. 0000 2. 0100 3. 1010 4. 1100 5. 1101 6. 0101 7. 0111 8. 1111
- Decode the following codes with the Hamming(7,4) code. Determine which bit needs to be corrected:
9. 1000000 10. 0100111 11. 1010001 12. 1101011 13. 1111100 14. 0101011 15. 0111001 16. 1111110
- Answer the following questions:
17. Compute the **redundancy rate** $r$ of an error correction method:
$$\boxed{r = \frac{\text{num of parity bits}}{\text{num of total bits}}}$$
- For example, the redundancy rate of Hamming(7,4) is $3/7$. Compute the redundancy rate of Shor's code. (Shor: 8 extra out of 9 → $8/9$)
18. We want to store $N$ qubits in our data storage equipped with the Surface code. Compute the redundancy rate of this data storage. Discuss why the Surface code outperforms Shor's code.
#### Worked example (mine, method from slides 51–52): decode #9 `1000000`
- Data $b = 1000$, received parity $= 000$
- Recompute: $p_1 = 1\oplus0\oplus0 = 1$, $p_2 = 1\oplus0\oplus0 = 1$, $p_3 = 0\oplus0\oplus0 = 0$
- Syndrome = recomputed ⊕ received = $110$ → circles $p_1,p_2$ but not $p_3$ → **$b_1$** is wrong → corrected block `0000000`
---
## 7. Conclusion
### Conclusion
- **Unlocking a combination padlock** with the Bernstein-Vazirani Algorithm and the **phase kickback technique**
- **Measuring the balance of hash functions** with the Deutsch-Jozsa Algorithm
- **Transferring a qubit securely** with quantum teleportation
- **Transferring two classical bits in one qubit** with superdense coding
- Quantum noisy theorem, density matrix, state purification, and state fidelity
- **Correcting error qubits** with redundancy codes: Shor's code and Surface code
### Target Learning Outcomes/1
- **Quantum Algorithm Analysis and Implementation**
1. **Differentiate** between the Bernstein-Vazirani and Deutsch-Jozsa problems, specifically identifying when a function is constant, balanced, or possesses a secret oracle.
2. **Apply** the phase kickback technique to manipulate ancillary qubits for state evaluation.
3. **Construct and simulate** these algorithms using quantum programming frameworks like PennyLane.
- **Quantum Communication Protocols**
1. **Explain** the limitations imposed by the No-Cloning Theorem and how quantum teleportation bypasses this to achieve secure communication.
2. **Execute** the superdense coding protocol to transmit two classical bits of information using a single qubit.
3. **Contrast** standard teleportation with Quantum Gate Teleportation for long-distance entangled state transfer.
### Target Learning Outcomes/2
- **Noise Modeling and Characterization**
1. **Formulate** the density matrix to represent mixed states and environmental interactions (noise).
2. **Identify and model** common quantum noisy channels, including bit-flip (X), sign-flip (Z), and depolarization.
3. **Calculate** state fidelity and purity to measure the similarity between ideal and noisy quantum states.
- **Error Correction and Fault Tolerance**
1. **Analyze** the Hamming Code as a classical foundation for understanding quantum redundancy.
2. **Design** error-correction circuits using the Shor Code (9-qubit) and 3-repetition code to mitigate bit-flip and sign-flip errors.
3. **Evaluate** the efficiency of the surface code in the context of NISQ hardware and "Minesweeper-style" syndrome detection.
> [!info] Related
> - Next: Grover search and Shor's algorithm (Final Q5): [[Lecture 6 - Searching and Cryptography]]
> - Hamiltonians (Final Q6): [[Lecture 7 - System Dynamics, Simulation, and Polynomial Functions]]
> - QML and fidelity-based losses (Final Q7): [[Lecture 8 - Quantum Artificial Intelligence]]
> - [[Final - Instructor Hints]]