Path: blob/main/notebooks/quantum-machine-learning/encoding.ipynb
3855 views
Data encoding
In this page, we will introduce the problem of data encoding for quantum machine learning, then describe and implement various data encoding methods.
Introduction
Data representation is crucial for the success of machine learning models. For classical machine learning, the problem is how to represent the data numerically, so that it can be best processed by a classical machine learning algorithm.
For quantum machine learning, this question is similar, but more fundamental: how to represent and efficiently input the data into a quantum system, so that it can be processed by a quantum machine learning algorithm. This is usually referred to as data encoding, but is also called data embedding or loading.
This process is a critical part of quantum machine learning algorithms and directly affects their computational power.
Methods
Let's consider a classical dataset consisting of samples, each with features:
ParseError: KaTeX parse error: Undefined control sequence: \class at position 1: \̲c̲l̲a̲s̲s̲{script-x}{\mat…where is an dimensional vector for . To represent this dataset in a qubit system, we can use various embedding techniques, some of which are briefly explained and implemented below, as per References 1 and 2.
Basis encoding
Basis encoding associates a classical -bit string with a computational basis state of a -qubit system. For example, if , this can be represented as a -bit string as , and by a -qubit system as the quantum state . More generally, for an -bit string: , the corresponding -qubit state is ParseError: KaTeX parse error: Undefined control sequence: \cssId at position 1: \̲c̲s̲s̲I̲d̲{ket-x}{| x \ra… with ParseError: KaTeX parse error: Undefined control sequence: \class at position 5: b_n \̲c̲l̲a̲s̲s̲{in}{\in} \{0,1… for .
For the classical dataset described above, to use basis encoding, each data point must be a -bit string: , which then can be mapped directly to the quantum state with for and . We can represent the entire dataset as superpositions of computational basis states:
ParseError: KaTeX parse error: Undefined control sequence: \cssId at position 1: \̲c̲s̲s̲I̲d̲{_ket-dataset}{…Basis encoding
In Qiskit, once we calculate what state will encode our dataset, we can use the initialize function to prepare it. For example, the dataset is encoded as the state :
This example illustrates a couple of disadvantages of basis encoding. While it is simple to understand, the state vectors can become quite sparse, and schemes to implement it are usually not efficient.
Amplitude encoding
Amplitude encoding encodes data into the amplitudes of a quantum state. It represents a normalised classical -dimensional data point, , as the amplitudes of a -qubit quantum state, :
where , is the element of and is the computational basis state.
To encode the classical dataset described above, we concatenate all -dimensional data points into one amplitude vector, of length :
ParseError: KaTeX parse error: Undefined control sequence: \cssId at position 8: \alpha=\̲c̲s̲s̲I̲d̲{_a-norm}{A_{\t…where is a normalisation constant, such that . The dataset can now be represented in the computational basis as:
where are elements of the amplitude vector and are the computational basis states. The number of amplitudes to be encoded is . As a system of qubits provides amplitudes, amplitude embedding requires qubits.
Amplitude encoding
As an example, let's encode the dataset using amplitude encoding. Concatenating both data points and normalizing the resulting vector, we get:
and the resulting 2-qubit quantum state would be:
In the example above, the total number of elements of the amplitude vector, , is a power of 2. When is not a power of 2, we can simply choose a value for such that and pad the amplitude vector with uninformative constants.
Like in basis encoding, once we calculate what state will encode our dataset, in Qiskit we can use the initialize function to prepare it:
The advantage of amplitude encoding is that it only requires qubits to encode. However, subsequent algorithms must operate on the amplitudes of a quantum state, and methods to prepare and measure the quantum states tend not to be efficient.
Angle encoding
Angle encoding encodes features into the rotation angles of qubits, where . For example, the data point can be encoded as:
ParseError: KaTeX parse error: Undefined control sequence: \cssId at position 1: \̲c̲s̲s̲I̲d̲{_}{|x\rangle} …This is different from the previous two encoding methods, as it only encodes one data point at a time, rather than a whole dataset. It does however, only use qubits and a constant depth quantum circuit, making it amenable to current quantum hardware.
We can specify angle encoding as a unitary:
ParseError: KaTeX parse error: Undefined control sequence: \class at position 11: S_{x_j} = \̲c̲l̲a̲s̲s̲{_big-o-times-n…where:
Remembering that a single-qubit rotation around the -axis is:
We note that , and as an example, encode the data point using qiskit:
Dense angle encoding is a slight generalization of angle encoding, that encodes two features per qubit using the relative phase, where the data point can be encoded as:
ParseError: KaTeX parse error: Undefined control sequence: \class at position 13: |x\rangle = \̲c̲l̲a̲s̲s̲{_big-o-times-n…Although the angle and dense angle encoding use sinusoids and exponentials, there is nothing special about these functions. We can easily abstract these to a general class of qubit encodings that use arbitrary functions, or define the encodings as arbitrary unitaries, implemented as parameterized quantum circuits.
Arbitrary encoding
Arbitrary encoding encodes features as rotations on parameterized gates on qubits, where . Like angle encoding, it only encodes one data point at a time, rather than a whole dataset. It also uses a constant depth quantum circuit and qubits, meaning it can be run on current quantum hardware.
For example, to use the Qiskit EfficientSU2 circuit to encode 12 features, would only use 3 qubits:
Here we encode the data point with 12 features, using each of the parameterized gates to encode a different feature.
The Qiskit ZZFeatureMap circuit with 3 qubits, only encodes a data point of 3 features, despite having 6 parameterized gates:
Quick quiz
A parameterized quantum circuit has 16 parameters. What is the largest number of features it can encode?
4
8
16
32
The performance of different parameterized quantum circuits on different types of data is an active area of investigation.
References
Maria Schuld and Francesco Petruccione, Supervised Learning with Quantum Computers, Springer 2018, doi:10.1007/978-3-319-96424-9.
Ryan LaRose and Brian Coyle, Robust data encodings for quantum classifiers, Physical Review A 102, 032420 (2020), doi:10.1103/PhysRevA.102.032420, arXiv:2003.01695.