@@ -97,9 +97,10 @@ def named_node_graph(): | |||||
g.act_circuit((edge, "cz") for edge in edges) | g.act_circuit((edge, "cz") for edge in edges) | ||||
return g | return g | ||||
def simple_graph(): | def simple_graph(): | ||||
""" A simple graph to test with""" | """ A simple graph to test with""" | ||||
edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200) | |||||
edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200) | |||||
g = ABPWrapper([0, 1, 2, 3, 100, 200]) | g = ABPWrapper([0, 1, 2, 3, 100, 200]) | ||||
g.act_circuit((i, "hadamard") for i in g.node) | g.act_circuit((i, "hadamard") for i in g.node) | ||||
g.act_circuit((edge, "cz") for edge in edges) | g.act_circuit((edge, "cz") for edge in edges) | ||||
@@ -1,77 +0,0 @@ | |||||
from abp import GraphState | |||||
from abp import clifford | |||||
from demograph import demograph | |||||
import time | |||||
def test_graph_basic(): | |||||
""" Test that we can construct graphs, delete edges, whatever """ | |||||
g = demograph() | |||||
assert set(g.adj[0].keys()) == set([1, 2, 3]) | |||||
g.del_edge(0, 1) | |||||
assert set(g.adj[0].keys()) == set([2, 3]) | |||||
assert g.has_edge(1, 2) | |||||
assert not g.has_edge(0, 1) | |||||
def test_local_complementation(): | |||||
""" Test that local complementation works as expected """ | |||||
g = demograph() | |||||
g.local_complementation(0) | |||||
assert g.has_edge(0, 1) | |||||
assert g.has_edge(0, 2) | |||||
assert not g.has_edge(1, 2) | |||||
assert g.has_edge(3, 2) | |||||
assert g.has_edge(3, 1) | |||||
# TODO: test VOP conditions | |||||
def test_remove_vop(): | |||||
""" Test that removing VOPs really works """ | |||||
g = demograph() | |||||
g.remove_vop(0, 1) | |||||
assert g.node[0]["vop"] == clifford.by_name["identity"] | |||||
g.remove_vop(1, 1) | |||||
assert g.node[1]["vop"] == clifford.by_name["identity"] | |||||
g.remove_vop(2, 1) | |||||
assert g.node[2]["vop"] == clifford.by_name["identity"] | |||||
g.remove_vop(0, 1) | |||||
assert g.node[0]["vop"] == clifford.by_name["identity"] | |||||
def test_edgelist(): | |||||
""" Test making edgelists """ | |||||
g = demograph() | |||||
el = g.edgelist() | |||||
assert (0, 3) in el | |||||
assert (0, 2) in el | |||||
assert (100, 200) in el | |||||
def test_stress(n = int(1e5)): | |||||
""" Testing that making a graph of ten thousand qubits takes less than half a second""" | |||||
g = GraphState(range(n+1)) | |||||
t = time.clock() | |||||
for i in xrange(n): | |||||
g.add_edge(i, i + 1) | |||||
assert time.clock() - t < .5 | |||||
def test_cz(): | |||||
""" Test CZ gate """ | |||||
g = GraphState([0, 1]) | |||||
g.act_local_rotation(0, clifford.by_name["hadamard"]) | |||||
g.act_local_rotation(1, clifford.by_name["hadamard"]) | |||||
g.act_local_rotation(1, clifford.by_name["py"]) | |||||
assert not g.has_edge(0, 1) | |||||
g.act_cz(0, 1) | |||||
assert g.has_edge(0, 1) | |||||
def test_stabilizer(): | |||||
""" Test that we can generate stabilizers okay """ | |||||
g = demograph() | |||||
stab = g.to_stabilizer() | |||||
#TODO: sux | |||||
#assert len(stab.split("\n")) == g.order() | |||||
@@ -1,24 +0,0 @@ | |||||
from abp import GraphState, fancy | |||||
from abp import clifford | |||||
from demograph import demograph | |||||
import time | |||||
import json | |||||
def test_json_basic(): | |||||
""" Test that we can export to JSON """ | |||||
g = demograph() | |||||
js = g.to_json() | |||||
assert "adj" in js | |||||
assert "node" in js | |||||
e = GraphState() | |||||
def test_tuple_keys(): | |||||
""" Test that we can write tuple-ish keys """ | |||||
g = fancy.GraphState() | |||||
g.add_node("string") | |||||
g.add_node((1, 2, 3)) | |||||
g.add_edge((1, 2, 3), "string") | |||||
print json.dumps(g.to_json(True)) | |||||
@@ -1,17 +0,0 @@ | |||||
from demograph import demograph | |||||
#TODO | |||||
def _test_nx_convert(): | |||||
g = demograph() | |||||
n = g.to_networkx() | |||||
assert len(g.ngbh) == len(n.edge) | |||||
assert len(g.vops) == len(n.node) | |||||
def _test_layout(): | |||||
g = demograph() | |||||
g.layout() | |||||
assert len(g.meta) == len(g.vops) | |||||
assert "pos" in g.meta[0] | |||||
assert "x" in g.meta[0]["pos"] | |||||
@@ -1,24 +0,0 @@ | |||||
from abp.fancy import GraphState | |||||
from abp import qi | |||||
def test_local_comp(): | |||||
""" Test that local complementation works okay """ | |||||
psi = GraphState() | |||||
psi.add_node(0) | |||||
psi.add_node(1) | |||||
psi.add_node(2) | |||||
psi.add_node(3) | |||||
for n in psi.node: | |||||
psi.act_hadamard(n) | |||||
psi.act_cz(0, 1) | |||||
psi.act_cz(0, 3) | |||||
psi.act_cz(1, 3) | |||||
psi.act_cz(1, 2) | |||||
before = psi.copy() | |||||
psi.local_complementation(1) | |||||
assert before.edgelist() != psi.edgelist() | |||||
assert before.to_state_vector() == psi.to_state_vector() | |||||
@@ -1,11 +0,0 @@ | |||||
from abp import clifford | |||||
from abp import qi | |||||
import numpy as np | |||||
def test_normalize_global_phase(): | |||||
for i in range(10): | |||||
u = qi.pz | |||||
phase = np.random.uniform(0, 2*np.pi) | |||||
m = np.exp(1j*phase) * u | |||||
normalized = qi.normalize_global_phase(m) | |||||
assert np.allclose(normalized, u) |
@@ -1,12 +0,0 @@ | |||||
from abp.graphstate import GraphState | |||||
from abp.util import xyz | |||||
import networkx as nx | |||||
def simple_test(): | |||||
g = GraphState() | |||||
g.add_node(0, position = xyz(10, 0, 0)) | |||||
g.add_node(1, position = xyz(1, 0, 0)) | |||||
g.act_hadamard(0) | |||||
g.act_hadamard(1) | |||||
g.act_cz(0, 1) | |||||
@@ -0,0 +1,36 @@ | |||||
import json | |||||
import networkx as nx | |||||
from abp import GraphState, fancy | |||||
from abp import clifford | |||||
from abp.util import xyz | |||||
from mock import simple_graph | |||||
def test_json_basic(): | |||||
""" Test that we can export to JSON """ | |||||
g = simple_graph() | |||||
js = g.to_json() | |||||
assert "adj" in js | |||||
assert "node" in js | |||||
def test_tuple_keys(): | |||||
""" Test that we can use tuple-ish keys """ | |||||
g = fancy.GraphState() | |||||
g.add_node("string") | |||||
g.add_node((1, 2, 3)) | |||||
g.add_edge((1, 2, 3), "string") | |||||
json.dumps(g.to_json(True)) | |||||
def networkx_test(): | |||||
""" Test that fancy graph states really behave like networkx graphs """ | |||||
g = fancy.GraphState() | |||||
g.add_node(0, position = xyz(10, 0, 0)) | |||||
g.add_node(1, position = xyz(1, 0, 0)) | |||||
g.act_hadamard(0) | |||||
g.act_hadamard(1) | |||||
g.act_cz(0, 1) | |||||
g.copy() | |||||
# TODO: more tests here! | |||||
@@ -1,7 +1,12 @@ | |||||
from abp import GraphState | |||||
from abp import GraphState, CircuitModel, clifford | |||||
from abp import clifford | from abp import clifford | ||||
from mock import simple_graph | from mock import simple_graph | ||||
import time | |||||
import random | |||||
import numpy as np | |||||
from tqdm import tqdm | |||||
REPEATS = 100 | |||||
DEPTH = 100 | |||||
def test_graph_basic(): | def test_graph_basic(): | ||||
@@ -51,6 +56,7 @@ def test_edgelist(): | |||||
def test_stress(n = int(1e5)): | def test_stress(n = int(1e5)): | ||||
""" Testing that making a graph of ten thousand qubits takes less than half a second""" | """ Testing that making a graph of ten thousand qubits takes less than half a second""" | ||||
import time | |||||
g = GraphState(range(n+1)) | g = GraphState(range(n+1)) | ||||
t = time.clock() | t = time.clock() | ||||
for i in xrange(n): | for i in xrange(n): | ||||
@@ -74,3 +80,101 @@ def test_stabilizer(): | |||||
stab = g.to_stabilizer() | stab = g.to_stabilizer() | ||||
#TODO | #TODO | ||||
def test_local_complementation(): | |||||
""" Test that local complementation works okay """ | |||||
psi = GraphState() | |||||
psi.add_node(0) | |||||
psi.add_node(1) | |||||
psi.add_node(2) | |||||
psi.add_node(3) | |||||
for n in psi.node: | |||||
psi.act_hadamard(n) | |||||
psi.act_cz(0, 1) | |||||
psi.act_cz(0, 3) | |||||
psi.act_cz(1, 3) | |||||
psi.act_cz(1, 2) | |||||
old_edges = psi.edgelist() | |||||
old_state = psi.to_state_vector() | |||||
psi.local_complementation(1) | |||||
assert old_edges != psi.edgelist() | |||||
assert old_state == psi.to_state_vector() | |||||
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 = np.random.choice(range(24)) | |||||
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 | |||||
@@ -92,13 +92,9 @@ def test_dumbness(): | |||||
a = qi.CircuitModel(1) | a = qi.CircuitModel(1) | ||||
b = qi.CircuitModel(1) | b = qi.CircuitModel(1) | ||||
assert a == b | assert a == b | ||||
a.act_local_rotation(0, qi.px) | a.act_local_rotation(0, qi.px) | ||||
assert not (a == b) | assert not (a == b) | ||||
a.act_local_rotation(0, qi.px) | a.act_local_rotation(0, qi.px) | ||||
assert (a == b) | assert (a == b) | ||||
@@ -111,3 +107,12 @@ def test_to_state_vector_single_qubit(): | |||||
g.act_local_rotation(1, "hadamard") | g.act_local_rotation(1, "hadamard") | ||||
g.act_cz(0, 1) | g.act_cz(0, 1) | ||||
assert np.allclose(g.to_state_vector().state, qi.bond) | assert np.allclose(g.to_state_vector().state, qi.bond) | ||||
def test_normalize_global_phase(): | |||||
""" We should be able to see that two states are equivalent up to a global phase """ | |||||
for i in range(10): | |||||
u = qi.pz | |||||
phase = np.random.uniform(0, 2*np.pi) | |||||
m = np.exp(1j*phase) * u | |||||
normalized = qi.normalize_global_phase(m) | |||||
assert np.allclose(normalized, u) |