0
$\begingroup$

I am trying to implement Quantum Kernel Ridge Regression (replacing the classical kernel with quantum kernel) in qiskit. The shape of my input x is (7165 x 529) and due to reshaping errors, I added an extra column with zeroes to make it (7165 x 530) and shape of y is (7165,).

I was able to implement the classical KRR but when I am trying to replace the kernel with the quantum counterpart, I am getting the mentioned error.

x and y:

2
(7165, 530)
[[36.858105   2.9076326  2.907612  ...  0.         0.         0.       ]
 [36.858105  12.599944   2.9019997 ...  0.         0.         0.       ]
 [36.858105  14.261827   1.503703  ...  0.         0.         0.       ]
 ...
 [36.858105   8.569991  13.293801  ...  0.         0.         0.       ]
 [36.858105  12.540301   8.026131  ...  0.         0.         0.       ]
 [36.858105  12.629306  12.609996  ...  0.         0.         0.       ]]
1
(7165,)
[ -417.96  -712.42  -564.21 ... -1662.1  -1782.01 -1919.  ]

Code:

data_feature_map = ZZFeatureMap(feature_dimension= 2, reps=2, entanglement="linear")
data_backend = QuantumInstance(
    BasicAer.get_backend("qasm_simulator"), shots=1024, seed_simulator=seed, seed_transpiler=seed
)

data_kernel = QuantumKernel(feature_map=data_feature_map, quantum_instance=data_backend)


quantum_kernel = KernelRidge(kernel=data_kernel.evaluate)
quantum_kernel.fit(x_train, y_train)

predictions = quantum_kernel.predict(x_test)

score = quantum_kernel.score(x_test,y_test)
 
rmse = math.sqrt(mean_squared_error(y_test, predictions))
print('Kernel Ridge Regression Score: %.1f' % score)
print('############################')
print('Root Mean Squared Error: %.1f' % rmse)

Error:

TypeError                                 Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Input In [74], in <cell line: 2>()
      1 quantum_kernel = KernelRidge(kernel=data_kernel.evaluate)
----> 2 quantum_kernel.fit(x_train, y_train)
      4 predictions = quantum_kernel.predict(x_test)
      6 score = quantum_kernel.score(x_test,y_test)

File /opt/conda/lib/python3.8/site-packages/sklearn/kernel_ridge.py:197, in KernelRidge.fit(self, X, y, sample_weight)
    194 if sample_weight is not None and not isinstance(sample_weight, float):
    195     sample_weight = _check_sample_weight(sample_weight, X)
--> 197 K = self._get_kernel(X)
    198 alpha = np.atleast_1d(self.alpha)
    200 ravel = False

File /opt/conda/lib/python3.8/site-packages/sklearn/kernel_ridge.py:155, in KernelRidge._get_kernel(self, X, Y)
    153 else:
    154     params = {"gamma": self.gamma, "degree": self.degree, "coef0": self.coef0}
--> 155 return pairwise_kernels(X, Y, metric=self.kernel, filter_params=True, **params)

File /opt/conda/lib/python3.8/site-packages/sklearn/metrics/pairwise.py:2053, in pairwise_kernels(X, Y, metric, filter_params, n_jobs, **kwds)
   2050 else:
   2051     raise ValueError("Unknown kernel %r" % metric)
-> 2053 return _parallel_pairwise(X, Y, func, n_jobs, **kwds)

File /opt/conda/lib/python3.8/site-packages/sklearn/metrics/pairwise.py:1430, in _parallel_pairwise(X, Y, func, n_jobs, **kwds)
   1427 X, Y, dtype = _return_float_dtype(X, Y)
   1429 if effective_n_jobs(n_jobs) == 1:
-> 1430     return func(X, Y, **kwds)
   1432 # enforce a threading backend to prevent data communication overhead
   1433 fd = delayed(_dist_wrapper)

File /opt/conda/lib/python3.8/site-packages/sklearn/metrics/pairwise.py:1457, in _pairwise_callable(X, Y, metric, force_all_finite, **kwds)
   1455 iterator = itertools.combinations(range(X.shape[0]), 2)
   1456 for i, j in iterator:
-> 1457     out[i, j] = metric(X[i], Y[j], **kwds)
   1459 # Make symmetric
   1460 # NB: out += out.T will produce incorrect results
   1461 out = out + out.T

ValueError: setting an array element with a sequence.

I am aware that this is caused because the callable kernel takes two rows as input and gives a scalar value as the kernel but I am not sure how to implement that on Quantum Kernel.

I did implement a precomputed kernel without any error but it takes very long for it to calculate the results.

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.