In [1]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import scipy as sp
import seaborn as sns
np.set_printoptions(precision=2, linewidth=120)
from copy import copy
from tqdm import *
from drift_qec.Q import *
Q0
Qc
(Controlled-bsais)Qeff = Qt.T * Q0
Qt
progressively betterQeff
alignment, you must transform it back to the standard basis before average with the existing channel estimateError x Time x Cycle_ratio
In [ ]:
D = 0.01
N_ERRORS = 1e6
N_TRIALS = 100
N_CYCLES = np.logspace(1, 3, 10).astype(np.int)
RECORDS = []
for trial in tqdm(range(N_TRIALS)):
for n_cycles in N_CYCLES:
n = int(N_ERRORS / n_cycles)
channel = Channel(kx=0.7, ky=0.2, kz=0.1,
Q=np.linalg.qr(np.random.randn(3,3))[0],
n=n, d=D)
RECORDS.append({
"trial": trial,
"cycle_length": n,
"n_cycles": n_cycles,
"time": 0,
"Mdist": np.linalg.norm(channel.Mhat-channel.C),
"Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3))
})
for cycle in range(n_cycles):
channel.update()
RECORDS.append({
"trial": trial,
"cycle_length": n,
"n_cycles": n_cycles,
"time": (cycle+1)*n,
"Mdist": np.linalg.norm(channel.Mhat-channel.C),
"Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3))
})
df = pd.DataFrame(RECORDS)
df.to_csv("{}errorsd{}.csv".format(N_ERRORS,D))
In [ ]:
df["cycle_length"] = (N_ERRORS / df["n_cycles"]).astype(np.int)
In [ ]:
df.tail(10)
In [ ]:
PAL = sns.color_palette("hls", len(N_CYCLES))
fig, ax = plt.subplots(1, 1, figsize=(8,6))
for idx, n_cycles in enumerate(N_CYCLES):
sel = (df["n_cycles"] == n_cycles)
subdf = df.loc[sel, :]
v = subdf.groupby("time").mean()
s = subdf.groupby("time").std()
t = v.index.values
y = v["Mdist"].values
e = s["Mdist"].values
ax.loglog(t, y, label=str(subdf.iloc[0, 2]), c=PAL[idx])
ax.fill_between(t, y-e, y+e, alpha=0.1, color=PAL[idx])
plt.title("Recover error over time for varied ratios of cycles to realignments")
plt.xlabel("Time [cycles]")
plt.ylabel("Basis recovery error")
plt.legend()
In [73]:
D = 0.01
N_TRIALS = 100
MAX_N = int(1e6)
N_STEP = int(1e3)
RECORDS = []
for trial in tqdm(range(N_TRIALS)):
channel = Channel(kx=0.7, ky=0.2, kz=0.1,
Q=np.linalg.qr(np.random.randn(3,3))[0],
n=N_STEP, d=D)
pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
RECORDS.append({
"trial": trial,
"time": 0,
"Mdist": np.linalg.norm(channel.Mhat-channel.C),
"Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
"pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
"pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
})
for time in range(0, MAX_N, N_STEP):
channel.update()
pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
RECORDS.append({
"trial": trial,
"time": time,
"Mdist": np.linalg.norm(channel.Mhat-channel.C),
"Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
"pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
"pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
})
df = pd.DataFrame(RECORDS)
df.to_csv("regime1.csv")
In [24]:
df = pd.read_csv("regime1.csv")
v = df.groupby("time").mean()["Qdist"]
s = df.groupby("time").std()["Qdist"]
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.plot(t, y,)
ax.fill_between(t, y-e, y+e, alpha=0.25)
plt.ylabel("Measure of orthonormality between $Q_{hat}$ and $Q_{val}$")
plt.xlabel("Time [n_errors]")
Out[24]:
In [44]:
df = pd.read_csv("regime1.csv")
v = df.groupby("time").mean()["Mdist"]
s = df.groupby("time").std()["Mdist"]
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.loglog(t, y,)
ax.fill_between(t, y-e, y+e, alpha=0.25)
plt.ylabel("Norm distance between $M_{hat}$ and $M_{val}$")
plt.xlabel("Time [n_errors]")
Out[44]:
In [72]:
D = 0.01
N_TRIALS = 100
MAX_N = int(1e6)
N_STEP = int(1e3)
RECORDS = []
for trial in tqdm(range(N_TRIALS)):
channel = Channel(kx=0.985, ky=0.01, kz=0.005,
Q=np.linalg.qr(np.random.randn(3,3))[0],
n=N_STEP, d=D)
pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
RECORDS.append({
"trial": trial,
"time": 0,
"Mdist": np.linalg.norm(channel.Mhat-channel.C),
"Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
"pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
"pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
})
for time in range(0, MAX_N, N_STEP):
channel.update()
pxhat, pyhat, pzhat = list(np.linalg.svd(channel.Mhat)[1])
RECORDS.append({
"trial": trial,
"time": time,
"Mdist": np.linalg.norm(channel.Mhat-channel.C),
"Qdist": np.linalg.norm(np.dot(channel.Qc.T, channel.Q) - np.eye(3)),
"pxval": channel.kx, "pyval": channel.ky, "pzval": channel.kz,
"pxhat": pxhat, "pyhat": pyhat, "pzhat": pzhat
})
df = pd.DataFrame(RECORDS)
df.to_csv("regime2.csv")
In [41]:
df = pd.read_csv("regime2.csv")
v = df.groupby("time").mean()["Qdist"]
s = df.groupby("time").std()["Qdist"]
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.plot(t, y)
ax.plot(t, y-e, ls="--")
ax.plot(t, y+e, ls="--")
plt.ylabel("Measure of orthonormality between $Q_{hat}$ and $Q_{val}$")
plt.xlabel("Time [n_errors]")
Out[41]:
In [39]:
df = pd.read_csv("regime2.csv")
v = df.groupby("time").mean()["Mdist"]
s = df.groupby("time").std()["Mdist"]
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
t = v.index.values
y = v.values
e = s.values
ax.loglog(t, y)
ax.fill_between(t, y-e, y+e, alpha=0.25)
plt.ylabel("Norm distance between $M_{hat}$ and $M_{val}$")
plt.xlabel("Time [n_errors]")
Out[39]:
In [128]:
df1 = pd.read_csv("regime1_1e3_1e6.csv")
df1["dpx"] = np.abs(df1["pxval"] - df1["pxhat"])
df1["dpy"] = np.abs(df1["pyval"] - df1["pyhat"])
df1["dpz"] = np.abs(df1["pzval"] - df1["pzhat"])
v1 = df1.groupby("time").mean()
s1 = df1.groupby("time").std()
df2 = pd.read_csv("regime2_1e3_1e6.csv")
df2["dpx"] = np.abs(df2["pxval"] - df2["pxhat"])
df2["dpy"] = np.abs(df2["pyval"] - df2["pyhat"])
df2["dpz"] = np.abs(df2["pzval"] - df2["pzhat"])
v2 = df2.groupby("time").mean()
s2 = df2.groupby("time").std()
fig, axs = plt.subplots(2, 3, figsize=(12, 8), sharey=True, sharex=True,
tight_layout={"h_pad": 1.0, "rect": [0.0, 0.0, 1.0, 0.95]})
for idx, stat in enumerate(["dpx", "dpy", "dpz"]):
t1 = v1[stat].index.values
y1 = v1[stat].values
e1 = s1[stat].values
x = np.log(v1.loc[1:, stat].index.values)
y = np.log(v1.loc[1:, stat].values)
reg = sp.stats.linregress(x, y)
fitted = np.exp(reg.intercept + reg.slope * x)
axs[0, idx].semilogy(t1, y1, ls="", marker=".", color=sns.color_palette()[idx], alpha=0.05)
axs[0, idx].semilogy(t1, y1+e1, ls="--", color=sns.color_palette()[idx])
axs[0, idx].semilogy(t1[1:], fitted, ls="-", color=sns.color_palette()[idx],
label="{} = {:0.2f} e^({:0.2f}*n)".format(stat, np.exp(reg.intercept), reg.slope))
axs[0, idx].set_title(stat)
axs[0, idx].legend(frameon=True)
t2 = v2[stat].index.values
y2 = v2[stat].values
e2 = s2[stat].values
x = np.log(v2.loc[1:, stat].index.values)
y = np.log(v2.loc[1:, stat].values)
reg = sp.stats.linregress(x, y)
fitted = np.exp(reg.intercept + reg.slope * x)
axs[1, idx].semilogy(t2, y2, ls="", marker=".", color=sns.color_palette()[idx], alpha=0.05)
axs[1, idx].semilogy(t2, y2+e2, ls="--", color=sns.color_palette()[idx])
axs[1, idx].semilogy(t2[1:], fitted, ls="-", color=sns.color_palette()[idx],
label="{} = {:0.2f} e^({:0.2f}*n)".format(stat, np.exp(reg.intercept), reg.slope))
axs[1, idx].set_xlabel("Number of errors")
axs[1, idx].legend(frameon=True)
fig.suptitle("Average difference in effective error probability (steps are 1e3, max is 1e6)")
axs[0, 0].set_ylabel("kx=0.7, ky=0.2, kz=0.1")
axs[1, 0].set_ylabel("kx=0.985, ky=0.01, kz=0.005")
fig.savefig("dp_1e3_1e6.pdf")
Out[128]:
In [130]:
df1 = pd.read_csv("regime1_1e5_1e8.csv")
df1["dpx"] = np.abs(df1["pxval"] - df1["pxhat"])
df1["dpy"] = np.abs(df1["pyval"] - df1["pyhat"])
df1["dpz"] = np.abs(df1["pzval"] - df1["pzhat"])
v1 = df1.groupby("time").mean()
s1 = df1.groupby("time").std()
df2 = pd.read_csv("regime2_1e5_1e8.csv")
df2["dpx"] = np.abs(df2["pxval"] - df2["pxhat"])
df2["dpy"] = np.abs(df2["pyval"] - df2["pyhat"])
df2["dpz"] = np.abs(df2["pzval"] - df2["pzhat"])
v2 = df2.groupby("time").mean()
s2 = df2.groupby("time").std()
fig, axs = plt.subplots(2, 3, figsize=(12, 8), sharey=True, sharex=True,
tight_layout={"h_pad": 1.0, "rect": [0.0, 0.0, 1.0, 0.95]})
for idx, stat in enumerate(["dpx", "dpy", "dpz"]):
t1 = v1[stat].index.values
y1 = v1[stat].values
e1 = s1[stat].values
x = np.log(v1.loc[1:, stat].index.values)
y = np.log(v1.loc[1:, stat].values)
reg = sp.stats.linregress(x, y)
fitted = np.exp(reg.intercept + reg.slope * x)
axs[0, idx].semilogy(t1, y1, ls="", marker=".", color=sns.color_palette()[idx], alpha=0.05)
axs[0, idx].semilogy(t1, y1+e1, ls="--", color=sns.color_palette()[idx])
axs[0, idx].semilogy(t1[1:], fitted, ls="-", color=sns.color_palette()[idx],
label="{} = {:0.2f} e^({:0.2f}*n)".format(stat, np.exp(reg.intercept), reg.slope))
axs[0, idx].set_title(stat)
axs[0, idx].legend(frameon=True)
t2 = v2[stat].index.values
y2 = v2[stat].values
e2 = s2[stat].values
x = np.log(v2.loc[1:, stat].index.values)
y = np.log(v2.loc[1:, stat].values)
reg = sp.stats.linregress(x, y)
fitted = np.exp(reg.intercept + reg.slope * x)
axs[1, idx].semilogy(t2, y2, ls="", marker=".", color=sns.color_palette()[idx], alpha=0.05)
axs[1, idx].semilogy(t2, y2+e2, ls="--", color=sns.color_palette()[idx])
axs[1, idx].semilogy(t2[1:], fitted, ls="-", color=sns.color_palette()[idx],
label="{} = {:0.2f} e^({:0.2f}*n)".format(stat, np.exp(reg.intercept), reg.slope))
axs[1, idx].set_xlabel("Number of errors")
axs[1, idx].legend(frameon=True)
fig.suptitle("Average difference in effective error probability (steps are 1e5, max is 1e8)")
axs[0, 0].set_ylabel("kx=0.7, ky=0.2, kz=0.1")
axs[1, 0].set_ylabel("kx=0.985, ky=0.01, kz=0.005")
fig.savefig("dp_1e5_1e8.pdf")
In [139]:
sel = (df1["pxhat"] + df1["pyhat"] + df1["pzhat"]) != 1.0
df1.loc[sel, :]
Out[139]:
In [132]:
print "Regime 1 advantage: {}".format((1.5 / (1.0-0.7))**2)
print "Regime 2 advantage: {}".format((1.5 / (1.0-0.985))**2)
Regime 2 lands kz within 0.002 of it's value of 0.005. That's good!
In [ ]: