@@ -116,15 +116,15 @@ def circuit_to_state(Base, n, circuit): | |||
g.act_circuit(circuit) | |||
return g | |||
def test_circuit(circuit): | |||
def test_circuit(circuit, n): | |||
""" Check that two classes exhibit the same behaviour for a given circuit """ | |||
a = circuit_to_state(ABPWrapper, 10, circuit) | |||
b = circuit_to_state(AndersWrapper, 10, circuit) | |||
a = circuit_to_state(ABPWrapper, n, circuit) | |||
b = circuit_to_state(AndersWrapper, n, circuit) | |||
assert a == b | |||
if __name__ == '__main__': | |||
for i in range(1000): | |||
test_circuit(random_graph_circuit(10)) | |||
test_circuit(random_stabilizer_circuit(10)) | |||
test_circuit(random_graph_circuit(10), 10) | |||
test_circuit(random_stabilizer_circuit(10), 10) | |||
@@ -1,83 +0,0 @@ | |||
from abp import GraphState | |||
from abp import CircuitModel | |||
from abp import clifford | |||
import numpy as np | |||
import random | |||
from tqdm import tqdm | |||
from config import * | |||
def test_single_qubit(): | |||
""" A multi qubit test with Hadamards only""" | |||
for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"): | |||
g = GraphState([0]) | |||
c = CircuitModel(1) | |||
for i in range(100): | |||
op = random.randint(0, 23) | |||
g.act_local_rotation(0, op) | |||
c.act_local_rotation(0, clifford.unitaries[op]) | |||
assert g.to_state_vector() == c | |||
def test_hadamard_only_multiqubit(n=6): | |||
""" A multi qubit test with Hadamards only""" | |||
for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"): | |||
g = GraphState(range(n)) | |||
c = CircuitModel(n) | |||
for i in range(n): | |||
g.act_hadamard(i) | |||
c.act_hadamard(i) | |||
assert g.to_state_vector() == c | |||
for i in range(100): | |||
a, b = np.random.choice(range(n), 2, False) | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
assert g.to_state_vector() == c | |||
def test_all_multiqubit(n=4): | |||
""" A multi qubit test with arbitrary local rotations """ | |||
g = GraphState(range(n)) | |||
c = CircuitModel(n) | |||
for i in range(10): | |||
qubit = np.random.randint(0, n - 1) | |||
rotation = np.random.randint(0, 24 - 1) | |||
g.act_local_rotation(qubit, rotation) | |||
c.act_local_rotation(qubit, clifford.unitaries[rotation]) | |||
assert g.to_state_vector() == c | |||
for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"): | |||
a, b = np.random.choice(range(n), 2, False) | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
assert np.allclose(np.sum(np.abs(c.state) ** 2), 1) | |||
assert np.allclose( | |||
np.sum(np.abs(g.to_state_vector().state) ** 2), 1) | |||
assert g.to_state_vector() == c | |||
assert g.to_state_vector() == c | |||
def test_all(n=8): | |||
""" A multi qubit test with arbitrary local rotations """ | |||
g = GraphState(range(n)) | |||
c = CircuitModel(n) | |||
for repeat in tqdm(xrange(REPEATS), "Testing against circuit model"): | |||
for step in xrange(DEPTH): | |||
if random.random()>0.5: | |||
qubit = np.random.randint(0, n - 1) | |||
rotation = np.random.randint(0, 24 - 1) | |||
g.act_local_rotation(qubit, rotation) | |||
c.act_local_rotation(qubit, clifford.unitaries[rotation]) | |||
else: | |||
a, b = np.random.choice(range(n), 2, False) | |||
g.act_cz(a, b) | |||
c.act_cz(a, b) | |||
assert g.to_state_vector() == c | |||
@@ -1,44 +0,0 @@ | |||
import chp | |||
from abp import qi | |||
import numpy as np | |||
n = 5 | |||
def get_chp_state(): | |||
""" Convert CHP to CircuitModel """ | |||
output = qi.CircuitModel(n) | |||
ket = chp.get_ket() | |||
nonzero = np.sqrt(len(ket)) | |||
output.state[0, 0]=0 | |||
for key, phase in ket.items(): | |||
output.state[key] = np.exp(1j*phase*np.pi/2)/nonzero | |||
return output | |||
def bell_test(): | |||
chp.init(n) | |||
chp.act_hadamard(0) | |||
chp.act_cnot(0, 1) | |||
psi = qi.CircuitModel(n) | |||
psi.act_hadamard(0) | |||
psi.act_cnot(0, 1) | |||
assert psi == get_chp_state() | |||
def random_test(): | |||
chp.init(n) | |||
psi = qi.CircuitModel(n) | |||
for i in range(1000): | |||
if np.random.rand()>.5: | |||
a = np.random.randint(0, n-1) | |||
chp.act_hadamard(a) | |||
psi.act_hadamard(a) | |||
else: | |||
a, b = np.random.randint(0, n-1, 2) | |||
if a!=b: | |||
chp.act_cnot(a, b) | |||
psi.act_cnot(a, b) | |||
assert psi == get_chp_state() | |||
@@ -1,30 +0,0 @@ | |||
import json | |||
import numpy as np | |||
import sys | |||
import os | |||
import itertools as it | |||
from string import maketrans | |||
from abp import clifford, qi, anders_cz, build_tables | |||
def test_cz_table(): | |||
""" Does our clifford code work with anders & briegel's table? """ | |||
state_table = build_tables.get_state_table(clifford.unitaries) | |||
ab_cz_table = anders_cz.cz_table | |||
rows = it.product([0, 1], it.combinations_with_replacement(range(24), 2)) | |||
for bond, (c1, c2) in rows: | |||
# Pick the input state | |||
input_state = state_table[bond, c1, c2] | |||
# Go and compute the output | |||
computed_output = np.dot(qi.cz, input_state) | |||
computed_output = qi.normalize_global_phase(computed_output) | |||
# Now look up the answer in the table | |||
bondp, c1p, c2p = ab_cz_table[bond, c1, c2] | |||
table_output = state_table[bondp, c1p, c2p] | |||
assert np.allclose(computed_output, table_output) | |||
@@ -0,0 +1,138 @@ | |||
from abp import GraphState, CircuitModel, clifford | |||
from anders_briegel import graphsim | |||
import numpy as np | |||
from tqdm import tqdm | |||
import itertools as it | |||
import mock | |||
REPEATS = 100 | |||
def test_hadamard(): | |||
""" Test hadamards """ | |||
a = graphsim.GraphRegister(1) | |||
b = GraphState() | |||
b.add_node(0) | |||
assert_equal(a, b) | |||
a.hadamard(0) | |||
b.act_hadamard(0) | |||
assert_equal(a, b) | |||
a.hadamard(0) | |||
b.act_hadamard(0) | |||
assert_equal(a, b) | |||
def test_local_rotations(): | |||
""" Test local rotations """ | |||
a = graphsim.GraphRegister(1) | |||
b = GraphState() | |||
b.add_node(0) | |||
assert_equal(a, b) | |||
for i in range(1000): | |||
j = random.randint(0, 23) | |||
a.local_op(0, graphsim.LocCliffOp(j)) | |||
b.act_local_rotation(0, j) | |||
assert_equal(a, b) | |||
def test_times_table(): | |||
""" Test times table """ | |||
for i, j in it.product(range(24), range(24)): | |||
a = graphsim.GraphRegister(1) | |||
b = GraphState([0]) | |||
a.local_op(0, graphsim.LocCliffOp(i)) | |||
a.local_op(0, graphsim.LocCliffOp(j)) | |||
b.act_local_rotation(0, i) | |||
b.act_local_rotation(0, j) | |||
assert_equal(a, b) | |||
def test_cz_table(N=10): | |||
""" Test the CZ table """ | |||
for i, j in it.product(range(24), range(24)): | |||
circuit = [(0, i), (1, j), ((0, 1), "cz")] | |||
mock.test_circuit(circuit, 2) | |||
#for i in range(24): | |||
#for j in range(24): | |||
#a = graphsim.GraphRegister(2) | |||
#b = GraphState() | |||
#b.add_nodes([0, 1]) | |||
#a.local_op(0, graphsim.LocCliffOp(10)) | |||
#b.act_local_rotation(0, 10) | |||
#a.cphase(0, 1) | |||
#b.act_cz(0, 1) | |||
#a.local_op(0, graphsim.LocCliffOp(i)) | |||
#b.act_local_rotation(0, i) | |||
#a.local_op(1, graphsim.LocCliffOp(j)) | |||
#b.act_local_rotation(1, j) | |||
#a.cphase(0, 1) | |||
#b.act_cz(0, 1) | |||
#assert_equal(a, b) | |||
def test_with_cphase_gates_hadamard_only(N=10): | |||
""" Hadamrds and CPHASEs, deterministic """ | |||
a = graphsim.GraphRegister(N) | |||
b = GraphState() | |||
for i in range(N): | |||
a.hadamard(i) | |||
b.add_node(i) | |||
b.act_hadamard(i) | |||
for i in range(N-1): | |||
a.cphase(i, i+1) | |||
b.act_cz(i, i+1) | |||
assert_equal(a, b) | |||
def test_cz_hadamard(N=10): | |||
""" Test CZs and Hadamards at random """ | |||
clifford.use_old_cz() | |||
a = graphsim.GraphRegister(N) | |||
b = GraphState(range(N)) | |||
for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"): | |||
if random.random()>0.5: | |||
j = random.randint(0, N-1) | |||
a.hadamard(j) | |||
b.act_hadamard(j) | |||
else: | |||
q = random.randint(0, N-2) | |||
a.cphase(q, q+1) | |||
b.act_cz(q, q+1) | |||
assert_equal(a, b) | |||
def test_all(N=9): | |||
""" Test everything""" | |||
clifford.use_old_cz() | |||
a = graphsim.GraphRegister(N) | |||
b = GraphState(range(N)) | |||
for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"): | |||
if random.random()>0.5: | |||
j = random.randint(0, N-1) | |||
u = random.randint(0, 23) | |||
a.local_op(j, graphsim.LocCliffOp(u)) | |||
b.act_local_rotation(j, u) | |||
else: | |||
q = random.randint(0, N-2) | |||
a.cphase(q, q+1) | |||
b.act_cz(q, q+1) | |||
assert_equal(a, b, str(i)) | |||
@@ -113,3 +113,4 @@ def test_cz_table(): | |||
table_output = state_table[bondp, c1p, c2p] | |||
assert np.allclose(computed_output, table_output) | |||