|
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 |
| - |
14 | 1 | import math
|
15 | 2 |
|
16 | 3 | import numpy as np
|
|
20 | 7 |
|
21 | 8 | def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts.Counts:
|
22 | 9 | """
|
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/═════════════════════════════════════════════ |
| 10 | + Creates a quantum Fourier transform circuit and simulates it on a quantum simulator. |
| 11 | +
|
34 | 12 | Args:
|
35 |
| - n : number of qubits |
| 13 | + number_of_qubits (int): The number of qubits in the quantum Fourier transform circuit. |
| 14 | +
|
36 | 15 | Returns:
|
37 |
| - qiskit.result.counts.Counts: distribute counts. |
| 16 | + qiskit.result.counts.Counts: The counts of the measurement results. |
| 17 | +
|
| 18 | + Raises: |
| 19 | + TypeError: If number_of_qubits is not an integer. |
| 20 | + ValueError: If number_of_qubits is not a positive integer or is too large to simulate. |
| 21 | +
|
| 22 | + Examples: |
| 23 | + >>> quantum_fourier_transform(2) |
| 24 | + {'00': 2500, '01': 2500, '10': 2500, '11': 2500} |
38 | 25 |
|
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): |
| 26 | + >>> quantum_fourier_transform(3) # returns a result close to this due to randomness |
| 27 | + {'000': 1250, '001': 1250, '010': 1250, '011': 1250, '100': 1250, '101': 1250, |
| 28 | + '110': 1250, '111': 1250} |
| 29 | +
|
| 30 | + >>> quantum_fourier_transform(1) |
| 31 | + {'0': 5000, '1': 5000} |
| 32 | +
|
| 33 | + >>> quantum_fourier_transform(-1) |
| 34 | + Traceback (most recent call last): |
43 | 35 | ...
|
44 |
| - ValueError: number of qubits must be > 0. |
45 |
| - >>> quantum_fourier_transform('a') |
46 |
| - Traceback (most recent call last): |
| 36 | + ValueError: number of qubits must be > 0. |
| 37 | +
|
| 38 | + >>> quantum_fourier_transform('a') |
| 39 | + Traceback (most recent call last): |
47 | 40 | ...
|
48 |
| - TypeError: number of qubits must be a integer. |
49 |
| - >>> quantum_fourier_transform(100) |
50 |
| - Traceback (most recent call last): |
| 41 | + TypeError: number of qubits must be a integer. |
| 42 | +
|
| 43 | + >>> quantum_fourier_transform(100) |
| 44 | + Traceback (most recent call last): |
51 | 45 | ...
|
52 |
| - ValueError: number of qubits too large to simulate(>10). |
53 |
| - >>> quantum_fourier_transform(0.5) |
54 |
| - Traceback (most recent call last): |
| 46 | + ValueError: number of qubits too large to simulate(>10). |
| 47 | +
|
| 48 | + >>> quantum_fourier_transform(0.5) |
| 49 | + Traceback (most recent call last): |
55 | 50 | ...
|
56 |
| - ValueError: number of qubits must be exact integer. |
| 51 | + ValueError: number of qubits must be exact integer. |
57 | 52 | """
|
58 | 53 | if isinstance(number_of_qubits, str):
|
59 | 54 | raise TypeError("number of qubits must be a integer.")
|
@@ -90,6 +85,8 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts
|
90 | 85 |
|
91 | 86 |
|
92 | 87 | if __name__ == "__main__":
|
| 88 | + import doctest |
| 89 | + doctest.testmod() |
93 | 90 | print(
|
94 | 91 | f"Total count for quantum fourier transform state is: \
|
95 | 92 | {quantum_fourier_transform(3)}"
|
|
0 commit comments