Skip to content

Implement Deutsch-Jozsa Algorithm In Qiskit #3447

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 4 commits into from
Oct 18, 2020

Conversation

abhishekjiitr
Copy link
Contributor

@abhishekjiitr abhishekjiitr commented Oct 17, 2020

Signed-off-by: Abhishek Jaisingh abhi2254015@gmail.com

Describe your change:

Implement Deutsch-Jozsa Algorithm In Qiskit

  • Add an algorithm?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@abhishekjiitr abhishekjiitr marked this pull request as draft October 17, 2020 12:38
@github-actions github-actions bot force-pushed the quantum-deutsch-jozsa branch from 8dd6f21 to a387935 Compare October 17, 2020 12:46
@github-actions github-actions bot force-pushed the quantum-deutsch-jozsa branch from b7ea8ec to ce74e77 Compare October 17, 2020 13:16
@abhishekjiitr abhishekjiitr marked this pull request as ready for review October 17, 2020 13:16
@github-actions github-actions bot force-pushed the quantum-deutsch-jozsa branch from a766869 to d740492 Compare October 17, 2020 13:17
@abhishekjiitr
Copy link
Contributor Author

Implemented the Deutsch-Jozsa Algorithm, one of the first examples of a quantum algorithm that is exponentially faster than any possible deterministic classical algorithm
@cclauss please review

@github-actions github-actions bot force-pushed the quantum-deutsch-jozsa branch from 809321b to 7c5da9e Compare October 17, 2020 13:23
Signed-off-by: Abhishek Jaisingh <abhi2254015@gmail.com>
@github-actions github-actions bot force-pushed the quantum-deutsch-jozsa branch from f17c98a to e4d6caf Compare October 17, 2020 13:24
classical algorithm

Premise:
We are given a hidden Boolean function f ,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
We are given a hidden Boolean function f ,
We are given a hidden Boolean function f,

Copy link
Contributor Author

@abhishekjiitr abhishekjiitr Oct 17, 2020

Choose a reason for hiding this comment

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

@cclauss fixed extra whitespace in newly added commit

@github-actions github-actions bot force-pushed the quantum-deutsch-jozsa branch from 06d8e34 to 07d33ab Compare October 17, 2020 13:42
Signed-off-by: Abhishek Jaisingh <abhi2254015@gmail.com>
@abhishekjiitr abhishekjiitr requested a review from cclauss October 17, 2020 13:43
@github-actions github-actions bot force-pushed the quantum-deutsch-jozsa branch from 55faf64 to 517d327 Compare October 17, 2020 13:44
@dhruvmanila
Copy link
Member

dhruvmanila commented Oct 18, 2020

@cclauss What's the deal with this GitHub action which applies black to the code? Why doesn't it work on every other PR as well? According to you, it does not work.

Oh, nevermind. I get it why it works on some and not on others. The user has turned on the workflow in their forked copy and this is coming from there.

I propose to remove it and the codespell action as both of them are checked with pre-commit.

@cclauss
Copy link
Member

cclauss commented Oct 18, 2020

The thing that it does do is... If a maintainer (with write privileges to the repo) creates a PR then the Action blackens the entire repo. This ensures that from time to time the entire repo is blackened even if someone lands a PR that is not green.

Go ahead and make the changes that you suggest above.

Comment on lines 121 to 125
counts = deutsch_jozsa("constant", 3)
print(f"Deutsch Jozsa - Constant Oracle: {counts}")

counts = deutsch_jozsa("balanced", 3)
print(f"Deutsch Jozsa - Balanced Oracle: {counts}")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
counts = deutsch_jozsa("constant", 3)
print(f"Deutsch Jozsa - Constant Oracle: {counts}")
counts = deutsch_jozsa("balanced", 3)
print(f"Deutsch Jozsa - Balanced Oracle: {counts}")
print(f"Deutsch Jozsa - Constant Oracle: {deutsch_jozsa("constant", 3)}")
print(f"Deutsch Jozsa - Balanced Oracle: {deutsch_jozsa("balanced", 3)}")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed 👍

# wrap in X-gates:
b = np.random.randint(1, 2 ** n)
# Next, format 'b' as a binary string of length 'n', padded with zeros:
b_str = format(b, "0" + str(n) + "b")
Copy link
Member

Choose a reason for hiding this comment

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

Please use a f-string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

switched to f-string

# Next, we place the first X-gates. Each digit in our binary string
# correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1
# we apply an X-gate to that qubit:
for qubit in range(len(b_str)):
Copy link
Member

Choose a reason for hiding this comment

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

Please use enumerate() instead of range(len()).

Copy link
Member

Choose a reason for hiding this comment

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

qubit is not a qubit, it is an int. Maybe call it I or index.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

using enumerate now, changed var name to index

for qubit in range(n):
oracle_qc.cx(qubit, n)
# Next, place the final X-gates
for qubit in range(len(b_str)):
Copy link
Member

Choose a reason for hiding this comment

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

enumerate()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

import qiskit as q


def dj_oracle(case: str, n: int) -> q.QuantumCircuit:
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this should be two separate functions.

Copy link
Member

Choose a reason for hiding this comment

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

The variable name n is not self documenting. Can we come up with a more descriptive name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed var name from n to num_qubits

Copy link
Contributor Author

@abhishekjiitr abhishekjiitr Oct 18, 2020

Choose a reason for hiding this comment

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

However, I feel the function is fine as it is.
I think we shouldn't split the function, as it seems to be more complete in its current form. @cclauss what do you think?

@github-actions github-actions bot force-pushed the quantum-deutsch-jozsa branch from 5745184 to c818db8 Compare October 18, 2020 14:43
@abhishekjiitr abhishekjiitr requested a review from cclauss October 18, 2020 14:50
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

RSLGTM

@cclauss cclauss merged commit beb2c35 into TheAlgorithms:master Oct 18, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Implement Deutsch-Jozsa Algorithm In Qiskit

Signed-off-by: Abhishek Jaisingh <abhi2254015@gmail.com>

* Add Changes Requested In Review

Signed-off-by: Abhishek Jaisingh <abhi2254015@gmail.com>

* Address Further Review Comments

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
* Implement Deutsch-Jozsa Algorithm In Qiskit

Signed-off-by: Abhishek Jaisingh <abhi2254015@gmail.com>

* Add Changes Requested In Review

Signed-off-by: Abhishek Jaisingh <abhi2254015@gmail.com>

* Address Further Review Comments

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
* Implement Deutsch-Jozsa Algorithm In Qiskit

Signed-off-by: Abhishek Jaisingh <abhi2254015@gmail.com>

* Add Changes Requested In Review

Signed-off-by: Abhishek Jaisingh <abhi2254015@gmail.com>

* Address Further Review Comments

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants