Anders and Briegel in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

134 行
3.0KB

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