Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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