Anders and Briegel in Python
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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