From ba89cc436310adf0817f602c8d48dd79fe126f67 Mon Sep 17 00:00:00 2001 From: Moritz <44642314+MoritzWillmann@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:51:03 +0100 Subject: [PATCH] optimize `FidelityQuantumKernel` parameterization (#719) * optimize fqk parameterization * Update qiskit_machine_learning/kernels/fidelity_quantum_kernel.py Co-authored-by: Anton Dekusar <62334182+adekusar-drl@users.noreply.github.com> * Update qiskit_machine_learning/kernels/fidelity_quantum_kernel.py * remove vstack --------- Co-authored-by: Anton Dekusar <62334182+adekusar-drl@users.noreply.github.com> --- .../kernels/fidelity_quantum_kernel.py | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/qiskit_machine_learning/kernels/fidelity_quantum_kernel.py b/qiskit_machine_learning/kernels/fidelity_quantum_kernel.py index ddca88936..24a373b09 100644 --- a/qiskit_machine_learning/kernels/fidelity_quantum_kernel.py +++ b/qiskit_machine_learning/kernels/fidelity_quantum_kernel.py @@ -127,17 +127,20 @@ def _get_parameterization( left_parameters = np.zeros((0, num_features)) right_parameters = np.zeros((0, num_features)) - indices = [] - for i, x_i in enumerate(x_vec): - for j, y_j in enumerate(y_vec): - if self._is_trivial(i, j, x_i, y_j, False): - continue + indices = np.asarray( + [ + (i, j) + for i, x_i in enumerate(x_vec) + for j, y_j in enumerate(y_vec) + if not self._is_trivial(i, j, x_i, y_j, False) + ] + ) - left_parameters = np.vstack((left_parameters, x_i)) - right_parameters = np.vstack((right_parameters, y_j)) - indices.append((i, j)) + if indices.size > 0: + left_parameters = x_vec[indices[:, 0]] + right_parameters = y_vec[indices[:, 1]] - return left_parameters, right_parameters, indices + return left_parameters, right_parameters, indices.tolist() def _get_symmetric_parameterization( self, x_vec: np.ndarray @@ -149,17 +152,20 @@ def _get_symmetric_parameterization( left_parameters = np.zeros((0, num_features)) right_parameters = np.zeros((0, num_features)) - indices = [] - for i, x_i in enumerate(x_vec): - for j, x_j in enumerate(x_vec[i:]): - if self._is_trivial(i, i + j, x_i, x_j, True): - continue + indices = np.asarray( + [ + (i, i + j) + for i, x_i in enumerate(x_vec) + for j, x_j in enumerate(x_vec[i:]) + if not self._is_trivial(i, i + j, x_i, x_j, True) + ] + ) - left_parameters = np.vstack((left_parameters, x_i)) - right_parameters = np.vstack((right_parameters, x_j)) - indices.append((i, i + j)) + if indices.size > 0: + left_parameters = x_vec[indices[:, 0]] + right_parameters = x_vec[indices[:, 1]] - return left_parameters, right_parameters, indices + return left_parameters, right_parameters, indices.tolist() def _get_kernel_matrix( self,