Anders and Briegel in Python
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

141 rinda
4.1KB

  1. """
  2. Mock graphs used for testing
  3. """
  4. import numpy as np
  5. import abp
  6. from abp import GraphState, clifford, qi
  7. from numpy import random
  8. import nose
  9. try:
  10. from anders_briegel import graphsim
  11. except ImportError:
  12. raise nose.SkipTest("Original C++ is not available, skipping test")
  13. # We always run with A&B's CZ table when we are testing
  14. clifford.use_old_cz()
  15. class AndersWrapper(graphsim.GraphRegister):
  16. """ A wrapper for A&B to make the interface identical and enable equality testing """
  17. def __init__(self, nodes):
  18. assert list(nodes) == range(len(nodes))
  19. super(AndersWrapper, self).__init__(len(nodes))
  20. def act_local_rotation(self, qubit, operation):
  21. operation = clifford.by_name[str(operation)]
  22. op = graphsim.LocCliffOp(operation)
  23. super(AndersWrapper, self).local_op(qubit, op)
  24. def act_cz(self, a, b):
  25. super(AndersWrapper, self).cphase(a, b)
  26. def measure(self, qubit, basis, force):
  27. basis = {1: graphsim.lco_X,
  28. 2: graphsim.lco_Y,
  29. 3: graphsim.lco_Z}[clifford.by_name[str(basis)]]
  30. return super(AndersWrapper, self).measure(qubit, basis, None, force)
  31. def __eq__(self, other):
  32. return self.to_json() == other.to_json()
  33. def act_circuit(self, circuit):
  34. for operation, node in circuit:
  35. if operation == "cz":
  36. self.act_cz(*node)
  37. else:
  38. self.act_local_rotation(node, operation)
  39. class ABPWrapper(GraphState):
  40. """ A wrapper for abp, just to ensure determinism """
  41. def __init__(self, nodes=[]):
  42. abp.DETERMINISTIC = True
  43. super(ABPWrapper, self).__init__(nodes, vop="hadamard")
  44. def print_stabilizer(self):
  45. print self.to_stabilizer()
  46. def __eq__(self, other):
  47. return self.to_json() == other.to_json()
  48. class CircuitModelWrapper(qi.CircuitModel):
  49. def __init__(self, nodes=[]):
  50. assert list(nodes) == range(len(nodes))
  51. super(CircuitModelWrapper, self).__init__(len(nodes))
  52. def act_circuit(self, circuit):
  53. """ Act a sequence of gates """
  54. for node, operation in circuit:
  55. if operation == "cz":
  56. self.act_cz(*node)
  57. else:
  58. u = clifford.unitaries[clifford.by_name[str(operation)]]
  59. self.act_local_rotation(node, u)
  60. def random_pair(n):
  61. """ Helper function to get random pairs"""
  62. return tuple(random.choice(range(n), 2, replace=False))
  63. def random_graph_circuit(n=10, depth=100):
  64. """ A random Graph state. """
  65. return [(i, "hadamard") for i in xrange(n)] + \
  66. [(random_pair(n), "cz") for i in xrange(depth)]
  67. def random_stabilizer_circuit(n=10, depth=100):
  68. """ Generate a random stabilizer state, without any VOPs """
  69. return random_graph_circuit(n, depth) + \
  70. [(i, random.choice(range(24))) for i in range(n)]
  71. def bell_pair():
  72. """ Generate a bell pair circuit """
  73. return [(0, "hadamard"), (1, "hadamard"), ((0, 1), "cz")]
  74. def named_node_graph():
  75. """ A graph with named nodes"""
  76. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200), (200, "named")
  77. g = ABPWrapper([0, 1, 2, 3, 100, 200, "named"])
  78. g.act_circuit(("hadamard", i) for i in g.node)
  79. g.act_circuit(("cz", edge) for edge in edges)
  80. return g
  81. def simple_graph():
  82. """ A simple graph to test with"""
  83. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200)
  84. g = ABPWrapper([0, 1, 2, 3, 100, 200])
  85. g.act_circuit(("hadamard", i) for i in g.node)
  86. g.act_circuit(("cz", edge) for edge in edges)
  87. return g
  88. def circuit_to_state(Base, n, circuit):
  89. """ Convert a circuit to a state, given a base class """
  90. g = Base(range(n))
  91. g.act_circuit(circuit)
  92. return g
  93. def test_circuit(circuit, n):
  94. """ Check that two classes exhibit the same behaviour for a given circuit """
  95. a = circuit_to_state(ABPWrapper, n, circuit)
  96. b = circuit_to_state(AndersWrapper, n, circuit)
  97. assert a == b
  98. if __name__ == '__main__':
  99. for i in range(1000):
  100. test_circuit(random_graph_circuit(10), 10)
  101. test_circuit(random_stabilizer_circuit(10), 10)