{"action":"created","comment":{"url":"https://api.github.com/repos/dwavesystems/dimod/pulls/comments/208346108","pull_request_review_id":144136557,"id":208346108,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwODM0NjEwOA==","diff_hunk":"@@ -1606,6 +1606,88 @@ def from_json(cls, obj):\n \n         return json.load(obj, object_hook=lambda d: bqm_decode_hook(d, cls=cls))\n \n+    def to_bson(self):\n+        \"\"\"Convert a binary quadratic model to an efficient BSON format.\n+\n+        Returns:\n+            bytes: BSON formatted bytes representing the original BQM.\n+        \"\"\"\n+                \n+\n+        import bson\n+\n+        num_variables = len(self)\n+        if num_variables > 2**16:\n+            raise ValueError\n+\n+        variable_order = sorted(self.linear)\n+        num_possible_edges = num_variables*(num_variables - 1) // 2\n+        density = len(self.quadratic) / num_possible_edges\n+        as_complete = density >= 0.5\n+\n+        lin, (i, j, _vals), off = self.to_numpy_vectors(\n+            dtype=np.float32,\n+            index_dtype=np.uint16,\n+            sort_indices=as_complete,\n+            variable_order=variable_order)\n+\n+        if as_complete:\n+            vals = np.zeros(num_possible_edges, dtype=np.float32)\n+            edge_idxs = i*(num_variables - 1) - i*(i+1)//2 + j - 1\n+            vals[edge_idxs] = _vals\n+\n+        else:\n+            vals = _vals\n+\n+        doc = {\n+            \"as_complete\": as_complete,\n+            \"linear\": bson.binary.Binary(lin.tobytes()),\n+            \"quadratic_vals\": bson.binary.Binary(vals.tobytes()),\n+            \"vartype\": \"SPIN\" if self.vartype == self.SPIN else \"BINARY\",\n+            \"offset\": off,\n+            \"variable_order\": variable_order,\n+        }\n+\n+        if not as_complete:\n+            doc[\"quadratic_head\"] = bson.binary.Binary(i.tobytes())\n+            doc[\"quadratic_tail\"] = bson.binary.Binary(j.tobytes())\n+\n+        return bson.BSON.encode(doc)\n+\n+    @classmethod\n+    def from_bson(cls, obj):\n+        \"\"\"Deserialize a binary quadratic model from a BSON encoding.\n+\n+        Args:\n+            obj: bytes:\n+                Byte string that represents linear and quadratic biases using \n+                the encoding used in `.to_bson`.","path":"dimod/binary_quadratic_model.py","position":59,"original_position":59,"commit_id":"e26d3070f2512bb87c2d35e4de47a4c7756ebc90","original_commit_id":"e26d3070f2512bb87c2d35e4de47a4c7756ebc90","user":{"login":"arcondello","id":8395238,"node_id":"MDQ6VXNlcjgzOTUyMzg=","avatar_url":"https://avatars0.githubusercontent.com/u/8395238?v=4","gravatar_id":"","url":"https://api.github.com/users/arcondello","html_url":"https://github.com/arcondello","followers_url":"https://api.github.com/users/arcondello/followers","following_url":"https://api.github.com/users/arcondello/following{/other_user}","gists_url":"https://api.github.com/users/arcondello/gists{/gist_id}","starred_url":"https://api.github.com/users/arcondello/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arcondello/subscriptions","organizations_url":"https://api.github.com/users/arcondello/orgs","repos_url":"https://api.github.com/users/arcondello/repos","events_url":"https://api.github.com/users/arcondello/events{/privacy}","received_events_url":"https://api.github.com/users/arcondello/received_events","type":"User","site_admin":false},"body":"```\r\n:meth:`to_bson`\r\n```","created_at":"2018-08-07T18:52:28Z","updated_at":"2018-08-07T18:52:28Z","html_url":"https://github.com/dwavesystems/dimod/pull/235#discussion_r208346108","pull_request_url":"https://api.github.com/repos/dwavesystems/dimod/pulls/235","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/dwavesystems/dimod/pulls/comments/208346108"},"html":{"href":"https://github.com/dwavesystems/dimod/pull/235#discussion_r208346108"},"pull_request":{"href":"https://api.github.com/repos/dwavesystems/dimod/pulls/235"}}},"pull_request":{"url":"https://api.github.com/repos/dwavesystems/dimod/pulls/235","id":204292309,"node_id":"MDExOlB1bGxSZXF1ZXN0MjA0MjkyMzA5","html_url":"https://github.com/dwavesystems/dimod/pull/235","diff_url":"https://github.com/dwavesystems/dimod/pull/235.diff","patch_url":"https://github.com/dwavesystems/dimod/pull/235.patch","issue_url":"https://api.github.com/repos/dwavesystems/dimod/issues/235","number":235,"state":"open","locked":false,"title":"BSON encoding for BinaryQuadraticModel","user":{"login":"wbernoudy","id":11012124,"node_id":"MDQ6VXNlcjExMDEyMTI0","avatar_url":"https://avatars2.githubusercontent.com/u/11012124?v=4","gravatar_id":"","url":"https://api.github.com/users/wbernoudy","html_url":"https://github.com/wbernoudy","followers_url":"https://api.github.com/users/wbernoudy/followers","following_url":"https://api.github.com/users/wbernoudy/following{/other_user}","gists_url":"https://api.github.com/users/wbernoudy/gists{/gist_id}","starred_url":"https://api.github.com/users/wbernoudy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wbernoudy/subscriptions","organizations_url":"https://api.github.com/users/wbernoudy/orgs","repos_url":"https://api.github.com/users/wbernoudy/repos","events_url":"https://api.github.com/users/wbernoudy/events{/privacy}","received_events_url":"https://api.github.com/users/wbernoudy/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-07-26T23:20:43Z","updated_at":"2018-08-07T18:52:28Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9a14cfd14b94f01a1f4592a53a62e95b1d0482be","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/dwavesystems/dimod/pulls/235/commits","review_comments_url":"https://api.github.com/repos/dwavesystems/dimod/pulls/235/comments","review_comment_url":"https://api.github.com/repos/dwavesystems/dimod/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dwavesystems/dimod/issues/235/comments","statuses_url":"https://api.github.com/repos/dwavesystems/dimod/statuses/e26d3070f2512bb87c2d35e4de47a4c7756ebc90","head":{"label":"wbernoudy:bqm-bson","ref":"bqm-bson","sha":"e26d3070f2512bb87c2d35e4de47a4c7756ebc90","user":{"login":"wbernoudy","id":11012124,"node_id":"MDQ6VXNlcjExMDEyMTI0","avatar_url":"https://avatars2.githubusercontent.com/u/11012124?v=4","gravatar_id":"","url":"https://api.github.com/users/wbernoudy","html_url":"https://github.com/wbernoudy","followers_url":"https://api.github.com/users/wbernoudy/followers","following_url":"https://api.github.com/users/wbernoudy/following{/other_user}","gists_url":"https://api.github.com/users/wbernoudy/gists{/gist_id}","starred_url":"https://api.github.com/users/wbernoudy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wbernoudy/subscriptions","organizations_url":"https://api.github.com/users/wbernoudy/orgs","repos_url":"https://api.github.com/users/wbernoudy/repos","events_url":"https://api.github.com/users/wbernoudy/events{/privacy}","received_events_url":"https://api.github.com/users/wbernoudy/received_events","type":"User","site_admin":false},"repo":{"id":140892950,"node_id":"MDEwOlJlcG9zaXRvcnkxNDA4OTI5NTA=","name":"dimod","full_name":"wbernoudy/dimod","owner":{"login":"wbernoudy","id":11012124,"node_id":"MDQ6VXNlcjExMDEyMTI0","avatar_url":"https://avatars2.githubusercontent.com/u/11012124?v=4","gravatar_id":"","url":"https://api.github.com/users/wbernoudy","html_url":"https://github.com/wbernoudy","followers_url":"https://api.github.com/users/wbernoudy/followers","following_url":"https://api.github.com/users/wbernoudy/following{/other_user}","gists_url":"https://api.github.com/users/wbernoudy/gists{/gist_id}","starred_url":"https://api.github.com/users/wbernoudy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wbernoudy/subscriptions","organizations_url":"https://api.github.com/users/wbernoudy/orgs","repos_url":"https://api.github.com/users/wbernoudy/repos","events_url":"https://api.github.com/users/wbernoudy/events{/privacy}","received_events_url":"https://api.github.com/users/wbernoudy/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/wbernoudy/dimod","description":"A shared API for QUBO/Ising samplers.","fork":true,"url":"https://api.github.com/repos/wbernoudy/dimod","forks_url":"https://api.github.com/repos/wbernoudy/dimod/forks","keys_url":"https://api.github.com/repos/wbernoudy/dimod/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wbernoudy/dimod/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wbernoudy/dimod/teams","hooks_url":"https://api.github.com/repos/wbernoudy/dimod/hooks","issue_events_url":"https://api.github.com/repos/wbernoudy/dimod/issues/events{/number}","events_url":"https://api.github.com/repos/wbernoudy/dimod/events","assignees_url":"https://api.github.com/repos/wbernoudy/dimod/assignees{/user}","branches_url":"https://api.github.com/repos/wbernoudy/dimod/branches{/branch}","tags_url":"https://api.github.com/repos/wbernoudy/dimod/tags","blobs_url":"https://api.github.com/repos/wbernoudy/dimod/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wbernoudy/dimod/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wbernoudy/dimod/git/refs{/sha}","trees_url":"https://api.github.com/repos/wbernoudy/dimod/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wbernoudy/dimod/statuses/{sha}","languages_url":"https://api.github.com/repos/wbernoudy/dimod/languages","stargazers_url":"https://api.github.com/repos/wbernoudy/dimod/stargazers","contributors_url":"https://api.github.com/repos/wbernoudy/dimod/contributors","subscribers_url":"https://api.github.com/repos/wbernoudy/dimod/subscribers","subscription_url":"https://api.github.com/repos/wbernoudy/dimod/subscription","commits_url":"https://api.github.com/repos/wbernoudy/dimod/commits{/sha}","git_commits_url":"https://api.github.com/repos/wbernoudy/dimod/git/commits{/sha}","comments_url":"https://api.github.com/repos/wbernoudy/dimod/comments{/number}","issue_comment_url":"https://api.github.com/repos/wbernoudy/dimod/issues/comments{/number}","contents_url":"https://api.github.com/repos/wbernoudy/dimod/contents/{+path}","compare_url":"https://api.github.com/repos/wbernoudy/dimod/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wbernoudy/dimod/merges","archive_url":"https://api.github.com/repos/wbernoudy/dimod/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wbernoudy/dimod/downloads","issues_url":"https://api.github.com/repos/wbernoudy/dimod/issues{/number}","pulls_url":"https://api.github.com/repos/wbernoudy/dimod/pulls{/number}","milestones_url":"https://api.github.com/repos/wbernoudy/dimod/milestones{/number}","notifications_url":"https://api.github.com/repos/wbernoudy/dimod/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wbernoudy/dimod/labels{/name}","releases_url":"https://api.github.com/repos/wbernoudy/dimod/releases{/id}","deployments_url":"https://api.github.com/repos/wbernoudy/dimod/deployments","created_at":"2018-07-13T21:06:07Z","updated_at":"2018-07-25T17:48:52Z","pushed_at":"2018-08-07T18:43:37Z","git_url":"git://github.com/wbernoudy/dimod.git","ssh_url":"git@github.com:wbernoudy/dimod.git","clone_url":"https://github.com/wbernoudy/dimod.git","svn_url":"https://github.com/wbernoudy/dimod","homepage":"http://dimod.readthedocs.io/en/latest/","size":1008,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dwavesystems:master","ref":"master","sha":"b1f8efa20a00a68711a226590adbbe8e4b1a889d","user":{"login":"dwavesystems","id":5449030,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU0NDkwMzA=","avatar_url":"https://avatars2.githubusercontent.com/u/5449030?v=4","gravatar_id":"","url":"https://api.github.com/users/dwavesystems","html_url":"https://github.com/dwavesystems","followers_url":"https://api.github.com/users/dwavesystems/followers","following_url":"https://api.github.com/users/dwavesystems/following{/other_user}","gists_url":"https://api.github.com/users/dwavesystems/gists{/gist_id}","starred_url":"https://api.github.com/users/dwavesystems/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dwavesystems/subscriptions","organizations_url":"https://api.github.com/users/dwavesystems/orgs","repos_url":"https://api.github.com/users/dwavesystems/repos","events_url":"https://api.github.com/users/dwavesystems/events{/privacy}","received_events_url":"https://api.github.com/users/dwavesystems/received_events","type":"Organization","site_admin":false},"repo":{"id":100658303,"node_id":"MDEwOlJlcG9zaXRvcnkxMDA2NTgzMDM=","name":"dimod","full_name":"dwavesystems/dimod","owner":{"login":"dwavesystems","id":5449030,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU0NDkwMzA=","avatar_url":"https://avatars2.githubusercontent.com/u/5449030?v=4","gravatar_id":"","url":"https://api.github.com/users/dwavesystems","html_url":"https://github.com/dwavesystems","followers_url":"https://api.github.com/users/dwavesystems/followers","following_url":"https://api.github.com/users/dwavesystems/following{/other_user}","gists_url":"https://api.github.com/users/dwavesystems/gists{/gist_id}","starred_url":"https://api.github.com/users/dwavesystems/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dwavesystems/subscriptions","organizations_url":"https://api.github.com/users/dwavesystems/orgs","repos_url":"https://api.github.com/users/dwavesystems/repos","events_url":"https://api.github.com/users/dwavesystems/events{/privacy}","received_events_url":"https://api.github.com/users/dwavesystems/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dwavesystems/dimod","description":"A shared API for QUBO/Ising samplers.","fork":false,"url":"https://api.github.com/repos/dwavesystems/dimod","forks_url":"https://api.github.com/repos/dwavesystems/dimod/forks","keys_url":"https://api.github.com/repos/dwavesystems/dimod/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dwavesystems/dimod/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dwavesystems/dimod/teams","hooks_url":"https://api.github.com/repos/dwavesystems/dimod/hooks","issue_events_url":"https://api.github.com/repos/dwavesystems/dimod/issues/events{/number}","events_url":"https://api.github.com/repos/dwavesystems/dimod/events","assignees_url":"https://api.github.com/repos/dwavesystems/dimod/assignees{/user}","branches_url":"https://api.github.com/repos/dwavesystems/dimod/branches{/branch}","tags_url":"https://api.github.com/repos/dwavesystems/dimod/tags","blobs_url":"https://api.github.com/repos/dwavesystems/dimod/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dwavesystems/dimod/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dwavesystems/dimod/git/refs{/sha}","trees_url":"https://api.github.com/repos/dwavesystems/dimod/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dwavesystems/dimod/statuses/{sha}","languages_url":"https://api.github.com/repos/dwavesystems/dimod/languages","stargazers_url":"https://api.github.com/repos/dwavesystems/dimod/stargazers","contributors_url":"https://api.github.com/repos/dwavesystems/dimod/contributors","subscribers_url":"https://api.github.com/repos/dwavesystems/dimod/subscribers","subscription_url":"https://api.github.com/repos/dwavesystems/dimod/subscription","commits_url":"https://api.github.com/repos/dwavesystems/dimod/commits{/sha}","git_commits_url":"https://api.github.com/repos/dwavesystems/dimod/git/commits{/sha}","comments_url":"https://api.github.com/repos/dwavesystems/dimod/comments{/number}","issue_comment_url":"https://api.github.com/repos/dwavesystems/dimod/issues/comments{/number}","contents_url":"https://api.github.com/repos/dwavesystems/dimod/contents/{+path}","compare_url":"https://api.github.com/repos/dwavesystems/dimod/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dwavesystems/dimod/merges","archive_url":"https://api.github.com/repos/dwavesystems/dimod/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dwavesystems/dimod/downloads","issues_url":"https://api.github.com/repos/dwavesystems/dimod/issues{/number}","pulls_url":"https://api.github.com/repos/dwavesystems/dimod/pulls{/number}","milestones_url":"https://api.github.com/repos/dwavesystems/dimod/milestones{/number}","notifications_url":"https://api.github.com/repos/dwavesystems/dimod/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dwavesystems/dimod/labels{/name}","releases_url":"https://api.github.com/repos/dwavesystems/dimod/releases{/id}","deployments_url":"https://api.github.com/repos/dwavesystems/dimod/deployments","created_at":"2017-08-18T01:02:17Z","updated_at":"2018-08-07T18:17:30Z","pushed_at":"2018-08-07T18:43:39Z","git_url":"git://github.com/dwavesystems/dimod.git","ssh_url":"git@github.com:dwavesystems/dimod.git","clone_url":"https://github.com/dwavesystems/dimod.git","svn_url":"https://github.com/dwavesystems/dimod","homepage":"http://dimod.readthedocs.io/en/latest/","size":1071,"stargazers_count":16,"watchers_count":16,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":17,"mirror_url":null,"archived":false,"open_issues_count":20,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":17,"open_issues":20,"watchers":16,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dwavesystems/dimod/pulls/235"},"html":{"href":"https://github.com/dwavesystems/dimod/pull/235"},"issue":{"href":"https://api.github.com/repos/dwavesystems/dimod/issues/235"},"comments":{"href":"https://api.github.com/repos/dwavesystems/dimod/issues/235/comments"},"review_comments":{"href":"https://api.github.com/repos/dwavesystems/dimod/pulls/235/comments"},"review_comment":{"href":"https://api.github.com/repos/dwavesystems/dimod/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dwavesystems/dimod/pulls/235/commits"},"statuses":{"href":"https://api.github.com/repos/dwavesystems/dimod/statuses/e26d3070f2512bb87c2d35e4de47a4c7756ebc90"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"arcondello"}}