paddle_quantum.dataset

The source file of the dataset.

class paddle_quantum.dataset.Dataset

Bases: object

Base class for all datasets, integrating multiple quantum encoding methods.

data2circuit(classical_data, encoding, num_qubits, can_describe_dimension, split_circuit, return_state, is_image=False)

Encode the input classical data into quantum states using encoding, where the classical data is truncated or filled with zero.

Parameters:
  • classical_data (list) – vectors needed to encode, which have been already truncated or filled with zero to the length can_describe_dimension For example, amplitude encoding can describe 2 ** n dimension vectors.

  • encoding (str) – The encoding method.

  • num_qubits (int) – The number of qubits.

  • can_describe_dimension (int) – The dimension which the circuit can describe by encoding.

  • split_circuit (bool) – Whether to split the circuit.

  • return_state (bool) – Whether to return quantum state.

  • is_image (bool | None) – Whether it is a picture, if it is a picture, the normalization method is not quite the same. Defaults to False.

Raises:

Exception – Not support to return circuit in amplitude encoding.

Returns:

If return_state == True, return encoded quantum state, otherwise return encoding circuits.

Return type:

list

filter_class(x, y, classes, data_num, need_relabel, seed=0)

Select data_num samples from x , y, whose label is in classes.

Parameters:
  • x (list | ndarray) – Training features.

  • y (list | ndarray) – Training labels.

  • classes (list) – Classes needed to select.

  • data_num (int) – The number of data needed to select.

  • need_relabel (bool) – Whether we need to relabel the labels to 0,1,2 for binary classification. For example [1,2] will be relabeled to [0,1].

  • seed (int | None) – Random seed. Defaults to 0.

Returns:

contains elements

  • new_x: selected features.

  • new_y: selected labels corresponded to new_x.

Return type:

Tuple[list]

class paddle_quantum.dataset.VisionDataset(figure_size)

Bases: Dataset

VisionDataset is the base class of all image datasets. By inheriting VisionDataset, users can easily generate their own quantum data.

Parameters:

figure_size (int) – The size of the figure.

encode(feature, encoding, num_qubits, split_circuit=False, downscaling_method='resize', target_dimension=-1, return_state=True, full_return=False)

Encode feature into num_qubits qubits using encoding after downscaling to target_dimension. feature is one-dimension image vectors.

Parameters:
  • feature (list | ndarray) – One-dimension image vectors which can be list or ndarray.

  • encoding (str) – angle_encoding denotes angle encoding, and one qubit encodes one number with a Ry gate. amplitude_encoding denotes amplitude encoding; pauli_rotation_encoding denotes using SU(3) rotation gate. linear_entanglement_encoding, real_entanglement_encoding , complex_entanglement_encoding and IQP_encoding encoding methods.

  • num_qubits (int) – Qubit number.

  • split_circuit (bool | None) – Whether to split the circuits. If true, every layer of the encoding circuit will be split into a list. Defaults to False.

  • downscaling_method (str | None) – Including PCA and resize. Defaults to resize.

  • target_dimension (int | None) – The dimension after downscaling. target_dimension is not allowed to surpass the figure size. Defaults to -1.

  • return_state (bool | None) – Whether to return quantum states. If it is False, return quantum circuits. Defaults to True.

  • full_return (bool | None) – Whether to return quantum_image_states, quantum_image_circuits, original_images and classical_image_vectors. Defaults to False.

Raises:
  • Exception – PCA dimension should be less than figure size.

  • Exception – Resize dimension should be a square.

  • Exception – Downscaling methods can only be resize and PCA.

  • Exception – Invalid encoding methods

Returns:

contain these elements

  • quantum_image_states: Quantum states, only full_return==True or return_state==True will return.

  • quantum_image_circuits: A list of circuits generating quantum states, only full_return==True or return_state==True will return.

  • original_images: One-dimension original vectors without any processing, only return_state==True will return.

  • classical_image_vectors: One-dimension original vectors after filling with zero, which are encoded to quantum states. only return_state==True will return.

Return type:

Tuple[Tensor, list, ndarray, ndarray]

class paddle_quantum.dataset.SimpleDataset(dimension)

Bases: Dataset

For simple dataset that does not require dimension reduction. You can inherit SimpleDataset to generate quantum states from your classical datasets.

Parameters:

dimension (int) – Dimension of encoding data.

encode(feature, encoding, num_qubits, return_state=True, full_return=False)

Encode feature with num_qubits qubits by encoding.

Parameters:
  • feature (list | ndarray) – Features needed to encode.

  • encoding (str) – Encoding methods.

  • num_qubits (int) – Qubit number.

  • return_state (bool | None) – Whether to return quantum states. Defaults to True.

  • full_return (bool | None) – Whether to return quantum_states, quantum_circuits, origin_feature and feature. Defaults to False.

Raises:
  • Exception – Invalid type of feature.

  • Exception – Invalid encoding methods.

  • Exception – The qubit number is not enough to encode the features.

Returns:

contain these element

  • quantum_states: Quantum states, only full_return==True or return_state==True will return;

  • quantum_circuits: A list of circuits generating quantum states, only full_return==True or return_state==True will return;

  • origin_feature: One-dimension original vectors without any processing, only return_state==True will return

  • feature: One-dimension original vectors after filling with zero, which are encoded to quantum states. only return_state==True will return.

Return type:

Tuple[ndarray, list, ndarray, ndarray]

class paddle_quantum.dataset.MNIST(mode, encoding, num_qubits, classes, data_num=-1, split_circuit=False, downscaling_method='resize', target_dimension=-1, need_cropping=True, need_relabel=True, return_state=True, seed=0)

Bases: VisionDataset

MNIST quantum dataset. It inherits VisionDataset.

Parameters:
  • mode (str) – Data mode including train and test.

  • encoding (str) – angle_encoding denotes angle encoding, and one qubit encodes one number with a Ry gate. amplitude_encoding denotes amplitude encoding; pauli_rotation_encoding denotes using SU(3) rotation gate. linear_entanglement_encoding, real_entanglement_encoding , complex_entanglement_encoding and IQP_encoding encoding methods.

  • num_qubits (int) – Qubit number.

  • classes (list) – Classes needed to classify, categories are indicated by numeric labels.

  • data_num (int | None) – Data number returned. Defaults to -1.

  • split_circuit (bool | None) – Whether to split the circuits. If True, every layer of the encoding circuit will be split into a list. Defaults to False.

  • downscaling_method (str | None) – Including PCA and resize. Defaults to resize.

  • target_dimension (int | None) – The dimension after downscaling, which is not allowed to surpass the figure size. Defaults to -1.

  • need_cropping (bool | None) – Whether needed to crop, If True, image[0:27][0:27] will be cropped to image[4:24][4:24]. Defaults to True.

  • need_relabel (bool | None) – Whether we need to relabel the labels to 0,1,2… for binary classification.For example [1,2] will be relabeled to [0,1] Defaults to True.

  • return_state (bool | None) – Whether to return quantum states. Defaults to True.

  • seed (int | None) – Select random seed. Defaults to 0.

Raises:

Exception – Data mode can only be train and test.

class paddle_quantum.dataset.FashionMNIST(mode, encoding, num_qubits, classes, data_num=-1, split_circuit=False, downscaling_method='resize', target_dimension=-1, need_relabel=True, return_state=True, seed=0)

Bases: VisionDataset

FashionMNIST quantum dataset. It inherits VisionDataset.

Parameters:
  • mode (str) – Data mode including train and test.

  • encoding (str) – angle_encoding denotes angle encoding, and one qubit encodes one number with a Ry gate. amplitude_encoding denotes amplitude encoding; pauli_rotation_encoding denotes using SU(3) rotation gate. linear_entanglement_encoding, real_entanglement_encoding , complex_entanglement_encoding and IQP_encoding encoding methods.

  • num_qubits (int) – Qubit number.

  • classes (list) – Classes needed to classify, categories are indicated by numeric labels.

  • data_num (int | None) – Data number returned. Defaults to -1.

  • split_circuit (bool | None) – Whether to split the circuits. If True, every layer of the encoding circuit will be split into a list. Defaults to False.

  • downscaling_method (str | None) – Including PCA and resize. Defaults to resize.

  • target_dimension (int | None) – The dimension after downscaling, which is not allowed to surpass the figure size. Defaults to -1.

  • need_cropping – Whether needed to crop, If True, image[0:27][0:27] will be cropped to image[4:24][4:24]. Defaults to True.

  • need_relabel (bool | None) – Whether we need to relabel the labels to 0,1,2… for binary classification.For example [1,2] will be relabeled to [0,1] Defaults to True.

  • return_state (bool | None) – Whether to return quantum states. Defaults to True.

  • seed (int | None) – Select random seed. Defaults to 0.

Raises:

Exception – Data mode can only be train and test.

class paddle_quantum.dataset.Iris(encoding, num_qubits, classes, test_rate=0.2, need_relabel=True, return_state=True, seed=0)

Bases: SimpleDataset

Iris dataset

Parameters:
  • encoding (str) – angle_encoding denotes angle encoding, and one qubit encodes one number with a Ry gate. amplitude_encoding denotes amplitude encoding; pauli_rotation_encoding denotes using SU(3) rotation gate. linear_entanglement_encoding, real_entanglement_encoding , complex_entanglement_encoding and IQP_encoding encoding methods.

  • num_qubits (int) – Qubit number.

  • classes (list) – Classes needed to classify, categories are indicated by numeric labels.

  • test_rate (float | None) – The proportion of the testing dataset. Defaults to 0.2.

  • need_relabel (bool | None) – Whether we need to relabel the labels to 0,1,2… for binary classification.For example [1,2] will be relabeled to [0,1]. Defaults to True.

  • return_state (bool | None) – Whether to return quantum states. Defaults to True.

  • seed (int | None) – Select random seed. Defaults to 0.

class paddle_quantum.dataset.BreastCancer(encoding, num_qubits, test_rate=0.2, return_state=True, seed=0)

Bases: SimpleDataset

BreastCancer quantum dataset.

Parameters:
  • encoding (str) – angle_encoding denotes angle encoding, and one qubit encodes one number with a Ry gate. amplitude_encoding denotes amplitude encoding; pauli_rotation_encoding denotes using SU(3) rotation gate. linear_entanglement_encoding, real_entanglement_encoding , complex_entanglement_encoding and IQP_encoding encoding methods.

  • num_qubits (int) – Qubit number.

  • test_rate (float | None) – The proportion of the testing dataset. Defaults to 0.2.

  • return_state (bool | None) – Whether to return quantum states. Defaults to True.

  • seed (int | None) – Select random seed. Defaults to 0.