-
Notifications
You must be signed in to change notification settings - Fork 226
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
base: main
Are you sure you want to change the base?
Conversation
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
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 @@ | |||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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 @@ | |||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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
examples/advanced_circuits_algorithms/Deutsch_Jozsa_Algorithm/Deutsch Jozsa Algorithm.ipynb
Show resolved
Hide resolved
@@ -0,0 +1,1071 @@ | |||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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. |
There was a problem hiding this 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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"## 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".
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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,
Reply via ReviewNB
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed this paragraph!
Added some resources at the end of the tutorial and referenced a textbook from where I learnt a lot about the algorithm. |
Implemented in most recent PR |
@@ -0,0 +1,1037 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,1037 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,1037 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,1037 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,1037 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,1037 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,1037 @@ | |||
{ |
There was a problem hiding this comment.
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
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.