1. Core Concept: Pythagorean Geometry & Distance
The L2 Distance (Euclidean Distance) calculates the straight-line physical distance between two points in Euclidean space using the Pythagorean theorem:
$$L2(\mathbf{a}, \mathbf{b}) = \sqrt{\sum_{i=1}^n (a_i - b_i)^2}$$
Why Production Engines Use Squared L2 ($L2^2$)
In Chroma DB, setting "space": "l2" calculates the Squared L2 Distance:
$$L2^2(\mathbf{a}, \mathbf{b}) = \sum_{i=1}^n (a_i - b_i)^2$$
- Monotonic Equivalence: The square root function $f(x) = \sqrt{x}$ is strictly increasing for all $x \ge 0$. If $L2^2(\mathbf{q}, \mathbf{a}) < L2^2(\mathbf{q}, \mathbf{b})$, then $L2(\mathbf{q}, \mathbf{a}) < L2(\mathbf{q}, \mathbf{b})$. The relative nearest neighbor rank ordering remains identical.
- FLOP Elimination: Omission of the CPU/GPU square root instruction saves millions of cycles during large-scale HNSW graph traversal.
| Property |
L2 Distance (Euclidean) |
Squared L2 Distance ($L2^2$) |
| Mathematical Formula |
$\sqrt{\sum (a_i - b_i)^2}$ |
$\sum (a_i - b_i)^2$ |
| Triangle Inequality |
Satisfies $d(x,z) \le d(x,y) + d(y,z)$ |
Does not strictly satisfy (semi-metric) |
| Relative Ranking |
Identical top-$k$ nearest neighbors |
Identical top-$k$ nearest neighbors |
| Chroma DB Default |
Math concept |
Engine default ("space": "l2") |
| Best Applications |
Physical spatial coordinates, computer vision |
High-throughput vector indexing (HNSW/IVF) |
2. Daily Targeted Drills (15–20 Mins)
Drill 3.1: 2D Manual Calculation
Given vector $\mathbf{a} = [4.0, 8.0]$ and vector $\mathbf{b} = [11.5, 5.0]$, calculate both the Squared L2 distance ($L2^2$) and the exact L2 distance.
Solution:
$\Delta_1^2 = (4.0 - 11.5)^2 = (-7.5)^2 = 56.25$
$\Delta_2^2 = (8.0 - 5.0)^2 = (3.0)^2 = 9.0$
$L2^2(\mathbf{a}, \mathbf{b}) = 56.25 + 9.0 = 65.25$
$L2(\mathbf{a}, \mathbf{b}) = \sqrt{65.25} \approx 8.0777$
Drill 3.2: 3D Vector Comparison
Calculate the L2 distance between query $\mathbf{q} = [1.0, 2.0, 3.0]$ and reference vector $\mathbf{r} = [4.0, 5.0, 6.0]$.
Solution:
$\sum (q_i - r_i)^2 = (1 - 4)^2 + (2 - 5)^2 + (3 - 6)^2 = (-3)^2 + (-3)^2 + (-3)^2 = 9 + 9 + 9 = 27$
$L2(\mathbf{q}, \mathbf{r}) = \sqrt{27} \approx 5.196$
Drill 3.3: Magnitude Sensitivity Trade-Off
Why does raw L2 distance struggle when comparing documents of varying lengths represented by unnormalized word count or sum-pooled embeddings?
Answer: L2 is sensitive to magnitude as well as direction. Two documents discussing the exact same topic will have substantially different magnitudes if one is 100 words and the other is 5,000 words. L2 interprets this magnitude difference as geometric separation, penalizing the longer text even when the semantic meaning is identical.
Drill 3.4: Chroma DB Distance Interpretation
When running results = collection.query(..., n_results=2) on an L2 collection, Chroma returns distances: [[0.15, 0.85]]. Which result is closer to the query?
Answer: The document with distance 0.15 is much closer. L2 and Squared L2 are true distance metrics: smaller values indicate proximity, while $0.0$ signifies an exact match.
Drill 3.5: Curse of Dimensionality Impact
What geometric phenomenon occurs to L2 distance distributions as dimension $d$ scales up to 1536 or higher for uniform random vectors?
Answer: Distance concentration. As dimensions increase, the distance between any arbitrary pair of points converges toward the average distance. The difference between the nearest neighbor distance ($D_{\min}$) and farthest neighbor distance ($D_{\max}$) shrinks relative to $D_{\min}$, making purely distance-based nearest-neighbor discrimination harder unless embeddings lie on lower-dimensional manifolds.
3. Hands-On Verification Snippet
Save and run this script to compare exact $L2$, Squared $L2$, and matrix-vector performance using NumPy and Chroma DB:
import numpy as np
import chromadb
# 1. NumPy verification of L2 and Squared L2
a = np.array([4.0, 8.0], dtype=np.float32)
b = np.array([11.5, 5.0], dtype=np.float32)
l2_sq = np.sum((a - b) ** 2)
l2 = np.sqrt(l2_sq)
print(f"NumPy Squared L2: {l2_sq:.4f}") # 65.2500
print(f"NumPy L2: {l2:.4f}") # 8.0777
# 2. Chroma DB Collection with L2 Metric Space
client = chromadb.Client()
collection = client.create_collection(
name="l2_metric_demo",
configuration={"hnsw": {"space": "l2"}} # Chroma computes squared L2
)
collection.add(
ids=["doc_a", "doc_b"],
embeddings=[[4.0, 8.0], [1.0, 2.0]],
documents=["Point A", "Point B"]
)
# Query with reference point b = [11.5, 5.0]
results = collection.query(
query_embeddings=[[11.5, 5.0]],
n_results=2
)
print("\nChroma Query Output:")
for doc_id, dist in zip(results["ids"][0], results["distances"][0]):
print(f"ID: {doc_id} | Chroma Distance (L2^2): {dist:.4f}")