Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize FidelityQuantumKernel parameterization #719

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions qiskit_machine_learning/kernels/fidelity_quantum_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down