qiskit.aqua.algorithms.VQC
class VQC(optimizer, feature_map, var_form, training_dataset, test_dataset=None, datapoints=None, max_evals_grouped=1, minibatch_size=- 1, callback=None, use_sigmoid_cross_entropy=False, quantum_instance=None)
The Variational Quantum Classifier algorithm.
Similar to QSVM
, the VQC algorithm also applies to classification problems. VQC uses the variational method to solve such problems in a quantum processor. Specifically, it optimizes a parameterized quantum circuit to provide a solution that cleanly separates the data.
The VQC stores the parameters of var_form and feature_map sorted by name to map the values provided by the optimizer to the circuit. This is done to ensure reproducible results, for example such that running the optimization twice with same random seeds yields the same result.
Parameters
- optimizer (
Optimizer
) – The classical optimizer to use. - feature_map (
Union
[QuantumCircuit
,FeatureMap
]) – The FeatureMap instance to use. - var_form (
Union
[QuantumCircuit
,VariationalForm
]) – The variational form instance. - training_dataset (
Dict
[str
,ndarray
]) – The training dataset, in the format {‘A’: np.ndarray, ‘B’: np.ndarray, …}. - test_dataset (
Optional
[Dict
[str
,ndarray
]]) – The test dataset, in same format as training_dataset. - datapoints (
Optional
[ndarray
]) – NxD array, N is the number of data and D is data dimension. - max_evals_grouped (
int
) – The maximum number of evaluations to perform simultaneously. - minibatch_size (
int
) – The size of a mini-batch. - callback (
Optional
[Callable
[[int
,ndarray
,float
,int
],None
]]) – a callback that can access the intermediate data during the optimization. Four parameter values are passed to the callback as follows during each evaluation. These are: the evaluation count, parameters of the variational form, the evaluated value, the index of data batch. - use_sigmoid_cross_entropy (
bool
) – whether to use sigmoid cross entropy or not. - quantum_instance (
Union
[QuantumInstance
,Backend
,BaseBackend
,None
]) – Quantum Instance or Backend
We use label to denote numeric results and class the class names (str).
Raises
AquaError – Missing feature map or missing training dataset.
__init__
__init__(optimizer, feature_map, var_form, training_dataset, test_dataset=None, datapoints=None, max_evals_grouped=1, minibatch_size=- 1, callback=None, use_sigmoid_cross_entropy=False, quantum_instance=None)
Parameters
- optimizer (
Optimizer
) – The classical optimizer to use. - feature_map (
Union
[QuantumCircuit
,FeatureMap
]) – The FeatureMap instance to use. - var_form (
Union
[QuantumCircuit
,VariationalForm
]) – The variational form instance. - training_dataset (
Dict
[str
,ndarray
]) – The training dataset, in the format {‘A’: np.ndarray, ‘B’: np.ndarray, …}. - test_dataset (
Optional
[Dict
[str
,ndarray
]]) – The test dataset, in same format as training_dataset. - datapoints (
Optional
[ndarray
]) – NxD array, N is the number of data and D is data dimension. - max_evals_grouped (
int
) – The maximum number of evaluations to perform simultaneously. - minibatch_size (
int
) – The size of a mini-batch. - callback (
Optional
[Callable
[[int
,ndarray
,float
,int
],None
]]) – a callback that can access the intermediate data during the optimization. Four parameter values are passed to the callback as follows during each evaluation. These are: the evaluation count, parameters of the variational form, the evaluated value, the index of data batch. - use_sigmoid_cross_entropy (
bool
) – whether to use sigmoid cross entropy or not. - quantum_instance (
Union
[QuantumInstance
,Backend
,BaseBackend
,None
]) – Quantum Instance or Backend
We use label to denote numeric results and class the class names (str).
Raises
AquaError – Missing feature map or missing training dataset.
Methods
__init__ (optimizer, feature_map, var_form, …) | type optimizerOptimizer |
batch_data (data[, labels, minibatch_size]) | batch data |
cleanup_parameterized_circuits () | set parameterized circuits to None |
construct_circuit (x, theta[, measurement]) | Construct circuit based on data and parameters in variational form. |
find_minimum ([initial_point, var_form, …]) | Optimize to find the minimum cost value. |
get_optimal_circuit () | get optimal circuit |
get_optimal_cost () | get optimal cost |
get_optimal_vector () | get optimal vector |
get_prob_vector_for_params (…[, …]) | Helper function to get probability vectors for a set of params |
get_probabilities_for_counts (counts) | get probabilities for counts |
is_gradient_really_supported () | returns is gradient really supported |
load_model (file_path) | load model |
predict (data[, quantum_instance, …]) | Predict the labels for the data. |
run ([quantum_instance]) | Execute the algorithm with selected backend. |
save_model (file_path) | save model |
set_backend (backend, **kwargs) | Sets backend with configuration. |
test (data, labels[, quantum_instance, …]) | Predict the labels for the data, and test against with ground truth labels. |
train (data, labels[, quantum_instance, …]) | Train the models, and save results. |
Attributes
backend | Returns backend. |
class_to_label | returns class to label |
datapoints | return data points |
feature_map | Return the feature map. |
initial_point | Returns initial point |
label_to_class | returns label to class |
optimal_params | returns optimal parameters |
optimizer | Returns optimizer |
quantum_instance | Returns quantum instance. |
random | Return a numpy random. |
ret | returns result |
test_dataset | returns test dataset |
training_dataset | returns training dataset |
var_form | Returns variational form |
backend
Returns backend.
Return type
Union
[Backend
, BaseBackend
]
batch_data
batch_data(data, labels=None, minibatch_size=- 1)
batch data
class_to_label
returns class to label
cleanup_parameterized_circuits
cleanup_parameterized_circuits()
set parameterized circuits to None
construct_circuit
construct_circuit(x, theta, measurement=False)
Construct circuit based on data and parameters in variational form.
Parameters
- x (numpy.ndarray) – 1-D array with D dimension
- theta (list[numpy.ndarray]) – list of 1-D array, parameters sets for variational form
- measurement (bool) – flag to add measurement
Returns
the circuit
Return type
Raises
AquaError – If x
and theta
share parameters with the same name.
datapoints
return data points
feature_map
Return the feature map.
Return type
Union
[FeatureMap
, QuantumCircuit
, None
]
find_minimum
find_minimum(initial_point=None, var_form=None, cost_fn=None, optimizer=None, gradient_fn=None)
Optimize to find the minimum cost value.
Parameters
- initial_point (
Optional
[ndarray
]) – If not None will be used instead of any initial point supplied via constructor. If None and None was supplied to constructor then a random point will be used if the optimizer requires an initial point. - var_form (
Union
[QuantumCircuit
,VariationalForm
,None
]) – If not None will be used instead of any variational form supplied via constructor. - cost_fn (
Optional
[Callable
]) – If not None will be used instead of any cost_fn supplied via constructor. - optimizer (
Optional
[Optimizer
]) – If not None will be used instead of any optimizer supplied via constructor. - gradient_fn (
Optional
[Callable
]) – Optional gradient function for optimizer
Returns
Optimized variational parameters, and corresponding minimum cost value.
Return type
dict
Raises
ValueError – invalid input
get_optimal_circuit
get_optimal_circuit()
get optimal circuit
get_optimal_cost
get_optimal_cost()
get optimal cost
get_optimal_vector
get_optimal_vector()
get optimal vector
get_prob_vector_for_params
get_prob_vector_for_params(construct_circuit_fn, params_s, quantum_instance, construct_circuit_args=None)
Helper function to get probability vectors for a set of params
get_probabilities_for_counts
get_probabilities_for_counts(counts)
get probabilities for counts
initial_point
Returns initial point
Return type
Optional
[ndarray
]
is_gradient_really_supported
is_gradient_really_supported()
returns is gradient really supported
label_to_class
returns label to class
load_model
load_model(file_path)
load model
optimal_params
returns optimal parameters
optimizer
Returns optimizer
Return type
Optional
[Optimizer
]
predict
predict(data, quantum_instance=None, minibatch_size=- 1, params=None)
Predict the labels for the data.
Parameters
- data (numpy.ndarray) – NxD array, N is number of data, D is data dimension
- quantum_instance (QuantumInstance) – quantum backend with all setting
- minibatch_size (int) – the size of each minibatched accuracy evaluation
- params (list) – list of parameters to populate in the variational form
Returns
for each data point, generates the predicted probability for each class list: for each data point, generates the predicted label (that with the highest prob)
Return type
list
quantum_instance
Returns quantum instance.
Return type
Optional
[QuantumInstance
]
random
Return a numpy random.
ret
returns result
run
run(quantum_instance=None, **kwargs)
Execute the algorithm with selected backend.
Parameters
- quantum_instance (
Union
[QuantumInstance
,Backend
,BaseBackend
,None
]) – the experimental setting. - kwargs (dict) – kwargs
Returns
results of an algorithm.
Return type
dict
Raises
AquaError – If a quantum instance or backend has not been provided
save_model
save_model(file_path)
save model
set_backend
set_backend(backend, **kwargs)
Sets backend with configuration.
Return type
None
test
test(data, labels, quantum_instance=None, minibatch_size=- 1, params=None)
Predict the labels for the data, and test against with ground truth labels.
Parameters
- data (numpy.ndarray) – NxD array, N is number of data and D is data dimension
- labels (numpy.ndarray) – Nx1 array, N is number of data
- quantum_instance (QuantumInstance) – quantum backend with all setting
- minibatch_size (int) – the size of each minibatched accuracy evaluation
- params (list) – list of parameters to populate in the variational form
Returns
classification accuracy
Return type
float
test_dataset
returns test dataset
train
train(data, labels, quantum_instance=None, minibatch_size=- 1)
Train the models, and save results.
Parameters
- data (numpy.ndarray) – NxD array, N is number of data and D is dimension
- labels (numpy.ndarray) – Nx1 array, N is number of data
- quantum_instance (QuantumInstance) – quantum backend with all setting
- minibatch_size (int) – the size of each minibatched accuracy evaluation
training_dataset
returns training dataset
var_form
Returns variational form
Return type
Union
[QuantumCircuit
, VariationalForm
, None
]