Korean
언어
English
Japanese
German
Korean
Shortcuts

qiskit.aqua.algorithms.QSVM

class QSVM(feature_map, training_dataset=None, test_dataset=None, datapoints=None, multiclass_extension=None, lambda2=0.001, quantum_instance=None)[소스]

Quantum SVM algorithm.

A key concept in classification methods is that of a kernel. Data cannot typically be separated by a hyperplane in its original space. A common technique used to find such a hyperplane consists on applying a non-linear transformation function to the data. This function is called a feature map, as it transforms the raw features, or measurable properties, of the phenomenon or subject under study. Classifying in this new feature space – and, as a matter of fact, also in any other space, including the raw original one – is nothing more than seeing how close data points are to each other. This is the same as computing the inner product for each pair of data in the set. In fact we do not need to compute the non-linear feature map for each datum, but only the inner product of each pair of data points in the new feature space. This collection of inner products is called the kernel and it is perfectly possible to have feature maps that are hard to compute but whose kernels are not.

The QSVM algorithm applies to classification problems that require a feature map for which computing the kernel is not efficient classically. This means that the required computational resources are expected to scale exponentially with the size of the problem. QSVM uses a Quantum processor to solve this problem by a direct estimation of the kernel in the feature space. The method used falls in the category of what is called supervised learning, consisting of a training phase (where the kernel is calculated and the support vectors obtained) and a test or classification phase (where new data without labels is classified according to the solution found in the training phase).

Internally, QSVM will run the binary classification or multiclass classification based on how many classes the data has. If the data has more than 2 classes then a multiclass_extension is required to be supplied. Aqua provides several multiclass_extensions.

See also https://arxiv.org/abs/1804.11326

매개변수
  • feature_map (Union[QuantumCircuit, FeatureMap]) – Feature map module, used to transform data

  • training_dataset (Optional[Dict[str, ndarray]]) – Training dataset.

  • test_dataset (Optional[Dict[str, ndarray]]) – Testing dataset.

  • datapoints (Optional[ndarray]) – Prediction dataset.

  • multiclass_extension (Optional[MulticlassExtension]) – If number of classes is greater than 2 then a multiclass scheme must be supplied, in the form of a multiclass extension.

  • lambda2 (float) – L2 norm regularization factor

  • quantum_instance (Union[QuantumInstance, Backend, BaseBackend, None]) – Quantum Instance or Backend

예외

AquaError – Multiclass extension not supplied when number of classes > 2

__init__(feature_map, training_dataset=None, test_dataset=None, datapoints=None, multiclass_extension=None, lambda2=0.001, quantum_instance=None)[소스]
매개변수
  • feature_map (Union[QuantumCircuit, FeatureMap]) – Feature map module, used to transform data

  • training_dataset (Optional[Dict[str, ndarray]]) – Training dataset.

  • test_dataset (Optional[Dict[str, ndarray]]) – Testing dataset.

  • datapoints (Optional[ndarray]) – Prediction dataset.

  • multiclass_extension (Optional[MulticlassExtension]) – If number of classes is greater than 2 then a multiclass scheme must be supplied, in the form of a multiclass extension.

  • lambda2 (float) – L2 norm regularization factor

  • quantum_instance (Union[QuantumInstance, Backend, BaseBackend, None]) – Quantum Instance or Backend

예외

AquaError – Multiclass extension not supplied when number of classes > 2

Methods

__init__(feature_map[, training_dataset, …])

type feature_map

Union[QuantumCircuit, FeatureMap]

construct_circuit(x1, x2[, measurement])

Generate inner product of x1 and x2 with the given feature map.

construct_kernel_matrix(x1_vec[, x2_vec, …])

Construct kernel matrix, if x2_vec is None, self-innerproduct is conducted.

get_kernel_matrix(quantum_instance, …[, …])

Construct kernel matrix, if x2_vec is None, self-innerproduct is conducted.

load_model(file_path)

Load a model from a file path.

predict(data[, quantum_instance])

Predict using the svm.

run([quantum_instance])

Execute the algorithm with selected backend.

save_model(file_path)

Save the model to a file path.

set_backend(backend, **kwargs)

Sets backend with configuration.

setup_datapoint(datapoints)

Setup data points, if the data were there, they would be overwritten.

setup_test_data(test_dataset)

Setup test data, if the data were there, they would be overwritten.

setup_training_data(training_dataset)

Setup training data, if the data were there, they would be overwritten.

test(data, labels[, quantum_instance])

Test the svm.

train(data, labels[, quantum_instance])

Train the svm.

Attributes

BATCH_SIZE

backend

Returns backend.

quantum_instance

Returns quantum instance.

random

Return a numpy random.

ret

returns result

property backend

Returns backend.

반환 형식

Union[Backend, BaseBackend]

construct_circuit(x1, x2, measurement=False)[소스]

Generate inner product of x1 and x2 with the given feature map.

The dimension of x1 and x2 must be the same.

매개변수
  • x1 (numpy.ndarray) – data points, 1-D array, dimension is D

  • x2 (numpy.ndarray) – data points, 1-D array, dimension is D

  • measurement (bool) – add measurement gates at the end

반환값

constructed circuit

반환 형식

QuantumCircuit

construct_kernel_matrix(x1_vec, x2_vec=None, quantum_instance=None)[소스]

Construct kernel matrix, if x2_vec is None, self-innerproduct is conducted.

참고

When using statevector_simulator, we only build the circuits for Psi(x1)|0> rather than Psi(x2)^dagger Psi(x1)|0>, and then we perform the inner product classically. That is, for statevector_simulator, the total number of circuits will be O(N) rather than O(N^2) for qasm_simulator.

매개변수
  • x1_vec (numpy.ndarray) – data points, 2-D array, N1xD, where N1 is the number of data, D is the feature dimension

  • x2_vec (numpy.ndarray) – data points, 2-D array, N2xD, where N2 is the number of data, D is the feature dimension

  • quantum_instance (QuantumInstance) – quantum backend with all settings

반환값

2-D matrix, N1xN2

반환 형식

numpy.ndarray

예외

AquaError – Quantum instance is not present.

static get_kernel_matrix(quantum_instance, feature_map, x1_vec, x2_vec=None, enforce_psd=True)[소스]

Construct kernel matrix, if x2_vec is None, self-innerproduct is conducted.

참고

When using statevector_simulator, we only build the circuits for Psi(x1)|0> rather than Psi(x2)^dagger Psi(x1)|0>, and then we perform the inner product classically. That is, for statevector_simulator, the total number of circuits will be O(N) rather than O(N^2) for qasm_simulator.

매개변수
  • quantum_instance (QuantumInstance) – quantum backend with all settings

  • feature_map (FeatureMap) – a feature map that maps data to feature space

  • x1_vec (numpy.ndarray) – data points, 2-D array, N1xD, where N1 is the number of data, D is the feature dimension

  • x2_vec (numpy.ndarray) – data points, 2-D array, N2xD, where N2 is the number of data, D is the feature dimension

  • enforce_psd (bool) – enforces that the kernel matrix is positive semi-definite by setting negative eigenvalues to zero. This is only applied in the symmetric case, i.e., if x2_vec == None.

반환값

2-D matrix, N1xN2

반환 형식

numpy.ndarray

load_model(file_path)[소스]

Load a model from a file path.

매개변수

file_path (str) – the path of the saved model.

predict(data, quantum_instance=None)[소스]

Predict using the svm.

매개변수
  • data (numpy.ndarray) – NxD array, where N is the number of data, D is the feature dimension.

  • quantum_instance (QuantumInstance) – quantum backend with all setting

반환값

predicted labels, Nx1 array

반환 형식

numpy.ndarray

예외

AquaError – Quantum instance is not present.

property quantum_instance

Returns quantum instance.

반환 형식

Optional[QuantumInstance]

property random

Return a numpy random.

property ret

returns result

run(quantum_instance=None, **kwargs)

Execute the algorithm with selected backend.

매개변수
  • quantum_instance (Union[QuantumInstance, Backend, BaseBackend, None]) – the experimental setting.

  • kwargs (dict) – kwargs

반환값

results of an algorithm.

반환 형식

dict

예외

AquaError – If a quantum instance or backend has not been provided

save_model(file_path)[소스]

Save the model to a file path.

매개변수

file_path (str) – a path to save the model.

set_backend(backend, **kwargs)

Sets backend with configuration.

반환 형식

None

setup_datapoint(datapoints)[소스]

Setup data points, if the data were there, they would be overwritten.

매개변수

datapoints (numpy.ndarray) – prediction dataset.

setup_test_data(test_dataset)[소스]

Setup test data, if the data were there, they would be overwritten.

매개변수

test_dataset (dict) – test dataset.

setup_training_data(training_dataset)[소스]

Setup training data, if the data were there, they would be overwritten.

매개변수

training_dataset (dict) – training dataset.

test(data, labels, quantum_instance=None)[소스]

Test the svm.

매개변수
  • data (numpy.ndarray) – NxD array, where N is the number of data, D is the feature dimension.

  • labels (numpy.ndarray) – Nx1 array, where N is the number of data

  • quantum_instance (QuantumInstance) – quantum backend with all setting

반환값

accuracy

반환 형식

float

예외

AquaError – Quantum instance is not present.

train(data, labels, quantum_instance=None)[소스]

Train the svm.

매개변수
  • data (numpy.ndarray) – NxD array, where N is the number of data, D is the feature dimension.

  • labels (numpy.ndarray) – Nx1 array, where N is the number of data

  • quantum_instance (QuantumInstance) – quantum backend with all setting

예외

AquaError – Quantum instance is not present.