From 1666b95cd1a8555699e6f39acf6e2539cd238914 Mon Sep 17 00:00:00 2001 From: Hristo Georgiev Date: Tue, 10 May 2022 23:26:29 +0100 Subject: [PATCH] More Pythonic boolean comparisons --- ch03/bb84.py | 6 +++--- ch03/qkd.py | 7 +++---- ch04/chsh.py | 5 ++--- ch05/chsh.py | 5 ++--- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/ch03/bb84.py b/ch03/bb84.py index e8e230f..f71cbe9 100644 --- a/ch03/bb84.py +++ b/ch03/bb84.py @@ -74,8 +74,8 @@ def simulate_bb84(n_bits: int) -> list: ((your_message, your_basis), (eve_result, eve_basis)) = \ send_single_bit_with_bb84(your_device, eve_device) - if your_basis == eve_basis: - assert your_message == eve_result + if your_basis is eve_basis: + assert your_message is eve_result key.append(your_message) print(f"Took {n_rounds} rounds to generate a {n_bits}-bit key.") @@ -114,4 +114,4 @@ def apply_one_time_pad(message: List[bool], key: List[bool]) -> List[bool]: decrypted_message = apply_one_time_pad(encrypted_message, key) print(f"Eve decrypted to get: {convert_to_hex(decrypted_message)}.") - \ No newline at end of file + diff --git a/ch03/qkd.py b/ch03/qkd.py index 2dc4211..c393dc7 100644 --- a/ch03/qkd.py +++ b/ch03/qkd.py @@ -12,7 +12,6 @@ ## from interface import QuantumDevice, Qubit -from simulator import SingleQubitSimulator def prepare_classical_message(bit: bool, q: Qubit) -> None: if bit: @@ -26,7 +25,7 @@ def send_classical_bit(device: QuantumDevice, bit: bool) -> None: prepare_classical_message(bit, q) result = eve_measure(q) q.reset() - assert result == bit + assert result is bit def qrng(device: QuantumDevice) -> bool: @@ -47,10 +46,10 @@ def send_classical_bit_plusminus(device: QuantumDevice, bit: bool) -> None: with device.using_qubit() as q: prepare_classical_message_plusminus(bit, q) result = eve_measure_plusminus(q) - assert result == bit + assert result is bit def send_classical_bit_wrong_basis(device: QuantumDevice, bit: bool) -> None: with device.using_qubit() as q: prepare_classical_message(bit, q) result = eve_measure_plusminus(q) - assert result == bit, "Two parties do not have the same bit value" + assert result is bit, "Two parties do not have the same bit value" diff --git a/ch04/chsh.py b/ch04/chsh.py index 5b69b11..7933c0f 100644 --- a/ch04/chsh.py +++ b/ch04/chsh.py @@ -20,7 +20,6 @@ from typing import Tuple, Callable import numpy as np -from interface import QuantumDevice, Qubit from simulator import Simulator Strategy = Tuple[Callable[[int], int], Callable[[int], int]] @@ -31,8 +30,8 @@ def random_bit() -> int: def referee(strategy: Callable[[], Strategy]) -> bool: you, eve = strategy() your_input, eve_input = random_bit(), random_bit() - parity = 0 if you(your_input) == eve(eve_input) else 1 - return parity == (your_input and eve_input) + parity = 0 if you(your_input) is eve(eve_input) else 1 + return bool(parity) is (bool(your_input) and bool(eve_input)) def est_win_probability(strategy: Callable[[], Strategy], n_games: int = 1000) -> float: diff --git a/ch05/chsh.py b/ch05/chsh.py index e1c92ec..ade5442 100644 --- a/ch05/chsh.py +++ b/ch05/chsh.py @@ -16,7 +16,6 @@ from typing import Tuple, Callable import numpy as np -from interface import QuantumDevice, Qubit from simulator import Simulator Strategy = Tuple[Callable[[int], int], Callable[[int], int]] @@ -27,8 +26,8 @@ def random_bit() -> int: def referee(strategy: Callable[[], Strategy]) -> bool: you, eve = strategy() your_input, eve_input = random_bit(), random_bit() - parity = 0 if you(your_input) == eve(eve_input) else 1 - return parity == (your_input and eve_input) + parity = 0 if you(your_input) is eve(eve_input) else 1 + return bool(parity) == (bool(your_input) and bool(eve_input)) def est_win_probability(strategy: Callable[[], Strategy], n_games: int = 1000) -> float: