class VQR(num_qubits=None, feature_map=None, ansatz=None, observable=None, loss='squared_error', optimizer=None, warm_start=False, initial_point=None, callback=None, *, estimator=None)[source]#

Bases: NeuralNetworkRegressor

A convenient Variational Quantum Regressor implementation.

  • num_qubits (int | None) -- The number of qubits for the underlying QNN. If None then the number of qubits is derived from the feature map or ansatz, but if neither of these are given an error is raised. The number of qubits in the feature map and ansatz are adjusted to this number if required.

  • feature_map (QuantumCircuit | None) -- The (parametrized) circuit to be used as a feature map for the underlying QNN. If None the ZZFeatureMap is used if the number of qubits is larger than 1. For a single qubit regression problem the ZFeatureMap is used by default.

  • ansatz (QuantumCircuit | None) -- The (parametrized) circuit to be used as an ansatz for the underlying QNN. If None then the RealAmplitudes circuit is used.

  • observable (BaseOperator | None) -- The observable to be measured in the underlying QNN. If None, use the default \(Z^{\otimes num\_qubits}\) observable.

  • loss (str | Loss) -- A target loss function to be used in training. Default is squared error.

  • optimizer (Optimizer | Minimizer | None) -- An instance of an optimizer or a callable to be used in training. Refer to Minimizer for more information on the callable protocol. When None defaults to SLSQP.

  • warm_start (bool) -- Use weights from previous fit to start next fit.

  • initial_point (np.ndarray | None) -- Initial point for the optimizer to start from.

  • callback (Callable[[np.ndarray, float], None] | None) -- A reference to a user's callback function that has two parameters and returns None. The callback can access intermediate data during training. On each iteration an optimizer invokes the callback and passes current weights as an array and a computed value as a float of the objective function being optimized. This allows to track how well optimization / training process is going on.

  • estimator (BaseEstimator | None) -- an optional Estimator primitive instance to be used by the underlying EstimatorQNN neural network. If None is passed then an instance of the reference Estimator will be used.

  • QiskitMachineLearningError -- Needs at least one out of num_qubits, feature_map or ansatz to be given. Or the number of qubits in the feature map and/or ansatz can't be adjusted to num_qubits.

  • ValueError -- if the type of the observable is not compatible with estimator.



Returns the used ansatz.


Return the callback.


Returns the used feature map.


Returns a resulting object from the optimization procedure. Please refer to the documentation of the OptimizerResult class for more details.


QiskitMachineLearningError -- If the model has not been fit.


Returns current initial point


Returns the underlying neural network.


Returns the underlying neural network.


Returns the number of qubits used by ansatz and feature map.


Returns an optimizer to be used in training.


Returns the warm start flag.


Returns trained weights as a numpy array. The weights can be also queried by calling model.fit_result.x, but in this case their representation depends on the optimizer used.


QiskitMachineLearningError -- If the model has not been fit.


fit(X, y)#

Fit the model to data matrix X and target(s) y.


returns a trained model.

রিটার্ন টাইপ:



QiskitMachineLearningError -- In case of invalid data (e.g. incompatible with network)

classmethod load(file_name)#

Loads a model from the file. If the loaded model is not an instance of the class whose method was called, then a warning is raised. Nevertheless, the loaded model may be a valid model.


file_name (str) -- a file name or path to load a model from.


A loaded model.


TypeError -- if a loaded model is not an instance of the expected class.

রিটার্ন টাইপ:



Predict using the network specified to the model.


X (ndarray) -- The input data.


QiskitMachineLearningError -- Model needs to be fit to some training data first


The predicted classes.

রিটার্ন টাইপ:



Saves this model to the specified file. Internally, the model is serialized via dill. All parameters are saved, including a primitive instance that is referenced by internal objects. That means if a model is loaded from a file and is used, for instance, for inference, the same primitive will be used even if a cloud primitive was used.


file_name (str) -- a file name or path where to save the model.

score(X, y, sample_weight=None)#

Returns a score of this model given samples and true values for the samples. In case of classification this should be mean accuracy, in case of regression the coefficient of determination \(R^2\) of the prediction.

  • X (ndarray) -- Test samples.

  • y (ndarray) -- True values for X.

  • sample_weight (ndarray | None) -- Sample weights. Default is None.


a float score of the model.

রিটার্ন টাইপ: