Note
இந்தப் பக்கம் docs/tutorials/02_neural_network_classifier_and_regressor.ipynb இலிருந்து உருவாக்கப்பட்டது.
நரம்பியல் நெட்வொர்க் வகைப்படுத்தி & பின்னடைவு¶
இந்த டுடோரியலில் NeuralNetworkClassifier
மற்றும் NeuralNetworkRegressor
எவ்வாறு பயன்படுத்தப்படுகின்றன என்பதைக் காட்டுகிறோம். இருவரும் ஒரு உள்ளீடாக ஒரு (குவாண்டம்) NeuralNetwork
ஐ எடுத்து ஒரு குறிப்பிட்ட சூழலில் பயன்படுத்துகிறார்கள். இரண்டு நிகழ்வுகளிலும் வசதிக்காக முன்பே உள்ளமைக்கப்பட்ட மாறுபாட்டை நாங்கள் வழங்குகிறோம், மாறுபட்ட குவாண்டம் வகைப்படுத்தி (VQC
) மற்றும் மாறுபட்ட குவாண்டம் பின்னடைவு (VQR
). பயிற்சி பின்வருமாறு கட்டமைக்கப்பட்டுள்ளது:
-
OpflowQNN
உடன் வகைப்பாடுCircuitQNN
உடன் வகைப்பாடுமாறுபட்ட குவாண்டம் வகைப்படுத்தி (
VQC
)
-
OpflowQNN
உடன் பின்னடைவுமாறுபட்ட குவாண்டம் பின்னடைவு (`` VQR``)
[1]:
import numpy as np
import matplotlib.pyplot as plt
from qiskit import Aer, QuantumCircuit
from qiskit.opflow import Z, I, StateFn
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit.circuit import Parameter
from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap
from qiskit.algorithms.optimizers import COBYLA, L_BFGS_B
from qiskit_machine_learning.neural_networks import TwoLayerQNN, CircuitQNN
from qiskit_machine_learning.algorithms.classifiers import NeuralNetworkClassifier, VQC
from qiskit_machine_learning.algorithms.regressors import NeuralNetworkRegressor, VQR
from typing import Union
from qiskit_machine_learning.exceptions import QiskitMachineLearningError
from IPython.display import clear_output
algorithm_globals.random_seed = 42
[2]:
quantum_instance = QuantumInstance(Aer.get_backend("aer_simulator"), shots=1024)
வகைப்படுத்துதல்¶
பின்வரும் வழிமுறைகளை விளக்குவதற்கு எளிய வகைப்பாடு தரவுத்தொகுப்பை நாங்கள் தயார் செய்கிறோம்.
[3]:
num_inputs = 2
num_samples = 20
X = 2 * algorithm_globals.random.random([num_samples, num_inputs]) - 1
y01 = 1 * (np.sum(X, axis=1) >= 0) # in { 0, 1}
y = 2 * y01 - 1 # in {-1, +1}
y_one_hot = np.zeros((num_samples, 2))
for i in range(num_samples):
y_one_hot[i, y01[i]] = 1
for x, y_target in zip(X, y):
if y_target == 1:
plt.plot(x[0], x[1], "bo")
else:
plt.plot(x[0], x[1], "go")
plt.plot([-1, 1], [1, -1], "--", color="black")
plt.show()

OpflowQNN
உடன் வகைப்பாடு¶
முதலில் NeuralNetworkClassifier
க்குள் வகைப்படுத்தலுக்கு OpflowQNN
எவ்வாறு பயன்படுத்தப்படலாம் என்பதைக் காண்பிப்போம். இந்த சூழலில், `` OpflowQNN`` இதில் ஒரு பரிமாண வெளியீட்டை வழங்கும் \([-1, +1]\). இது பைனரி வகைப்பாட்டிற்கு மட்டுமே செயல்படும், மேலும் இரண்டு வகுப்புகளையும் இதற்கு ஒதுக்குகிறோம் \(\{-1, +1\}\). வசதிக்காக, TwoLayerQNN
ஐப் பயன்படுத்துகிறோம், இது ஒரு சிறப்பு வகை OpflowQNN
ஒரு அம்ச வரைபடம் மற்றும் ஒரு அன்சாட்ஸ் வழியாக வரையறுக்கப்பட்டுள்ளது.
[4]:
# construct QNN
opflow_qnn = TwoLayerQNN(num_inputs, quantum_instance=quantum_instance)
[5]:
# QNN maps inputs to [-1, +1]
opflow_qnn.forward(X[0, :], algorithm_globals.random.random(opflow_qnn.num_weights))
[5]:
array([[0.25]])
callback_graph
எனப்படும் திரும்ப அழைக்கும் செயல்பாட்டைச் சேர்ப்போம். இது ஆப்டிமைசரின் ஒவ்வொரு மறு செய்கைக்கும் அழைக்கப்படும் மற்றும் இரண்டு அளவுருக்கள் அனுப்பப்படும்: தற்போதைய எடைகள் மற்றும் அந்த எடைகளில் புறநிலை செயல்பாட்டின் மதிப்பு. எங்கள் செயல்பாட்டிற்காக, புறநிலை செயல்பாட்டின் மதிப்பை ஒரு வரிசையில் சேர்க்கிறோம், எனவே மறு செய்கை மற்றும் புறநிலை செயல்பாட்டு மதிப்புக்கு எதிராக திட்டமிடலாம் மற்றும் ஒவ்வொரு மறு செய்கையுடன் வரைபடத்தைப் புதுப்பிக்கலாம். இருப்பினும், குறிப்பிடப்பட்ட இரண்டு அளவுருக்களைப் பெறும் வரை நீங்கள் திரும்ப அழைக்கும் செயல்பாட்டின் மூலம் நீங்கள் விரும்பியதைச் செய்யலாம்.
[6]:
# callback function that draws a live plot when the .fit() method is called
def callback_graph(weights, obj_func_eval):
clear_output(wait=True)
objective_func_vals.append(obj_func_eval)
plt.title("Objective function value against iteration")
plt.xlabel("Iteration")
plt.ylabel("Objective function value")
plt.plot(range(len(objective_func_vals)), objective_func_vals)
plt.show()
[7]:
# construct neural network classifier
opflow_classifier = NeuralNetworkClassifier(opflow_qnn, optimizer=COBYLA(), callback=callback_graph)
[8]:
# create empty array for callback to store evaluations of the objective function
objective_func_vals = []
plt.rcParams["figure.figsize"] = (12, 6)
# fit classifier to data
opflow_classifier.fit(X, y)
# return to default figsize
plt.rcParams["figure.figsize"] = (6, 4)
# score classifier
opflow_classifier.score(X, y)

[8]:
0.8
[9]:
# evaluate data points
y_predict = opflow_classifier.predict(X)
# plot results
# red == wrongly classified
for x, y_target, y_p in zip(X, y, y_predict):
if y_target == 1:
plt.plot(x[0], x[1], "bo")
else:
plt.plot(x[0], x[1], "go")
if y_target != y_p:
plt.scatter(x[0], x[1], s=200, facecolors="none", edgecolors="r", linewidths=2)
plt.plot([-1, 1], [1, -1], "--", color="black")
plt.show()

CircuitQNN
உடன் வகைப்பாடு¶
அடுத்து NeuralNetworkClassifier
க்குள் வகைப்படுத்தலுக்கு CircuitQNN
எவ்வாறு பயன்படுத்தப்படலாம் என்பதைக் காண்பிப்போம். இந்த சூழலில், CircuitQNN
திரும்பும் என்று எதிர்பார்க்கப்படுகிறது \(d\)-பரிமாண நிகழ்தகவு திசையன் வெளியீடாக, எங்கே \(d\) என்பது வகுப்புகளின் எண்ணிக்கையைக் குறிக்கிறது. QuantumCircuit
இலிருந்து மாதிரியானது தானாக நிகழ்தகவு விநியோகத்தில் விளைகிறது, மேலும் அளவிடப்பட்ட பிட்ஸ்ட்ரிங்கிலிருந்து வெவ்வேறு வகுப்புகளுக்கு ஒரு மேப்பிங்கை வரையறுக்க வேண்டும். பைனரி வகைப்பாட்டிற்கு நாங்கள் பரிதி மேப்பிங்கைப் பயன்படுத்துகிறோம்.
[10]:
# construct feature map
feature_map = ZZFeatureMap(num_inputs)
# construct ansatz
ansatz = RealAmplitudes(num_inputs, reps=1)
# construct quantum circuit
qc = QuantumCircuit(num_inputs)
qc.append(feature_map, range(num_inputs))
qc.append(ansatz, range(num_inputs))
qc.decompose().draw(output="mpl")
[10]:

[11]:
# parity maps bitstrings to 0 or 1
def parity(x):
return "{:b}".format(x).count("1") % 2
output_shape = 2 # corresponds to the number of classes, possible outcomes of the (parity) mapping.
[12]:
# construct QNN
circuit_qnn = CircuitQNN(
circuit=qc,
input_params=feature_map.parameters,
weight_params=ansatz.parameters,
interpret=parity,
output_shape=output_shape,
quantum_instance=quantum_instance,
)
[13]:
# construct classifier
circuit_classifier = NeuralNetworkClassifier(
neural_network=circuit_qnn, optimizer=COBYLA(), callback=callback_graph
)
[14]:
# create empty array for callback to store evaluations of the objective function
objective_func_vals = []
plt.rcParams["figure.figsize"] = (12, 6)
# fit classifier to data
circuit_classifier.fit(X, y01)
# return to default figsize
plt.rcParams["figure.figsize"] = (6, 4)
# score classifier
circuit_classifier.score(X, y01)

[14]:
0.75
[15]:
# evaluate data points
y_predict = circuit_classifier.predict(X)
# plot results
# red == wrongly classified
for x, y_target, y_p in zip(X, y01, y_predict):
if y_target == 1:
plt.plot(x[0], x[1], "bo")
else:
plt.plot(x[0], x[1], "go")
if y_target != y_p:
plt.scatter(x[0], x[1], s=200, facecolors="none", edgecolors="r", linewidths=2)
plt.plot([-1, 1], [1, -1], "--", color="black")
plt.show()

மாறுபட்ட குவாண்டம் வகைப்படுத்தி (VQC
)¶
VQC
என்பது CircuitQNN
உடன் NeuralNetworkClassifier
சிறப்பு மாறுபாடாகும். இது பிட்ஸ்ட்ரிங்கிலிருந்து வகைப்பாட்டிற்கு வரைபட ஒரு சமநிலை மேப்பிங்கை (அல்லது பல வகுப்புகளுக்கு நீட்டிப்புகள்) பயன்படுத்துகிறது, இதன் விளைவாக நிகழ்தகவு திசையன் உருவாகிறது, இது ஒரு சூடான குறியாக்கப்பட்ட விளைவாக விளக்கப்படுகிறது. இயல்பாக, இது ஒரு ஹாட் என்கோடட் வடிவத்தில் கொடுக்கப்பட்ட லேபிள்களை எதிர்பார்க்கும் CrossEntropyLoss
செயல்பாட்டைப் பயன்படுத்துகிறது, மேலும் அந்த வடிவமைப்பிலும் கணிப்புகளைத் தரும்.
[16]:
# construct feature map, ansatz, and optimizer
feature_map = ZZFeatureMap(num_inputs)
ansatz = RealAmplitudes(num_inputs, reps=1)
# construct variational quantum classifier
vqc = VQC(
feature_map=feature_map,
ansatz=ansatz,
loss="cross_entropy",
optimizer=COBYLA(),
quantum_instance=quantum_instance,
callback=callback_graph,
)
[17]:
# create empty array for callback to store evaluations of the objective function
objective_func_vals = []
plt.rcParams["figure.figsize"] = (12, 6)
# fit classifier to data
vqc.fit(X, y_one_hot)
# return to default figsize
plt.rcParams["figure.figsize"] = (6, 4)
# score classifier
vqc.score(X, y_one_hot)

[17]:
0.5
[18]:
# evaluate data points
y_predict = vqc.predict(X)
# plot results
# red == wrongly classified
for x, y_target, y_p in zip(X, y_one_hot, y_predict):
if y_target[0] == 1:
plt.plot(x[0], x[1], "bo")
else:
plt.plot(x[0], x[1], "go")
if not np.all(y_target == y_p):
plt.scatter(x[0], x[1], s=200, facecolors="none", edgecolors="r", linewidths=2)
plt.plot([-1, 1], [1, -1], "--", color="black")
plt.show()

பின்னடைவு¶
பின்வரும் வழிமுறைகளை விளக்குவதற்கு எளிய பின்னடைவு தரவுத்தொகுப்பை நாங்கள் தயார் செய்கிறோம்.
[19]:
num_samples = 20
eps = 0.2
lb, ub = -np.pi, np.pi
X_ = np.linspace(lb, ub, num=50).reshape(50, 1)
f = lambda x: np.sin(x)
X = (ub - lb) * algorithm_globals.random.random([num_samples, 1]) + lb
y = f(X[:, 0]) + eps * (2 * algorithm_globals.random.random(num_samples) - 1)
plt.plot(X_, f(X_), "r--")
plt.plot(X, y, "bo")
plt.show()

OpflowQNN
உடன் பின்னடைவு¶
\([-1, +1]\) இல் மதிப்புகளை வழங்கும் OpflowQNN
உடன் பின்னடைவை இங்கு கட்டுப்படுத்துகிறோம். CircuitQNN
ஐ அடிப்படையாகக் கொண்டு மிகவும் சிக்கலான மற்றும் பல பரிமாண மாதிரிகள் உருவாக்கப்படலாம், ஆனால் இது இந்த டுடோரியலின் நோக்கத்தை மீறுகிறது.
[20]:
# construct simple feature map
param_x = Parameter("x")
feature_map = QuantumCircuit(1, name="fm")
feature_map.ry(param_x, 0)
# construct simple ansatz
param_y = Parameter("y")
ansatz = QuantumCircuit(1, name="vf")
ansatz.ry(param_y, 0)
# construct QNN
regression_opflow_qnn = TwoLayerQNN(1, feature_map, ansatz, quantum_instance=quantum_instance)
[21]:
# construct the regressor from the neural network
regressor = NeuralNetworkRegressor(
neural_network=regression_opflow_qnn,
loss="squared_error",
optimizer=L_BFGS_B(),
callback=callback_graph,
)
[22]:
# create empty array for callback to store evaluations of the objective function
objective_func_vals = []
plt.rcParams["figure.figsize"] = (12, 6)
# fit to data
regressor.fit(X, y)
# return to default figsize
plt.rcParams["figure.figsize"] = (6, 4)
# score the result
regressor.score(X, y)

[22]:
0.9763610762801076
[23]:
# plot target function
plt.plot(X_, f(X_), "r--")
# plot data
plt.plot(X, y, "bo")
# plot fitted line
y_ = regressor.predict(X_)
plt.plot(X_, y_, "g-")
plt.show()

மாறுபட்ட குவாண்டம் பின்னடைவுடன் பின்னடைவு (VQR
)¶
வகைப்பாட்டிற்கான VQC
ஐப் போலவே, VQR
என்பது OpflowQNN
உடன் NeuralNetworkRegressor
இன் சிறப்பு மாறுபாடாகும். முன்னிருப்பாக இது கணிப்புகள் மற்றும் இலக்குகளுக்கு இடையிலான சராசரி ஸ்கொயர் பிழையைக் குறைக்க L2Loss
செயல்பாட்டைக் கருதுகிறது.
[24]:
vqr = VQR(
feature_map=feature_map,
ansatz=ansatz,
optimizer=L_BFGS_B(),
quantum_instance=quantum_instance,
callback=callback_graph,
)
[25]:
# create empty array for callback to store evaluations of the objective function
objective_func_vals = []
plt.rcParams["figure.figsize"] = (12, 6)
# fit regressor
vqr.fit(X, y)
# return to default figsize
plt.rcParams["figure.figsize"] = (6, 4)
# score result
vqr.score(X, y)

[25]:
0.9748043399155905
[26]:
# plot target function
plt.plot(X_, f(X_), "r--")
# plot data
plt.plot(X, y, "bo")
# plot fitted line
y_ = vqr.predict(X_)
plt.plot(X_, y_, "g-")
plt.show()

[28]:
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright
Version Information
Qiskit Software | Version |
---|---|
qiskit-terra | 0.19.0 |
qiskit-aer | 0.9.0 |
qiskit-ignis | 0.7.0 |
qiskit-ibmq-provider | 0.17.0 |
qiskit-aqua | 0.10.0 |
qiskit-machine-learning | 0.3.0 |
System information | |
Python | 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0] |
OS | Linux |
CPUs | 4 |
Memory (Gb) | 7.6849517822265625 |
Mon Aug 30 16:41:59 2021 IST |
This code is a part of Qiskit
© Copyright IBM 2017, 2021.
This code is licensed under the Apache License, Version 2.0. You may
obtain a copy of this license in the LICENSE.txt file in the root directory
of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
Any modifications or derivative works of this code must retain this
copyright notice, and modified files need to carry a notice indicating
that they have been altered from the originals.