Skip to content

Commit 90618a2

Browse files
committed
Add Parameter Rename to Simon's Algorithm'
1 parent 7b90b1c commit 90618a2

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

quantum/simons_algorithm.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@ def xor_bits(bits1: list[int], bits2: list[int]) -> list[int]:
2828
return [x ^ y for x, y in zip(bits1, bits2)]
2929

3030

31-
def simons_algorithm(f: Callable[[list[int]], list[int]], num_bits: int) -> list[int]:
31+
def simons_algorithm(
32+
hidden_function: Callable[[list[int]], list[int]], num_bits: int
33+
) -> list[int]:
3234
"""
3335
Simulate Simon's algorithm classically to find the hidden bitstring s.
3436
3537
Args:
36-
f: A function mapping n-bit input to n-bit output.
38+
hidden_function: A function mapping n-bit input to n-bit output.
3739
num_bits: Number of bits in the input.
3840
3941
Returns:
4042
The hidden bitstring s as a list of bits.
4143
4244
>>> # Example with hidden bitstring s = [1, 0, 1]
4345
>>> s = [1, 0, 1]
44-
>>> def f(input_bits):
46+
>>> def hidden_function(input_bits):
4547
... mapping = {
4648
... (0,0,0): (1,1,0),
4749
... (1,0,1): (1,1,0),
@@ -53,14 +55,14 @@ def simons_algorithm(f: Callable[[list[int]], list[int]], num_bits: int) -> list
5355
... (1,1,0): (0,0,0),
5456
... }
5557
... return mapping[tuple(input_bits)]
56-
>>> simons_algorithm(f, 3)
58+
>>> simons_algorithm(hidden_function, 3)
5759
[1, 0, 1]
5860
"""
5961
mapping: dict[tuple[int, ...], tuple[int, ...]] = {}
6062
inputs = list(product([0, 1], repeat=num_bits))
6163

6264
for bits in inputs:
63-
fx = tuple(f(list(bits)))
65+
fx = tuple(hidden_function(list(bits)))
6466
if fx in mapping:
6567
prev_bits = mapping[fx]
6668
return xor_bits(list(bits), list(prev_bits))

0 commit comments

Comments
 (0)