|
@@ -1,6 +1,5 @@ |
|
|
from abp import GraphState, CircuitModel, clifford |
|
|
from abp import GraphState, CircuitModel, clifford |
|
|
from abp import clifford |
|
|
|
|
|
from mock import simple_graph |
|
|
|
|
|
|
|
|
import mock |
|
|
import random |
|
|
import random |
|
|
import numpy as np |
|
|
import numpy as np |
|
|
from tqdm import tqdm |
|
|
from tqdm import tqdm |
|
@@ -11,7 +10,7 @@ DEPTH = 100 |
|
|
|
|
|
|
|
|
def test_graph_basic(): |
|
|
def test_graph_basic(): |
|
|
""" Test that we can construct graphs, delete edges, whatever """ |
|
|
""" Test that we can construct graphs, delete edges, whatever """ |
|
|
g = simple_graph() |
|
|
|
|
|
|
|
|
g = mock.simple_graph() |
|
|
assert set(g.adj[0].keys()) == set([1, 2, 3]) |
|
|
assert set(g.adj[0].keys()) == set([1, 2, 3]) |
|
|
g._del_edge(0, 1) |
|
|
g._del_edge(0, 1) |
|
|
assert set(g.adj[0].keys()) == set([2, 3]) |
|
|
assert set(g.adj[0].keys()) == set([2, 3]) |
|
@@ -21,7 +20,7 @@ def test_graph_basic(): |
|
|
|
|
|
|
|
|
def test_local_complementation(): |
|
|
def test_local_complementation(): |
|
|
""" Test that local complementation works as expected """ |
|
|
""" Test that local complementation works as expected """ |
|
|
g = simple_graph() |
|
|
|
|
|
|
|
|
g = mock.simple_graph() |
|
|
g.local_complementation(0) |
|
|
g.local_complementation(0) |
|
|
assert g.has_edge(0, 1) |
|
|
assert g.has_edge(0, 1) |
|
|
assert g.has_edge(0, 2) |
|
|
assert g.has_edge(0, 2) |
|
@@ -34,7 +33,7 @@ def test_local_complementation(): |
|
|
|
|
|
|
|
|
def test_remove_vop(): |
|
|
def test_remove_vop(): |
|
|
""" Test that removing VOPs really works """ |
|
|
""" Test that removing VOPs really works """ |
|
|
g = simple_graph() |
|
|
|
|
|
|
|
|
g = mock.simple_graph() |
|
|
g.remove_vop(0, 1) |
|
|
g.remove_vop(0, 1) |
|
|
assert g.node[0]["vop"] == clifford.by_name["identity"] |
|
|
assert g.node[0]["vop"] == clifford.by_name["identity"] |
|
|
g.remove_vop(1, 1) |
|
|
g.remove_vop(1, 1) |
|
@@ -47,17 +46,17 @@ def test_remove_vop(): |
|
|
|
|
|
|
|
|
def test_edgelist(): |
|
|
def test_edgelist(): |
|
|
""" Test making edgelists """ |
|
|
""" Test making edgelists """ |
|
|
g = simple_graph() |
|
|
|
|
|
|
|
|
g = mock.simple_graph() |
|
|
el = g.edgelist() |
|
|
el = g.edgelist() |
|
|
assert (0, 3) in el |
|
|
assert (0, 3) in el |
|
|
assert (0, 2) in el |
|
|
assert (0, 2) in el |
|
|
assert (100, 200) in el |
|
|
assert (100, 200) in el |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
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): |
|
|
g._add_edge(i, i + 1) |
|
|
g._add_edge(i, i + 1) |
|
@@ -74,107 +73,50 @@ def test_cz(): |
|
|
g.act_cz(0, 1) |
|
|
g.act_cz(0, 1) |
|
|
assert g.has_edge(0, 1) |
|
|
assert g.has_edge(0, 1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stabilizer(): |
|
|
def test_stabilizer(): |
|
|
""" Test that we can generate stabilizers okay """ |
|
|
""" Test that we can generate stabilizers okay """ |
|
|
g = simple_graph() |
|
|
|
|
|
|
|
|
g = mock.simple_graph() |
|
|
stab = g.to_stabilizer() |
|
|
stab = g.to_stabilizer() |
|
|
#TODO |
|
|
|
|
|
|
|
|
# TODO |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_local_complementation(): |
|
|
def test_local_complementation(): |
|
|
""" Test that local complementation works okay """ |
|
|
""" 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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pairs = (0, 1), (0, 3), (1, 3), (1, 2), |
|
|
|
|
|
psi = GraphState(range(4)) |
|
|
|
|
|
psi.act_circuit([(i, "hadamard") for i in psi.node]) |
|
|
|
|
|
psi.act_circuit([(pair, "cz") for pair in pairs]) |
|
|
old_edges = psi.edgelist() |
|
|
old_edges = psi.edgelist() |
|
|
old_state = psi.to_state_vector() |
|
|
old_state = psi.to_state_vector() |
|
|
psi.local_complementation(1) |
|
|
psi.local_complementation(1) |
|
|
assert old_edges != psi.edgelist() |
|
|
assert old_edges != psi.edgelist() |
|
|
assert old_state == psi.to_state_vector() |
|
|
assert old_state == psi.to_state_vector() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_single_qubit(): |
|
|
def test_single_qubit(): |
|
|
""" A multi qubit test with Hadamards only""" |
|
|
""" 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 |
|
|
|
|
|
|
|
|
for repeat in tqdm(range(REPEATS), desc="Randomly testing single qubit operations against circuit model"): |
|
|
|
|
|
circuit = [(0, random.choice(range(24))) for i in range(DEPTH)] |
|
|
|
|
|
a = mock.circuit_to_state(mock.ABPWrapper, 1, circuit) |
|
|
|
|
|
b = mock.circuit_to_state(mock.CircuitModelWrapper, 1, circuit) |
|
|
|
|
|
assert a.to_state_vector() == b |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_hadamard_only_multiqubit(n=6): |
|
|
|
|
|
|
|
|
def test_graphstate_multiqubit(n=6): |
|
|
""" A multi qubit test with Hadamards only""" |
|
|
""" 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 repeat in tqdm(range(REPEATS), desc="Randomly testing multiqubit operations against circuit model"): |
|
|
|
|
|
circuit = mock.random_graph_circuit(n) |
|
|
|
|
|
a = mock.circuit_to_state(mock.ABPWrapper, n, circuit) |
|
|
|
|
|
b = mock.circuit_to_state(mock.CircuitModelWrapper, n, circuit) |
|
|
|
|
|
assert a.to_state_vector() == b |
|
|
|
|
|
|
|
|
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): |
|
|
|
|
|
|
|
|
def test_stabilizerstate_multiqubit(n=6): |
|
|
""" A multi qubit test with arbitrary local rotations """ |
|
|
""" 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for repeat in tqdm(range(REPEATS), desc="Randomly testing multiqubit operations against circuit model"): |
|
|
|
|
|
circuit = mock.random_stabilizer_circuit(n) |
|
|
|
|
|
a = mock.circuit_to_state(mock.ABPWrapper, n, circuit) |
|
|
|
|
|
b = mock.circuit_to_state(mock.CircuitModelWrapper, n, circuit) |
|
|
|
|
|
assert a.to_state_vector() == b |
|
|
|
|
|
|