Assumption
Environments
- JupyterNotebook
- python3
About neural network
The NeuralNetwork represents the interface for all neural networks available in Qiskit Machine Learning. It exposes a forward and a backward pass taking the data samples and trainable weights as input. A NeuralNetwork does not contain any training capabilities, these are pushed to the actual algorithms / applications. Thus, a NeuralNetwork also does not store the values for trainable weights. In the following, different implementations of this interfaces are introduced.
What is CircuitQNN?
A Sampling Neural Network based on a given quantum circuit.
Instllation
$ pip install qiskit_machine_learning
Setting
import numpy as np
from qiskit import Aer, QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap
from qiskit.opflow import StateFn, PauliSumOp, AerPauliExpectation, ListOp, Gradient
from qiskit.utils import QuantumInstance, algorithm_globals
algorithm_globals.random_seed = 42
expval = AerPauliExpectation()
gradient = Gradient()
qi_sv = QuantumInstance(Aer.get_backend("aer_simulator_statevector"))
qi_qasm = QuantumInstance(Aer.get_backend("aer_simulator"), shots=10)
Suppose a NeuralNetwork called nn. Then, the nn.forward(input, weights) pass takes either flat inputs for the data and weights of size nn.num_inputs and nn.num_weights, respectively. NeuralNetwork supports batching of inputs and returns batches of output of the corresponding shape.
Coding
from qiskit_machine_learning.neural_networks import CircuitQNN
qc = RealAmplitudes(num_qubits, entanglement="linear", reps=1)
qc.draw(output="mpl")
↓Out