Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

mock.py 3.8KB

il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
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 = clifford.by_name[basis]
  23. basis = {1: graphsim.lco_X,
  24. 2: graphsim.lco_Y,
  25. 3: graphsim.lco_Z}[clifford.by_name[basis]]
  26. super(AndersWrapper, self).measure(qubit, basis, None, force)
  27. def __eq__(self, other):
  28. return self.to_json() == other.to_json()
  29. def act_circuit(self, circuit):
  30. for node, operation in circuit:
  31. if operation == "cz":
  32. self.act_cz(*node)
  33. else:
  34. self.act_local_rotation(node, operation)
  35. class ABPWrapper(GraphState):
  36. """ A wrapper for abp, just to ensure determinism """
  37. def __init__(self, nodes=[]):
  38. super(ABPWrapper, self).__init__(nodes, deterministic=True)
  39. def __eq__(self, other):
  40. return self.to_json() == other.to_json()
  41. class CircuitModelWrapper(qi.CircuitModel):
  42. def __init__(self, nodes=[]):
  43. assert list(nodes) == range(len(nodes))
  44. super(CircuitModelWrapper, self).__init__(len(nodes))
  45. def act_circuit(self, circuit):
  46. """ Act a sequence of gates """
  47. for node, operation in circuit:
  48. if operation == "cz":
  49. self.act_cz(*node)
  50. else:
  51. u = clifford.unitaries[clifford.by_name[str(operation)]]
  52. self.act_local_rotation(node, u)
  53. def random_pair(n):
  54. """ Helper function to get random pairs"""
  55. return tuple(random.choice(range(n), 2, replace=False))
  56. def random_graph_circuit(n=10):
  57. """ A random Graph state. """
  58. return [(i, "hadamard") for i in range(n)] + \
  59. [(random_pair(n), "cz") for i in range(n * 2)]
  60. def random_stabilizer_circuit(n=10):
  61. """ Generate a random stabilizer state, without any VOPs """
  62. return random_graph_circuit(n) + \
  63. [(i, random.choice(range(24))) for i in range(n)]
  64. def bell_pair():
  65. """ Generate a bell pair circuit """
  66. return [(0, "hadamard"), (1, "hadamard"), ((0, 1), "cz")]
  67. def named_node_graph():
  68. """ A graph with named nodes"""
  69. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200), (200, "named")
  70. g = ABPWrapper([0, 1, 2, 3, 100, 200, "named"])
  71. g.act_circuit((i, "hadamard") for i in g.node)
  72. g.act_circuit((edge, "cz") for edge in edges)
  73. return g
  74. def simple_graph():
  75. """ A simple graph to test with"""
  76. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200)
  77. g = ABPWrapper([0, 1, 2, 3, 100, 200])
  78. g.act_circuit((i, "hadamard") for i in g.node)
  79. g.act_circuit((edge, "cz") for edge in edges)
  80. return g
  81. def circuit_to_state(Base, n, circuit):
  82. """ Convert a circuit to a state, given a base class """
  83. g = Base(range(n))
  84. g.act_circuit(circuit)
  85. return g
  86. def test_circuit(circuit):
  87. """ Check that two classes exhibit the same behaviour for a given circuit """
  88. a = circuit_to_state(ABPWrapper, 10, circuit)
  89. b = circuit_to_state(AndersWrapper, 10, circuit)
  90. assert a == b
  91. if __name__ == '__main__':
  92. for i in range(1000):
  93. test_circuit(random_graph_circuit(10))
  94. test_circuit(random_stabilizer_circuit(10))