Simulate graph states in the browser
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

anders_briegel.js 1.5KB

pirms 7 gadiem
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. };