Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
algorithms-python/quantum/bb84.py at master · zinating/algorithms-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
zinating
/
algorithms-python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
algorithms-python
/
quantum
/
bb84.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
133 lines (102 loc) · 4.11 KB
master
Breadcrumbs
algorithms-python
/
quantum
/
bb84.py
Copy path
Top
File metadata and controls
Code
Blame
133 lines (102 loc) · 4.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
"""
Simulation of the Quantum Key Distribution (QKD) protocol called BB84,
created by Charles Bennett and Gilles Brassard in 1984.
BB84 is a key-distribution protocol that ensures secure key distribution
using qubits instead of classical bits. The generated key is the result
of simulating a quantum circuit. Our algorithm to construct the circuit
is as follows:
Alice generates two binary strings. One encodes the basis for each qubit:
- 0 -> {0,1} basis.
- 1 -> {+,-} basis.
The other encodes the state:
- 0 -> |0> or |+>.
- 1 -> |1> or |->.
Bob also generates a binary string and uses the same convention to choose
a basis for measurement. Based on the following results, we follow the
algorithm below:
X|0> = |1>
H|0> = |+>
HX|0> = |->
1. Whenever Alice wants to encode 1 in a qubit, she applies an
X (NOT) gate to the qubit. To encode 0, no action is needed.
2. Wherever she wants to encode it in the {+,-} basis, she applies
an H (Hadamard) gate. No action is necessary to encode a qubit in
the {0,1} basis.
3. She then sends the qubits to Bob (symbolically represented in
this circuit using wires).
4. Bob measures the qubits according to his binary string for
measurement. To measure a qubit in the {+,-} basis, he applies
an H gate to the corresponding qubit and then performs a measurement.
References:
https://en.wikipedia.org/wiki/BB84
https://qiskit.org/textbook/ch-algorithms/quantum-key-distribution.html
"""
import
numpy
as
np
import
qiskit
def
bb84
(
key_len
:
int
=
8
,
seed
:
int
|
None
=
None
)
->
str
:
"""
Performs the BB84 protocol using a key made of `key_len` bits.
The two parties in the key distribution are called Alice and Bob.
Args:
key_len: The length of the generated key in bits. The default is 8.
seed: Seed for the random number generator.
Mostly used for testing. Default is None.
Returns:
key: The key generated using BB84 protocol.
>>> bb84(16, seed=0)
'1101101100010000'
>>> bb84(8, seed=0)
'01011011'
"""
# Set up the random number generator.
rng
=
np
.
random
.
default_rng
(
seed
=
seed
)
# Roughly 25% of the qubits will contribute to the key.
# So we take more than we need.
num_qubits
=
6
*
key_len
# Measurement basis for Alice's qubits.
alice_basis
=
rng
.
integers
(
2
,
size
=
num_qubits
)
# The set of states Alice will prepare.
alice_state
=
rng
.
integers
(
2
,
size
=
num_qubits
)
# Measurement basis for Bob's qubits.
bob_basis
=
rng
.
integers
(
2
,
size
=
num_qubits
)
# Quantum Circuit to simulate BB84
bb84_circ
=
qiskit
.
QuantumCircuit
(
num_qubits
,
name
=
"BB84"
)
# Alice prepares her qubits according to rules above.
for
index
,
_
in
enumerate
(
alice_basis
):
if
alice_state
[
index
]
==
1
:
bb84_circ
.
x
(
index
)
if
alice_basis
[
index
]
==
1
:
bb84_circ
.
h
(
index
)
bb84_circ
.
barrier
()
# Bob measures the received qubits according to rules above.
for
index
,
_
in
enumerate
(
bob_basis
):
if
bob_basis
[
index
]
==
1
:
bb84_circ
.
h
(
index
)
bb84_circ
.
barrier
()
bb84_circ
.
measure_all
()
# Simulate the quantum circuit.
sim
=
qiskit
.
Aer
.
get_backend
(
"aer_simulator"
)
# We only need to run one shot because the key is unique.
# Multiple shots will produce the same key.
job
=
qiskit
.
execute
(
bb84_circ
,
sim
,
shots
=
1
,
seed_simulator
=
seed
)
# Returns the result of measurement.
result
=
job
.
result
().
get_counts
(
bb84_circ
).
most_frequent
()
# Extracting the generated key from the simulation results.
# Only keep measurement results where Alice and Bob chose the same basis.
gen_key
=
""
.
join
(
[
result_bit
for
alice_basis_bit
,
bob_basis_bit
,
result_bit
in
zip
(
alice_basis
,
bob_basis
,
result
)
if
alice_basis_bit
==
bob_basis_bit
]
)
# Get final key. Pad with 0 if too short, otherwise truncate.
key
=
gen_key
[:
key_len
]
if
len
(
gen_key
)
>=
key_len
else
gen_key
.
ljust
(
key_len
,
"0"
)
return
key
if
__name__
==
"__main__"
:
print
(
f"The generated key is :
{
bb84
(
8
,
seed
=
0
)
}
"
)
from
doctest
import
testmod
testmod
()
You can’t perform that action at this time.