def test_condinit(): print("conditional init") z = qq.reg([5, 6]) # conditional initialization? x = qq.reg([0, 1]) with qq.q_if(x): y = qq.reg([2, 3]) qq.print_amp(x, y) with qq.q_if(x): y.clean([2, 3]) x.clean([0, 1])
def test_if(): print("if") x = qq.reg([0, 1]) y = qq.reg(0) with qq.q_if(x): y += 1 qq.print(x, y) with qq.q_if(x == 0): y += 1 y.clean(1) x.clean([0, 1])
def test_stateprep(): print("state prep") z = qq.reg([5, 6]) v = {0: 0.5, 1: 0.3} x = qq.reg([0, 1]) with qq.q_if(x): y = qq.reg(v) qq.print_amp(x, y) with qq.q_if(x): y.clean(v) x.clean([0, 1])
def oracle(x): num_bad = qq.reg(0) clique_size = qq.reg(0) for i in range(n): with qq.q_if(x[i]): clique_size += 1 for j in range(i + 1, n): if [i, j] not in edges: with qq.q_if(x[i] & x[j]): num_bad += 1 return (num_bad == 0) & (clique_size >= k)
def test_inv_if(): print("inv if") x, y = qq.reg([0, 1], 0) with qq.inv(): with qq.q_if(x): y += 1 with qq.q_if(y): with qq.inv(): x += 1 qq.print(x, y) x.clean(0) y.clean([0, -1])
def grover(): print("grover") n = 8 # generate a random graph import random k = 4 edges = [] for i in range(n): for j in range(i + 1, n): if i != j + 1 % n: edges.append([i, j]) # if random.random() > 0.5: @qq.garbage("oracle") def oracle(x): num_bad = qq.reg(0) clique_size = qq.reg(0) for i in range(n): with qq.q_if(x[i]): clique_size += 1 for j in range(i + 1, n): if [i, j] not in edges: with qq.q_if(x[i] & x[j]): num_bad += 1 return (num_bad == 0) & (clique_size >= k) x = qq.reg(range(2**n)) for i in range(1): with qq.q_if(oracle(x)): qq.phase_pi(1) with qq.inv(): oracle(x) for j in range(n): x.had(j) with qq.q_if(x == 0): qq.phase_pi(1) for j in range(n): x.had(j) values, probs, _ = qq.distribution(x) plt.bar(values, probs) plt.show()
def rep_square(b, x, N): out = qq.reg(0) tmp = qq.reg(b) for i in range(5): with qq.q_if(x[i]): out += tmp tmp **= 2 tmp %= N return out % 13
def __init__(self, i, maxval): self.i = i self.maxval = maxval self.tmp = qq.reg(0) # temporary register # compute the number of iterations self.num_iter = qq.reg(0) with qq.q_if(i < maxval): self.num_iter += maxval - i self.q_while = qq.q_while(i < maxval, self.tmp)
def test_collatz(): print("collatz") # collatz test x, l = qq.reg(range(1, 11), 0) y = qq.reg(x) # nested while with qq.q_while(x > 1, l): tmp = qq.reg(x % 2) with qq.q_if(tmp == 0): x //= 2 with qq.q_if(tmp == 1): x *= 3 x += 1 qq.print(y, l)
def __exit__(self, *args): self.i += 1 self.q_while.__exit__() # clean the temporary register self.tmp.clean(self.num_iter) # return i to previous value self.i -= self.num_iter # uncompute the number of iterations with qq.q_if(self.i < self.maxval): self.num_iter -= self.maxval - self.i self.num_iter.clean(0)
def test_quantum(): print("quantum") #### simple quantum teleportation test y = qq.reg([-1, 1]) with qq.q_if(y < 0): qq.phase_pi(1) # Z gate # y[-1] now |-> # Bell state x = qq.reg(0) x.had(0) x.cnot(0, 1) # cnot across registers with qq.q_if(y[-1]): x ^= 1 # measure x_meas = int(qq.measure(x[0])) y.had(-1) y_meas = int(qq.measure(y[-1])) # apply x correction and z correction if x_meas: x ^= 2 with qq.q_if(y_meas & x[1]): qq.phase_pi(1) # x[1] is now |-> x.had(1) x.clean(x_meas + 2) #### gentle measurement test x = qq.reg([-5, 5, -2, 2]) out = qq.measure(x**2) qq.print(x)