pdft#

Trainable quantum-circuit transforms for image compression and inpainting, in JAX.

pdft starts from the quantum Fourier transform as a circuit of Hadamard and controlled-phase gates and trains the gates: on their unitary manifolds so an image becomes sparse in the basis, or the phases alone, as free numbers, through a solver that fills an image in from a fraction of its pixels. It is a faithful Python port of ParametricDFT.jl, with results checked against committed Julia goldens, and the reference implementation of two papers.

API reference

Bases, optimizers, losses, training loops, the compression and completion tasks, I/O and coherence tools, generated from the source docstrings.

API reference
Example gallery

Short runnable scripts that train bases, compare optimizers and fill in an image from a tenth of its pixels, with their figures rendered at build time.

Example gallery
Papers

The two arXiv papers this package accompanies, and which example demonstrates each.

Papers

Installation#

From PyPI (Python 3.11+):

pip install "pdft>=0.4.0"

Note: 0.4.0 turns the faster slices and merge_diagonals arithmetic on by default, so results differ from 0.3.0 by rounding; a CircuitCode with both off gives 0.3.0’s numbers. 0.3.0 is the first release with the completion task, the step trainer and the parameter views, and it moves compress / recover from pdft.io to pdft.tasks. The older pdft==0.2.2 wheel predates DCT4Basis and the parametrization="u4" option of TEBDBasis / MERABasis.

From source:

git clone https://github.com/zazabap/pdft.git
cd pdft
pip install -e ".[dev]"

Optional extras: pdft[plot] adds matplotlib for the plotting helpers in pdft.viz, and pdft[gpu] installs a CUDA 12 build of JAX.

Note

Importing pdft turns on JAX’s 64-bit mode for the whole process. Julia parity depends on complex128 arithmetic, so import pdft before you create any JAX arrays.

Quick start#

Train a parametric QFT basis on a target image with Riemannian gradient descent, the compression objective:

import jax
import jax.numpy as jnp
import pdft

target = jax.random.normal(jax.random.PRNGKey(7), (4, 4)).astype(jnp.complex128)
basis = pdft.QFTBasis(m=2, n=2)

result = pdft.train_basis(
    basis,
    target=target,
    loss=pdft.L1Norm(),
    optimizer=pdft.RiemannianGD(lr=0.01),
    steps=50,
    seed=0,
)
print(result.loss_history[0], "->", result.loss_history[-1])

Fill in an image from a tenth of its pixels, then train the phases of the circuit through that solver (the inpainting paper’s Model B):

from pdft.bases import cp_diagonals_view
from pdft.circuit import bit_reverse
from pdft.tasks import complete, completion_loss

# image: a (64, 64) array in [0, 1]; mask: bool, True where a pixel was observed;
# images: a stack of training images. A QFTBasis works on the bit-reversed
# image, and a mask lives on the pixels, hence the reversals.
basis = pdft.QFTBasis(m=6, n=6)
filled = bit_reverse(complete(basis, bit_reverse(image * mask), bit_reverse(mask), k=61, steps=60))

result = pdft.train_basis_steps(
    basis,
    dataset=images,                                  # in the image's own frame
    objective=completion_loss(k=61, steps=10),
    view=cp_diagonals_view,                          # the four phases of every gate, plain Adam
    optimizer=pdft.RiemannianAdam(lr=0.02),
    steps=60, rate=0.10, batch_size=2, seed=0,
    frame=bit_reverse,
)

A separable circuit (no gate between the two registers: QFT, Rich, RealRich, DCT4, TEBD, MERA) can also be trained with no gradient steps at all. train_basis_exact alternates the codes H_k(U x) with an exact update of every gate given the others (the polar factor of its environment, or its phases for a phase tensor), so the sparsity objective never rises but by rounding; a RichBasis started at the QFT moves away from it, which phase training cannot:

import numpy as np
from pdft.circuit import bit_reverse

result = pdft.train_basis_exact(
    pdft.RichBasis(m=6, n=6),
    dataset=[bit_reverse(x) / np.linalg.norm(x) for x in images],  # each image weighs the same
    k=[205, 410],                                                  # budgets, averaged
    sweeps=20, passes=2,
)

precision="single" computes everything the size of an image in single precision, for every basis: the transforms, the codes and each block’s gate step. The sums over the images and the updates stay in double. On an RTX 5070, whose double precision is slow, a sweep over four 2^12 x 2^13 images under a Rich circuit of four blocks (6, 6, 6 and 7 qubits, one per mode of a 4-D scan) took about 6 s instead of 59 s, and a two-register RichBasis(10, 10) needs half the memory. On complex images the two precisions agree to a few 1e-6; on real images at the Fourier start the top k settles ties by rounding, and the two (like a CPU and a GPU) can end up about 1e-3 apart after twenty sweeps.

What’s in the package#

Area

Contents

Circuit bases

QFTBasis, EntangledQFTBasis, TEBDBasis, MERABasis, RichBasis, RealRichBasis, DCT4Basis

Block bases

BlockedBasis, freeze_as_blocked()

Optimizers

RiemannianGD (Armijo line search), RiemannianAdam

Losses

L1Norm, MSELoss with top-k truncation

Training

train_basis() (single target), train_basis_batched() (multi-image, cosine schedule, early stopping), train_basis_steps() (a fresh batch and mask per step, for training through a solver)

Parameter views

tensors_view, cp_phases_view, cp_diagonals_view: what a trainer moves

Tasks

pdft.tasks: top-k compression (compress(), recover()) and completion from observed pixels (complete(), completion_loss())

Coherence

coherence(), certify_flat_modulus()

I/O

pdft.io: JSON serialization compatible with Julia