Anders and Briegel in Python
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

anders_briegel.js 3.4KB

8 年前
8 年前
8 年前
8 年前
8 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. var keys = Object.keys(group);
  83. return keys.length == 1 && keys[0] == node;
  84. };
  85. abj.act_cz = function(a, b) {
  86. if (abj.is_sole_member(abj.adj[a], b)) {
  87. abj.remove_vop(a, b);
  88. }
  89. if (abj.is_sole_member(abj.adj[b], a)) {
  90. abj.remove_vop(b, a);
  91. }
  92. if (abj.is_sole_member(abj.adj[a], b)) {
  93. abj.remove_vop(a, b);
  94. }
  95. var edge = abj.has_edge(a, b);
  96. var new_state = tables.cz_table[edge ? 1 : 0][abj.node[a].vop][abj.node[b].vop];
  97. abj.node[a].vop = new_state[1];
  98. abj.node[b].vop = new_state[2];
  99. if (new_state[0] != edge) {
  100. abj.toggle_edge(a, b);
  101. }
  102. };
  103. abj.edgelist = function() {
  104. var seen = {};
  105. var output = [];
  106. for (var i in abj.adj) {
  107. for (var j in abj.adj[i]) {
  108. if (!Object.prototype.hasOwnProperty.call(seen, j)) {
  109. output.push([i, j]);
  110. }
  111. }
  112. seen[i] = true;
  113. }
  114. return output;
  115. };
  116. abj.log_graph_state = function() {
  117. console.log(JSON.stringify(abj.node));
  118. console.log(JSON.stringify(abj.adj));
  119. };
  120. abj.update = function(newState) {
  121. abj.node = newState.node;
  122. abj.adj = newState.adj;
  123. };
  124. abj.order = function(){
  125. return Object.keys(abj.node).length;
  126. };