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.

121 lines
2.8KB

  1. var abj = {};
  2. abj.add_node = function(node, m) {
  3. abj.ngbh[node] = {};
  4. abj.vops[node] = tables.clifford.hadamard;
  5. abj.meta[node] = m ? m : {};
  6. };
  7. abj.add_nodes = function(nodes) {
  8. nodes.forEach(add_node);
  9. };
  10. abj.add_edge = function(a, b) {
  11. abj.ngbh[a][b] = true;
  12. abj.ngbh[b][a] = true;
  13. };
  14. abj.add_edges = function(edges) {
  15. edges.forEach(function(e) {
  16. add_edge(e[0], e[1]);
  17. });
  18. };
  19. abj.del_edge = function(a, b) {
  20. delete abj.ngbh[a][b];
  21. delete abj.ngbh[b][a];
  22. };
  23. abj.has_edge = function(a, b) {
  24. return Object.prototype.hasOwnProperty.call(abj.ngbh[a], b);
  25. };
  26. abj.toggle_edge = function(a, b) {
  27. if (has_edge(a, b)) {
  28. del_edge(a, b);
  29. } else {
  30. add_edge(a, b);
  31. }
  32. };
  33. abj.get_swap = function(node, avoid) {
  34. for (var i in abj.ngbh[node]) {
  35. if (i != avoid) {
  36. return i;
  37. }
  38. }
  39. return avoid;
  40. };
  41. abj.remove_vop = function(node, avoid) {
  42. var swap_qubit = get_swap(node, avoid);
  43. var decomposition = decompositions[abj.vops[node]];
  44. for (var i = decomposition.length - 1; i >= 0; --i) {
  45. var v = decomposition[i];
  46. local_complementation(v == "x" ? a : swap_qubit);
  47. }
  48. };
  49. abj.local_complementation = function(node) {
  50. var keys = Object.keys(abj.ngbh[node]);
  51. for (var i = 0; i < keys.length; ++i) {
  52. for (var j = i + 1; j < keys.length; ++j) {
  53. toggle_edge(keys[i], keys[j]);
  54. }
  55. abj.vops[i] = tables.times_table[abj.vops[keys[i]]][sqz_h];
  56. }
  57. abj.vops[node] = tables.times_table[abj.vops[node]][msqx_h];
  58. };
  59. abj.act_local_rotation = function(node, operation) {
  60. var rotation = tables.clifford[operation];
  61. abj.vops[node] = tables.times_table[rotation][abj.vops[node]];
  62. };
  63. abj.act_hadamard = function(node) {
  64. act_local_rotation(node, 10);
  65. };
  66. abj.is_sole_member = function(node, group) {
  67. return group.length == 1 && group[0] == node;
  68. };
  69. abj.act_cz = function(a, b) {
  70. if (is_sole_member(abj.ngbh[a], b)) {
  71. remove_vop(a, b);
  72. }
  73. if (is_sole_member(abj.ngbh[b], a)) {
  74. remove_vop(b, a);
  75. }
  76. if (is_sole_member(abj.ngbh[a], b)) {
  77. remove_vop(a, b);
  78. }
  79. var edge = has_edge(a, b);
  80. var new_state = tables.cz_table[edge ? 1 : 0][abj.vops[a]][abj.vops[b]];
  81. abj.vops[a] = new_state[1];
  82. abj.vops[b] = new_state[2];
  83. if (new_state[0] != edge) {
  84. toggle_edge(a, b);
  85. }
  86. };
  87. abj.edgelist = function() {
  88. var seen = {};
  89. var output = [];
  90. for (var i in abj.ngbh) {
  91. for (var j in abj.ngbh[i]) {
  92. if (!Object.prototype.hasOwnProperty.call(seen, j)) {
  93. output.push([i, j]);
  94. }
  95. }
  96. seen[i] = true;
  97. }
  98. return output;
  99. };
  100. abj.log_graph_state = function() {
  101. console.log(JSON.stringify(abj.vops));
  102. console.log(JSON.stringify(abj.ngbh));
  103. };