Anders and Briegel in Python
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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