Simulate graph states in the browser
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.

74 lignes
1.5KB

  1. var abj = {};
  2. abj.node = {};
  3. abj.adj = {};
  4. abj.add_node = function(node, meta) {
  5. if (meta === undefined){meta = {};}
  6. abj.adj[node] = {};
  7. abj.node[node] = {};
  8. abj.node[node].vop = tables.clifford.hadamard;
  9. Object.assign(abj.node[node], meta);
  10. };
  11. abj.add_nodes = function(nodes) {
  12. nodes.forEach(add_node);
  13. };
  14. abj.del_node = function(node) {
  15. for (var i in abj.adj[node]) {
  16. abj.del_edge(node, i);
  17. }
  18. delete abj.node[node];
  19. };
  20. abj.add_edge = function(a, b) {
  21. abj.adj[a][b] = {};
  22. abj.adj[b][a] = {};
  23. };
  24. abj.add_edges = function(edges) {
  25. edges.forEach(function(e) {
  26. add_edge(e[0], e[1]);
  27. });
  28. };
  29. abj.del_edge = function(a, b) {
  30. delete abj.adj[a][b];
  31. delete abj.adj[b][a];
  32. };
  33. abj.has_edge = function(a, b) {
  34. return Object.prototype.hasOwnProperty.call(abj.adj[a], b);
  35. };
  36. abj.is_sole_member = function(group, node) {
  37. // TODO: this is slow as heck
  38. var keys = Object.keys(group);
  39. return keys.length == 1 && keys[0] == node;
  40. };
  41. abj.update = function(newState) {
  42. abj.node = newState.node;
  43. abj.adj = newState.adj;
  44. };
  45. abj.order = function(){
  46. return Object.keys(abj.node).length;
  47. };
  48. abj.edgelist = function() {
  49. var seen = {};
  50. var output = [];
  51. for (var i in abj.adj) {
  52. for (var j in abj.adj[i]) {
  53. if (!Object.prototype.hasOwnProperty.call(seen, j)) {
  54. output.push([i, j]);
  55. }
  56. }
  57. seen[i] = true;
  58. }
  59. return output;
  60. };