|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- """
- Mock graphs used for testing
- """
-
- import numpy as np
- from abp import GraphState, clifford, qi
- from anders_briegel import graphsim
- from numpy import random
-
- # We always run with A&B's CZ table when we are testing
- clifford.use_old_cz()
-
- class AndersWrapper(graphsim.GraphRegister):
-
- """ A wrapper for A&B to make the interface identical and enable equality testing """
-
- def __init__(self, nodes):
- assert list(nodes) == range(len(nodes))
- super(AndersWrapper, self).__init__(len(nodes))
-
- def act_local_rotation(self, qubit, operation):
- operation = clifford.by_name[str(operation)]
- op = graphsim.LocCliffOp(operation)
- super(AndersWrapper, self).local_op(qubit, op)
-
- def act_cz(self, a, b):
- super(AndersWrapper, self).cphase(a, b)
-
- def measure(self, qubit, basis, force):
- basis = {1: graphsim.lco_X,
- 2: graphsim.lco_Y,
- 3: graphsim.lco_Z}[clifford.by_name[str(basis)]]
- return super(AndersWrapper, self).measure(qubit, basis, None, force)
-
- def __eq__(self, other):
- return self.to_json() == other.to_json()
-
- def act_circuit(self, circuit):
- for node, operation in circuit:
- if operation == "cz":
- self.act_cz(*node)
- else:
- self.act_local_rotation(node, operation)
-
-
- class ABPWrapper(GraphState):
-
- """ A wrapper for abp, just to ensure determinism """
-
- def __init__(self, nodes=[]):
- super(ABPWrapper, self).__init__(nodes, deterministic=True)
-
- def print_stabilizer(self):
- print self.to_stabilizer()
-
- def __eq__(self, other):
- return self.to_json() == other.to_json()
-
-
- class CircuitModelWrapper(qi.CircuitModel):
-
- def __init__(self, nodes=[]):
- assert list(nodes) == range(len(nodes))
- super(CircuitModelWrapper, self).__init__(len(nodes))
-
- def act_circuit(self, circuit):
- """ Act a sequence of gates """
- for node, operation in circuit:
- if operation == "cz":
- self.act_cz(*node)
- else:
- u = clifford.unitaries[clifford.by_name[str(operation)]]
- self.act_local_rotation(node, u)
-
-
- def random_pair(n):
- """ Helper function to get random pairs"""
- return tuple(random.choice(range(n), 2, replace=False))
-
-
- def random_graph_circuit(n=10, depth=100):
- """ A random Graph state. """
- return [(i, "hadamard") for i in xrange(n)] + \
- [(random_pair(n), "cz") for i in xrange(depth)]
-
-
- def random_stabilizer_circuit(n=10, depth=100):
- """ Generate a random stabilizer state, without any VOPs """
- return random_graph_circuit(n, depth) + \
- [(i, random.choice(range(24))) for i in range(n)]
-
-
- def bell_pair():
- """ Generate a bell pair circuit """
- return [(0, "hadamard"), (1, "hadamard"), ((0, 1), "cz")]
-
-
- def named_node_graph():
- """ A graph with named nodes"""
- edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200), (200, "named")
- g = ABPWrapper([0, 1, 2, 3, 100, 200, "named"])
- g.act_circuit((i, "hadamard") for i in g.node)
- g.act_circuit((edge, "cz") for edge in edges)
- return g
-
-
- def simple_graph():
- """ A simple graph to test with"""
- edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200)
- g = ABPWrapper([0, 1, 2, 3, 100, 200])
- g.act_circuit((i, "hadamard") for i in g.node)
- g.act_circuit((edge, "cz") for edge in edges)
- return g
-
-
- def circuit_to_state(Base, n, circuit):
- """ Convert a circuit to a state, given a base class """
- g = Base(range(n))
- g.act_circuit(circuit)
- return g
-
-
- def test_circuit(circuit, n):
- """ 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)
- assert a == b
-
-
- if __name__ == '__main__':
- for i in range(1000):
- test_circuit(random_graph_circuit(10), 10)
- test_circuit(random_stabilizer_circuit(10), 10)
|