Anders and Briegel in Python
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

127 satır
2.8KB

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