Skip to content

Commit a1c22f7

Browse files
authored
Update q_fourier_transform.py
1 parent 260e3d8 commit a1c22f7

File tree

1 file changed

+31
-72
lines changed

1 file changed

+31
-72
lines changed

quantum/q_fourier_transform.py

+31-72
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,55 @@
1-
"""
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-
.
9-
References:
10-
https://en.wikipedia.org/wiki/Quantum_Fourier_transform
11-
https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html
12-
"""
13-
141
import math
15-
162
import numpy as np
17-
import qiskit
183
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute
194

20-
21-
def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts.Counts:
5+
def quantum_fourier_transform(number_of_qubits: int = 3) -> dict:
226
"""
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/═════════════════════════════════════════════
7+
Build and simulate the Quantum Fourier Transform (QFT) circuit
8+
for a given number of qubits using the Qiskit framework.
9+
3410
Args:
35-
n : number of qubits
36-
Returns:
37-
qiskit.result.counts.Counts: distribute counts.
11+
number_of_qubits (int): The number of qubits for the QFT circuit.
3812
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.
13+
Returns:
14+
dict: A dictionary containing the counts of measurement results.
15+
16+
Raises:
17+
ValueError: If the number of qubits is less than or equal to 0,
18+
greater than 10, or not an integer.
19+
TypeError: If the input is not an integer.
5720
"""
58-
if isinstance(number_of_qubits, str):
59-
raise TypeError("number of qubits must be a integer.")
21+
if not isinstance(number_of_qubits, int):
22+
raise TypeError("Number of qubits must be an integer.")
6023
if number_of_qubits <= 0:
61-
raise ValueError("number of qubits must be > 0.")
62-
if math.floor(number_of_qubits) != number_of_qubits:
63-
raise ValueError("number of qubits must be exact integer.")
24+
raise ValueError("Number of qubits must be > 0.")
6425
if number_of_qubits > 10:
65-
raise ValueError("number of qubits too large to simulate(>10).")
26+
raise ValueError("Number of qubits too large to simulate (>10).")
6627

6728
qr = QuantumRegister(number_of_qubits, "qr")
6829
cr = ClassicalRegister(number_of_qubits, "cr")
69-
7030
quantum_circuit = QuantumCircuit(qr, cr)
7131

72-
counter = number_of_qubits
32+
# Apply the QFT circuit
33+
for i in range(number_of_qubits):
34+
quantum_circuit.h(i)
35+
for j in range(i + 1, number_of_qubits):
36+
quantum_circuit.cp(np.pi / 2 ** (j - i), j, i)
7337

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)
38+
# Swap the qubits
39+
for i in range(number_of_qubits // 2):
40+
quantum_circuit.swap(i, number_of_qubits - i - 1)
7941

80-
for k in range(number_of_qubits // 2):
81-
quantum_circuit.swap(k, number_of_qubits - k - 1)
82-
83-
# measure all the qubits
42+
# Measure all qubits
8443
quantum_circuit.measure(qr, cr)
85-
# simulate with 10000 shots
44+
45+
# Simulate the circuit with 10000 shots
8646
backend = Aer.get_backend("qasm_simulator")
8747
job = execute(quantum_circuit, backend, shots=10000)
48+
result = job.result()
8849

89-
return job.result().get_counts(quantum_circuit)
50+
return result.get_counts(quantum_circuit)
9051

9152

9253
if __name__ == "__main__":
93-
print(
94-
f"Total count for quantum fourier transform state is: \
95-
{quantum_fourier_transform(3)}"
96-
)
54+
result_counts = quantum_fourier_transform(3)
55+
print(f"Total count for quantum fourier transform state is: {result_counts}")

0 commit comments

Comments
 (0)