Note
இந்த பக்கம் tutorials/algorithms/07_grover_examples.ipynb. லிருந்து உருவாக்கப்பட்டது.
க்ரோவர் வழிமுறையின் எடுத்துக்காட்டுகள்¶
இந்த குறிப்பேடு Qiskit Grover தேடல் வழிமுறை, வெவ்வேறு ஆரக்கிள்களுடன் எவ்வாறு பயன்படுத்துவது என்பதை விளக்கும் எடுத்துக்காட்டுகள் உள்ளன.
3-SAT சிக்கல்களுக்கு தீர்வுகளைக் கண்டறிதல்¶
ஒரு எடுத்துக்காட்டு 3-Satisfiabitlity (3-SAT) சிக்கலைப் பார்ப்போம், அதன் திருப்திகரமான தீர்வுகளைக் கண்டறிய குவாண்டம் தேடலை எவ்வாறு பயன்படுத்தலாம் என்பதைப் பார்ப்போம். 3-SAT சிக்கல்கள் வழக்கமாக Conjunctive Normal Forms (CNF) மற்றும் DIMACS-CNF முறையில் வெளிப்படுத்தலாம். உதாரணத்திற்கு:
[1]:
input_3sat_instance = '''
c example DIMACS-CNF 3-SAT
p cnf 3 5
-1 -2 -3 0
1 -2 3 0
1 2 -3 0
1 -2 -3 0
-1 2 3 0
'''
இந்த 3-SAT உதாரணம் CNF 3 மாறிகள் மற்றும் 5 உட்பிரிவுகளைக் கொண்டுள்ளது:
\((\neg v_1 \vee \neg v_2 \vee \neg v_3) \wedge (v_1 \vee \neg v_2 \vee v_3) \wedge (v_1 \vee v_2 \vee \neg v_3) \wedge (v_1 \vee \neg v_2 \vee \neg v_3) \wedge (\neg v_1 \vee v_2 \vee v_3)\)
இந்த 3-SAT சிக்கல் உதாரணம் மூன்று திருப்திகரமான தீர்வுகளைக் கொண்டுள்ளது என்பதை சரிபார்க்க முடியும்:
\((v_1, v_2, v_3) = (T, F, T)\) or \((F, F, F)\) or \((T, T, F)\)
அல்லது, DIMACS குறியீட்டைப் பயன்படுத்தி வெளிப்படுத்தப்படுகிறது:
1 -2 3
, அல்லது -1 -2 -3
, அல்லது 1 2 -3
.
இந்த எடுத்துக்காட்டு சிக்கல் உள்ளீட்டைக் கொண்டு, எங்கள் Grover
தேடலுக்கான தொடர்புடைய oracle
ஐ உருவாக்குகிறோம். குறிப்பாக, நாங்கள் PhaseOracle
கூறுகளைப் பயன்படுத்துகிறோம், இது DIMACS-CNF வடிவமைப்பு ஸ்ட்ரிஂங்களைப் பாகுபடுத்துவதற்கும் அதனுடன் தொடர்புடைய ஆரக்கிள் சர்க்யூட்களை உருவாக்குவதற்கும் துணைபுரிகிறது.
[2]:
import os
import tempfile
from qiskit.exceptions import MissingOptionalLibraryError
from qiskit.circuit.library.phase_oracle import PhaseOracle
fp = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
fp.write(input_3sat_instance)
file_name = fp.name
fp.close()
oracle = None
try:
oracle = PhaseOracle.from_dimacs_file(file_name)
except MissingOptionalLibraryError as ex:
print(ex)
finally:
os.remove(file_name)
க்ரோவர் உதாரணத்தை உருவாக்க oracle
இப்போது பயன்படுத்தப்படலாம்:
[3]:
from qiskit.algorithms import AmplificationProblem
problem = None
if oracle is not None:
problem = AmplificationProblem(oracle, is_good_state=oracle.evaluate_bitstring)
முடிவைப் பெற நாம் பின்தளத்தில் உள்ளமைத்து க்ரோவர் உதாரணத்தை இயக்கலாம்:
[4]:
from qiskit.algorithms import Grover
from qiskit.primitives import Sampler
grover = Grover(sampler=Sampler())
result = None
if problem is not None:
result = grover.amplify(problem)
print(result.assignment)
000
மேலே பார்த்தபடி, குறிப்பிட்ட 3-SAT சிக்கலுக்கு திருப்திகரமான தீர்வு பெறப்படுகிறது. இது உண்மையில் மூன்று திருப்திகரமான தீர்வுகளில் ஒன்றாகும்.
Since we used the Sampler
, the complete measurement result is also returned, as shown in the plot below, where it can be seen that the binary strings 000
, 011
, and 101
(note the bit order in each string), corresponding to the three satisfying solutions all have high probabilities associated with them.
[5]:
from qiskit.tools.visualization import plot_histogram
if result is not None:
display(plot_histogram(result.circuit_results[0]))

பூலியன் தருக்க வெளிப்பாடுகள்¶
DIMACS-உடன் கூடுதலாக, பிற வழிகளிலிருந்து கட்டப்பட்ட Oracle
இல் குவாண்டம் தேடலைச் செய்ய Qiskit-ன் Grover
பயன்படுத்தப்படலாம். எடுத்துக்காட்டாக, PhaseOracle
உண்மையில் கீழே காட்டப்பட்டுள்ளபடி தன்னிச்சையான பூலியன் தருக்க வெளிப்பாடுகளைப் பயன்படுத்தி கட்டமைக்க முடியும்.
[6]:
expression = '(w ^ x) & ~(y ^ z) & (x & y & z)'
try:
oracle = PhaseOracle(expression)
problem = AmplificationProblem(oracle, is_good_state=oracle.evaluate_bitstring)
grover = Grover(sampler=Sampler())
result = grover.amplify(problem)
display(plot_histogram(result.circuit_results[0]))
except MissingOptionalLibraryError as ex:
print(ex)

மேலே உள்ள எடுத்துக்காட்டில், பூலியன் தருக்க வெளிப்பாடு '(w ^ x) & ~(y ^ z) & (x & y & z)'
மிகவும் சுய விளக்கமாக இருக்க வேண்டும், அங்கு ^
, ~
, மற்றும் &
முறையே பூலியன் தருக்க XOR, NOT மற்றும் AND ஆபரேட்டர்களைக் குறிக்கின்றன. அதன் பகுதிகளை ஆராய்வதன் மூலம் திருப்திகரமான தீர்வைக் கண்டறிவது மிகவும் எளிதாக இருக்க வேண்டும்: w ^ x
, w
மற்றும் x
வெவ்வேறு மதிப்புகளை எடுத்துக்கொள்வதற்கான அழைப்புகள்; ~ (y ^ z)
க்கு y
மற்றும் z
ஆகியவை ஒரே மாதிரியாக இருக்க வேண்டும்; x & y & z
மூன்றையும் True
என்று ஆணையிடுகிறது. இவற்றைச் சேர்த்து, திருப்திகரமான தீர்வைப் பெறுகிறோம் (w, x, y, z) = (False, True, True, True)
, இது எங்கள் Grover
’s இன் முடிவு ஒப்புக்கொள்கிறது.
[16]:
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright
Version Information
Qiskit Software | Version |
---|---|
qiskit-terra | 0.22.2 |
qiskit-aer | 0.11.1 |
qiskit-ibmq-provider | 0.19.2 |
qiskit | 0.39.2 |
qiskit-optimization | 0.4.0 |
System information | |
Python version | 3.9.10 |
Python compiler | Clang 13.1.6 (clang-1316.0.21.2.5) |
Python build | main, Aug 9 2022 18:26:17 |
OS | Darwin |
CPUs | 10 |
Memory (Gb) | 64.0 |
Thu Nov 17 09:09:08 2022 JST |
This code is a part of Qiskit
© Copyright IBM 2017, 2022.
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.
[ ]: