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

Added BB84_example #2395

Merged
merged 23 commits into from
Mar 13, 2020
Merged

Added BB84_example #2395

merged 23 commits into from
Mar 13, 2020

Conversation

abhishekchak52
Copy link
Contributor

Fix for #2367

@googlebot
Copy link

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 @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@abhishekchak52
Copy link
Contributor Author

@googlebot I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes Makes googlebot stop complaining. and removed cla: no labels Oct 23, 2019
Copy link
Collaborator

@vtomole vtomole 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 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.

@@ -0,0 +1,148 @@
# Example program to demonstrate BB84 QKD Protocol
Copy link
Collaborator

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

print_results(alice_basis, bob_basis, alice_state, key_bitstr)


def build_bb84_circ(num_qubits, alice_basis, bob_basis, alice_state):
Copy link
Collaborator

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

import cirq


def main():
Copy link
Collaborator

@vtomole vtomole Oct 23, 2019

Choose a reason for hiding this comment

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

print(
f'Alice\'s sent bits:\t{np.array2string(alice_state, separator="")[1:-1]}'
)
print(f'Both bases Match::\t{basis_match}')
Copy link
Collaborator

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.


def main():

num_qubits = 24
Copy link
Collaborator

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.

Copy link
Contributor Author

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 :)

print(
f'Alice\'s sent bits:\t{np.array2string(alice_state, separator="")[1:-1]}'
)
print(f'Both bases Match::\t{basis_match}')
Copy link
Collaborator

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))

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]}'
Copy link
Collaborator

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.

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"))}')
Copy link
Collaborator

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.

basis_match += 'X' if alice_basis[i] == bob_basis[i] else '_'
key_expected += str(char) if alice_basis[i] == bob_basis[i] else '_'

print(
Copy link
Collaborator

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.

@abhishekchak52
Copy link
Contributor Author

I've added the suggested changes. Thanks for the guidance. What else do I need to do?

Copy link
Collaborator

@vtomole vtomole left a 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.

examples/bb84.py Outdated Show resolved Hide resolved
examples/bb84.py Outdated Show resolved Hide resolved
examples/bb84.py Outdated Show resolved Hide resolved
@abhishekchak52
Copy link
Contributor Author

@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.

@vtomole
Copy link
Collaborator

vtomole commented Oct 24, 2019

@deadbeatfour What's an example of what you need?

@vtomole
Copy link
Collaborator

vtomole commented Nov 11, 2019

Ping @deadbeatfour

@abhishekchak52
Copy link
Contributor Author

Hey, sorry! I'm busy with some exams right now. I'll get back to you ASAP!

@vtomole
Copy link
Collaborator

vtomole commented Nov 12, 2019

@deadbeatfour No rush. Good luck on your exams!

@vtomole
Copy link
Collaborator

vtomole commented Feb 18, 2020

Hey @deadbeatfour , I'd like to get this in. Please respond to my comments.

@abhishekchak52
Copy link
Contributor Author

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
Copy link
Collaborator

@vtomole vtomole left a 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.

examples/bb84.py Outdated Show resolved Hide resolved
examples/bb84.py Outdated Show resolved Hide resolved
examples/bb84.py Outdated Show resolved Hide resolved
examples/bb84.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@vtomole vtomole left a 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.

examples/bb84.py Outdated Show resolved Hide resolved
@abhishekchak52
Copy link
Contributor Author

Should I add Eve to the same file?
In the case of an eavesdropper being present, the assert at the end will fail sometimes. So what would the final output of the program be?

Should I reduce the qubit number to 8?

Changed angle brackets, corrected typos
@vtomole
Copy link
Collaborator

vtomole commented Feb 25, 2020

Should I add Eve to the same file?

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

Simulating non-eavesdropped protocol
Alice's basis:  HHHCHHCHCCHHHHHH
Bob's basis:    HCCHCCHCHHHHHHCH
Alice's bits:   1100101110101101
Bases match::   X_________XXXX_X
Expected key:   110111
Actual key:     110111


Simulating eavesdropped protocol
.....
Expected key:   100000
Actual key:     110111


Should I reduce the qubit number to 8?

Sure.

@abhishekchak52
Copy link
Contributor Author

We should have another assert for when the channel is eavesdropped.

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.

@vtomole
Copy link
Collaborator

vtomole commented Feb 28, 2020

@deadbeatfour What if we seed the random generator so we get the same data every time? Just for the protocol that involves Eve.

@vtomole
Copy link
Collaborator

vtomole commented Mar 13, 2020

@deadbeatfour I now think Eve is best left for a subsequent pull request. Let's just get this merged.

@abhishekchak52
Copy link
Contributor Author

Hey, I've put in the bit with eavesdropping.

Copy link
Collaborator

@vtomole vtomole left a 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.

Copy link
Collaborator

@vtomole vtomole left a 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.

@vtomole vtomole added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Mar 13, 2020
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Mar 13, 2020
@abhishekchak52
Copy link
Contributor Author

Thanks :)

@vtomole
Copy link
Collaborator

vtomole commented Mar 13, 2020

No, thank you. :D

@CirqBot CirqBot merged commit 626e16e into quantumlib:master Mar 13, 2020
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants