Skip to content

Fix doctest q fourier transform.py #9943 #10624

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repos:
- id: auto-walrus

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
rev: v0.1.0
hooks:
- id: ruff

Expand Down Expand Up @@ -46,7 +46,7 @@ repos:
pass_filenames: false

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.14
rev: v0.15
hooks:
- id: validate-pyproject

Expand Down
30 changes: 21 additions & 9 deletions quantum/q_fourier_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
https://en.wikipedia.org/wiki/Quantum_Fourier_transform
https://qiskit.org/textbook/ch-algorithms/quantum-fourier-transform.html
"""

import math

import numpy as np
Expand All @@ -20,8 +19,6 @@

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─
Expand All @@ -36,8 +33,24 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts
Returns:
qiskit.result.counts.Counts: distribute counts.

>>> quantum_fourier_transform(2)
{'00': 2500, '01': 2500, '10': 2500, '11': 2500}
>>> result = quantum_fourier_transform(2)
>>> 2350<=result['10']<=2600
True
>>> 2350<=result['00']<=2600
True
>>> 2350<=result['11']<=2600
True
>>> 2350<=result['01']<=2600
True
>>> res = quantum_fourier_transform(3)
>>> 1150<=res['000']<=1350 and 1150<=res['001']<=1350
True
>>> 1150<=res['010']<=1350 and 1150<=res['100']<=1350
True
>>> 1150<=res['101']<=1350 and 1150<=res['110']<=1350
True
>>> 1150<=res['011']<=1350 and 1150<=res['111']<=1350
True
>>> quantum_fourier_transform(-1)
Traceback (most recent call last):
...
Expand Down Expand Up @@ -90,7 +103,6 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts


if __name__ == "__main__":
print(
f"Total count for quantum fourier transform state is: \
{quantum_fourier_transform(3)}"
)
Comment on lines -93 to -96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this?

Copy link
Contributor Author

@Jaivignesh-afk Jaivignesh-afk Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i got error relating to more no of lines in the pre-commit.So i checked with other algorithms there were no print statements..So,but i can change it back
I think, qiskit module is not yet available for Python 3.12 +
maybe that is the reason?

import doctest

doctest.testmod()