Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Encoder Types

Classical Encoders

Encode or compress classical data into smaller-sized data via a deterministic algorithm. For example, JPEG is essentially an algorithm that compresses images into smaller-sized images.

Classical Autoencoders

Use machine-learning techniques and train a variational network for compressing data. In general, an autoencoder network looks as follows:

Classical autoencoder layout (from Wikipedia)

The network has three main parts:
  1. The encoder compresses the data into a smaller, coded layer.
  2. The latter is the input to a decoder part.
  3. Typically, training is done against the comparison between the input and the output of this network.
Classical autoencoders can also be used for anomaly detection (see below).

Quantum Autoencoders

In a similar fashion to the classical counterpart, a quantum autoencoder compresses quantum data stored initially on nn qubits into a smaller quantum register of m<nm<n qubits via a variational circuit. However, quantum computing is reversible; therefore, qubits cannot be “erased”. Alternatively, a quantum autoencoder tries to achieve the following transformation from an uncoded quantum register of size nn to a coded one of size mm: ∣ψ⟩n→∣ψ′⟩m∣0⟩n−m|\psi\rangle_n \rightarrow |\psi'\rangle_m|0\rangle_{n-m} Namely, we try to decouple the initial state to a product state of a smaller register of size mm and a register that is in the zero state. The former is usually called the coded state and the latter the trash state.

Training Quantum Autoencoders

To train a quantum autoencoder, we define a proper cost function. Below are two common approaches, one using a swap test and the other using Hamiltonian measurements. We focus on the swap test case, and comment on the other approach at the end of this notebook.

The Swap Test

The swap test is a quantum function that checks the overlap between two quantum states. The inputs of the function are two quantum registers of the same size, ∣ψ1⟩, ∣ψ2⟩|\psi_1\rangle, \,|\psi_2\rangle, and it returns as output a single “test” qubit whose state encodes the overlap between the two inputs: ∣q⟩test=α∣0⟩+1−α2∣1⟩|q\rangle_{\rm test} = \alpha|0\rangle + \sqrt{1-\alpha^2}|1\rangle, with α2=12(1+∣⟨ψ1∣ψ2⟩∣2).\alpha^2 = \frac{1}{2}\left(1+|\langle \psi_1 |\psi_2 \rangle |^2\right). Thus, the probability to measure the test qubit at state 00 is 1 if the states are identical (up to a global phase) and 0 if the states are orthogonal to each other. The quantum model starts with an H gate on the test qubit, followed by swapping between the two states controlled on the test qubit and a final H gate on the test qubit.

Quantum Neural Networks for Quantum Autoencoders

The quantum autoencoder can be built as a quantum neural network with these parts:
  1. A data loading block that loads classical data on nn qubits.
  2. An encoder block, which is a variational quantum ansatz with input port of size nn and output ports of size mm and n−mn-m.
  3. A swap test block between the n−mn-m trash output of the encoder and new n−mn-m zero registers.
We train the network such that the test qubit of the swap test is at state ∣0⟩|0\rangle with probability
Screenshot 2025-07-02 at 23.22.05.png

Quantum autoencoder layout: uncoded data of size 4 transforms into two outputs, a coded register of size 2 and trash outputs of size 2 at state $|00\rangle$

Predefined Functions That Construct the Quantum Layer

In the first step we build user-defined functions that allow flexible modeling:
  1. angle_encoding: This function loads data of size num_qubits on num_qubits qubits via RY gates.
  2. encoder_ansatz : A simple variational ansatz for encoding num_qubits qubits on num_encoding_qubits qubits (see the description in the code block).

Example: Autoencoder for Domain Wall Data

In the following example we try to encode data which has a domain wall structure. Let us define the relevant data for strings of size

The Data

Output:

The Quantum Program

We encode this data of size 4 on 2 qubits. Let us build the corresponding quantum layer based on the predefined functions above:
We construct the model:
We synthesize and visualize the quantum layer:
Output:

The Network

The network for training contains only a quantum layer. The corresponding quantum program was already defined above, so what remains is to define the classical output of the layer: 1−α21-\alpha^2, with α2\alpha^2 being the probability of the test qubit being at state
  1. We read it from the measurement histogram of the test output, using a SamplingReadout.

Creating the Dataset

The cost function to minimize is ∣1−α2∣|1-\alpha^2| for all our training data. Looking at the Qlayer output, this means that we should define the corresponding labels as 00:

Defining the Training

Setting Hyperparameters

The L1 loss function fits the intended cost function we aim to minimize:

Training

In this demo we initialize the network with trained parameters and run only one epoch for demonstration purposes. Reasonable training with the above hyperparameters can be achieved with ∼40\sim 40 epochs. To train the network from the beginning, uncomment the following code line:
Output:

Verifying

Once we have trained the network, we can build a new network with the trained variables. We verify our encoder by taking only the encoding block, changing the postprocess, etc. Below, we verify our quantum autoencoder by comparing the input with the output of an encoder-decoder network. We create the following network containing three quantum blocks:
  • The first two blocks of the previous network: a block for loading the inputs followed by our quantum encoder.
  • We reset the trash qubits, assigning them to be at the zero state explicitly.
  • The inverse of the quantum encoder.
The network weights are allocated with the trained ones. Screenshot 2025-07-03 at 0.00.11.png

Qlayer for verifying the quantum autoencoder containing three quantum blocks: (1) data encoding, (2) trained encoder, and (3) inverse of trained encoder (i.e., decoder)

Building the Quantum Validator

Output:

Validating the Result

For the validator postprocessing, we take the output with the maximum counts. We run the validator quantum program with the trained weights, and compare every input data with its output.
Now we can compare the input with the output of the validator for different data:
Output:

Detecting Anomalies

We can use our trained network for anomaly detection. Let’s see what happens to the trash qubits when we insert an anomaly; namely, non-domain-wall data:
We print all the anomaly data based on predefined accuracy for the cost function:
Output:

Alternative Network for Training a Quantum Autoencoder

Another way to introduce a cost function is by estimating Hamiltonians. Measuring the Pauli ZZ matrix on a qubit at the general state ∣q⟩=a∣0⟩+b∣1⟩|q\rangle=a|0\rangle+b|1\rangle is ⟨q∣Z∣q⟩=a2−b2\langle q |Z|q \rangle=a^2-b^2. Therefore, a cost function can be defined by taking expectation values on the trash output (without a swap test) as follows: Cost=12∑k=1num of trash qubits1−⟨Zk⟩.\text{Cost} = \frac{1}{2}\sum^{\text{num of trash qubits}}_{k=1} 1 - \langle Z_k \rangle. Below we show how to define the corresponding QLayer: the quantum program and the observable.

The Quantum Program

Output:

The Observable

The outputs of main occupy the qubits in order: coded on qubits 0 and 1, and trash on qubits 2 and
  1. The cost function is the expectation value of a single observable on the trash qubits:
Cost=⟨12(I−Z2)+12(I−Z3)⟩.\text{Cost} = \Big\langle \frac{1}{2}(I - Z_2) + \frac{1}{2}(I - Z_3) \Big\rangle.
Output: