Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

133 lignes
3.1KB

  1. var abj = {};
  2. abj.node = {};
  3. abj.adj = {};
  4. abj.add_node = function(node, meta) {
  5. abj.adj[node] = {};
  6. abj.node[node] = {};
  7. abj.node[node].vop = tables.clifford.hadamard;
  8. Object.assign(abj.node[node], meta);
  9. };
  10. abj.add_nodes = function(nodes) {
  11. nodes.forEach(add_node);
  12. };
  13. abj.add_edge = function(a, b) {
  14. abj.adj[a][b] = {};
  15. abj.adj[b][a] = {};
  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.adj[a][b];
  24. delete abj.adj[b][a];
  25. };
  26. abj.has_edge = function(a, b) {
  27. return Object.prototype.hasOwnProperty.call(abj.adj[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.adj[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.node[node].vop];
  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.adj[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.node[i].vop = tables.times_table[abj.node[keys[i]].vop][sqz_h];
  59. }
  60. abj.node[node].vop = tables.times_table[abj.node[node].vop][msqx_h];
  61. };
  62. abj.act_local_rotation = function(node, operation) {
  63. var rotation = tables.clifford[operation];
  64. abj.node[node].vop = tables.times_table[rotation][abj.node[node].vop];
  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.adj[a], b)) {
  74. remove_vop(a, b);
  75. }
  76. if (is_sole_member(abj.adj[b], a)) {
  77. remove_vop(b, a);
  78. }
  79. if (is_sole_member(abj.adj[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.node[a].vop][abj.node[b].vop];
  84. abj.node[a].vop = new_state[1];
  85. abj.node[b].vop = 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.adj) {
  94. for (var j in abj.adj[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.node));
  105. console.log(JSON.stringify(abj.adj));
  106. };
  107. abj.update = function(newState) {
  108. abj.node = newState.node;
  109. abj.adj = newState.adj;
  110. };
  111. abj.order = function(){
  112. return Object.keys(abj.node).length;
  113. };