Path: blob/main/notebooks/summer-school/2021/resources/lab-notebooks/lab-3.ipynb
3855 views
Lab 3: Quantum Kernels and Support Vector Machines
In this lab, you will learn how to implement quantum feature maps, quantum kernels and quantum support vector classification in Qiskit.
We will first load the required packages.
Data
The data we are going to work with today will be a small subset of the well known handwritten digits dataset, which is available through scikit-learn. We will be aiming to differentiate between '0' and '1'.
Data Preprocessing
There are a total of 360 datapoints in the dataset. Each datapoint is a 8x8 image of a digit, collapsed into an array, where each element is an integer between 0 (white) and 16 (black). As per classical classification, we need to split the dataset into training (100) and testing (20) samples, and normalise it. To use the dataset for quantum classification, we need to scale the range to between -1 and 1, and reduce the dimensionality to the number of qubits we want to use (4).
Note that in Qiskit, this is done for this dataset in qiskit_machine_learning.datasets.digits, but it is made explicit in this lab for future reference.
Data Encoding
We will take the classical data and encode it to the quantum state space using a quantum feature map. The choice of which feature map to use is important and may depend on the given dataset we want to classify. Here we'll look at the feature maps available in Qiskit, before selecting and customising one to encode our data.
Quantum Feature Maps
As the name suggests, a quantum feature map is a map from the classical feature vector to the quantum state . This is faciliated by applying the unitary operation on the initial state where n is the number of qubits being used for encoding.
The feature maps currently available in Qiskit (PauliFeatureMap, ZZFeatureMap and ZFeatureMap) are those introduced in Havlicek et al. Nature 567, 209-212 (2019), in particular the ZZFeatureMap is conjectured to be hard to simulate classically and can be implemented as short-depth circuits on near-term quantum devices.
The PauliFeatureMap is defined as:
and describes the unitary operator of depth :
which contains layers of Hadamard gates interleaved with entangling blocks, , encoding the classical data as shown in circuit diagram below for .
Within the entangling blocks, : denotes the Pauli matrices, the index describes connectivities between different qubits or datapoints: , and by default the data mapping function is
when , this is the ZFeatureMap:
which is defined as:
note the lack of entanglement in this feature map, this means that this feature map is simple to simulate classically and will not provide quantum advantage.
and when , this is the ZZFeatureMap:
which is defined as:
now that there is entanglement in the feature map, we can define the entanglement map:
We can also customise the Pauli gates in the feature map, for example, :
We can also define a custom data mapping function, for example:
The NLocal and TwoLocal functions in Qiskit's circuit library can also be used to create parameterized quantum circuits as feature maps.
Both functions create parameterized circuits of alternating rotation and entanglement layers. In both layers, parameterized circuit-blocks act on the circuit in a defined way. In the rotation layer, the blocks are applied stacked on top of each other, while in the entanglement layer according to the entanglement strategy. Each layer is repeated a number of times, and by default a final rotation layer is appended.
In NLocal, the circuit blocks can have arbitrary sizes (smaller equal to the number of qubits in the circuit), while in TwoLocal, the rotation layers are single qubit gates applied on all qubits and the entanglement layer uses two-qubit gates.
For example, here is a TwoLocal circuit, with and gates in the rotation layer and gates in the entangling layer with circular entanglement:
and the equivalent NLocal circuit:
Here is another NLocal circuit, with a rotation block on 2 qubits and an entanglement block on 4 qubits using linear entanglement:
It is also possible to create a completely custom parameterized circuit feature map. To do so, simply create a QuantumCircuit with a ParameterVector. Here's an example:
Let's encode the first training sample using the ZZFeatureMap:
Exercise 1: Data Encoding
Encode the data point using the ZZFeatureMap with 4 repetitions and default data mapping function.
Quantum Kernel Estimation
A quantum feature map, , naturally gives rise to a quantum kernel, , which can be seen as a measure of similarity: is large when and are close.
When considering finite data, we can represent the quantum kernel as a matrix: . We can calculate each element of this kernel matrix on a quantum computer by calculating the transition amplitude: assuming the feature map is a parameterized quantum circuit, which can be described as a unitary transformation on qubits.
This provides us with an estimate of the quantum kernel matrix, which we can then use in a kernel machine learning algorithm, such as support vector classification.
As discussed in Havlicek et al. Nature 567, 209-212 (2019), quantum kernel machine algorithms only have the potential of quantum advantage over classical approaches if the corresponding quantum kernel is hard to estimate classically.
As we will see later, the hardness of estimating the kernel with classical resources is of course only a necessary and not always sufficient condition to obtain a quantum advantage.
However, it was proven recently in Liu et al. arXiv:2010.02174 (2020) that learning problems exist for which learners with access to quantum kernel methods have a quantum advantage over allclassical learners.
With our training and testing datasets ready, we set up the QuantumKernel class with the ZZFeatureMap, and use the BasicAer statevector_simulator to estimate the training and testing kernel matrices.
Let's calculate the transition amplitude between the first and second training data samples, one of the entries in the training kernel matrix.
First we create and draw the circuit:
The parameters in the gates are a little difficult to read, but notice how the circuit is symmetrical, with one half encoding one of the data samples, the other half encoding the other.
We then simulate the circuit. We will use the qasm_simulator since the circuit contains measurements, but increase the number of shots to reduce the effect of sampling noise.
The transition amplitude is the proportion of counts in the zero state:
This process is then repeated for each pair of training data samples to fill in the training kernel matrix, and between each training and testing data sample to fill in the testing kernel matrix. Note that each matrix is symmetric, so to reduce computation time, only half the entries are calculated explictly.
Here we compute and plot the training and testing kernel matrices:
Exercise 2: Quantum Kernel
Calculate the transition amplitute between and using the ZZFeatureMap with 4 repetitions and default data mapping function. Use the qasm_simulator with shots = 8192, seed_simulator = 1024 and seed_transpiler = 1024.
Quantum Support Vector Classification
Introduced in Havlicek et al. Nature 567, 209-212 (2019), the quantum kernel support vector classification algorithm consists of these steps:
Build the train and test quantum kernel matrices.
For each pair of datapoints in the training dataset , apply the feature map and measure the transition probability: .
For each training datapoint and testing point , apply the feature map and measure the transition probability: .
Use the train and test quantum kernel matrices in a classical support vector machine classification algorithm.
The scikit-learn svc algorithm allows us to define a custom kernel in two ways: by providing the kernel as a callable function or by precomputing the kernel matrix. We can do either of these using the QuantumKernel class in Qiskit.
The following code takes the training and testing kernel matrices we calculated earlier and provides them to the scikit-learn svc algorithm:
The following code gives the kernel as a callable function:
This is convenient, as we can easily compare the quantum kernel with various classical kernels:
Exploration: Quantum Support Vector Classification
Try running Quantum Support Vector Classification with different Qiskit feature maps and datasets.