-
Notifications
You must be signed in to change notification settings - Fork 983
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
Added BB84_example #2395
Added BB84_example #2395
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
@googlebot I signed it! |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
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 doing this. This program ran for over 10 minutes on my machine without terminating (I had to do it manually). I'll add more feedback once we get this running on Travis.
examples/BB84_cirq.py
Outdated
@@ -0,0 +1,148 @@ | |||
# Example program to demonstrate BB84 QKD Protocol |
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.
Rename this file to bb84.py
examples/BB84_cirq.py
Outdated
print_results(alice_basis, bob_basis, alice_state, key_bitstr) | ||
|
||
|
||
def build_bb84_circ(num_qubits, alice_basis, bob_basis, alice_state): |
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.
Rename to make_bb84_circuit
examples/BB84_cirq.py
Outdated
import cirq | ||
|
||
|
||
def main(): |
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.
Call this code in https://github.com/quantumlib/Cirq/blob/master/examples/examples_test.py to cover it on Travis.
examples/BB84_cirq.py
Outdated
print( | ||
f'Alice\'s sent bits:\t{np.array2string(alice_state, separator="")[1:-1]}' | ||
) | ||
print(f'Both bases Match::\t{basis_match}') |
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.
Call an assert
on expected_key_bits == actual_key_bits
.
examples/BB84_cirq.py
Outdated
|
||
def main(): | ||
|
||
num_qubits = 24 |
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 a lot of qubits for NISQ. Since we are going to be running this on Travis, let's use less resources.
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've reduced it to 16 :)
examples/BB84_cirq.py
Outdated
print( | ||
f'Alice\'s sent bits:\t{np.array2string(alice_state, separator="")[1:-1]}' | ||
) | ||
print(f'Both bases Match::\t{basis_match}') |
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.
.format
will make this an easier read. print('Both bases Match: {}'.format(basis_match))
examples/BB84_cirq.py
Outdated
print( | ||
f'Bob\'s Encoding basis:\t{"".join(np.where(bob_basis==0, "C", "H"))}') | ||
print( | ||
f'Alice\'s sent bits:\t{np.array2string(alice_state, separator="")[1:-1]}' |
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.
Use .format
to make this cleaner. See below suggestion.
examples/BB84_cirq.py
Outdated
f'Alice\'s Encoding basis:\t{"".join(np.where(alice_basis==0, "C","H"))}' | ||
) | ||
print( | ||
f'Bob\'s Encoding basis:\t{"".join(np.where(bob_basis==0, "C", "H"))}') |
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.
Use .format
to make this cleaner. See below suggestion.
examples/BB84_cirq.py
Outdated
basis_match += 'X' if alice_basis[i] == bob_basis[i] else '_' | ||
key_expected += str(char) if alice_basis[i] == bob_basis[i] else '_' | ||
|
||
print( |
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.
Use .format
to make this cleaner. See below suggestion.
Based on error logs from formatting test on Travis
I've added the suggested changes. Thanks for the guidance. What else do I need to do? |
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 takes 6 seconds to run on my machine. If we can half the runtime, that would be good.
A huge improvement we can make is on this program being filled with conversion of types. It makes it harder to read as an example. We could start with arrays of ints or strings as the states and bases and get rid of the brackets at the very end of the program. Try to reduce the number of calls to things like str
, np.array2string
, [1:-1]
, np.array
. Let's try to defer the conversions to the very end when we need the data to be in a human readable format, not between the programs.
@vtomole Could you point me to some resources which talk about how to get formatted output from the simulator? So far, I've been able to get output in Boolean form only. |
@deadbeatfour What's an example of what you need? |
Ping @deadbeatfour |
Hey, sorry! I'm busy with some exams right now. I'll get back to you ASAP! |
@deadbeatfour No rush. Good luck on your exams! |
Hey @deadbeatfour , I'd like to get this in. Please respond to my comments. |
Hey, really sorry. I'll get on it right away. I'll make the changes you requested. |
- Removed all numpy calls - Replaced all numpy.random functions with python standard library random function - Using list comprehensions instead of np.where() - Assert equality of keys before printing result - Use doctring at the beginnnig
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.
Using 16 qubits to demonstrate this is a lot. It takes about 3 seconds to run on my machine. Let's reduce this.
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.
Still need to address change >
to ⟩
" comments.
Should I add Eve to the same file? Should I reduce the qubit number to 8? |
Changed angle brackets, corrected typos
Yes. Let's add a situation with Eve. We should have another assert for when the channel is evesdropped. I'm thinking something like this output
Sure. |
What should we assert here? There's a probability that the keys do not match, but for a small number of qubits, it's reasonably likely for eavesdropping to go undetected as well. |
@deadbeatfour What if we seed the random generator so we get the same data every time? Just for the protocol that involves Eve. |
@deadbeatfour I now think Eve is best left for a subsequent pull request. Let's just get this merged. |
Hey, I've put in the bit with eavesdropping. |
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.
Thank you so much! One last thing. The papers you cited require that i have a subscription to read them. Let's replace them with https://en.wikipedia.org/wiki/BB84 and https://en.wikipedia.org/wiki/No-cloning_theorem.
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.
Attempting to re-merge.
Thanks :) |
No, thank you. :D |
Fix for #2367