@@ -10,7 +10,6 @@ from numpy import random | |||||
# We always run with A&B's CZ table when we are testing | # We always run with A&B's CZ table when we are testing | ||||
clifford.use_old_cz() | clifford.use_old_cz() | ||||
class AndersWrapper(graphsim.GraphRegister): | class AndersWrapper(graphsim.GraphRegister): | ||||
""" A wrapper for A&B to make the interface identical and enable equality testing """ | """ A wrapper for A&B to make the interface identical and enable equality testing """ | ||||
@@ -28,11 +27,10 @@ class AndersWrapper(graphsim.GraphRegister): | |||||
super(AndersWrapper, self).cphase(a, b) | super(AndersWrapper, self).cphase(a, b) | ||||
def measure(self, qubit, basis, force): | def measure(self, qubit, basis, force): | ||||
basis = clifford.by_name[basis] | |||||
basis = {1: graphsim.lco_X, | basis = {1: graphsim.lco_X, | ||||
2: graphsim.lco_Y, | 2: graphsim.lco_Y, | ||||
3: graphsim.lco_Z}[clifford.by_name[basis]] | |||||
super(AndersWrapper, self).measure(qubit, basis, None, force) | |||||
3: graphsim.lco_Z}[clifford.by_name[str(basis)]] | |||||
return super(AndersWrapper, self).measure(qubit, basis, None, force) | |||||
def __eq__(self, other): | def __eq__(self, other): | ||||
return self.to_json() == other.to_json() | return self.to_json() == other.to_json() | ||||
@@ -55,6 +53,7 @@ class ABPWrapper(GraphState): | |||||
def __eq__(self, other): | def __eq__(self, other): | ||||
return self.to_json() == other.to_json() | return self.to_json() == other.to_json() | ||||
class CircuitModelWrapper(qi.CircuitModel): | class CircuitModelWrapper(qi.CircuitModel): | ||||
def __init__(self, nodes=[]): | def __init__(self, nodes=[]): | ||||
@@ -85,7 +84,7 @@ def random_graph_circuit(n=10): | |||||
def random_stabilizer_circuit(n=10): | def random_stabilizer_circuit(n=10): | ||||
""" Generate a random stabilizer state, without any VOPs """ | """ Generate a random stabilizer state, without any VOPs """ | ||||
return random_graph_circuit(n) + \ | return random_graph_circuit(n) + \ | ||||
[(i, random.choice(range(24))) for i in range(n)] | |||||
[(i, random.choice(range(24))) for i in range(n)] | |||||
def bell_pair(): | def bell_pair(): | ||||
@@ -110,16 +109,18 @@ def simple_graph(): | |||||
g.act_circuit((edge, "cz") for edge in edges) | g.act_circuit((edge, "cz") for edge in edges) | ||||
return g | return g | ||||
def circuit_to_state(Base, n, circuit): | def circuit_to_state(Base, n, circuit): | ||||
""" Convert a circuit to a state, given a base class """ | """ Convert a circuit to a state, given a base class """ | ||||
g = Base(range(n)) | g = Base(range(n)) | ||||
g.act_circuit(circuit) | g.act_circuit(circuit) | ||||
return g | return g | ||||
def test_circuit(circuit, n): | def test_circuit(circuit, n): | ||||
""" Check that two classes exhibit the same behaviour for a given circuit """ | """ Check that two classes exhibit the same behaviour for a given circuit """ | ||||
a = circuit_to_state(ABPWrapper, n, circuit) | |||||
b = circuit_to_state(AndersWrapper, n, circuit) | |||||
a = circuit_to_state(ABPWrapper, n, circuit) | |||||
b = circuit_to_state(AndersWrapper, n, circuit) | |||||
assert a == b | assert a == b | ||||
@@ -127,4 +128,3 @@ if __name__ == '__main__': | |||||
for i in range(1000): | for i in range(1000): | ||||
test_circuit(random_graph_circuit(10), 10) | test_circuit(random_graph_circuit(10), 10) | ||||
test_circuit(random_stabilizer_circuit(10), 10) | test_circuit(random_stabilizer_circuit(10), 10) | ||||
@@ -1,38 +0,0 @@ | |||||
from abp import GraphState, clifford | |||||
from anders_briegel import graphsim | |||||
import numpy as np | |||||
from tqdm import tqdm | |||||
import dummy | |||||
import itertools as it | |||||
import networkx as nx | |||||
#def all_simple_graphs(filename="tests/graph5.g6"): | |||||
#""" Generate all possible simple graphs """ | |||||
#with open(filename) as f: | |||||
#for line in tqdm(f): | |||||
#yield nx.parse_graph6(line.strip()) | |||||
#def rotated(simple_graphs): | |||||
#for g in simple_graphs: | |||||
#for r in it.product(*[range(24)]*2): | |||||
#yield g, r | |||||
#print len(list(rotated(all_simple_graphs()))) | |||||
#N = 3 | |||||
#m = {1: graphsim.lco_X, 2: graphsim.lco_Y, 3: graphsim.lco_Z} | |||||
#measurements = (3, 2, 1) | |||||
#outcomes = (0, 1) | |||||
#local_ops = it.combinations_with_replacement(range(24), N) | |||||
#edge_patterns = | |||||
#print list(local_ops) | |||||
#print len(list(local_ops)) | |||||
#print list(edge_patterns) | |||||
@@ -21,6 +21,7 @@ def test_local_rotations(): | |||||
circuit = [(0, random.choice(range(24))) for j in range(DEPTH)] | circuit = [(0, random.choice(range(24))) for j in range(DEPTH)] | ||||
mock.test_circuit(circuit, 1) | mock.test_circuit(circuit, 1) | ||||
def test_times_table(): | def test_times_table(): | ||||
""" Test times table """ | """ Test times table """ | ||||
for i, j in it.product(range(24), range(24)): | for i, j in it.product(range(24), range(24)): | ||||
@@ -39,18 +40,29 @@ def test_cz_hadamard(n=10): | |||||
""" Test CZs and Hadamards at random """ | """ Test CZs and Hadamards at random """ | ||||
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"): | ||||
circuit = random.choice(["cz", "hadamard"], DEPTH) | circuit = random.choice(["cz", "hadamard"], DEPTH) | ||||
circuit = [(mock.random_pair(n), gate) if gate =="cz" | |||||
circuit = [(mock.random_pair(n), gate) if gate == "cz" | |||||
else (random.choice(range(n)), gate) | else (random.choice(range(n)), gate) | ||||
for gate in circuit] | for gate in circuit] | ||||
mock.test_circuit(circuit, n) | mock.test_circuit(circuit, n) | ||||
def test_all(n=10): | def test_all(n=10): | ||||
""" Test everything""" | """ Test everything""" | ||||
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"): | ||||
circuit = random.choice(["cz"]*10 + range(24), DEPTH) | |||||
circuit = [(mock.random_pair(n), gate) if gate =="cz" | |||||
circuit = random.choice(["cz"] * 10 + range(24), DEPTH) | |||||
circuit = [(mock.random_pair(n), gate) if gate == "cz" | |||||
else (random.choice(range(n)), gate) | else (random.choice(range(n)), gate) | ||||
for gate in circuit] | for gate in circuit] | ||||
mock.test_circuit(circuit, n) | mock.test_circuit(circuit, n) | ||||
def test_single_qubit_measurement(): | |||||
""" Check that single qubits work """ | |||||
space = it.product(range(24), ("px", "py", "pz"), (0, 1)) | |||||
for rotation, measurement, outcome in tqdm(space, "Testing single qubit measurements"): | |||||
a = mock.circuit_to_state(mock.ABPWrapper, 1, [(0, rotation)]) | |||||
b = mock.circuit_to_state(mock.AndersWrapper, 1, [(0, rotation)]) | |||||
result_a = a.measure(0, measurement, outcome) | |||||
result_b = b.measure(0, measurement, outcome) | |||||
assert result_a == result_b | |||||
assert a == b |