VQC
class VQC(optimizer, feature_map, var_form, training_dataset, test_dataset=None, datapoints=None, max_evals_grouped=1, minibatch_size=- 1, callback=None, 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. - quantum_instance (
Union
[QuantumInstance
,BaseBackend
,None
]) – Quantum Instance or Backend
We use label to denotes numeric results and class the class names (str).
Raises
AquaError – Missing feature map or missing training dataset.
Attributes
backend
class_to_label
returns class to label
datapoints
return data points
feature_map
Type: Optional[Union[qiskit.aqua.components.feature_maps.feature_map.FeatureMap, qiskit.circuit.quantumcircuit.QuantumCircuit]]
Return the feature map.
Return type
Union
[FeatureMap
, QuantumCircuit
, None
]
initial_point
Type: Optional[numpy.ndarray]
Returns initial point
Return type
Optional
[ndarray
]
label_to_class
returns label to class
optimal_params
returns optimal parameters
optimizer
Type: Optional[qiskit.aqua.components.optimizers.optimizer.Optimizer]
Returns optimizer
Return type
Optional
[Optimizer
]
quantum_instance
Type: Union[None, qiskit.aqua.quantum_instance.QuantumInstance]
Returns quantum instance.
Return type
Optional
[QuantumInstance
]
random
Return a numpy random.
ret
returns result
test_dataset
returns test dataset
training_dataset
returns training dataset
var_form
Type: Optional[Union[qiskit.circuit.quantumcircuit.QuantumCircuit, qiskit.aqua.components.variational_forms.variational_form.VariationalForm]]
Returns variational form
Return type
Union
[QuantumCircuit
, VariationalForm
, None
]
Methods
batch_data
VQC.batch_data(data, labels=None, minibatch_size=- 1)
batch data
cleanup_parameterized_circuits
VQC.cleanup_parameterized_circuits()
set parameterized circuits to None
construct_circuit
VQC.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.
find_minimum
VQC.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
VQC.get_optimal_circuit()
get optimal circuit
get_optimal_cost
VQC.get_optimal_cost()
get optimal cost
get_optimal_vector
VQC.get_optimal_vector()
get optimal vector
get_prob_vector_for_params
VQC.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
VQC.get_probabilities_for_counts(counts)
get probabilities for counts
is_gradient_really_supported
VQC.is_gradient_really_supported()
returns is gradient really supported
load_model
VQC.load_model(file_path)
load model
predict
VQC.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
run
VQC.run(quantum_instance=None, **kwargs)
Execute the algorithm with selected backend.
Parameters
- quantum_instance (
Union
[QuantumInstance
,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
VQC.save_model(file_path)
save model
set_backend
VQC.set_backend(backend, **kwargs)
Sets backend with configuration.
Return type
None
test
VQC.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
train
VQC.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