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.

pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 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) == list(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. abp.DETERMINISTIC = True
  42. super(ABPWrapper, self).__init__(nodes, vop="hadamard")
  43. def print_stabilizer(self):
  44. print(self.to_stabilizer())
  45. def __eq__(self, other):
  46. return self.to_json() == other.to_json()
  47. class CircuitModelWrapper(qi.CircuitModel):
  48. def __init__(self, nodes=[]):
  49. assert list(nodes) == list(range(len(nodes)))
  50. super(CircuitModelWrapper, self).__init__(len(nodes))
  51. def act_circuit(self, circuit):
  52. """ Act a sequence of gates """
  53. for node, operation in circuit:
  54. if operation == "cz":
  55. self.act_cz(*node)
  56. else:
  57. u = clifford.unitaries[clifford.by_name[str(operation)]]
  58. self.act_local_rotation(node, u)
  59. def random_pair(n):
  60. """ Helper function to get random pairs"""
  61. return tuple(random.choice(list(range(n)), 2, replace=False))
  62. def random_graph_circuit(n=10, depth=100):
  63. """ A random Graph state. """
  64. return [(i, "hadamard") for i in range(n)] + \
  65. [(random_pair(n), "cz") for i in range(depth)]
  66. def random_stabilizer_circuit(n=10, depth=100):
  67. """ Generate a random stabilizer state, without any VOPs """
  68. return random_graph_circuit(n, depth) + \
  69. [(i, random.choice(list(range(24)))) for i in range(n)]
  70. def bell_pair():
  71. """ Generate a bell pair circuit """
  72. return [(0, "hadamard"), (1, "hadamard"), ((0, 1), "cz")]
  73. def named_node_graph():
  74. """ A graph with named nodes"""
  75. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200), (200, "named")
  76. g = ABPWrapper([0, 1, 2, 3, 100, 200, "named"])
  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 simple_graph():
  81. """ A simple graph to test with"""
  82. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200)
  83. g = ABPWrapper([0, 1, 2, 3, 100, 200])
  84. g.act_circuit((i, "hadamard") for i in g.node)
  85. g.act_circuit((edge, "cz") for edge in edges)
  86. return g
  87. def circuit_to_state(Base, n, circuit):
  88. """ Convert a circuit to a state, given a base class """
  89. g = Base(list(range(n)))
  90. g.act_circuit(circuit)
  91. return g
  92. def test_circuit(circuit, n):
  93. """ Check that two classes exhibit the same behaviour for a given circuit """
  94. a = circuit_to_state(ABPWrapper, n, circuit)
  95. b = circuit_to_state(AndersWrapper, n, circuit)
  96. assert a == b
  97. if __name__ == '__main__':
  98. for i in range(1000):
  99. test_circuit(random_graph_circuit(10), 10)
  100. test_circuit(random_stabilizer_circuit(10), 10)