github

karldray / quantum

  • пятница, 25 марта 2016 г. в 02:14:17
https://github.com/karldray/quantum

Python
Simulate reverse causality using quantum suicide.



quantum

Simulate reverse causality using quantum suicide.

import quantum
x = quantum.choice([1,2,3,4,5,6])  # creates 6 universes
quantum.assert_(x > 3)  # destroys universes 1,2,3
quantum.assert_(x < 6)  # destroys universe 6
print(x)  # prints either 4 or 5

quantum.choice(sequence)

Returns every element of the provided sequence, each in a different alternative universe (which this call creates). If the provided sequence is empty, this is equivalent to quantum.fail().

quantum.fail()

Silently and instantaneously destroys the universe, preventing you from observing the timeline in which this call was made. Warning: Only useful if a previous call to quantum.choice() has created other universes. See notes below.

quantum.assert_(condition)

Shorthand for if not condition: quantum.fail().

Examples

Quantum sort

def qsort(xs):
    # choose a permutation of the input
    r = quantum.choice(itertools.permutations(xs))
    # assert that it's sorted
    quantum.assert_(all(r[i - 1] <= r[i] for i in range(1, len(r))))
    # return it
    return r

print(qsort([3, 0, 5, 1, 2]))  # prints [0, 1, 2, 3, 5]

Sudoku solver

def solve(board):
    '''
    Given a Sudoku puzzle as a list of 81 ints in {0,...,9}
    with 0 representing an empty cell, solve the puzzle in-place.
    '''
    for i in range(81):
        if board[i] == 0:
            neighbors = set(board[j] for group in GROUPS[i] for j in group)
            board[i] = quantum.choice(x for x in range(1, 10) if x not in neighbors)

board = [0] * 81
solve(board)
printboard(board)

Output:

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 1 4 3 6 5 8 9 7
3 6 5 8 9 7 2 1 4
8 9 7 2 1 4 3 6 5
5 3 1 6 4 2 9 7 8
6 4 2 9 7 8 5 3 1
9 7 8 5 3 1 6 4 2

Notes

How it works: Because you can never observe a scenario in which fail has been called (since it destroys the universe), you will necessarily observe a "fortuitous" timeline in which the values returned by choice cause your program to complete without ever calling fail. Be careful to ensure that such a timeline exists!

Do not run a program that calls fail() unconditionally or in every timeline. Such a program destroys every universe in which it runs correctly, so the only observable outcome is one in which an extremely unlikely (and potentially dangerous) event prevents it from completing.

When there are multiple possible (non-failing) executions of a quantum program, we consistently end up observing the lexicographically smallest one with respect to the iteration order of sequences passed to choice. This phenomenon remains unexplained.