QuantumKernelTrainer#

class QuantumKernelTrainer(quantum_kernel, loss=None, optimizer=None, initial_point=None)[исходный код]#

Базовые классы: 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
Параметры:
  • 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 underlying SVCLoss object will exhibit default behavior.

  • optimizer (Optimizer | Minimizer | None) – An instance of Optimizer or a callable to be used in training. Refer to Minimizer 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 to SPSA.

  • initial_point (Sequence[float] | None) – Initial point from which the optimizer will begin.

Исключение:

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)[исходный код]#

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.

Параметры:
  • data (numpy.ndarray) – (N, D) array of training data, where N is the number of samples and D is the feature dimension

  • labels (numpy.ndarray) – (N, 1) array of target values for the training samples

Результат:

the results of kernel training

Тип результата:

QuantumKernelTrainerResult

Исключение:

ValueError – No trainable user parameters specified in quantum kernel