Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

97 lines
2.6KB

  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. # We always run with A&B's CZ table when we are testing
  10. clifford.use_old_cz()
  11. class ABPWrapper(GraphState):
  12. """ A wrapper for abp, just to ensure determinism """
  13. def __init__(self, nodes=[]):
  14. abp.DETERMINISTIC = True
  15. super(ABPWrapper, self).__init__(nodes, vop="hadamard")
  16. def print_stabilizer(self):
  17. print(self.to_stabilizer())
  18. def __eq__(self, other):
  19. return self.to_json() == other.to_json()
  20. class CircuitModelWrapper(qi.CircuitModel):
  21. def __init__(self, nodes=[]):
  22. assert list(nodes) == list(range(len(nodes)))
  23. super(CircuitModelWrapper, self).__init__(len(nodes))
  24. def act_circuit(self, circuit):
  25. """ Act a sequence of gates """
  26. for node, operation in circuit:
  27. if operation == "cz":
  28. self.act_cz(*node)
  29. else:
  30. u = clifford.unitaries[clifford.by_name[str(operation)]]
  31. self.act_local_rotation(node, u)
  32. def random_pair(n):
  33. """ Helper function to get random pairs"""
  34. return tuple(random.choice(list(range(n)), 2, replace=False))
  35. def random_graph_circuit(n=10, depth=100):
  36. """ A random Graph state. """
  37. return [(i, "hadamard") for i in range(n)] + \
  38. [(random_pair(n), "cz") for i in range(depth)]
  39. def random_stabilizer_circuit(n=10, depth=100):
  40. """ Generate a random stabilizer state, without any VOPs """
  41. return random_graph_circuit(n, depth) + \
  42. [(i, random.choice(list(range(24)))) for i in range(n)]
  43. def bell_pair():
  44. """ Generate a bell pair circuit """
  45. return [(0, "hadamard"), (1, "hadamard"), ((0, 1), "cz")]
  46. def named_node_graph():
  47. """ A graph with named nodes"""
  48. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200), (200, "named")
  49. g = ABPWrapper([0, 1, 2, 3, 100, 200, "named"])
  50. g.act_circuit((i, "hadamard") for i in g.node)
  51. g.act_circuit((edge, "cz") for edge in edges)
  52. return g
  53. def simple_graph():
  54. """ A simple graph to test with"""
  55. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200)
  56. g = ABPWrapper([0, 1, 2, 3, 100, 200])
  57. g.act_circuit((i, "hadamard") for i in g.node)
  58. g.act_circuit((edge, "cz") for edge in edges)
  59. return g
  60. def circuit_to_state(Base, n, circuit):
  61. """ Convert a circuit to a state, given a base class """
  62. g = Base(list(range(n)))
  63. g.act_circuit(circuit)
  64. return g
  65. if __name__ == '__main__':
  66. for i in range(1000):
  67. test_circuit(random_graph_circuit(10), 10)
  68. test_circuit(random_stabilizer_circuit(10), 10)