def singleProjectorSample(args): (P, L, seed) = args # unpack arguments (easier for parallel code) (phases, xs, zs) = P t = len(xs[0]) # init seed np.random.seed(seed) # sample random theta theta = StabilizerState.randomStabilizerState(t) # project random state to P projfactor = 1 for g in range(len(phases)): res = theta.measurePauli(phases[g], zs[g], xs[g]) projfactor *= res if res == 0: return 0 # theta annihilated by P total = 0 if L is None: # exact decomposition size = int(np.ceil(t / 2)) for i in range(0, 2**size): phi = prepH(i, t) total += StabilizerState.innerProduct(theta, phi) else: # approximate decomposition size = len(L) for i in range(0, 2**size): phi = prepL(i, t, L) total += StabilizerState.innerProduct(theta, phi) return 2**t * np.abs(projfactor * total)**2
def exactThread(args): (P, L, i) = args (phases, xs, zs) = P t = len(xs[0]) if L is None: size = int(np.ceil(t/2)) else: size = len(L) total = 0 if L is None: theta = prepH(i, t) else: theta = prepL(i, t, L) projfactor = 1 for g in range(len(phases)): res = theta.measurePauli(phases[g], zs[g], xs[g]) projfactor *= res if res == 0: return 0 # theta annihilated by P for j in range(0, 2**size): if L is None: phi = prepH(j, t) else: phi = prepL(j, t, L) inner = StabilizerState.innerProduct(theta, phi) total += inner * projfactor return total
def evalLcomponent(args): (i, L, theta, t) = args # unpack arguments (easier for parallel code) # compute bitstring by adding rows of l Lbits = list(np.binary_repr(i, width=len(L))) bitstring = np.zeros(t) for idx in range(len(Lbits)): if Lbits[idx] == '1': bitstring += L[idx] bitstring = bitstring.astype(int) % 2 # Stabilizer state is product of |0> and |+> # 1's in bitstring indicate positions of |+> # initialize stabilizer state phi = StabilizerState(t, t) # construct state by measuring paulis for xtildeidx in range(t): vec = np.zeros(t) vec[xtildeidx] = 1 if bitstring[xtildeidx] == 1: # |+> at index, so measure X phi.measurePauli(0, np.zeros(t), vec) else: # |0> at index, so measure Z phi.measurePauli(0, vec, np.zeros(t)) return StabilizerState.innerProduct(theta, phi)
def evalHcomponent(args): (i, _, theta, t) = args # unpack arguments (easier for parallel code) size = int(np.ceil(t/2)) odd = t % 2 == 1 bits = list(np.binary_repr(i, width=size)) # initialize stabilizer state phi = StabilizerState(t, t) for idx in range(size): bit = int(bits[idx]) if bit == 0 and not (odd and idx == size-1): # phi.J = np.array([[0, 4], [4, 0]]) phi.J[idx*2+1, idx*2] = 4 phi.J[idx*2, idx*2+1] = 4 for idx in range(size): bit = int(bits[idx]) vec = np.zeros(t) if odd and idx == size-1: vec[t-1] = 1 # last qubit: |H> = (1/2v)(|0> + |+>) if bit == 0: phi.measurePauli(0, vec, np.zeros(t)) # |0>, measure Z else: phi.measurePauli(0, np.zeros(t), vec) # |+>, measure X continue if bit == 0: continue vec[idx*2+1] = 1 vec[idx*2] = 1 phi.measurePauli(0, np.zeros(t), vec) # measure XX phi.measurePauli(0, vec, np.zeros(t)) # measure ZZ return StabilizerState.innerProduct(theta, phi)
def exactProjectorWork(args): (P, L, l) = args (phases, xs, zs) = P t = len(xs[0]) if L is None: size = int(np.ceil(t / 2)) else: size = len(L) chi = 2**size i = 0 while l >= chi - i: l -= chi - i i += 1 j = l + i if L is None: theta = prepH(i, t) else: theta = prepL(i, t, L) projfactor = 1 for g in range(len(phases)): res, status = theta.measurePauli(phases[g], zs[g], xs[g], give_status=True) projfactor *= res if res == 0: return 0 # theta annihilated by P if L is None: phi = prepH(j, t) else: phi = prepL(j, t, L) inner = StabilizerState.innerProduct(theta, phi) if i == j: return inner * projfactor else: return 2 * np.real(inner) * projfactor
tests = json.loads(f.read()) tests = tests[:275] # tests = [] indexcut = 164 failed = 0 index = 0 starttime = datetime.now() for test in tests: index += 1 if index < indexcut: continue state1 = load(test["state1"]["state1"]) state2 = load(test["state2"]["state2"]) (eps, p, m) = StabilizerState.innerProduct(state1, state2, exact=True) if eps != test["eps_out"]\ or m != test["m_out"] % 8\ or p != test["p_out"]: if failed == 0: print("InnerProduct errors:") print("InnerProduct %d failed: (%d,%d,%d) should be (%d,%d,%d)" % (index, eps, p, m, test["eps_out"], test["p_out"], test["m_out"])) failed += 1 if len(tests) == 0: print("[\033[93mSkipped\033[0m] InnerProduct") else: if failed == 0: print("[\033[92mPassed\033[0m] InnerProduct: %d tests in " % len(tests) + str(datetime.now() - starttime)) else: print("[\033[91mFailed\033[0m] InnerProduct: %d/%d tests failed" % (failed, len(tests))) # -------------------- Extend ------------------- ktrue = test['psi']['psi']['k']
f = open(directory + "InnerProduct.txt") tests = json.loads(f.read()) tests = [] indexcut = 4 # 164 failed = 0 index = 0 starttime = datetime.now() for test in tests: index += 1 if index < indexcut: continue state1 = load(test["state1"]["state1"]) state2 = load(test["state2"]["state2"]) (eps, p, m) = StabilizerState.innerProduct(state1, state2, exact=True) if eps != test["eps_out"]\ or m != test["m_out"] % 8\ or p != test["p_out"]: if failed == 0: print("InnerProduct errors:") print( "InnerProduct %d failed: (%d,%d,%d) should be (%d,%d,%d)" % (index, eps, p, m, test["eps_out"], test["p_out"], test["m_out"])) failed += 1 break if len(tests) == 0: print("[\033[93mSkipped\033[0m] InnerProduct") else: if failed == 0: print("[\033[92mPassed\033[0m] InnerProduct: %d tests in " %