Anders and Briegel in Python
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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