Skip to content

More Pythonic boolean comparisons #25

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ch03/bb84.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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)}.")

7 changes: 3 additions & 4 deletions ch03/qkd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
##

from interface import QuantumDevice, Qubit
from simulator import SingleQubitSimulator

def prepare_classical_message(bit: bool, q: Qubit) -> None:
if bit:
Expand All @@ -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:
Expand All @@ -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"
5 changes: 2 additions & 3 deletions ch04/chsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand All @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions ch05/chsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand All @@ -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:
Expand Down