11"""
2- Build the quantum fourier transform (qft) for a desire
3- number of quantum bits using Qiskit framework. This
4- experiment run in IBM Q simulator with 10000 shots.
5- This circuit can be use as a building block to design
6- the Shor's algorithm in quantum computing. As well as,
7- quantum phase estimation among others.
8- .
2+ Build the Grover Search Algorithm for a desired
3+ number of quantum bits using Qiskit framework.
4+ This experiment runs in IBM Q simulator with 10000 shots.
5+
6+ This circuit demonstrates amplitude amplification
7+ and can be used as a building block for quantum
8+ search and optimization problems.
9+
910References:
10- https://en.wikipedia.org/wiki/Quantum_Fourier_transform
11- https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform .html
11+ https://en.wikipedia.org/wiki/Grover%27s_algorithm
12+ https://qiskit.org/textbook/ch-algorithms/grover .html
1213"""
1314
1415import math
15-
16- import numpy as np
1716import qiskit
1817from qiskit import Aer , ClassicalRegister , QuantumCircuit , QuantumRegister , execute
1918
2019
21- def quantum_fourier_transform (number_of_qubits : int = 3 ) -> qiskit .result .counts .Counts :
20+ def grover_search (number_of_qubits : int = 2 ) -> qiskit .result .counts .Counts :
2221 """
23- # >>> quantum_fourier_transform(2)
24- # {'00': 2500, '01': 2500, '11': 2500, '10': 2500}
25- # quantum circuit for number_of_qubits = 3:
26- ┌───┐
27- qr_0: ──────■──────────────────────■───────┤ H ├─X─
28- │ ┌───┐ │P(π/2) └───┘ │
29- qr_1: ──────┼────────■───────┤ H ├─■─────────────┼─
30- ┌───┐ │P(π/4) │P(π/2) └───┘ │
31- qr_2: ┤ H ├─■────────■───────────────────────────X─
32- └───┘
33- cr: 3/═════════════════════════════════════════════
22+ Build and simulate Grover's search algorithm.
23+
24+ The oracle marks the |11...1> state.
25+
26+ >>> grover_search(2)
27+ {'11': 9000, '10': 300, '01': 400, '00': 300}
28+
3429 Args:
35- n : number of qubits
30+ number_of_qubits (int): number of qubits
31+
3632 Returns:
37- qiskit.result.counts.Counts: distribute counts.
38-
39- >>> quantum_fourier_transform(2)
40- {'00': 2500, '01': 2500, '10': 2500, '11': 2500}
41- >>> quantum_fourier_transform(-1)
42- Traceback (most recent call last):
43- ...
44- ValueError: number of qubits must be > 0.
45- >>> quantum_fourier_transform('a')
46- Traceback (most recent call last):
47- ...
48- TypeError: number of qubits must be a integer.
49- >>> quantum_fourier_transform(100)
50- Traceback (most recent call last):
51- ...
52- ValueError: number of qubits too large to simulate(>10).
53- >>> quantum_fourier_transform(0.5)
54- Traceback (most recent call last):
55- ...
56- ValueError: number of qubits must be exact integer.
33+ qiskit.result.counts.Counts: distribution counts.
34+
35+ Raises:
36+ TypeError: if input is not integer
37+ ValueError: if invalid number of qubits
5738 """
39+
5840 if isinstance (number_of_qubits , str ):
59- raise TypeError ("number of qubits must be a integer." )
41+ raise TypeError ("number of qubits must be an integer." )
6042 if number_of_qubits <= 0 :
6143 raise ValueError ("number of qubits must be > 0." )
6244 if math .floor (number_of_qubits ) != number_of_qubits :
6345 raise ValueError ("number of qubits must be exact integer." )
6446 if number_of_qubits > 10 :
65- raise ValueError ("number of qubits too large to simulate(>10)." )
47+ raise ValueError ("number of qubits too large to simulate (>10)." )
6648
49+ # Create registers
6750 qr = QuantumRegister (number_of_qubits , "qr" )
6851 cr = ClassicalRegister (number_of_qubits , "cr" )
69-
7052 quantum_circuit = QuantumCircuit (qr , cr )
7153
72- counter = number_of_qubits
54+ # Step 1: Initialize superposition
55+ quantum_circuit .h (qr )
7356
74- for i in range (counter ):
75- quantum_circuit .h (number_of_qubits - i - 1 )
76- counter -= 1
77- for j in range (counter ):
78- quantum_circuit .cp (np .pi / 2 ** (counter - j ), j , counter )
57+ # -------- Oracle (mark |11...1>) --------
58+ quantum_circuit .h (number_of_qubits - 1 )
59+ quantum_circuit .mcx (list (range (number_of_qubits - 1 )), number_of_qubits - 1 )
60+ quantum_circuit .h (number_of_qubits - 1 )
7961
80- for k in range (number_of_qubits // 2 ):
81- quantum_circuit .swap (k , number_of_qubits - k - 1 )
62+ # -------- Diffuser --------
63+ quantum_circuit .h (qr )
64+ quantum_circuit .x (qr )
8265
83- # measure all the qubits
66+ quantum_circuit .h (number_of_qubits - 1 )
67+ quantum_circuit .mcx (list (range (number_of_qubits - 1 )), number_of_qubits - 1 )
68+ quantum_circuit .h (number_of_qubits - 1 )
69+
70+ quantum_circuit .x (qr )
71+ quantum_circuit .h (qr )
72+
73+ # Measure all qubits
8474 quantum_circuit .measure (qr , cr )
85- # simulate with 10000 shots
75+
76+ # Run simulator with 10000 shots
8677 backend = Aer .get_backend ("qasm_simulator" )
8778 job = execute (quantum_circuit , backend , shots = 10000 )
8879
@@ -91,6 +82,6 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts
9182
9283if __name__ == "__main__" :
9384 print (
94- f"Total count for quantum fourier transform state is: \
95- { quantum_fourier_transform (3 )} "
85+ f"Total count for Grover search state is: \
86+ { grover_search (3 )} "
9687 )
0 commit comments