@@ -4,12 +4,14 @@ from abp import CircuitModel | |||||
from abp import clifford | from abp import clifford | ||||
import random | import random | ||||
from copy import deepcopy | from copy import deepcopy | ||||
import numpy as np | |||||
from tqdm import tqdm | from tqdm import tqdm | ||||
from abp.anders_cz import cz_table as abczt | |||||
REPEATS = 100 | |||||
REPEATS = 100000 | |||||
def assert_equal(a, b): | |||||
assert a.to_json() == b.to_json() | |||||
def assert_equal(a, b, debug=""): | |||||
assert a.to_json() == b.to_json(), "\n\n" + debug + "\n\n" + str(a.to_json()) + "\n\n" + str(b.to_json()) | |||||
def test_hadamard(): | def test_hadamard(): | ||||
""" Test hadamards """ | """ Test hadamards """ | ||||
@@ -103,14 +105,14 @@ def test_with_cphase_gates_hadamard_only(N=10): | |||||
assert_equal(a, b) | assert_equal(a, b) | ||||
def test_cz_hadamard(N=3): | |||||
def test_cz_hadamard(N=20): | |||||
""" Test CZs and Hadamards at random """ | """ Test CZs and Hadamards at random """ | ||||
clifford.use_old_cz() | clifford.use_old_cz() | ||||
assert np.allclose(clifford.cz_table, abczt) | |||||
a = graphsim.GraphRegister(N) | a = graphsim.GraphRegister(N) | ||||
b = GraphState(range(N)) | b = GraphState(range(N)) | ||||
previous_state, previous_cz = None, None | |||||
for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"): | for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"): | ||||
if random.random()>0.5: | if random.random()>0.5: | ||||
j = random.randint(0, N-1) | j = random.randint(0, N-1) | ||||
@@ -118,14 +120,13 @@ def test_cz_hadamard(N=3): | |||||
b.act_hadamard(j) | b.act_hadamard(j) | ||||
else: | else: | ||||
q = random.randint(0, N-2) | q = random.randint(0, N-2) | ||||
if a!=b: | |||||
a.cphase(q, q+1) | |||||
b.act_cz(q, q+1) | |||||
a.cphase(q, q+1) | |||||
b.act_cz(q, q+1) | |||||
assert_equal(a, b) | assert_equal(a, b) | ||||
def test_all(N=5): | |||||
def _test_all(N=9): | |||||
""" Test everything""" | """ Test everything""" | ||||
clifford.use_old_cz() | clifford.use_old_cz() | ||||
@@ -141,9 +142,8 @@ def test_all(N=5): | |||||
b.act_local_rotation(j, u) | b.act_local_rotation(j, u) | ||||
else: | else: | ||||
q = random.randint(0, N-2) | q = random.randint(0, N-2) | ||||
if a!=b: | |||||
a.cphase(q, q+1) | |||||
b.act_cz(q, q+1) | |||||
assert_equal(a, b) | |||||
a.cphase(q, q+1) | |||||
b.act_cz(q, q+1) | |||||
assert_equal(a, b, str(i)) | |||||
@@ -1,7 +1,14 @@ | |||||
import numpy as np | import numpy as np | ||||
from abp import GraphState | from abp import GraphState | ||||
from abp import qi | |||||
from abp import qi, clifford | |||||
from anders_briegel import graphsim | from anders_briegel import graphsim | ||||
from tqdm import tqdm | |||||
import random | |||||
REPEATS = 100000 | |||||
LOCAL_ROTATION = 0 | |||||
CZ = 1 | |||||
MEASURE = 2 | |||||
def test_single_qubit_measurements(): | def test_single_qubit_measurements(): | ||||
""" Various simple tests of measurements """ | """ Various simple tests of measurements """ | ||||
@@ -23,7 +30,6 @@ def test_single_qubit_measurements(): | |||||
g.act_local_rotation(0, "pz") | g.act_local_rotation(0, "pz") | ||||
assert g.measure(0, "px") == 1, "Measuring |-> in X gives 1" | assert g.measure(0, "px") == 1, "Measuring |-> in X gives 1" | ||||
def test_random_outcomes(): | def test_random_outcomes(): | ||||
""" Testing random behaviour """ | """ Testing random behaviour """ | ||||
ones = 0 | ones = 0 | ||||
@@ -47,9 +53,6 @@ def test_another_projection(): | |||||
g.measure(0, "pz", 1) | g.measure(0, "pz", 1) | ||||
assert np.allclose(g.to_state_vector().state, qi.one) | assert np.allclose(g.to_state_vector().state, qi.one) | ||||
def test_z_measurement_against_ab(): | def test_z_measurement_against_ab(): | ||||
for i in range(10): | for i in range(10): | ||||
a = graphsim.GraphRegister(1) | a = graphsim.GraphRegister(1) | ||||
@@ -57,3 +60,33 @@ def test_z_measurement_against_ab(): | |||||
b.add_node(0) | b.add_node(0) | ||||
#print a.measure(0, graphsim.lco_Z) | #print a.measure(0, graphsim.lco_Z) | ||||
#print b.measure(0, "pz") | #print b.measure(0, "pz") | ||||
def test_all(N=20): | |||||
""" Test everything""" | |||||
clifford.use_old_cz() | |||||
a = graphsim.GraphRegister(N) | |||||
b = GraphState(range(N)) | |||||
previous_state, previous_cz = None, None | |||||
for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"): | |||||
which = random.choice([LOCAL_ROTATION, CZ, MEASURE]) | |||||
if which == LOCAL_ROTATION: | |||||
j = random.randint(0, N-1) | |||||
u = random.randint(0, 23) | |||||
a.local_op(j, graphsim.LocCliffOp(u)) | |||||
b.act_local_rotation(j, u) | |||||
elif which == CZ: | |||||
q = random.randint(0, N-2) | |||||
if a!=b: | |||||
a.cphase(q, q+1) | |||||
b.act_cz(q, q+1) | |||||
else: | |||||
pass | |||||
#q = random.randint(0, N-2) | |||||
#m = random.choice(["px", "py", "pz"]) | |||||
#a.measure(q, m) | |||||
#b.measure(q, mm) | |||||
assert a.to_json() == b.to_json() | |||||