Anders and Briegel in Python
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

anders_briegel.js 3.4KB

8 년 전
8 년 전
8 년 전
8 년 전
8 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var abj = {};
  2. abj.node = {};
  3. abj.adj = {};
  4. abj.add_node = function(node, meta) {
  5. if (meta === undefined){meta = {};}
  6. abj.adj[node] = {};
  7. abj.node[node] = {};
  8. abj.node[node].vop = tables.clifford.hadamard;
  9. Object.assign(abj.node[node], meta);
  10. };
  11. abj.add_nodes = function(nodes) {
  12. nodes.forEach(add_node);
  13. };
  14. abj.del_node = function(node) {
  15. for (var i in abj.adj[node]) {
  16. abj.del_edge(node, i);
  17. }
  18. delete abj.node[node];
  19. };
  20. abj.add_edge = function(a, b) {
  21. abj.adj[a][b] = {};
  22. abj.adj[b][a] = {};
  23. };
  24. abj.add_edges = function(edges) {
  25. edges.forEach(function(e) {
  26. add_edge(e[0], e[1]);
  27. });
  28. };
  29. abj.del_edge = function(a, b) {
  30. delete abj.adj[a][b];
  31. delete abj.adj[b][a];
  32. };
  33. abj.has_edge = function(a, b) {
  34. return Object.prototype.hasOwnProperty.call(abj.adj[a], b);
  35. };
  36. abj.toggle_edge = function(a, b) {
  37. if (abj.has_edge(a, b)) {
  38. abj.del_edge(a, b);
  39. } else {
  40. abj.add_edge(a, b);
  41. }
  42. };
  43. abj.get_swap = function(node, avoid) {
  44. for (var i in abj.adj[node]) {
  45. if (i != avoid) {
  46. return i;
  47. }
  48. }
  49. return avoid;
  50. };
  51. abj.remove_vop = function(node, avoid) {
  52. var swap_qubit = abj.get_swap(node, avoid);
  53. var decomposition = tables.decompositions[abj.node[node].vop];
  54. for (var i = decomposition.length - 1; i >= 0; --i) {
  55. var v = decomposition[i];
  56. abj.local_complementation(v == "x" ? node : swap_qubit);
  57. }
  58. };
  59. abj.local_complementation = function(node) {
  60. // TODO: inefficient
  61. var done = {};
  62. for (var i in abj.adj) {
  63. for (var j in abj.adj[i]) {
  64. var name = i>j ? [i,j] : [j,i];
  65. if (done[name]===false){
  66. abj.toggle_edge(i, j);
  67. done[name] = true;
  68. }
  69. }
  70. abj.node[i].vop = tables.times_table[abj.node[i].vop][6];
  71. }
  72. abj.node[node].vop = tables.times_table[abj.node[node].vop][14];
  73. };
  74. abj.act_local_rotation = function(node, operation) {
  75. var rotation = tables.clifford[operation];
  76. abj.node[node].vop = tables.times_table[rotation][abj.node[node].vop];
  77. };
  78. abj.act_hadamard = function(node) {
  79. abj.act_local_rotation(node, 10);
  80. };
  81. abj.is_sole_member = function(group, node) {
  82. // TODO: this is slow as heck
  83. var keys = Object.keys(group);
  84. return keys.length == 1 && keys[0] == node;
  85. };
  86. abj.act_cz = function(a, b) {
  87. if (abj.is_sole_member(abj.adj[a], b)) {
  88. abj.remove_vop(a, b);
  89. }
  90. if (abj.is_sole_member(abj.adj[b], a)) {
  91. abj.remove_vop(b, a);
  92. }
  93. if (abj.is_sole_member(abj.adj[a], b)) {
  94. abj.remove_vop(a, b);
  95. }
  96. var edge = abj.has_edge(a, b);
  97. var new_state = tables.cz_table[edge ? 1 : 0][abj.node[a].vop][abj.node[b].vop];
  98. abj.node[a].vop = new_state[1];
  99. abj.node[b].vop = new_state[2];
  100. if (new_state[0] != edge) {
  101. abj.toggle_edge(a, b);
  102. }
  103. };
  104. abj.edgelist = function() {
  105. var seen = {};
  106. var output = [];
  107. for (var i in abj.adj) {
  108. for (var j in abj.adj[i]) {
  109. if (!Object.prototype.hasOwnProperty.call(seen, j)) {
  110. output.push([i, j]);
  111. }
  112. }
  113. seen[i] = true;
  114. }
  115. return output;
  116. };
  117. abj.log_graph_state = function() {
  118. console.log(JSON.stringify(abj.node));
  119. console.log(JSON.stringify(abj.adj));
  120. };
  121. abj.update = function(newState) {
  122. abj.node = newState.node;
  123. abj.adj = newState.adj;
  124. };
  125. abj.order = function(){
  126. return Object.keys(abj.node).length;
  127. };