QuantumKernelTrainer#
- class QuantumKernelTrainer(quantum_kernel, loss=None, optimizer=None, initial_point=None)[source]#
Bases :
object
Quantum Kernel Trainer. This class provides utility to train quantum kernel feature map parameters.
Example
# Create 2-qubit feature map qc = QuantumCircuit(2) # Vectors of input and trainable user parameters input_params = ParameterVector("x_par", 2) training_params = ParameterVector("θ_par", 2) # Create an initial rotation layer of trainable parameters for i, param in enumerate(training_params): qc.ry(param, qc.qubits[i]) # Create a rotation layer of input parameters for i, param in enumerate(input_params): qc.rz(param, qc.qubits[i]) quant_kernel = TrainableFidelityQuantumKernel( feature_map=qc, training_parameters=training_params, ) loss_func = ... optimizer = ... initial_point = ... qk_trainer = QuantumKernelTrainer( quantum_kernel=quant_kernel, loss=loss_func, optimizer=optimizer, initial_point=initial_point, ) qkt_results = qk_trainer.fit(X_train, y_train) optimized_kernel = qkt_results.quantum_kernel
- Paramètres:
quantum_kernel (TrainableKernel) – a trainable quantum kernel to be trained.
loss (str | KernelLoss | None) – A loss function available via string is « svc_loss » which is the same as
SVCLoss
. If a string is passed as the loss function, then the underlyingSVCLoss
object will exhibit default behavior.optimizer (Optimizer | Minimizer | None) – An instance of
Optimizer
or a callable to be used in training. Refer toMinimizer
for more information on the callable protocol. Since no analytical gradient is defined for kernel loss functions, gradient-based optimizers are not recommended for training kernels. When None defaults toSPSA
.initial_point (Sequence[float] | None) – Initial point from which the optimizer will begin.
- Lève:
ValueError – unknown loss function.
Attributes
- initial_point#
Return initial point
- loss#
Return the loss object.
- optimizer#
Return an optimizer to be used in training.
- quantum_kernel#
Return the quantum kernel object.
Methods
- fit(data, labels)[source]#
Train the QuantumKernel by minimizing loss over the kernel parameters. The input quantum kernel will not be altered, and an optimized quantum kernel will be returned.
- Paramètres:
data (numpy.ndarray) –
(N, D)
array of training data, whereN
is the number of samples andD
is the feature dimensionlabels (numpy.ndarray) –
(N, 1)
array of target values for the training samples
- Renvoie:
the results of kernel training
- Type renvoyé:
- Lève:
ValueError – No trainable user parameters specified in quantum kernel