I am working with QSVM and have been reading a few things about this. I tried QSVM from the qiskit documentation as well. I also went through the link Quantum circuit for the ZZ feature map which beautifully explains the working of ZZ feature map to a great extent. Can someone suggest to me, how can I change this kernel to radial bias kernel or any other function like x^2+y^2?
1 Answers
If you're trying to change how data is mapped into the circuit using ZZFeatureMap from Qiskit, perhaps I can help. By default, ZZFeatureMap maps the data as follows:
$$
\begin{cases}
x_i & \text{if } S =\{i\} \\
(\pi - x_i)(\pi - x_j) & \text{if } S = \{i, j\}
\end{cases}
$$
To create a custom mapping, you need to pass a custom function to the ZZFeatureMap. For example, if you want something like:
$$
\begin{cases}
\sin(x_i) & \text{if } S = \{i\} \\
\sin(\pi - x_i) \sin(\pi - x_j) & \text{if } S = \{i, j\}
\end{cases}
$$
You can implement it with the following function:
def custom_data_map_func(x):
"""Define a function map from R^n to R.
Args:
x (np.ndarray): Data array.
Returns:
float: The mapped value.
"""
coeff = functools.reduce(lambda m, n: m * n, np.sin(np.pi - x))
return coeff
For a more specific case, such as: $$ \begin{cases} x_i & \text{if } S = {i} \ \cos(\pi - x_i) \cos(\pi - x_j) & \text{if } S = {i, j} \end{cases} $$ You can use this function:
def custom_data_map_func(x):
"""Define a function map from R^n to R.
Args:
x (np.ndarray): Data array.
Returns:
float: The mapped value.
"""
coeff = x[0] if len(x) == 1 else functools.reduce(lambda m, n: m * n, np.cos(np.pi - x))
return coeff
The ZZFeatureMap is
feature_map = ZZFeatureMap(feature_dimension=num_features, reps=1, entanglement='linear', data_map_func=custom_data_map_func)
I hope this helps!
- 4,242
- 3
- 12
- 49
- 11
- 2