Skip to content

Instantly share code, notes, and snippets.

View Scinawa's full-sized avatar
🎯
Focusing

Alessandro Luongo Scinawa

🎯
Focusing
View GitHub Profile
@Scinawa
Scinawa / space_hhl.py
Created December 20, 2017 01:14
Space estimation for HHL
#!/usr/bin/python3
import matplotlib.pyplot as plt
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--nqubit", required=False, help="number of qubits", type=int)
parser.add_argument("--precision", required=False, default=32, type=int)
@Scinawa
Scinawa / integerpartition.py
Created March 29, 2016 22:53
Integer Partition
# http://stackoverflow.com/questions/10244180/python-generating-integer-partitions (checked)
def accelAsc(n):
a = [0 for i in range(n + 1)]
k = 1
a[0] = 0
y = n - 1
while k != 0:
x = a[k - 1] + 1
k -= 1
while 2*x <= y:
@Scinawa
Scinawa / stirling_second.py
Created March 25, 2016 22:56
stirling number of the second kind
def stirling_second(x,n):
if n <= 1 or x == n: return 1
if n > x or x <= 0: return 0
return stirling_second(x-1, n-1) + n * stirling_second(x-1, n)
@Scinawa
Scinawa / comp_enum.py
Created March 25, 2016 22:05
compositions of the integer x into n parts.
#!/usr/bin/env python
def comp_enum ( x, n ):
#******************************************************************************/
#
## COMP_ENUM returns the number of compositions of the integer x into n parts.
#
# Discussion:
#
@Scinawa
Scinawa / binomial.py
Created March 25, 2016 22:00
Calculate binomial x chose n
#!/usr/bin/env python
def i4_choose ( x, n ):
#*****************************************************************************
#
## I4_CHOOSE computes the binomial coefficient C(x,n) as an I4.
#
# Discussion:
#
@Scinawa
Scinawa / function_n_x_up_n.py
Last active March 24, 2016 06:16
Functions from N to X, up to a permutation of N
from math import factorial
x=10
n=3
res = math.factorial(x+n-1) / ( math.factorial(n) * math.factorial(x+n-1-n))
>>> len([i for i in itertools.combinations_with_replacement(range(0,x), n)]) == res
True
@Scinawa
Scinawa / stirling_1.py
Created March 24, 2016 05:23
Stirling number of the first kind
@memo # can be memoized, otherwise comment this line
def stirling1(n,k):
"""Returns the stirling number of the first kind using recursion.."""
if n == 0 and k == 0:
return 1
if k == 0 and n >= 1:
return 0
if k > n:
return 0
return stirling1(n-1, k-1) - (n - 1) * stirling1(n-1, k)
@Scinawa
Scinawa / inj_fun_n_x_perm_n.py
Last active March 24, 2016 16:17
Injective functions from N to X, up to a permutation of N
import itertools
s="ABCD"
rep=3
a = [i for i in itertools.combinations(s, rep)]
len(a) == math.factorial(len(s)) / (math.factorial(rep) * math.factorial(len(s)-rep))
@Scinawa
Scinawa / inj_fun_n_x.py
Created March 24, 2016 04:23
Injective functions from N to X
>>> s="ABCD"
>>> repetition=3
>>> a = [i for i in itertools.permutations(s, repetition)]
>>> len(a) == math.factorial(len(s)) / math.factorial(len(s)-repetition)
True
def sample_function(n=0, s=set() ):
f = {}
for i in range(0, n):
@Scinawa
Scinawa / fun_n_x.py
Created March 24, 2016 04:19
function from N to X
>>> x = len("ABCDEF") # x= 6,
>>> n=3
>>> for i in product("ABCDEF", repeat=n):
... print(i)
('A', 'A', 'A')
...
('F', 'F', 'F')
>>> len( [i for i in product("ABCDEF", repeat=n)] ) == x**n