Skip to content

Commit

Permalink
Fix classical shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Nov 14, 2024
1 parent 1a12b59 commit cb41504
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 80 deletions.
26 changes: 10 additions & 16 deletions rcs/fc_elided_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@
from scipy.stats import binom


# sin(math.pi / 4) / 2
epsilon = 0.353553390593273762


def ct_pair_prob(sim, q1, q2):
r = [0] * 4

r[0] = sim.prob(q1)
r[1] = sim.prob(q2)
r[2] = r[0]
r[3] = q2
if r[0] < r[1]:
r[3] = q1
r[2] = r[1]
p1 = sim.prob(q1)
p2 = sim.prob(q2)
p1Hi = p1 > p2
pHi = p1 if p1Hi else p2
pLo = p2 if p1Hi else p1
cState = abs(pHi - 0.5) > abs(pLo - 0.5)
t = q1 if p1Hi == cState else q2

return r
return cState, t


def cz_shadow(sim, q1, q2):
prob1, prob2, prob_max, t = ct_pair_prob(sim, q1, q2)
if prob_max > (0.5 + epsilon):
cState, t = ct_pair_prob(sim, q1, q2)
if cState:
sim.z(t)


Expand Down
26 changes: 14 additions & 12 deletions rcs/rcs_nn_elided.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ def factor_width(width):


def ct_pair_prob(sim, q1, q2):
r = [0] * 4
p1 = sim.prob(q1)
p2 = sim.prob(q2)
p1Hi = p1 > p2
pHi = p1 if p1Hi else p2
pLo = p2 if p1Hi else p1
cState = abs(pHi - 0.5) > abs(pLo - 0.5)
t = q1 if p1Hi == cState else q2

r[0] = sim.prob(q1)
r[1] = sim.prob(q2)
r[2] = r[0]
r[3] = q2
if r[0] < r[1]:
r[3] = q1
r[2] = r[1]

return r
return cState, t


def cz_shadow(sim, q1, q2, anti = False):
prob1, prob2, prob_max, t = ct_pair_prob(sim, q1, q2)
if ((not anti) and (prob_max > (0.5 + epsilon))) or (anti and (prob_max < (0.5 - epsilon))):
if (anti):
sim.x(q1)
cState, t = ct_pair_prob(sim, q1, q2)
if cState:
sim.z(t)
if (anti):
sim.x(q1)


def cx_shadow(sim, c, t, anti = False):
Expand Down
30 changes: 14 additions & 16 deletions rcs/rcs_nn_elided_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
from pyqrack import QrackSimulator, Pauli


# sin(math.pi / 4) / 2
epsilon = 0.353553390593273762


def factor_width(width):
row_len = math.floor(math.sqrt(width))
while (((width // row_len) * row_len) != width):
Expand All @@ -31,23 +27,25 @@ def factor_width(width):


def ct_pair_prob(sim, q1, q2):
r = [0] * 4

r[0] = sim.prob(q1)
r[1] = sim.prob(q2)
r[2] = r[0]
r[3] = q2
if r[0] < r[1]:
r[3] = q1
r[2] = r[1]
p1 = sim.prob(q1)
p2 = sim.prob(q2)
p1Hi = p1 > p2
pHi = p1 if p1Hi else p2
pLo = p2 if p1Hi else p1
cState = abs(pHi - 0.5) > abs(pLo - 0.5)
t = q1 if p1Hi == cState else q2

return r
return cState, t


def cz_shadow(sim, q1, q2, anti = False):
prob1, prob2, prob_max, t = ct_pair_prob(sim, q1, q2)
if ((not anti) and (prob_max > (0.5 + epsilon))) or (anti and (prob_max < (0.5 - epsilon))):
if (anti):
sim.x(q1)
cState, t = ct_pair_prob(sim, q1, q2)
if cState:
sim.z(t)
if (anti):
sim.x(q1)


def cx_shadow(sim, c, t, anti = False):
Expand Down
34 changes: 16 additions & 18 deletions rcs/sycamore_2019_elided.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
from pyqrack import QrackSimulator, Pauli


# sin(math.pi / 4) / 2
epsilon = 0.353553390593273762


def factor_width(width):
col_len = math.floor(math.sqrt(width))
while (((width // col_len) * col_len) != width):
Expand All @@ -29,23 +25,25 @@ def factor_width(width):


def ct_pair_prob(sim, q1, q2):
r = [0] * 4

r[0] = sim.prob(q1)
r[1] = sim.prob(q2)
r[2] = r[0]
r[3] = q2
if r[0] < r[1]:
r[3] = q1
r[2] = r[1]
p1 = sim.prob(q1)
p2 = sim.prob(q2)
p1Hi = p1 > p2
pHi = p1 if p1Hi else p2
pLo = p2 if p1Hi else p1
cState = abs(pHi - 0.5) > abs(pLo - 0.5)
t = q1 if p1Hi == cState else q2

return r
return cState, t


def cz_shadow(sim, q1, q2, anti = False):
prob1, prob2, prob_max, t = ct_pair_prob(sim, q1, q2)
if ((not anti) and (prob_max > (0.5 + epsilon))) or (anti and (prob_max < (0.5 - epsilon))):
if (anti):
sim.x(q1)
cState, t = ct_pair_prob(sim, q1, q2)
if cState:
sim.z(t)
if (anti):
sim.x(q1)


def cx_shadow(sim, c, t, anti = False):
Expand Down Expand Up @@ -146,8 +144,8 @@ def bench_qrack(width, depth):
if ((b1 < patch_bound) and (b2 >= patch_bound)) or ((b2 < patch_bound) and (b1 >= patch_bound)):
# This is our version of ("semi-classical") gate "elision":

prob1, prob2, prob_max, t = ct_pair_prob(patch_sim, b1, b2)
if prob_max > (0.5 + epsilon):
cState, t = ct_pair_prob(patch_sim, b1, b2)
if cState:
# FSim controlled phase
patch_sim.u(t, 0, 0, -math.pi / 6)

Expand Down
34 changes: 16 additions & 18 deletions rcs/sycamore_2019_elided_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
from pyqrack import QrackSimulator, Pauli


# sin(math.pi / 4) / 2
epsilon = 0.353553390593273762


def factor_width(width):
col_len = math.floor(math.sqrt(width))
while (((width // col_len) * col_len) != width):
Expand All @@ -29,23 +25,25 @@ def factor_width(width):


def ct_pair_prob(sim, q1, q2):
r = [0] * 4

r[0] = sim.prob(q1)
r[1] = sim.prob(q2)
r[2] = r[0]
r[3] = q2
if r[0] < r[1]:
r[3] = q1
r[2] = r[1]
p1 = sim.prob(q1)
p2 = sim.prob(q2)
p1Hi = p1 > p2
pHi = p1 if p1Hi else p2
pLo = p2 if p1Hi else p1
cState = abs(pHi - 0.5) > abs(pLo - 0.5)
t = q1 if p1Hi == cState else q2

return r
return cState, t


def cz_shadow(sim, q1, q2, anti = False):
prob1, prob2, prob_max, t = ct_pair_prob(sim, q1, q2)
if ((not anti) and (prob_max > (0.5 + epsilon))) or (anti and (prob_max < (0.5 - epsilon))):
if (anti):
sim.x(q1)
cState, t = ct_pair_prob(sim, q1, q2)
if cState:
sim.z(t)
if (anti):
sim.x(q1)


def cx_shadow(sim, c, t, anti = False):
Expand Down Expand Up @@ -141,8 +139,8 @@ def bench_qrack(width, depth):
if ((b1 < patch_bound) and (b2 >= patch_bound)) or ((b2 < patch_bound) and (b1 >= patch_bound)):
# This is our version of ("semi-classical") gate "elision":

prob1, prob2, prob_max, t = ct_pair_prob(patch_sim, b1, b2)
if prob_max > (0.5 + epsilon):
cState, t = ct_pair_prob(patch_sim, b1, b2)
if cState:
# FSim controlled phase
patch_sim.u(t, 0, 0, -math.pi / 6)

Expand Down

0 comments on commit cb41504

Please sign in to comment.