From afebdba325fc73c0affdde40c6a6f77f5d525f7b Mon Sep 17 00:00:00 2001 From: Abhishek Chakraborty Date: Sun, 30 Oct 2022 15:58:23 -0700 Subject: [PATCH 1/8] Added BB84 algorithm. --- quantum/bb84.py | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 quantum/bb84.py diff --git a/quantum/bb84.py b/quantum/bb84.py new file mode 100644 index 000000000000..289b4072f246 --- /dev/null +++ b/quantum/bb84.py @@ -0,0 +1,134 @@ +#!/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 +""" +from typing import Optional +import qiskit +import numpy as np + + +def BB84(key_len: int = 8, seed: Optional[int] = 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 (int): THe length of the generated key in bits. Default is 8. + + seed (int, Optional): Seed for the random number generator. + Mostly used for testing. Default is None. + + Returns: + key (str): 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) + # Retruns 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() From 010e02e0483aa4393e6e506d7032a3a7ca270567 Mon Sep 17 00:00:00 2001 From: Abhishek Chakraborty Date: Sun, 30 Oct 2022 16:09:53 -0700 Subject: [PATCH 2/8] Function name lowercase + imports fix I thought uppercase was appropriate because they're initials. --- quantum/bb84.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/quantum/bb84.py b/quantum/bb84.py index 289b4072f246..c8c0f2241e36 100644 --- a/quantum/bb84.py +++ b/quantum/bb84.py @@ -46,12 +46,11 @@ https://en.wikipedia.org/wiki/BB84 https://qiskit.org/textbook/ch-algorithms/quantum-key-distribution.html """ -from typing import Optional -import qiskit import numpy as np +import qiskit -def BB84(key_len: int = 8, seed: Optional[int] = None) -> str: +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. @@ -64,10 +63,10 @@ def BB84(key_len: int = 8, seed: Optional[int] = None) -> str: Returns: key (str): The key generated using BB84 protocol. - >>> BB84(16, seed=0) + >>> bb84(16, seed=0) '1101101100010000' - >>> BB84(8, seed=0) + >>> bb84(8, seed=0) '01011011' """ # Set up the random number generator. @@ -107,7 +106,7 @@ def BB84(key_len: int = 8, seed: Optional[int] = None) -> str: # 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) - # Retruns the result of measurement. + # Returns the result of measurement. result = job.result().get_counts(bb84_circ).most_frequent() # Extracting the generated key from the simulation results. @@ -128,7 +127,7 @@ def BB84(key_len: int = 8, seed: Optional[int] = None) -> str: if __name__ == "__main__": - print(f"The generated key is : {BB84(8, seed=0)}") + print(f"The generated key is : {bb84(8, seed=0)}") from doctest import testmod testmod() From 436bc7aa149162602b521f5fea0c4c711a124c95 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Oct 2022 23:04:26 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- quantum/bb84.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/bb84.py b/quantum/bb84.py index c8c0f2241e36..f01db21419cd 100644 --- a/quantum/bb84.py +++ b/quantum/bb84.py @@ -50,7 +50,7 @@ import qiskit -def bb84(key_len: int = 8, seed: int | None = None) -> str: +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. From e4fc4cefefb9ebf64b88a93f7dbd80ebbfc809ec Mon Sep 17 00:00:00 2001 From: Abhishek Chakraborty Date: Mon, 7 Nov 2022 10:21:06 -0800 Subject: [PATCH 4/8] Update quantum/bb84.py Co-authored-by: Christian Clauss --- quantum/bb84.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/quantum/bb84.py b/quantum/bb84.py index f01db21419cd..7b16e82fb5d5 100644 --- a/quantum/bb84.py +++ b/quantum/bb84.py @@ -55,13 +55,13 @@ 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 (int): THe length of the generated key in bits. Default is 8. + key_len: The length of the generated key in bits. The default is 8. - seed (int, Optional): Seed for the random number generator. + seed: Seed for the random number generator. Mostly used for testing. Default is None. Returns: - key (str): The key generated using BB84 protocol. + key: The key generated using BB84 protocol. >>> bb84(16, seed=0) '1101101100010000' From 025631f971439e9e7142fc1139799280a5e4f064 Mon Sep 17 00:00:00 2001 From: Abhishek Chakraborty Date: Mon, 7 Nov 2022 10:22:22 -0800 Subject: [PATCH 5/8] Removed python < 3.11 restriction from qiskit --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 00f31b85e404..d3002bb5d108 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ opencv-python pandas pillow projectq -qiskit; python_version < "3.11" +qiskit requests rich scikit-fuzzy From f49759717f0474d3f3dcab0f861b978610f8ec0d Mon Sep 17 00:00:00 2001 From: Abhishek Chakraborty Date: Mon, 7 Nov 2022 10:22:22 -0800 Subject: [PATCH 6/8] Removed python < 3.11 restriction from qiskit --- quantum/bb84.py | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/bb84.py b/quantum/bb84.py index 7b16e82fb5d5..ca43e63a7c5c 100644 --- a/quantum/bb84.py +++ b/quantum/bb84.py @@ -50,7 +50,7 @@ import qiskit -def BB84(key_len: int = 8, seed: int | None = None) -> str: +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. diff --git a/requirements.txt b/requirements.txt index 00f31b85e404..d3002bb5d108 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ opencv-python pandas pillow projectq -qiskit; python_version < "3.11" +qiskit requests rich scikit-fuzzy From 982e14b881b6e6c7489784c69d7b855538547cca Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 8 Nov 2022 11:31:53 +0100 Subject: [PATCH 7/8] scikit-learn --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index d3002bb5d108..a1d607df07e1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,7 @@ qiskit requests rich scikit-fuzzy -sklearn +scikit-learn statsmodels sympy tensorflow; python_version < "3.11" From 027294f54f2aaaf8d7249b5c45795eb269a6dd10 Mon Sep 17 00:00:00 2001 From: Abhishek Chakraborty Date: Tue, 8 Nov 2022 08:39:37 -0800 Subject: [PATCH 8/8] Update quantum/bb84.py Correct typo in `default_rng()` call Co-authored-by: Maxim Smolskiy --- quantum/bb84.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/bb84.py b/quantum/bb84.py index ca43e63a7c5c..60d64371fe63 100644 --- a/quantum/bb84.py +++ b/quantum/bb84.py @@ -70,7 +70,7 @@ def bb84(key_len: int = 8, seed: int | None = None) -> str: '01011011' """ # Set up the random number generator. - rng = np.random.default_rng(seed == seed) + rng = np.random.default_rng(seed=seed) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need.