Anders and Briegel in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

134 行
3.9KB

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