Skip to content

Quantum Algorithms and Communication Protocols

This guide introduces the core algorithmic primitives of quantum computing. Rather than treating quantum algorithms as abstract matrix multiplication, this section explains the physical intuition, the clever quantum mechanism behind each speedup, the step-by-step mathematical evolution, and how to simulate each protocol using quantum-emotion.


1. The Deutsch Algorithm: Instant Global Evaluation

The Intuition: The Coin-Tossing Puzzle

Imagine someone gives you a mystery coin and asks you: "Is this coin fair (one heads, one tails) or trick (both sides identical)?"

In the classical world, you must flip the coin and look at Side A, then flip it again and look at Side B. You cannot determine if Side A equals Side B without looking at both sides, requiring two inspections.

David Deutsch (1985) discovered that a quantum computer can answer whether the two sides match in a single evaluation, without ever revealing what is on either individual side.

The Problem

We are given a black-box function \(f: \{0, 1\} \to \{0, 1\}\). We want to decide whether \(f\) is: - Constant: \(f(0) = f(1)\) (both inputs produce the same output) - Balanced: \(f(0) \ne f(1)\) (one input produces 0, the other produces 1)

The Quantum Mechanism: Phase Kickback

In classical computing, an oracle writes its answer into an output register: \(y \to y \oplus f(x)\). In quantum mechanics, if we prepare the output qubit in the superposition state \(|-\rangle = \frac{|0\rangle - |1\rangle}{\sqrt{2}}\), a remarkable phenomenon occurs: the answer \(f(x)\) is kicked back into the relative phase of the input qubit:

\[ U_f |x\rangle |-\rangle = (-1)^{f(x)} |x\rangle |-\rangle \]

Instead of storing \(f(x)\) as a bit, the oracle modulates the wave crests and troughs of the input qubit.

Step-by-Step Walkthrough

Deutsch algorithm circuit

Step 1: Superposition Preparation
We prepare qubit 0 in \(|0\rangle\) and ancilla qubit 1 in \(|1\rangle\), then apply a Hadamard gate to both:

\[ |\psi_1\rangle = (H|0\rangle) \otimes (H|1\rangle) = \left( \frac{|0\rangle + |1\rangle}{\sqrt{2}} \right) \otimes |-\rangle \]

Step 2: Oracle Query with Phase Kickback
Applying the oracle \(U_f\) evaluates \(f(0)\) and \(f(1)\) simultaneously:

\[ |\psi_2\rangle = U_f |\psi_1\rangle = \left( \frac{(-1)^{f(0)}|0\rangle + (-1)^{f(1)}|1\rangle}{\sqrt{2}} \right) \otimes |-\rangle \]

Factoring out the global phase \((-1)^{f(0)}\):

\[ |\psi_2\rangle = (-1)^{f(0)} \left( \frac{|0\rangle + (-1)^{f(0) \oplus f(1)}|1\rangle}{\sqrt{2}} \right) \otimes |-\rangle \]
  • If \(f(x) = 0\):
\[ \frac{|x\rangle|0\rangle - |x\rangle|1\rangle}{\sqrt{2}} = + |x\rangle |-\rangle \]
  • If \(f(x) = 1\):
\[ \frac{|x\rangle|1\rangle - |x\rangle|0\rangle}{\sqrt{2}} = - |x\rangle |-\rangle \]

Step 3: Interference via Final Hadamard
A final Hadamard gate rotates the basis back to the computational axis (\(H|+\rangle = |0\rangle\) and \(H|-\rangle = |1\rangle\)):

\[ |\psi_3\rangle = (-1)^{f(0)} |f(0) \oplus f(1)\rangle \otimes |-\rangle \]

Measuring wire 0 yields: - Outcome 0 with 100% certainty \(\implies f\) is Constant. - Outcome 1 with 100% certainty \(\implies f\) is Balanced.

Quick Implementation in quantum-emotion

import numpy as np
from quantum_emotion import KetState, Hadamard, PauliX, CNOT

# Prepare 2-qubit register: wire 0 is input, wire 1 is ancilla
# For a balanced oracle f(x) = x, U_f is a CNOT gate:
psi = (
    Hadamard(wires=0)
    @ CNOT(wires=(0, 1))           # Balanced Oracle
    @ Hadamard(wires=0)
    @ Hadamard(wires=1)
    @ PauliX(wires=1)              # Prepare ancilla in |1>
    @ KetState(N=2)
)

# Measure wire 0: Outcome 1 confirms the function is balanced
probs = np.abs(psi.eval()) ** 2
print("Probability of state |1-> (Balanced):", probs[2] + probs[3])

Tutorial Signpost: - Interactive Notebook: Tutorial 3: Deutsch's Algorithm - GitHub Source: 3_deutsch_algorithm.ipynb

Academic Citation

Deutsch, D. (1985). Quantum theory, the Church-Turing principle and the universal quantum computer. Proceedings of the Royal Society of London. A. Mathematical and Physical Sciences, 400(1818), 97-117. doi:10.1098/rspa.1985.0070


2. The Bernstein-Vazirani Algorithm: Cracking Secret Bitstrings

The Intuition: The Master Key Lock

Suppose an electronic lock contains an unknown secret passcode \(\mathbf{s} = (s_0, s_1, \dots, s_{n-1})\) of length \(n\). When you enter a test bitstring \(\mathbf{x}\), the lock returns a single parity bit:

\[ f(\mathbf{x}) = \mathbf{s} \cdot \mathbf{x} \pmod 2 = s_0 x_0 \oplus s_1 x_1 \oplus \dots \oplus s_{n-1} x_{n-1} \]

Classically, each test reveals at most 1 bit of information about \(\mathbf{s}\). To discover an \(n\)-bit passcode, you must test \(n\) individual queries: \(\mathbf{x} = (1, 0, 0, \dots)\), then \((0, 1, 0, \dots)\), and so forth.

The Quantum Mechanism: Multi-Qubit Parallel Phase Kickback

By placing all \(n\) input qubits into a uniform superposition and applying phase kickback:

\[ U_f \left( \frac{1}{\sqrt{2^n}} \sum_{\mathbf{x}} |\mathbf{x}\rangle \right) |-\rangle = \left( \frac{1}{\sqrt{2^n}} \sum_{\mathbf{x}} (-1)^{\mathbf{s} \cdot \mathbf{x}} |\mathbf{x}\rangle \right) |-\rangle \]

The secret string \(\mathbf{s}\) is encoded across all \(2^n\) basis amplitudes as a multidimensional Fourier phase pattern. Applying an \(n\)-qubit Hadamard transform \(H^{\otimes n}\) causes constructive interference at exactly one state: \(|\mathbf{s}\rangle\). Every other possible candidate string experiences total destructive cancellation.

Step-by-Step Mathematical Walkthrough

Applying the Hadamard transform to computational basis states satisfies:

\[ H^{\otimes n} |\mathbf{x}\rangle = \frac{1}{\sqrt{2^n}} \sum_{\mathbf{y}} (-1)^{\mathbf{x} \cdot \mathbf{y}} |\mathbf{y}\rangle \]

Applying \(H^{\otimes n}\) to the state produced by the oracle:

\[ |\psi_{\text{final}}\rangle = \frac{1}{2^n} \sum_{\mathbf{y}} \left( \sum_{\mathbf{x}} (-1)^{\mathbf{x} \cdot (\mathbf{s} \oplus \mathbf{y})} \right) |\mathbf{y}\rangle \otimes |-\rangle \]

The inner sum evaluates to:

\[ \sum_{\mathbf{x} \in \{0, 1\}^n} (-1)^{\mathbf{x} \cdot (\mathbf{s} \oplus \mathbf{y})} = \begin{cases} 2^n & \text{if } \mathbf{y} = \mathbf{s} \\ 0 & \text{if } \mathbf{y} \ne \mathbf{s} \end{cases} \]

Why? When \(\mathbf{y} = \mathbf{s}\), every exponent is \(0\), so all \(2^n\) terms equal \(+1\) and add constructively. Whenever \(\mathbf{y} \ne \mathbf{s}\), \(\mathbf{s} \oplus \mathbf{y}\) has at least one nonzero bit, so exactly half the \(\mathbf{x}\) terms give \(+1\) and half give \(-1\), cancelling to zero. Measuring the \(n\) input qubits therefore returns \(\mathbf{s}\) with probability \(1\).

Quick Implementation in quantum-emotion

import numpy as np
from quantum_emotion import KetState, Hadamard, PauliX, CNOT

# Secret string s = '101' (n = 3, ancilla on wire 3)
secret = "101"
n = len(secret)

# Build circuit
circuit_ops = []
for i in range(n):
    circuit_ops.append(Hadamard(wires=i))

# Oracle: CNOT from wire i to ancilla (wire 3) wherever secret bit is 1
for i, bit in enumerate(secret):
    if bit == "1":
        circuit_ops.append(CNOT(wires=(i, n)))

for i in range(n):
    circuit_ops.append(Hadamard(wires=i))

# Execute on |000>|->
psi0 = PauliX(wires=n) @ KetState(N=n+1)
for op in [Hadamard(wires=n)] + circuit_ops:
    psi0 = op @ psi0

probs = np.abs(psi0.eval()) ** 2
top_index = np.argmax(probs)
cracked_key = format(top_index, f"0{n+1}b")[:n]
print(f"Secret Key: '{secret}' -> Quantum Recovered: '{cracked_key}'")

Tutorial Signpost: - Interactive Notebook: Tutorial 4: Bernstein-Vazirani - GitHub Source: 4_bernstein_vazirani.ipynb

Academic Citation

Bernstein, E., & Vazirani, U. (1997). Quantum complexity theory. SIAM Journal on Computing, 26(5), 1411-1473. doi:10.1137/S0097539796300921


3. Quantum Teleportation: Transporting States Without Physical Transfer

The Intuition: The Quantum Fax Machine

If you want to send a document over a classical fax machine, the machine scans the paper, encodes the letters as classical bits, and prints a copy on the other side.

In quantum mechanics, this is forbidden: 1. You cannot measure an unknown qubit \(|\psi\rangle = \alpha|0\rangle + \beta|1\rangle\) to determine \(\alpha\) and \(\beta\), because measurement collapses the state. 2. You cannot copy the qubit due to the No-Cloning Theorem (Wootters & Zurek 1982).

Quantum teleportation (Bennett et al. 1993) solves this dilemma. By consuming a pre-shared entangled Bell pair and sending just 2 classical bits, Alice can transfer the complete, continuous state vector \((\alpha, \beta)\) to Bob without ever knowing \(\alpha\) or \(\beta\) herself.

The Protocol Circuit

Quantum teleportation circuit

Step-by-Step Derivation

Step 1: The Initial 3-Qubit State
Alice holds qubit 0 (\(|\psi\rangle = \alpha|0\rangle + \beta|1\rangle\)) and qubit 1 (Alice's half of the Bell pair). Bob holds qubit 2 (Bob's half):

\[ |\Psi_0\rangle = (\alpha|0\rangle + \beta|1\rangle)_0 \otimes \frac{|00\rangle + |11\rangle_{12}}{\sqrt{2}} \]

Expanding this into all eight basis states:

\[ |\Psi_0\rangle = \frac{1}{\sqrt{2}} \Big( \alpha|000\rangle + \alpha|011\rangle + \beta|100\rangle + \beta|111\rangle \Big) \]

Step 2: Alice's Bell Measurement
Alice applies a CNOT gate from wire 0 to wire 1, followed by a Hadamard on wire 0. Rearranging the terms by Alice's qubits:

\[ |\Psi_2\rangle = \frac{1}{2} \Big( |00\rangle(\alpha|0\rangle + \beta|1\rangle) + |01\rangle(\alpha|1\rangle + \beta|0\rangle) + |10\rangle(\alpha|0\rangle - \beta|1\rangle) + |11\rangle(\alpha|1\rangle - \beta|0\rangle) \Big) \]

Step 3: Classical Feedforward Corrections
Alice measures her two qubits, getting classical bits \((m_0, m_1)\). Bob's qubit collapses into one of four states:

Alice Measures \((m_0, m_1)\) Bob's Collapsed State Bob Applies Gate Bob's Final State
\((0, 0)\) \(\alpha\vert 0\rangle + \beta\vert 1\rangle\) \(I\) (Do nothing) \(\vert\psi\rangle\)
\((0, 1)\) \(\alpha\vert 1\rangle + \beta\vert 0\rangle\) \(X\) (Bit flip) \(\vert\psi\rangle\)
\((1, 0)\) \(\alpha\vert 0\rangle - \beta\vert 1\rangle\) \(Z\) (Phase flip) \(\vert\psi\rangle\)
\((1, 1)\) \(\alpha\vert 1\rangle - \beta\vert 0\rangle\) \(ZX\) (Bit then Phase flip) \(\vert\psi\rangle\)

Once Bob applies \(Z^{m_0} X^{m_1}\), his state is identically \(|\psi\rangle\) with 100% fidelity. Alice's original state is completely destroyed during measurement, preserving the No-Cloning theorem.

Quick Implementation in quantum-emotion

import numpy as np
from quantum_emotion import KetState, Hadamard, CNOT, Measure, PauliX, PauliZ

# Prepare arbitrary target state on wire 0: alpha|0> + beta|1>
target = np.array([0.6, 0.8], dtype=complex)
psi0 = KetState(N=3, psi0=np.kron(target, [1, 0, 0, 0]))

# Teleportation pipeline with mid-circuit measurement and classical feedforward
psi_teleported = (
    PauliZ(wires=2).c_if(0)      # Apply Z on Bob if wire 0 was 1
    @ PauliX(wires=2).c_if(1)    # Apply X on Bob if wire 1 was 1
    @ Measure(wires=(0, 1))      # Alice measures wires 0 and 1
    @ Hadamard(wires=0)
    @ CNOT(wires=(0, 1))
    @ CNOT(wires=(1, 2))         # Prepare Bell pair on (1, 2)
    @ Hadamard(wires=1)
    @ psi0
)

# Verify Bob's reconstructed density matrix
rho_bob = psi_teleported.eval_density_matrix(wires=2)
target_rho = np.outer(target, np.conj(target))
print("Fidelity:", np.real(np.trace(rho_bob @ target_rho)))

Tutorial Signpost: - Interactive Notebook: Tutorial 5: Quantum Teleportation - GitHub Source: 5_quantum_teleportation.ipynb

Academic Citation

Bennett, C. H., Brassard, G., Crépeau, C., Jozsa, R., Peres, A., & Wootters, W. K. (1993). Teleporting an unknown quantum state via dual classical and Einstein-Podolsky-Rosen channels. Physical Review Letters, 70(13), 1895-1899. doi:10.1103/PhysRevLett.70.1895


4. Grover's Search Algorithm: Searching with Wave Interference

The Intuition: The Needle in the Haystack

Suppose you have an unsorted database of \(N\) items. One item is the "needle" \(|\omega\rangle\), and \(N-1\) items are "hay". Classically, if you check items randomly, you need to inspect \(N/2\) items on average, and \(N\) in the worst case (\(\mathcal{O}(N)\)).

Lov Grover (1996) showed that quantum mechanics can find the target in \(\mathcal{O}(\sqrt{N})\) queries. For \(N = 1{,}000{,}000\), classical search takes \(500{,}000\) queries; Grover takes roughly \(1{,}000\) steps.

The Quantum Mechanism: Amplitude Amplification

Instead of inspecting items, Grover treats the database as a set of sound waves. In each step, two operations occur: 1. The Phase Oracle: Flips the sign of the target item's wave from positive to negative. 2. The Diffusion Operator (Inversion about the Mean): Calculates the average amplitude of all states and flips every amplitude across this average line.

Because the target state was flipped upside down, it sits far below the average. When reflected across the mean, its amplitude bounces high into the positive direction, while all the wrong answers drop closer to zero.

Two-Dimensional Geometric Derivation

The \(N\)-dimensional space is spanned by two orthogonal states: - The target state: \(|\omega\rangle\) - The non-target state: \(|s'\rangle = \frac{1}{\sqrt{N - 1}} \sum_{x \ne \omega} |x\rangle\)

The uniform superposition state \(|s\rangle = H^{\otimes n}|0\rangle\) makes an angle \(\theta/2\) with \(|s'\rangle\):

\[ \sin\left(\frac{\theta}{2}\right) = \frac{1}{\sqrt{N}} \]

Each Grover iteration \(G = R_s R_\omega\) rotates the state vector toward \(|\omega\rangle\) by an angle of \(\theta\):

\[ G^k |s\rangle = \sin\left(\frac{2k + 1}{2}\theta\right) |\omega\rangle + \cos\left(\frac{2k + 1}{2}\theta\right) |s'\rangle \]

To reach maximum probability \(P(\omega) \approx 1\), we choose \(k\) such that:

\[ k_{\text{optimal}} \approx \frac{\pi}{4}\sqrt{N} \]

For \(N = 4\) (\(2\) qubits), \(k_{\text{optimal}} = 1\). A single Grover iteration amplifies the target state to 100% deterministic probability.

Quick Implementation in quantum-emotion

import numpy as np
from quantum_emotion import KetState, Hadamard, PauliX, ControlledZ

# Target state |11> in 2 qubits (N = 4)
# Phase oracle: ControlledZ marks |11> with a -1 phase
# Diffusion operator: H(0,1) @ X(0,1) @ ControlledZ @ X(0,1) @ H(0,1)
psi = (
    Hadamard(wires=1)
    @ Hadamard(wires=0)
    @ PauliX(wires=1)
    @ PauliX(wires=0)
    @ ControlledZ(wires=(0, 1))    # Reflection across |00>
    @ PauliX(wires=1)
    @ PauliX(wires=0)
    @ Hadamard(wires=1)
    @ Hadamard(wires=0)
    @ ControlledZ(wires=(0, 1))    # Oracle marks |11>
    @ Hadamard(wires=1)
    @ Hadamard(wires=0)            # Initial equal superposition
    @ KetState(N=2)
)

probs = np.abs(psi.eval()) ** 2
print("Final Probabilities [P(00), P(01), P(10), P(11)]:", np.round(probs, 4))

Tutorial Signpost: - Interactive Notebook: Tutorial 7: Grover's Search - GitHub Source: 7_grovers_search.ipynb

Academic Citation

Grover, L. K. (1996). A fast quantum mechanical algorithm for database search. Proceedings of the twenty-eighth annual ACM symposium on Theory of computing (STOC '96), 212-219. doi:10.1145/237814.237866


5. Quantum Key Distribution (BB84 Protocol)

The Intuition: Polarized Eavesdropping Detection

In classical cryptography, an eavesdropper can tap a wire, copy every electrical packet, and leave no evidence of intrusion.

In quantum mechanics, if Alice sends single photons, any attempt by an eavesdropper (Eve) to read them must obey quantum physics: 1. Eve cannot clone the photons (No-Cloning Theorem). 2. Eve cannot measure in both bases simultaneously (Heisenberg Uncertainty). 3. If Eve measures in the wrong basis, she collapses the photon state, introducing errors that Alice and Bob can detect immediately.

The Conjugate Bases

Alice encodes random bits using two mutually unbiased bases: - Computational (\(Z\)-basis): \(|0\rangle\) (horizontal), \(|1\rangle\) (vertical) - Hadamard (\(X\)-basis): \(|+\rangle\) (diagonal \(+45^\circ\)), \(|-\rangle\) (diagonal \(-45^\circ\))

If a photon prepared in the \(Z\)-basis is measured in the \(X\)-basis, the outcome is completely random:

\[ |\langle 0|+\rangle|^2 = 0.5, \quad |\langle 0|-\rangle|^2 = 0.5 \]

Why Eavesdropping Generates 25% Error (QBER)

When Eve intercepts every photon: - Half the time (\(50\%\)), Eve guesses Alice's basis correctly: no disturbance is introduced. - Half the time (\(50\%\)), Eve guesses the wrong basis: the photon collapses into the conjugate basis. When Bob measures this photon in Alice's original basis, he gets the wrong bit \(50\%\) of the time. - Expected Theoretical Error Rate:

\[ \text{QBER} = 0.5 \times 0\% + 0.5 \times 50\% = 25.0\% \]

If Alice and Bob test a sample of their sifted key and observe an error rate approaching \(25\%\), they immediately know an eavesdropper is present and discard the key.

Tutorial Signpost: - Interactive Notebook: Tutorial 8: Key Distribution - GitHub Source: 8_quantum_key_distribution.ipynb

Academic Citation

Bennett, C. H., & Brassard, G. (1984). Quantum cryptography: Public key distribution and coin tossing. IEEE International Conference on Computers, Systems and Signal Processing, Bangalore, India, 175-179.


6. Superdense Coding: Doubling Classical Capacity

The Intuition

Can you send two letters by mailing only one envelope? Classically, no: a two-state system (like a coin) can only store 1 bit of information (Holevo bound).

However, if Alice and Bob already share an entangled pair of particles before the mission begins, Alice can manipulate her particle alone to encode two full classical bits (00, 01, 10, 11). When she sends her single particle to Bob, Bob measures both together and decodes both bits.

The Encoding Operations

Alice applies a local single-qubit operation on wire 0:

\[ |\Phi^+\rangle = \frac{|00\rangle + |11\rangle}{\sqrt{2}} \]
  • Message 00: Apply \(I \implies |\Phi^+\rangle = \frac{|00\rangle + |11\rangle}{\sqrt{2}}\)
  • Message 01: Apply \(X \implies |\Psi^+\rangle = \frac{|10\rangle + |01\rangle}{\sqrt{2}}\)
  • Message 10: Apply \(Z \implies |\Phi^-\rangle = \frac{|00\rangle - |11\rangle}{\sqrt{2}}\)
  • Message 11: Apply \(XZ \implies |\Psi^-\rangle = \frac{|10\rangle - |01\rangle}{\sqrt{2}}\)

Because all four Bell states are orthogonal (\(\langle \Phi^i | \Phi^j \rangle = \delta_{ij}\)), Bob can distinguish them with 100% accuracy using a CNOT and a Hadamard.

Tutorial Signpost: - Interactive Notebook: Tutorial 10: Superdense Coding - GitHub Source: 10_superdense_coding.ipynb

Academic Citation

Bennett, C. H., & Wiesner, S. J. (1992). Communication via one- and two-particle operators on Einstein-Podolsky-Rosen states. Physical Review Letters, 69(20), 2881-2884. doi:10.1103/PhysRevLett.69.2881


7. Quantum Phase Estimation: The Molecular Clock

The Intuition

In quantum chemistry and physics, finding the energy of a molecule or factoring a large RSA number comes down to finding an eigenphase \(\theta\) of a unitary transformation:

\[ U |\psi\rangle = e^{2\pi i \theta} |\psi\rangle \]

Quantum Phase Estimation (QPE) acts like an ultra-precise quantum clock. It uses an array of counting qubits that tick at binary frequencies (\(2^0, 2^1, 2^2, \dots\)), accumulating the phase into an interference pattern that is converted into a binary number by the Inverse Quantum Fourier Transform (\(\text{QFT}^\dagger\)).

QPE provides an exponential speedup over classical eigenvalue search and forms the mathematical backbone of Shor's algorithm.

Tutorial Signpost: - Interactive Notebook: Tutorial 11: Phase Estimation - GitHub Source: 11_quantum_phase_estimation.ipynb

Academic Citation

Kitaev, A. Y. (1995). Quantum measurements and the Abelian Stabilizer Problem. arXiv:quant-ph/9511026. doi:10.48550/arXiv.quant-ph/9511026


8. Entanglement Swapping and Quantum Repeaters

The Intuition: Teleporting Entanglement Across Oceans

In fiber optic cables, light dims exponentially. After 100 kilometers, 99% of photons are lost. Classical repeaters cannot amplify quantum signals because cloning is forbidden.

The solution is Entanglement Swapping: - Station A (Alice) entangles with local station Charlie 1. - Station B (Bob) entangles with local station Charlie 2. - Neither Alice nor Bob have ever interacted with each other.

Charlie performs a joint Bell measurement on his two local particles and transmits the classical measurement result to Bob. Instantly, Alice and Bob become maximally entangled across hundreds of kilometers.

This technique is the foundation of the worldwide Quantum Internet.

Tutorial Signpost: - Interactive Notebook: Tutorial 12: Entanglement Swapping - GitHub Source: 12_entanglement_swapping.ipynb

Academic Citation

Żukowski, M., Zeilinger, A., Horne, M. A., & Ekert, A. K. (1993). "Event-ready-detectors" Bell experiment via entanglement swapping. Physical Review Letters, 71(26), 4287-4290. doi:10.1103/PhysRevLett.71.4287