Bengali
Languages
English
Bengali
French
German
Japanese
Korean
Portuguese
Spanish
Tamil

নোট

এই পৃষ্ঠাটি tutorials/algorithms/07_grover_examples.ipynb -থেকে নেওয়া হয়েছে।

Grover’s algorithm examples

বিভিন্ন ওরাকেল ব্যবহার করে কিভাবে Qiskit Grover <https://qiskit.org/documentation/stubs/qiskit.algorithms.Grover.html> এর অনুসন্ধান অ্যালগোরিথম ব্যবহার করতে হ​য় তা এই নোটবুকে দেখানো হয়েছে।

3-SAT সমস্যার সমাধান খুজে বের করা।

Let’s look at an example 3-Satisfiability (3-SAT) problem and walk-through how we can use Quantum Search to find its satisfying solutions. 3-SAT problems are usually expressed in Conjunctive Normal Forms (CNF) and written in the DIMACS-CNF format. For example:

[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 ImportError as ex:
    print(ex)
finally:
    os.remove(file_name)
"The 'tweedledum' library is required to use 'classical function oracles'. You can install it with 'pip install tweedledum'."

এখন গ্রোভার দৃষ্টান্ত তৈরি করতে 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)

উপরিউক্ত বিধি মোতাবেক, নির্দিষ্টকৃত 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]))

Boolean লজিকাল এক্সপ্রেশন

Qiskit’s Grover can also be used to perform Quantum Search on an Oracle constructed from other means, in addition to DIMACS. For example, the PhaseOracle can actually be configured using arbitrary Boolean logical expressions, as demonstrated below.

[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)
"The 'tweedledum' library is required to use 'PhaseOracle'. You can install it with 'pip install tweedledum'."

In the example above, the input Boolean logical expression '(w ^ x) & ~(y ^ z) & (x & y & z)' should be quite self-explanatory, where ^, ~, and & represent the Boolean logical XOR, NOT, and AND operators, respectively. It should be quite easy to figure out the satisfying solution by examining its parts: w ^ x calls for w and x taking different values; ~(y ^ z) requires y and z be the same; x & y & z dictates all three to be True. Putting these together, we get the satisfying solution (w, x, y, z) = (False, True, True, True), which our Grover’s result agrees with.

[7]:
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright

Version Information

Qiskit SoftwareVersion
qiskit-terra0.23.3
qiskit-aer0.12.0
qiskit-ibmq-provider0.20.2
qiskit0.42.1
System information
Python version3.10.10
Python compilerGCC 12.2.1 20230201
Python buildmain, Mar 5 2023 22:26:53
OSLinux
CPUs32
Memory (Gb)125.66083908081055
Thu May 04 15:38:15 2023 EDT

This code is a part of Qiskit

© Copyright IBM 2017, 2023.

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.

[ ]: