You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The default optimizer of the QuantumKernelTrainer class is SPSA, which is a free constraint optimizer. However, if another QisKit class optimizer which supports constraints is used such as SLSQP or L_BFGS_B, the code of the mentioned class doens't support this options because of how the optimizer is called.
Notice that the following lines from the fitmethod of the QuantumKernelTrainer class, there is no bounds argument included in the call of the minimize routine.
Here is a short small example that prompts the error:
from qiskit.circuit.library import TwoLocal
from qiskit_machine_learning.kernels import TrainableFidelityQuantumKernel
from qiskit_machine_learning.kernels.algorithms import QuantumKernelTrainer
from qiskit.algorithms.optimizers import SLSQP
from qiskit.utils import algorithm_globals
import numpy as np
# Define a feature_map (8 total parameters, first 4 to encode data, last 4 to train)
ansatz = TwoLocal(
num_qubits=2,
rotation_blocks=['rz','ry'],
entanglement_blocks='cx',
reps=1)
# Random data set (4 features, 5 items)
rand = algorithm_globals.random.uniform
x_train = rand(0,2*np.pi,(5,4))
y_train = np.heaviside(rand(-1,1,5),0)
# Define a trainable kernel (last 4 parameters for training)
TFQK = TrainableFidelityQuantumKernel(
feature_map = ansatz,
training_parameters = ansatz.parameters[4:8]
)
# Define quantum kernel trainer
QKT = QuantumKernelTrainer(
quantum_kernel=TFQK,
optimizer=SLSQP(maxiter=1000,options={'bounds':[(-np.pi,np.pi)]})
# SLSQP(maxiter=1000,bounds=[(-np.pi,np.pi)]) would also prompt the error
)
# Fit the quantum kernel
# This line prompts the error
QKT.fit(x_train,y_train)
What should happen?
You would expect the fit process to run properly, since the option bounds is supported by the optimizer QisKit class.
Any suggestions?
Since bounds argument is not working, I guess that jac argument won't work. In the case of the latter, I think it would be rarely used, but I think it would be right to include it, for completeness.
With the last git version (but not the current qiskit_machine_learning release), this error can be patched by defining a custom optimziation function as follows, for example for maxiter=1000 and bounds=[(-np.pi,np.pi)], for the SLSQPoptimizer:
One alternative solution for the bounds would be to use the validate_bounds class from qiskit.utils which retrieves the bounds from the ansatz (in this case the feature_map).
I would like to work on this issue if possible, so I would like to be assigned to it!
The text was updated successfully, but these errors were encountered:
Thanks for raising this issue. I think the problem is broader rather than just in QuantumKernelTrainer. While the proposed solution closes the local problem in QKT the same issue will persist in quantum neural models as well. So, the solution must fix all similar problems, not only in QKT. There's an issue opened to discuss the problem: Qiskit/qiskit#9718.
This issue does not originate from qiskit-machine-learning, but falls within the qiskit-algorithms scope; see references to the PR above qiskit-community/qiskit-algorithms#155, which will fix it upon merge.
Environment
What is happening?
The default optimizer of the
QuantumKernelTrainer
class isSPSA
, which is a free constraint optimizer. However, if another QisKit class optimizer which supports constraints is used such asSLSQP
orL_BFGS_B
, the code of the mentioned class doens't support this options because of how the optimizer is called.Notice that the following lines from the
fit
method of theQuantumKernelTrainer
class, there is no bounds argument included in the call of the minimize routine.https://github.com/Qiskit/qiskit-machine-learning/blob/5abda6c0fd303453229f9caabcd831de792fdd75/qiskit_machine_learning/kernels/algorithms/quantum_kernel_trainer.py#LL212-L218C14
Therefore, submiting an optimizer with the bounds argument results in the following error
How can we reproduce the issue?
Here is a short small example that prompts the error:
What should happen?
You would expect the fit process to run properly, since the option bounds is supported by the optimizer QisKit class.
Any suggestions?
Since
bounds
argument is not working, I guess thatjac
argument won't work. In the case of the latter, I think it would be rarely used, but I think it would be right to include it, for completeness.With the last git version (but not the current qiskit_machine_learning release), this error can be patched by defining a custom optimziation function as follows, for example for
maxiter=1000
andbounds=[(-np.pi,np.pi)]
, for theSLSQP
optimizer:One alternative solution for the bounds would be to use the
validate_bounds
class fromqiskit.utils
which retrieves the bounds from the ansatz (in this case the feature_map).I would like to work on this issue if possible, so I would like to be assigned to it!
The text was updated successfully, but these errors were encountered: