-  
-   Notifications  You must be signed in to change notification settings 
- Fork 49.1k
Add Simon's Algorithm #13754
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  
  Rajasree2004 wants to merge 8 commits into TheAlgorithms:master       Choose a base branch                                                 from Rajasree2004:add-simons-algo                
 base: master
Could not load branches
    Branch not found: {{ refName }} 
     Loading  
 Could not load tags
   Nothing to show
        Loading  
 Are you sure you want to change the base?
 Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated. 
    +185   −0      
     Open  
 Add Simon's Algorithm #13754
Changes from 1 commit
 Commits 
  Show all changes 
  8 commits   Select commit Hold shift + click to select a range 
 244d2de  Add Merkle Tree construction and verification algorithm 
  Rajasree2004 825b730  [pre-commit.ci] auto fixes from pre-commit.com hooks 
  pre-commit-ci[bot] ee5b1bf  Added doctest to sha256 function in merkle tree 
  Rajasree2004 f357809  Merge branch 'add-merkle-tree-algo' of https://github.com/Rajasree200… 
  Rajasree2004 65c0e39  Add Simon's Algorithm Simulation' 
  Rajasree2004 ed920a3  [pre-commit.ci] auto fixes from pre-commit.com hooks 
  pre-commit-ci[bot] 7b90b1c  Add Parameter Rename to Simon's Algorithm' 
  Rajasree2004 90618a2  Add Parameter Rename to Simon's Algorithm' 
  Rajasree2004 File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
 Add Simon's Algorithm Simulation' 
 - Loading branch information
   commit 65c0e39bcdeb770cd22e7cc22175c4725152c93c  
 There are no files selected for viewing
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """ | ||
| Simon's Algorithm (Classical Simulation) | ||
|  | ||
| Simon's algorithm finds a hidden bitstring s such that | ||
| f(x) = f(y) if and only if x XOR y = s. | ||
|  | ||
| Here we simulate the mapping behavior classically to | ||
| illustrate how the hidden period can be discovered by | ||
| analyzing collisions in f(x). | ||
|  | ||
| References: | ||
| https://en.wikipedia.org/wiki/Simon's_problem | ||
| """ | ||
|  | ||
| from collections.abc import Callable | ||
| from itertools import product | ||
|  | ||
|  | ||
| def xor_bits(a: list[int], b: list[int]) -> list[int]: | ||
| """ | ||
| Return the bitwise XOR of two equal-length bit lists. | ||
|  | ||
| >>> xor_bits([1, 0, 1], [1, 1, 0]) | ||
| [0, 1, 1] | ||
| """ | ||
| if len(a) != len(b): | ||
| raise ValueError("Bit lists must be of equal length.") | ||
| return [x ^ y for x, y in zip(a, b)] | ||
|  | ||
|  | ||
| def simons_algorithm(f: Callable[[list[int]], list[int]], n: int) -> list[int]: | ||
|   Rajasree2004 marked this conversation as resolved. Outdated   Show resolved Hide resolved | ||
|  | ||
| """ | ||
| Simulate Simon's algorithm classically to find the hidden bitstring s. | ||
|  | ||
| Args: | ||
| f: A function mapping n-bit input to n-bit output. | ||
| n: Number of bits in the input. | ||
|  | ||
| Returns: | ||
| The hidden bitstring s as a list of bits. | ||
|  | ||
| >>> # Example with hidden bitstring s = [1, 0, 1] | ||
| >>> s = [1, 0, 1] | ||
| >>> def f(x): | ||
| ... mapping = { | ||
| ... (0,0,0): (1,1,0), | ||
| ... (1,0,1): (1,1,0), | ||
| ... (0,0,1): (0,1,1), | ||
| ... (1,0,0): (0,1,1), | ||
| ... (0,1,0): (1,0,1), | ||
| ... (1,1,1): (1,0,1), | ||
| ... (0,1,1): (0,0,0), | ||
| ... (1,1,0): (0,0,0), | ||
| ... } | ||
| ... return mapping[tuple(x)] | ||
| >>> simons_algorithm(f, 3) | ||
| [1, 0, 1] | ||
| """ | ||
| mapping: dict[tuple[int, ...], tuple[int, ...]] = {} | ||
| inputs = list(product([0, 1], repeat=n)) | ||
|  | ||
| for x in inputs: | ||
| fx = tuple(f(list(x))) | ||
| if fx in mapping: | ||
| y = mapping[fx] | ||
| return xor_bits(list(x), list(y)) | ||
| mapping[fx] = x | ||
|  | ||
| # If no collision found, function might be constant | ||
| return [0] * n | ||
|  | ||
|  | ||
| if __name__ == "__main__": | ||
| import doctest | ||
|  | ||
| doctest.testmod() | ||
 Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
Uh oh!
There was an error while loading. Please reload this page.