diff --git a/py/.gitignore b/py/.gitignore index a0c3440..e02d976 100644 --- a/py/.gitignore +++ b/py/.gitignore @@ -1,3 +1,4 @@ .DS_Store ._* -tmp/ \ No newline at end of file +tmp/ +/.ipynb_checkpoints/ diff --git a/py/.gitignore b/py/.gitignore index a0c3440..e02d976 100644 --- a/py/.gitignore +++ b/py/.gitignore @@ -1,3 +1,4 @@ .DS_Store ._* -tmp/ \ No newline at end of file +tmp/ +/.ipynb_checkpoints/ diff --git a/py/20191105_cirq.py b/py/20191105_cirq.py new file mode 100644 index 0000000..fac8294 --- /dev/null +++ b/py/20191105_cirq.py @@ -0,0 +1,12 @@ +# %% [markdown] +# # Cirq NISQ Payground +# Here are some code snippets that I'm playing with to learn quantum computing concepts +# +# use the following command to install cirq: +# ```bash +# pip install cirq +# ``` + +#%% +import cirq + diff --git a/py/.gitignore b/py/.gitignore index a0c3440..e02d976 100644 --- a/py/.gitignore +++ b/py/.gitignore @@ -1,3 +1,4 @@ .DS_Store ._* -tmp/ \ No newline at end of file +tmp/ +/.ipynb_checkpoints/ diff --git a/py/20191105_cirq.py b/py/20191105_cirq.py new file mode 100644 index 0000000..fac8294 --- /dev/null +++ b/py/20191105_cirq.py @@ -0,0 +1,12 @@ +# %% [markdown] +# # Cirq NISQ Payground +# Here are some code snippets that I'm playing with to learn quantum computing concepts +# +# use the following command to install cirq: +# ```bash +# pip install cirq +# ``` + +#%% +import cirq + diff --git a/py/20191107_kl_divergence.py b/py/20191107_kl_divergence.py new file mode 100644 index 0000000..8827ef4 --- /dev/null +++ b/py/20191107_kl_divergence.py @@ -0,0 +1,73 @@ +# %% +import numpy as np +from scipy.stats import norm, poisson +from matplotlib import pyplot as plt +import seaborn as sns +sns.set() + +# %% [markdown] +# The following function calculates the KL-divergence of two distributions (p and q). +# +# The KL formula that is being used here is: +# +# $D_{KL}(P || Q)=\Sigma (p \times \log {p \over q})$ + +# %% +def kl(p, q): + """ + Calculates relative entropy a.k.a. Kullback–Leibler divergence. + """ + return np.sum(np.where(p!=0, p*np.log(p/q),0)) + + +# 1. generate example distributions +x = np.arange(-10,10,0.001) +p = norm.pdf(x,0,1) +q = norm.pdf(x,1,2) + +# 2. calc kl divergence +kl_score = kl(p, q) + +# plot distributiions +plt.title(f'KL(P||Q) = {kl(p,q):.2f}') +plt.plot(x,p) +plt.plot(x,q, color="red") +plt.show() + + +# %% +# [snippet[ plot poisson distribution generated by scipy +from scipy.stats import poisson as poi +r = poi.cdf(x, 0.9) +plt.plot(x, r, color="green") +plt.show() + +x = np.arange(20) +lambda_ = [1.5, 4.25] +colors = ["#348ABD", "#A60628"] + +# lambda = 1.5 +plt.bar(x, poi.pmf(x, 1.5), color="red", label="$\lambda = 1.5$", alpha=0.60, lw="3") + +plt.bar(x, poi.pmf(x, 4.5), color="blue", label="$\lambda = 4.5$", alpha=0.60, lw="3") + +plt.xticks(x + 0.4, x) +plt.ylabel("Probability of $k$") +plt.xlabel("$k$") +plt.show() + + +# %% +# [snippet] plot multiple exponentional distributions (EXP) +from scipy.stats import expon as expo + +x = np.linspace(0,4,100) +colors = ['red','blue','green'] +lambdas = [0.5, 1.0, 1.5] + +for l, c in zip(lambdas, colors): + y = expo.pdf(x, scale=1.0/l) + plt.plot(x, y, lw=2, color=c) + + +# %% diff --git a/py/.gitignore b/py/.gitignore index a0c3440..e02d976 100644 --- a/py/.gitignore +++ b/py/.gitignore @@ -1,3 +1,4 @@ .DS_Store ._* -tmp/ \ No newline at end of file +tmp/ +/.ipynb_checkpoints/ diff --git a/py/20191105_cirq.py b/py/20191105_cirq.py new file mode 100644 index 0000000..fac8294 --- /dev/null +++ b/py/20191105_cirq.py @@ -0,0 +1,12 @@ +# %% [markdown] +# # Cirq NISQ Payground +# Here are some code snippets that I'm playing with to learn quantum computing concepts +# +# use the following command to install cirq: +# ```bash +# pip install cirq +# ``` + +#%% +import cirq + diff --git a/py/20191107_kl_divergence.py b/py/20191107_kl_divergence.py new file mode 100644 index 0000000..8827ef4 --- /dev/null +++ b/py/20191107_kl_divergence.py @@ -0,0 +1,73 @@ +# %% +import numpy as np +from scipy.stats import norm, poisson +from matplotlib import pyplot as plt +import seaborn as sns +sns.set() + +# %% [markdown] +# The following function calculates the KL-divergence of two distributions (p and q). +# +# The KL formula that is being used here is: +# +# $D_{KL}(P || Q)=\Sigma (p \times \log {p \over q})$ + +# %% +def kl(p, q): + """ + Calculates relative entropy a.k.a. Kullback–Leibler divergence. + """ + return np.sum(np.where(p!=0, p*np.log(p/q),0)) + + +# 1. generate example distributions +x = np.arange(-10,10,0.001) +p = norm.pdf(x,0,1) +q = norm.pdf(x,1,2) + +# 2. calc kl divergence +kl_score = kl(p, q) + +# plot distributiions +plt.title(f'KL(P||Q) = {kl(p,q):.2f}') +plt.plot(x,p) +plt.plot(x,q, color="red") +plt.show() + + +# %% +# [snippet[ plot poisson distribution generated by scipy +from scipy.stats import poisson as poi +r = poi.cdf(x, 0.9) +plt.plot(x, r, color="green") +plt.show() + +x = np.arange(20) +lambda_ = [1.5, 4.25] +colors = ["#348ABD", "#A60628"] + +# lambda = 1.5 +plt.bar(x, poi.pmf(x, 1.5), color="red", label="$\lambda = 1.5$", alpha=0.60, lw="3") + +plt.bar(x, poi.pmf(x, 4.5), color="blue", label="$\lambda = 4.5$", alpha=0.60, lw="3") + +plt.xticks(x + 0.4, x) +plt.ylabel("Probability of $k$") +plt.xlabel("$k$") +plt.show() + + +# %% +# [snippet] plot multiple exponentional distributions (EXP) +from scipy.stats import expon as expo + +x = np.linspace(0,4,100) +colors = ['red','blue','green'] +lambdas = [0.5, 1.0, 1.5] + +for l, c in zip(lambdas, colors): + y = expo.pdf(x, scale=1.0/l) + plt.plot(x, y, lw=2, color=c) + + +# %% diff --git a/py/kl.py b/py/kl.py deleted file mode 100644 index c65d079..0000000 --- a/py/kl.py +++ /dev/null @@ -1,55 +0,0 @@ -#%% -import numpy as np -from scipy.stats import norm, poisson - -from matplotlib import pyplot as plt -import tensorflow as tf -import seaborn as sns -sns.set() - - -def kl(p, q): - return np.sum(np.where(p!=0, p*np.log(p/q),0)) - - -# example -x = np.arange(-10,10,0.001) -p = norm.pdf(x,0,1) -q = norm.pdf(x,1,2) - -plt.title(f'KL(P||Q) = {kl(p,q):.2f}') -plt.plot(x,p) -plt.plot(x,q, color="red") -plt.show() -#%% -# poisson distribution -from scipy.stats import poisson as poi -r = poisson.cdf(x, 0.9) -plt.plot(x, r, color="green") -plt.show() - -x = np.arange(20) -lambda_ = [1.5, 4.25] -colors = ["#348ABD", "#A60628"] - -# lambda = 1.5 -plt.bar(x, poi.pmf(x, 1.5), color="red", label="$\lambda = 1.5$", alpha=0.60, lw="3") - -plt.bar(x, poi.pmf(x, 4.5), color="blue", label="$\lambda = 4.5$", alpha=0.60, lw="3") - -plt.xticks(x + 0.4, x) -plt.ylabel("Probability of $k$") -plt.xlabel("$k$") -plt.show() - -#%% -# exponentional dist (EXP) -from scipy.stats import expon as expo - -x = np.linspace(0,4,100) -colors = ['red','blue'] -lambdas = [0.5, 1.0] - -for l, c in zip(lambdas, colors): - y = expo.pdf(x, scale=1.0/l) - plt.plot(x, y, lw=2, color=c)