Skip to content
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

add deutsch jozsa algorithm #177

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

tanishabassan
Copy link

Issue #, if available:

Description of changes:
Adding the Deutsch-Jozsa Algorithm example notebook.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@tanishabassan tanishabassan requested a review from a team as a code owner August 3, 2022 18:48
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@speller26
Copy link
Member

General comment: the notebook should be a mix of code and exposition, rather than just code is it currently is. For example take a look at the superdense coding and Simon's algorithm notebooks. They have many Markdown cells including background, explanations of steps, and diagrams.

@@ -0,0 +1,909 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for contributing! It would also more readable if we break down the functions in smaller components and likewise for other cells.


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good, I'll be making more changes to the notebook, making it more readable and adding more markdown.

@tanishabassan
Copy link
Author

General comment: the notebook should be a mix of code and exposition, rather than just code is it currently is. For example take a look at the superdense coding and Simon's algorithm notebooks. They have many Markdown cells including background, explanations of steps, and diagrams.

Hi Cody! Yes that's my next task, I'll be adding markdown to support my code and adding to my PR.

@@ -0,0 +1,1071 @@
{
Copy link
Member

@speller26 speller26 Aug 8, 2022

Choose a reason for hiding this comment

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

Rather than all text at the beginning followed by all code at the end, it's easier to follow code interspersed with text. For example, this step could directly follow step 2, while the next blocks can follow step 3.


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Done!

@@ -0,0 +1,1071 @@
{
Copy link
Member

Choose a reason for hiding this comment

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

Line #13.        single_gate_set = "x"

It's easier to work with if the list contains the gate itself:

single_gate_set = [gates.X()]

This way, you can programmatically attach gates to the circuit:

circuit.add_instruction(Instruction(random.choice(single_gate_set), qubit))


Reply via ReviewNB

@@ -0,0 +1,1071 @@
{
Copy link
Member

Choose a reason for hiding this comment

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

Line #16.        multiple_gate_set = ["cnot", "ccnot"]

Same goes here; put the gate instances in the list directly:

multiple_gate_set = [gates.CNot(), gates.CCNot()]

and add them to the circuit:

circuit.add_instruction(Instruction(gate, qubits))


Reply via ReviewNB

@@ -0,0 +1,1071 @@
{
Copy link
Member

Choose a reason for hiding this comment

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

Line #3.    def dj_algorithm(case, n_qubits):

This is a long function worth splitting into multiple smaller ones; as well, it looks like it contains duplicate or similar code from the previous cell that can be extracted out into common functions.


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Cut down this function!

@@ -0,0 +1,1071 @@
{
Copy link
Member

Choose a reason for hiding this comment

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

Line #2.    def classical_checker(n_qubits):

Same here


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Done!

@@ -0,0 +1,1071 @@
{

Choose a reason for hiding this comment

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

Line #29.            random_gate_set = np.random.choice(choice_gate_set, p=[0.30, 0.70])

Is this doing a weighting of 30/70% between the two options? Why not 50/50?


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

We found that if it was 50/50 then the function would create too many constant oracles and we wanted a good mix of both that's why the probability is uneven.

@aaronmarkham
Copy link

I would like to see some markdown discussion in between the code cells. It's helpful to set up what's about to happen preceding some action, then discuss / reflect on the outcome in the markdown cell after some code is executed.

Copy link

@aaronmarkham aaronmarkham left a comment

Choose a reason for hiding this comment

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

Thanks for the updated PR, Tanisha. While Braket notebooks don't have to conform to SageMaker's, there are some best practices to be found in the SageMaker notebooks template. Most notably, I was wondering if there's any cleanup of resources to be done at the end. And how about any references? Is there a recommended next tutorial after this one?

"source": [
"---------------------------------------------------------------------------------------------------------------------\n",
"\n",
"## For a random function:\n",

Choose a reason for hiding this comment

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

Suggested change
"## For a random function:\n",
"## For a random function\n",

Your other titles don't use a colon and it'll stand out on the website's index.
Also, an editor might ask you to make it active... like "Create a random oracle function".

Copy link
Author

Choose a reason for hiding this comment

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

Fixed the colon issue and sent it for review to Jessica Price for other text enhancements.

@@ -0,0 +1,1047 @@
{

Choose a reason for hiding this comment

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

Maybe change 'For the two possible cases, if the function is constant then the measurement will observe all 0’s, if the function is balanced then you will observe a mix of 1's and 0's and if you have both measurements present for a function the it's neither constant or balanced.' to 'For the two possible cases, if the function is constant then the measurement will observe all 0’s for the first qubit register and if the function is balanced then you will observe an equal mix of 1's and 0's for the first register and if you have both types of measurements present for a function the it's neither constant or balanced.'

Also, this is a standard mistake that everybody makes but you need to get your mathematical latex to work properly. So, for instance, $Uf$ should be $U_f$. Latex is not fun at first!


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Updated in most recent PR.

@@ -0,0 +1,1047 @@
{

Choose a reason for hiding this comment

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

I would change 'We create a list of different types of gates you can apply, the X gate a single gate and CNOT and CCNOT require multiple gates.' to 'We create a list of different types of gates you can apply, the X gate, which is a single qubit gate, and CNOT and CCNOT gates, which are multiple qubit gates.'


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Updated in most recent PR

@@ -0,0 +1,1047 @@
{

Choose a reason for hiding this comment

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

Okay first, I am curious if we should move the part of this section that I added to become a sub-section of the section titled 'Technical Background for Deutsch-Jozsa Algorithm.'

Also, I would change this sentence 'If the function was constant then the quantum state after the H-gate is applied should be the same as it’s initial state.' to 'If the function was constant then the quantum state after the H-gates are applied should be the same as it’s initial state.'

'


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Updated in most recent PR, did not move the section to Technical Background but can move it if you think it makes more sense to put it there.

@@ -0,0 +1,1047 @@
{

Choose a reason for hiding this comment

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

I like the first paragraph. I believe 2^n-1 should just be 2^n. Also, I think it might be good to change 'but this time on a quantum circuit.' to 'on a quantum circuit.'


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Updated on most recent PR

@@ -0,0 +1,1047 @@
{

Choose a reason for hiding this comment

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

It seems like there are missing word here. I think everybody would agree.

Also, maybe mention that we can also implement functions that are neither constant or balanced.


Reply via ReviewNB

Copy link
Author

Choose a reason for hiding this comment

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

Fixed this paragraph!

@tanishabassan
Copy link
Author

Thanks for the updated PR, Tanisha. While Braket notebooks don't have to conform to SageMaker's, there are some best practices to be found in the SageMaker notebooks template. Most notably, I was wondering if there's any cleanup of resources to be done at the end. And how about any references? Is there a recommended next tutorial after this one?

Added some resources at the end of the tutorial and referenced a textbook from where I learnt a lot about the algorithm.

@tanishabassan
Copy link
Author

I would like to see some markdown discussion in between the code cells. It's helpful to set up what's about to happen preceding some action, then discuss / reflect on the outcome in the markdown cell after some code is executed.

Implemented in most recent PR

@@ -0,0 +1,1037 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

"what type of function Uf is"


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

*basis state-- only |0> is the ground state


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: can will


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Line #22.        #Designate the device being used as the local simulator, feel free to use another device

maybe change to instruct how to use another device (ie. commented out loc)


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the qiskit link the right resource here?


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

This is just me, but I think that the placement of commas can be improved upon.

In this tutorial, we...

solved classically, but instead...

programming language, linear algebra, and has...

this tutorial, but if you'd like....


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

quantum algorithms that can be natively implemented on...


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

black box problem, where the task at hand is trying to determine what the black box is.

applicable in real life, but showcases...

computes a Boolean function, which only...

what type of function U_f is based on only...

is as large as 2^n, then the amount...

large enough n, this problem...

However, through leveraging a quantum algorithm, we only...

different answers, each of which solves the problem.


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

To build the algorithm, we first prepare our qubits according to our input; the input state prepares n-qubits in the |0⟩ state for the query register and an extra qubit for the answer register is prepared in the |1⟩ state.

superposition of all possible binary values and the...

for the oracle U_f with the inputs being ∣x,y⟩→∣x,y⊕f(x)⟩, giving....

We apply Hadamard gates to the query register to remove the qubits from their superposition state.

For the two possible cases, if the function is constant, then the measurement will observe all 0’s for the first qubit register and if the function is balanced, then you will observe an equal mix of 1's and 0's for the first register. However, if you have both types of measurements present for a function, the function is neither constant or balanced.


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

Now for both our qubit registers, we apply a Hadamard gate.

For example, if you have a coin, you only have two sides: heads or tails.

Now imagine that you apply some force to this coin and balance it so that it's neither head or tails, but in a superposition of both possible sides.

The qubits in the first and second registers are now in a superposition of 0 and 1.


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

We create a list of different types of gates you can apply: the X gate, which is a single qubit gate, as well as CNOT and CCNOT gates, which are multiple qubit gates.

We randomly choose which type of gates to apply and then apply these gates on randomly chosen qubits to see what kind of function has been created.


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

After applying the quantum oracle, an H-gate is applied to all the qubits in the first register.

This is because if there are three qubits in the first register, then the initial quantum state looks as follows after the first set of H-gates have acted on it:


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

possible combinations, where n is the number of qubits you've initialized. 

If n = 3, then we have 8 different possible inputs. We then determine if all of these inputs result in a function that is constant, balanced or neither, so we can compare it with the results we obtain when we run the quantum version of this algorithm.


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

based on the output, we can determine what type...


Reply via ReviewNB

@@ -0,0 +1,1037 @@
{

Choose a reason for hiding this comment

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

Once you've run the classical and quantum version of the random circuit, you can compare the results. If both correspond to the same type of function, then we have found another way to implement the constant or balanced function!

You can keep generating more random circuits to see how the constant and balanced functions can be created; including for circuits that are extremely large, for example 10 qubits!


Reply via ReviewNB

@tanishabassan tanishabassan changed the title add deutsch jozsa aglorithm add deutsch jozsa algorithm Sep 14, 2022
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.

6 participants