paddle_quantum.qml.qnnqd

The quality detection model based on quantum neural networks.

class paddle_quantum.qml.qnnqd.ImageDataset(file_path, num_samples, pca=None, scaler=None, centering=None)

Bases: Dataset

The class used for loading classical datasets.

Parameters:
  • file_path (str) – The path of the input image.

  • num_samples (int) – The number of the data in the test dataset.

  • pca (PCA) – Whether use principal component analysis. Default to None.

  • scaler (StandardScaler) – Whether scale the data. Default to None.

  • centering (MinMaxScaler) – Whether remove the mean. Default to None.

class paddle_quantum.qml.qnnqd.QNNQD(num_qubits, num_depths, observables)

Bases: Layer

The class of the quality detection using quantum neural networks (QNN).

Parameters:
  • num_qubits (List[int]) – The number of qubits of the quantum circuit in each layer.

  • num_depths (List[int]) – The depth of quantum circuit in each layer.

  • observables (List) – The observables of the quantum circuit in each layer.

forward(batch_input)

The forward function.

Parameters:

batch_input (List[Tensor]) – The input of the model. It’s shape is \((\text{batch_size}, -1)\) .

Returns:

Return the output of the model. It’s shape is \((\text{batch_size}, \text{num_classes})\) .

Return type:

Tensor

paddle_quantum.qml.qnnqd.train(model_name, num_qubits, num_depths, observables, batch_size=20, num_epochs=4, learning_rate=0.1, dataset='SurfaceCrack', saved_dir='./', using_validation=False, num_train=-1, num_val=-1, num_test=-1, early_stopping=1000, num_workers=0)

The function of training the QNNQD model.

Parameters:
  • model_name (str) – The name of the model, which is used to save the model.

  • num_qubits (List) – The number of qubits of the quantum circuit in each layer.

  • num_depths (List[int]) – The depth of quantum circuit in each layer.

  • observables (List) – The observables of the quantum circuit in each layer.

  • batch_size (int) – The size of the batch samplers. Default to 20 .

  • num_epochs (int) – The number of epochs to train the model. Default to 4 .

  • learning_rate (float) – The learning rate used to update the parameters. Default to 0.1.

  • dataset (str) – The path of the dataset. It defaults to SurfaceCrack.

  • saved_dir (str) – The path used to save logs. Default to ./.

  • using_validation (bool) – Whether use the validation. It is false means the dataset only contains training datasets and test datasets.

  • num_train (int) – The number of the data in the training dataset. Default to -1 , which will use all training data.

  • num_val (int) – The number of the data in the validation dataset. Default to -1 , which will use all validation data.

  • num_test (int) – The number of the data in the test dataset. Default to -1 , which will use all test data.

  • early_stopping (int | None) – Number of the iterations with no improvement after which training will be stopped. Defulat to 1000.

  • num_workers (int | None) – The number of subprocess to load data, 0 for no subprocess used and loading data in main process. Default to 0.

paddle_quantum.qml.qnnqd.evaluate(model, data_loader)

Evaluating the performance of the model on the dataset.

Parameters:
  • model (Layer) – The QNN model.

  • data_loader (DataLoader) – The data loader for the data.

Returns:

Return the accuracy of the model on the given datasets.

Return type:

Tuple[float, float]

paddle_quantum.qml.qnnqd.test(model, saved_dir, model_name, test_loader)

Evaluating the performance of the model on the test dataset.

Parameters:
  • model (Layer) – QNN model.

  • saved_dir (str) – The path of the saved model.

  • model_name (str) – The name of the model.

  • test_loader (DataLoader) – The data loader for testing datasets.

paddle_quantum.qml.qnnqd.inference(image_path, num_samples, model_path, num_qubits, num_depths, observables)

The prediction function for the provided test dataset.

Parameters:
  • image_path (str) – The path of the input image.

  • num_samples (int) – The number of the data need to be classified.

  • model_path (str) – The path of the trained model, which will be loaded.

  • num_qubits (List) – The number of qubits of the quantum circuit in each layer.

  • num_depths (List) – The depth of quantum circuit in each layer.

  • observables (List) – The observables of the quantum circuit in each layer.

Returns:

Return the prediction of the given datasets together with the associated level of certainty.

Return type:

float | list