paddle_quantum.qml.qnnmic

The medical image classification model based on quantum neural networks.

class paddle_quantum.qml.qnnmic.ImageDataset(file_path, num_samples, task, 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.

  • task (str) – The training, validation, or testing task.

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

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

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

Raises:

ValueError – If the task is not training, validation, or test, raises the error.

class paddle_quantum.qml.qnnmic.QNNMIC(num_qubits, num_depths, observables)

Bases: Layer

The class of the medical image classification using quantum neural networks (QNN).

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

  • num_depths (List[int]) – The number of depths of quantum circuit in each layer. Defaults to [2, 2]

  • observables (List) – The observables of quantum circuit in each layer. Defaults to [['Z0','Z1','Z2','Z3'], ['X0','X1','X2','X3']].

forward(batch_input)

The forward function.

Parameters:

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

Returns:

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

Return type:

Tensor

paddle_quantum.qml.qnnmic.train(model_name, num_qubits, num_depths, observables, batch_size=20, num_epochs=4, learning_rate=0.1, dataset='pneumoniamnist', 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 QNNMIC 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 quantum circuit in each layer.

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

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

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

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

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

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

  • saved_dir (str) – The path used to save logs. Defaults 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. Defaults to -1 , which will use all training data.

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

  • num_test (int) – The number of the data in the test dataset. Defaults 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. Defaults to 1000.

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

paddle_quantum.qml.qnnmic.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.qnnmic.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.qnnmic.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 quantum circuit in each layer.

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

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

Returns:

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

Return type:

float | list