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.

121 lignes
2.7KB

  1. var ngbh = {};
  2. var vops = {};
  3. var meta = {};
  4. var abj = {};
  5. abj.add_node = function(node, m) {
  6. ngbh[node] = {};
  7. vops[node] = clifford.hadamard;
  8. 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. ngbh[a][b] = true;
  15. 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 ngbh[a][b];
  24. delete ngbh[b][a];
  25. };
  26. abj.has_edge = function(a, b) {
  27. return Object.prototype.hasOwnProperty.call(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 ngbh[node]) {
  38. if (i != avoid) {return i;}
  39. }
  40. return avoid;
  41. };
  42. abj.remove_vop = function(node, avoid) {
  43. var swap_qubit = get_swap(node, avoid);
  44. var decomposition = decompositions[vops[node]];
  45. for (var i=decomposition.length-1; i >=0; --i) {
  46. var v = decomposition[i];
  47. local_complementation(v == "x" ? a : swap_qubit);
  48. }
  49. };
  50. abj.local_complementation = function(node) {
  51. var keys = Object.keys(ngbh[node]);
  52. for (var i=0; i < keys.length; ++i) {
  53. for (var j=i+1; j < keys.length; ++j) {
  54. toggle_edge(keys[i], keys[j]);
  55. }
  56. vops[i] = times_table[vops[keys[i]]][sqz_h];
  57. }
  58. vops[node] = times_table[vops[node]][msqx_h];
  59. };
  60. abj.act_local_rotation = function(node, operation) {
  61. var rotation = clifford[operation];
  62. vops[node] = times_table[rotation][vops[node]];
  63. };
  64. abj.act_hadamard = function(node){
  65. act_local_rotation(node, 10);
  66. };
  67. abj.is_sole_member = function(node, group){
  68. return group.length == 1 && group[0] == node;
  69. };
  70. abj.act_cz = function(a, b){
  71. if (is_sole_member(ngbh[a], b)) {
  72. remove_vop(a, b);
  73. }
  74. if (is_sole_member(ngbh[b], a)) {
  75. remove_vop(b, a);
  76. }
  77. if (is_sole_member(ngbh[a], b)) {
  78. remove_vop(a, b);
  79. }
  80. var edge = has_edge(a, b);
  81. var new_state = cz_table[edge ? 1 : 0][vops[a]][vops[b]];
  82. vops[a] = new_state[1];
  83. vops[b] = new_state[2];
  84. if (new_state[0] != edge){
  85. toggle_edge(a, b);
  86. }
  87. };
  88. abj.edgelist = function() {
  89. var seen = {};
  90. var output = [];
  91. for (var i in ngbh) {
  92. for (var j in ngbh[i]) {
  93. if (!Object.prototype.hasOwnProperty.call(seen, j)){
  94. output.push([i, j]);
  95. }
  96. }
  97. seen[i] = true;
  98. }
  99. return output;
  100. };
  101. abj.log_graph_state = function() {
  102. console.log(JSON.stringify(vops));
  103. console.log(JSON.stringify(ngbh));
  104. };