Anders and Briegel in Python
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

anders_briegel.js 2.7KB

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