Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.8KB

  1. from abp import GraphState, CircuitModel, clifford
  2. from anders_briegel import graphsim
  3. import numpy as np
  4. from numpy import random
  5. from tqdm import tqdm
  6. import itertools as it
  7. import mock
  8. REPEATS = 100
  9. DEPTH = 100
  10. PAULIS = ("px", "py", "pz")
  11. def test_hadamard():
  12. """ Test hadamards """
  13. circuit = [(0, "hadamard")]
  14. mock.test_circuit(circuit, 1)
  15. def test_local_rotations():
  16. """ Test local rotations """
  17. for i in tqdm(range(REPEATS), "Testing local rotations"):
  18. circuit = [(0, random.choice(range(24))) for j in range(DEPTH)]
  19. mock.test_circuit(circuit, 1)
  20. def test_times_table():
  21. """ Test times table """
  22. for i, j in it.product(range(24), range(24)):
  23. circuit = [(0, i), (0, j)]
  24. mock.test_circuit(circuit, 1)
  25. def test_cz_table():
  26. """ Test the CZ table """
  27. for i, j in it.product(range(24), range(24)):
  28. circuit = [(0, i), (1, j), ((0, 1), "cz")]
  29. mock.test_circuit(circuit, 2)
  30. def test_cz_hadamard(n=10):
  31. """ Test CZs and Hadamards at random """
  32. for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"):
  33. circuit = random.choice(["cz", "hadamard"], DEPTH)
  34. circuit = [(mock.random_pair(n), gate) if gate == "cz"
  35. else (random.choice(range(n)), gate)
  36. for gate in circuit]
  37. mock.test_circuit(circuit, n)
  38. def test_all(n=10):
  39. """ Test everything """
  40. for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"):
  41. circuit = random.choice(["cz"] * 10 + range(24), DEPTH)
  42. circuit = [(mock.random_pair(n), gate) if gate == "cz"
  43. else (random.choice(range(n)), gate)
  44. for gate in circuit]
  45. mock.test_circuit(circuit, n)
  46. def test_single_qubit_measurement():
  47. """ Determinstic test of all single-qubit situations """
  48. space = it.product(range(24), PAULIS, (0, 1))
  49. for rotation, measurement, outcome in tqdm(space, "Testing single qubit measurements"):
  50. a = mock.circuit_to_state(mock.ABPWrapper, 1, [(0, rotation)])
  51. b = mock.circuit_to_state(mock.AndersWrapper, 1, [(0, rotation)])
  52. result_a = a.measure(0, measurement, outcome)
  53. result_b = b.measure(0, measurement, outcome)
  54. assert result_a == result_b
  55. assert a == b
  56. def test_two_qubit_measurement():
  57. """ Various two-qubit measurements on a Bell state"""
  58. for measurement, outcome in it.product(PAULIS, (0, 1)):
  59. circuit = mock.bell_pair()
  60. a = mock.circuit_to_state(mock.ABPWrapper, 2, circuit)
  61. b = mock.circuit_to_state(mock.AndersWrapper, 2, circuit)
  62. assert a.measure(0, measurement, outcome) == \
  63. b.measure(0, measurement, outcome)
  64. assert a == b
  65. def test_graph_state_measurement(n = 10):
  66. """ Measuring random graph states """
  67. space = list(it.product(range(REPEATS), PAULIS, (0, 1)))
  68. for i, measurement, outcome in tqdm(space, "Measuring random graph states"):
  69. circuit = mock.random_graph_circuit(n, DEPTH)
  70. a = mock.circuit_to_state(mock.ABPWrapper, n, circuit)
  71. b = mock.circuit_to_state(mock.AndersWrapper, n, circuit)
  72. a.measure(0, measurement, outcome)
  73. b.measure(0, measurement, outcome)
  74. assert a == b
  75. def test_stabilizer_state_measurement(n = 10):
  76. """ Measuring random stabilizer states """
  77. space = list(it.product(range(REPEATS), PAULIS, (0, 1)))
  78. for i, measurement, outcome in tqdm(space, "Measuring random stabilizer states"):
  79. circuit = mock.random_stabilizer_circuit(n, DEPTH)
  80. a = mock.circuit_to_state(mock.ABPWrapper, n, circuit)
  81. b = mock.circuit_to_state(mock.AndersWrapper, n, circuit)
  82. a.measure(0, measurement, outcome)
  83. b.measure(0, measurement, outcome)
  84. assert a == b