QNNCircuit#
- class QNNCircuit(num_qubits=None, feature_map=None, ansatz=None)[source]#
Bases:
BlueprintCircuit
The QNN circuit is a blueprint circuit that wraps feature map and ansatz circuits. It can be used to simplify the composition of these two.
If only the number of qubits is provided the
RealAmplitudes
ansatz and theZZFeatureMap
feature map are used. If the number of qubits is 1 theZFeatureMap
is used. If only a feature map is provided, theRealAmplitudes
ansatz with the corresponding number of qubits is used. If only an ansatz is provided theZZFeatureMap
with the corresponding number of qubits is used.At least one parameter has to be provided. If a feature map and an ansatz is provided, the number of qubits must be the same.
In case number of qubits is provided along with either a feature map, an ansatz or both, a potential mismatch between the three inputs with respect to the number of qubits is resolved by constructing the
QNNCircuit
with the given number of qubits. If one of theQNNCircuit
properties is set after the class construction, the circuit is adjusted to incorporate the changes. This means, a new valid configuration that considers the latest property update will be derived. This ensures that the classes properties are consistent at all times.Example:
from qiskit_machine_learning.circuit.library import QNNCircuit qnn_qc = QNNCircuit(2) print(qnn_qc) # prints: # ┌──────────────────────────┐» # q_0: ┤0 ├» # │ ZZFeatureMap(x[0],x[1]) │» # q_1: ┤1 ├» # └──────────────────────────┘» # « ┌──────────────────────────────────────────────────────────┐ # «q_0: ┤0 ├ # « │ RealAmplitudes(θ[0],θ[1],θ[2],θ[3],θ[4],θ[5],θ[6],θ[7]) │ # «q_1: ┤1 ├ # « └──────────────────────────────────────────────────────────┘ print(qnn_qc.num_qubits) # prints: 2 print(qnn_qc.input_parameters) # prints: ParameterView([ParameterVectorElement(x[0]), ParameterVectorElement(x[1])]) print(qnn_qc.weight_parameters) # prints: ParameterView([ParameterVectorElement(θ[0]), ParameterVectorElement(θ[1]), # ParameterVectorElement(θ[2]), ParameterVectorElement(θ[3]), # ParameterVectorElement(θ[4]), ParameterVectorElement(θ[5]), # ParameterVectorElement(θ[6]), ParameterVectorElement(θ[7])])
Although all parameters default to None at least one parameter must be provided, to determine the number of qubits from it, when the instance is created.
If more than one parameter is passed:
1) If num_qubits is provided the feature map and/or ansatz supplied will be overridden to circuits with num_qubits, as long as the respective circuit supports updating its number of qubits.
2) If num_qubits is not provided the feature_map and ansatz must be set to the same number of qubits.
- Parameters:
num_qubits (int | None) -- Number of qubits, a positive integer. Optional if feature_map or ansatz is provided, otherwise required. If not provided num_qubits defaults from the sizes of feature_map and ansatz.
feature_map (QuantumCircuit | None) -- A feature map. Optional if num_qubits or ansatz is provided, otherwise required. If not provided defaults to
ZZFeatureMap
orZFeatureMap
if num_qubits is determined to be 1.ansatz (QuantumCircuit | None) -- An ansatz. Optional if num_qubits or feature_map is provided, otherwise required. If not provided defaults to
RealAmplitudes
.
- Returns:
The composed feature map and ansatz circuit.
- Raises:
QiskitMachineLearningError -- If a valid number of qubits cannot be derived from the provided input arguments.
- Return type:
None
Attributes
- ansatz#
Returns ansatz.
- Returns:
The ansatz.
- feature_map#
Returns feature_map.
- Returns:
The feature map.
- input_parameters#
Returns the parameters of the feature map.
- Returns:
The parameters of the feature map.
- num_input_parameters#
Returns the number of input parameters in the circuit.
- Returns:
The number of input parameters.
- num_qubits#
Returns the number of qubits in this circuit.
- Returns:
The number of qubits.
- num_weight_parameters#
Returns the number of weights in the circuit.
- Returns:
The number of weights.
- weight_parameters#
Returns the parameters of the ansatz. These corresponding to the trainable weights.
- Returns:
The parameters of the ansatz.
Methods