What Are PQ Codes?

PQ codes are the compact vector representations produced by product quantization. Instead of storing every original floating-point dimension, a vector database stores a short sequence of codes that point to learned centroids.

In simple terms, a PQ code is the compressed form of one vector.

Short Definition

A PQ code is a sequence of centroid IDs, with one ID for each segment of a product-quantized vector.

Each ID tells the system which learned centroid should approximate that segment of the original vector.

Where PQ Codes Come From

PQ codes are created by a product quantizer.

The quantizer splits a vector into segments, compares each segment to a learned codebook, and stores the ID of the nearest centroid.

The result is a compact list of IDs instead of the original vector values.

A Simple Example

Suppose a vector is split into eight segments.

After product quantization, the vector might be stored as:

[12, 88, 4, 191, 33, 7, 240, 102]

Each number is a code for one segment. The meaning of each number depends on the codebook for that segment position.

PQ Codes Need Codebooks

A PQ code cannot be interpreted by itself.

The codebooks define what each code means. If code 12 appears in segment position 1, it points to centroid 12 in the first codebook. If code 12 appears in segment position 2, it points to centroid 12 in the second codebook.

The same numeric ID can refer to different centroids in different segment positions.

Why PQ Codes Are Small

PQ codes are small because each segment is stored as an ID rather than several floating-point numbers.

If a segment has 256 possible centroids, its ID can fit in one byte.

That means a vector with 128 segments can often be represented as roughly 128 bytes of codes, before accounting for codebook overhead and any stored original vectors.

What PQ Codes Replace

PQ codes replace full-precision segment values for search-time representation.

A segment that originally contained several 32-bit floats becomes one compact ID. The ID points to the learned centroid that best approximates that segment.

This replacement is the source of memory savings.

What PQ Codes Do Not Store

PQ codes do not store exact original coordinates.

They store approximate region identifiers. Many different original segment values may map to the same code.

This is why product quantization is lossy.

PQ Codes and Reconstruction

A system can reconstruct an approximate vector from a PQ code.

It does this by looking up each segment code in the matching codebook, retrieving the centroid for that code, and concatenating the centroids.

The reconstructed vector is an approximation, not a perfect copy of the original vector.

PQ Codes and Distance Estimation

PQ codes are useful because a search system can estimate distances from them.

Some systems reconstruct approximate vectors and then compare those approximations. Others use lookup tables to add up segment-level distances directly from code IDs.

Either approach lets the system score candidates without loading every full vector.

PQ Codes in Nearest Neighbor Search

In nearest neighbor search, PQ codes are usually used during candidate generation.

The system searches using compact codes, keeps likely candidates, and may later rescore the best candidates with original full-precision vectors.

This allows broad search to be memory efficient while final ranking can still use more accurate distances when needed.

One-Byte Codes

A common PQ configuration uses 256 centroids per segment.

Because 256 values fit in one byte, each segment code can be stored as a single byte.

More centroids can preserve more detail, but may require more storage per code. Fewer centroids can save space, but usually increase distortion.

Code Length

The length of a PQ code is usually the number of vector segments.

If a vector is split into 96 segments, its PQ code has 96 entries. If it is split into 128 segments, the PQ code has 128 entries.

More entries generally preserve more vector detail, but they also use more memory.

How PQ Codes Save Memory

Consider a 768-dimensional vector stored as 32-bit floats:

768 x 4 bytes = 3072 bytes

If PQ represents that vector as 128 one-byte codes, the compressed code is roughly:

128 x 1 byte = 128 bytes

The real total also includes codebooks and possibly original vectors for rescoring, but the searchable representation can be much smaller.

Why PQ Codes Can Reduce Recall

PQ codes can reduce recall because they approximate original vectors.

If two different vectors map to similar or identical code patterns, the search system may not distinguish them as accurately as it could with full vectors.

More candidate expansion and rescoring can help, but only for candidates that survive the compressed search stage.

PQ Codes vs Vector IDs

A PQ code is not the same as a database object ID or vector ID.

The vector ID identifies the stored object. The PQ code represents the compressed vector values for that object.

One object may have an ID, metadata, original content, and a PQ code used for vector search.

PQ Codes vs Codebooks

PQ codes and codebooks work together, but they are different.

  • The codebook stores learned centroids.
  • The PQ code stores centroid IDs for one vector.

The codebook is shared across many vectors. Each vector has its own PQ code.

What Engineers Tune

Important choices that affect PQ codes include:

  • number of segments
  • number of centroids per segment
  • bytes per segment code
  • training sample quality
  • whether original vectors are retained
  • candidate pool size during search
  • rescoring limit

What to Benchmark

When evaluating PQ codes, measure:

  • compressed bytes per vector
  • codebook overhead
  • recall at the target k
  • distance distortion
  • candidate search latency
  • rescoring latency
  • behavior under filters
  • quality after new data is added

Summary

PQ codes are compact centroid-ID sequences that represent product-quantized vectors. Each code entry points to a learned centroid for one vector segment.

They make vector search more memory efficient, but they replace exact coordinates with approximations. The quality of PQ codes depends on codebook training, segment configuration, candidate expansion, and rescoring strategy.