-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
create quantum_fourier_transform #6682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ba5499f
create quantum_fourier_transform
KevinJoven11 0b6170b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d8098c8
Update q_fourier_transform.py
KevinJoven11 1da7270
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ba2fe16
Add the doctest!
KevinJoven11 b9ae107
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0392116
Update q_fourier_transform.py
KevinJoven11 6bd53c2
Pass first then fail
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
""" | ||
Build the quantum fourier transform (qft) for a desire | ||
number of quantum bits using Qiskit framework. This | ||
experiment run in IBM Q simulator with 10000 shots. | ||
This circuit can be use as a building block to design | ||
the Shor's algorithm in quantum computing. As well as, | ||
quantum phase estimation among others. | ||
. | ||
References: | ||
https://en.wikipedia.org/wiki/Quantum_Fourier_transform | ||
https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html | ||
""" | ||
|
||
import math | ||
|
||
import numpy as np | ||
import qiskit | ||
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute | ||
|
||
|
||
def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts.Counts: | ||
""" | ||
# >>> quantum_fourier_transform(2) | ||
# {'00': 2500, '01': 2500, '11': 2500, '10': 2500} | ||
# quantum circuit for number_of_qubits = 3: | ||
┌───┐ | ||
qr_0: ──────■──────────────────────■───────┤ H ├─X─ | ||
│ ┌───┐ │P(π/2) └───┘ │ | ||
qr_1: ──────┼────────■───────┤ H ├─■─────────────┼─ | ||
┌───┐ │P(π/4) │P(π/2) └───┘ │ | ||
qr_2: ┤ H ├─■────────■───────────────────────────X─ | ||
└───┘ | ||
cr: 3/═════════════════════════════════════════════ | ||
Args: | ||
n : number of qubits | ||
Returns: | ||
qiskit.result.counts.Counts: distribute counts. | ||
|
||
>>> quantum_fourier_transform(-1) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Traceback (most recent call last): | ||
... | ||
ValueError: number of qubits must be > 0. | ||
>>> quantum_fourier_transform('a') | ||
Traceback (most recent call last): | ||
... | ||
TypeError: number of qubits must be a integer. | ||
>>> quantum_fourier_transform(100) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: number of qubits too large to simulate(>10). | ||
>>> quantum_fourier_transform(0.5) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: number of qubits must be exact integer. | ||
""" | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if type(number_of_qubits) == str: | ||
raise TypeError("number of qubits must be a integer.") | ||
if not number_of_qubits > 0: | ||
raise ValueError("number of qubits must be > 0.") | ||
if math.floor(number_of_qubits) != number_of_qubits: | ||
raise ValueError("number of qubits must be exact integer.") | ||
if number_of_qubits > 10: | ||
raise ValueError("number of qubits too large to simulate(>10).") | ||
|
||
qr = QuantumRegister(number_of_qubits, "qr") | ||
cr = ClassicalRegister(number_of_qubits, "cr") | ||
|
||
quantum_circuit = QuantumCircuit(qr, cr) | ||
|
||
counter = number_of_qubits | ||
|
||
for i in range(counter): | ||
|
||
quantum_circuit.h(number_of_qubits - i - 1) | ||
counter -= 1 | ||
for j in range(counter): | ||
quantum_circuit.cp(np.pi / 2 ** (counter - j), j, counter) | ||
|
||
for k in range(number_of_qubits // 2): | ||
quantum_circuit.swap(k, number_of_qubits - k - 1) | ||
|
||
# measure all the qubits | ||
quantum_circuit.measure(qr, cr) | ||
# simulate with 10000 shots | ||
backend = Aer.get_backend("qasm_simulator") | ||
job = execute(quantum_circuit, backend, shots=10000) | ||
|
||
return job.result().get_counts(quantum_circuit) | ||
|
||
|
||
if __name__ == "__main__": | ||
print( | ||
f"Total count for quantum fourier transform state is: \ | ||
{quantum_fourier_transform(3)}" | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.