{"action":"created","comment":{"url":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/comments/198334129","pull_request_review_id":132247836,"id":198334129,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDE5ODMzNDEyOQ==","diff_hunk":"@@ -703,58 +703,69 @@ def sparse_eigenspectrum(sparse_operator):\n     return numpy.sort(eigenspectrum)\n \n \n-def expectation(sparse_operator, state):\n-    \"\"\"Compute expectation value of operator with a state.\n+def expectation(operator, state):\n+    \"\"\"Compute the expectation value of an operator with a state.\n \n     Args:\n-        state: scipy.sparse.csc vector representing a pure state,\n-            ndarray vector representing a pure state,\n-            or, a scipy.sparse.csc matrix representing a density matrix.\n+        operator(scipy.sparse.spmatrix or scipy.sparse.linalg.LinearOperator):\n+            The operator whose expectation value is desired.\n+        state(numpy.ndarray or scipy.sparse.spmatrix): A numpy array\n+            representing a pure state or a sparse matrix representing a density\n+            matrix. If `operator` is a LinearOperator, then this must be a\n+            numpy array.\n \n     Returns:\n-        A real float giving expectation value.\n+        A complex number giving the expectation value.\n \n     Raises:\n         ValueError: Input state has invalid format.\n     \"\"\"\n-    # Handle density matrix.\n-    if state.shape == sparse_operator.shape:\n-        product = state * sparse_operator\n+\n+    if isinstance(state, scipy.sparse.spmatrix):\n+        # Handle density matrix.\n+        if isinstance(operator, scipy.sparse.linalg.LinearOperator):\n+            raise ValueError('Taking the expectation of a LinearOperator with '\n+                             'a density matrix is not supported.')\n+        product = state * operator\n         expectation = numpy.sum(product.diagonal())\n \n-    elif (state.shape == (sparse_operator.shape[0], 1) or\n-          state.shape == (sparse_operator.shape[0], )):\n+    elif isinstance(state, numpy.ndarray):\n         # Handle state vector.\n-        if scipy.sparse.issparse(state):\n-            expectation = (state.getH() * sparse_operator * state)\n+        if len(state.shape) == 1:\n+            # Row vector\n+            expectation = numpy.dot(numpy.conjugate(state), operator * state)\n         else:\n-            expectation = numpy.dot(numpy.conj(state.T),\n-                                    sparse_operator.dot(state))\n-        if expectation.shape != ():\n-            expectation = expectation[0, 0]\n+            # Column vector\n+            expectation = numpy.dot(numpy.conjugate(state.T),","path":"src/openfermion/utils/_sparse_tools.py","position":54,"original_position":54,"commit_id":"d44e98fb9440baaf32bd18bef305b5561bca3b2c","original_commit_id":"d44e98fb9440baaf32bd18bef305b5561bca3b2c","user":{"login":"jarrodmcc","id":7718204,"node_id":"MDQ6VXNlcjc3MTgyMDQ=","avatar_url":"https://avatars2.githubusercontent.com/u/7718204?v=4","gravatar_id":"","url":"https://api.github.com/users/jarrodmcc","html_url":"https://github.com/jarrodmcc","followers_url":"https://api.github.com/users/jarrodmcc/followers","following_url":"https://api.github.com/users/jarrodmcc/following{/other_user}","gists_url":"https://api.github.com/users/jarrodmcc/gists{/gist_id}","starred_url":"https://api.github.com/users/jarrodmcc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jarrodmcc/subscriptions","organizations_url":"https://api.github.com/users/jarrodmcc/orgs","repos_url":"https://api.github.com/users/jarrodmcc/repos","events_url":"https://api.github.com/users/jarrodmcc/events{/privacy}","received_events_url":"https://api.github.com/users/jarrodmcc/received_events","type":"User","site_admin":false},"body":"Okay i think this looks good now.  As a final note if you wanted to include it before merging, you could probably do both with the transpose (and both would be correct), then you could extract [0,0] in one case and not the other.  But it's fine either way.","created_at":"2018-06-27T00:15:13Z","updated_at":"2018-06-27T00:15:16Z","html_url":"https://github.com/quantumlib/OpenFermion/pull/377#discussion_r198334129","pull_request_url":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/377","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/comments/198334129"},"html":{"href":"https://github.com/quantumlib/OpenFermion/pull/377#discussion_r198334129"},"pull_request":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/377"}}},"pull_request":{"url":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/377","id":197524501,"node_id":"MDExOlB1bGxSZXF1ZXN0MTk3NTI0NTAx","html_url":"https://github.com/quantumlib/OpenFermion/pull/377","diff_url":"https://github.com/quantumlib/OpenFermion/pull/377.diff","patch_url":"https://github.com/quantumlib/OpenFermion/pull/377.patch","issue_url":"https://api.github.com/repos/quantumlib/OpenFermion/issues/377","number":377,"state":"open","locked":false,"title":"Allow taking expectation of LinearOperator and switch HF state to numpy array","user":{"login":"kevinsung","id":4296166,"node_id":"MDQ6VXNlcjQyOTYxNjY=","avatar_url":"https://avatars1.githubusercontent.com/u/4296166?v=4","gravatar_id":"","url":"https://api.github.com/users/kevinsung","html_url":"https://github.com/kevinsung","followers_url":"https://api.github.com/users/kevinsung/followers","following_url":"https://api.github.com/users/kevinsung/following{/other_user}","gists_url":"https://api.github.com/users/kevinsung/gists{/gist_id}","starred_url":"https://api.github.com/users/kevinsung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kevinsung/subscriptions","organizations_url":"https://api.github.com/users/kevinsung/orgs","repos_url":"https://api.github.com/users/kevinsung/repos","events_url":"https://api.github.com/users/kevinsung/events{/privacy}","received_events_url":"https://api.github.com/users/kevinsung/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-06-26T19:17:11Z","updated_at":"2018-06-27T00:15:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b68c4b299008f636eb78890964ea0b617fb54cf3","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":923815919,"node_id":"MDU6TGFiZWw5MjM4MTU5MTk=","url":"https://api.github.com/repos/quantumlib/OpenFermion/labels/cla:%20yes","name":"cla: yes","color":"0e8a16","default":false}],"milestone":null,"commits_url":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/377/commits","review_comments_url":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/377/comments","review_comment_url":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/comments{/number}","comments_url":"https://api.github.com/repos/quantumlib/OpenFermion/issues/377/comments","statuses_url":"https://api.github.com/repos/quantumlib/OpenFermion/statuses/d44e98fb9440baaf32bd18bef305b5561bca3b2c","head":{"label":"kevinsung:expectation_linear_op","ref":"expectation_linear_op","sha":"d44e98fb9440baaf32bd18bef305b5561bca3b2c","user":{"login":"kevinsung","id":4296166,"node_id":"MDQ6VXNlcjQyOTYxNjY=","avatar_url":"https://avatars1.githubusercontent.com/u/4296166?v=4","gravatar_id":"","url":"https://api.github.com/users/kevinsung","html_url":"https://github.com/kevinsung","followers_url":"https://api.github.com/users/kevinsung/followers","following_url":"https://api.github.com/users/kevinsung/following{/other_user}","gists_url":"https://api.github.com/users/kevinsung/gists{/gist_id}","starred_url":"https://api.github.com/users/kevinsung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kevinsung/subscriptions","organizations_url":"https://api.github.com/users/kevinsung/orgs","repos_url":"https://api.github.com/users/kevinsung/repos","events_url":"https://api.github.com/users/kevinsung/events{/privacy}","received_events_url":"https://api.github.com/users/kevinsung/received_events","type":"User","site_admin":false},"repo":{"id":114693363,"node_id":"MDEwOlJlcG9zaXRvcnkxMTQ2OTMzNjM=","name":"OpenFermion","full_name":"kevinsung/OpenFermion","owner":{"login":"kevinsung","id":4296166,"node_id":"MDQ6VXNlcjQyOTYxNjY=","avatar_url":"https://avatars1.githubusercontent.com/u/4296166?v=4","gravatar_id":"","url":"https://api.github.com/users/kevinsung","html_url":"https://github.com/kevinsung","followers_url":"https://api.github.com/users/kevinsung/followers","following_url":"https://api.github.com/users/kevinsung/following{/other_user}","gists_url":"https://api.github.com/users/kevinsung/gists{/gist_id}","starred_url":"https://api.github.com/users/kevinsung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kevinsung/subscriptions","organizations_url":"https://api.github.com/users/kevinsung/orgs","repos_url":"https://api.github.com/users/kevinsung/repos","events_url":"https://api.github.com/users/kevinsung/events{/privacy}","received_events_url":"https://api.github.com/users/kevinsung/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/kevinsung/OpenFermion","description":"OpenFermion: The Electronic Structure Package For Quantum Computers.","fork":true,"url":"https://api.github.com/repos/kevinsung/OpenFermion","forks_url":"https://api.github.com/repos/kevinsung/OpenFermion/forks","keys_url":"https://api.github.com/repos/kevinsung/OpenFermion/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kevinsung/OpenFermion/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kevinsung/OpenFermion/teams","hooks_url":"https://api.github.com/repos/kevinsung/OpenFermion/hooks","issue_events_url":"https://api.github.com/repos/kevinsung/OpenFermion/issues/events{/number}","events_url":"https://api.github.com/repos/kevinsung/OpenFermion/events","assignees_url":"https://api.github.com/repos/kevinsung/OpenFermion/assignees{/user}","branches_url":"https://api.github.com/repos/kevinsung/OpenFermion/branches{/branch}","tags_url":"https://api.github.com/repos/kevinsung/OpenFermion/tags","blobs_url":"https://api.github.com/repos/kevinsung/OpenFermion/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kevinsung/OpenFermion/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kevinsung/OpenFermion/git/refs{/sha}","trees_url":"https://api.github.com/repos/kevinsung/OpenFermion/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kevinsung/OpenFermion/statuses/{sha}","languages_url":"https://api.github.com/repos/kevinsung/OpenFermion/languages","stargazers_url":"https://api.github.com/repos/kevinsung/OpenFermion/stargazers","contributors_url":"https://api.github.com/repos/kevinsung/OpenFermion/contributors","subscribers_url":"https://api.github.com/repos/kevinsung/OpenFermion/subscribers","subscription_url":"https://api.github.com/repos/kevinsung/OpenFermion/subscription","commits_url":"https://api.github.com/repos/kevinsung/OpenFermion/commits{/sha}","git_commits_url":"https://api.github.com/repos/kevinsung/OpenFermion/git/commits{/sha}","comments_url":"https://api.github.com/repos/kevinsung/OpenFermion/comments{/number}","issue_comment_url":"https://api.github.com/repos/kevinsung/OpenFermion/issues/comments{/number}","contents_url":"https://api.github.com/repos/kevinsung/OpenFermion/contents/{+path}","compare_url":"https://api.github.com/repos/kevinsung/OpenFermion/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kevinsung/OpenFermion/merges","archive_url":"https://api.github.com/repos/kevinsung/OpenFermion/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kevinsung/OpenFermion/downloads","issues_url":"https://api.github.com/repos/kevinsung/OpenFermion/issues{/number}","pulls_url":"https://api.github.com/repos/kevinsung/OpenFermion/pulls{/number}","milestones_url":"https://api.github.com/repos/kevinsung/OpenFermion/milestones{/number}","notifications_url":"https://api.github.com/repos/kevinsung/OpenFermion/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kevinsung/OpenFermion/labels{/name}","releases_url":"https://api.github.com/repos/kevinsung/OpenFermion/releases{/id}","deployments_url":"https://api.github.com/repos/kevinsung/OpenFermion/deployments","created_at":"2017-12-18T22:19:46Z","updated_at":"2018-06-12T19:20:18Z","pushed_at":"2018-06-26T21:56:49Z","git_url":"git://github.com/kevinsung/OpenFermion.git","ssh_url":"git@github.com:kevinsung/OpenFermion.git","clone_url":"https://github.com/kevinsung/OpenFermion.git","svn_url":"https://github.com/kevinsung/OpenFermion","homepage":"","size":3550,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"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":"quantumlib:master","ref":"master","sha":"ce22181890055f14991e5fbce46a589525eba3d6","user":{"login":"quantumlib","id":31279789,"node_id":"MDEyOk9yZ2FuaXphdGlvbjMxMjc5Nzg5","avatar_url":"https://avatars1.githubusercontent.com/u/31279789?v=4","gravatar_id":"","url":"https://api.github.com/users/quantumlib","html_url":"https://github.com/quantumlib","followers_url":"https://api.github.com/users/quantumlib/followers","following_url":"https://api.github.com/users/quantumlib/following{/other_user}","gists_url":"https://api.github.com/users/quantumlib/gists{/gist_id}","starred_url":"https://api.github.com/users/quantumlib/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/quantumlib/subscriptions","organizations_url":"https://api.github.com/users/quantumlib/orgs","repos_url":"https://api.github.com/users/quantumlib/repos","events_url":"https://api.github.com/users/quantumlib/events{/privacy}","received_events_url":"https://api.github.com/users/quantumlib/received_events","type":"Organization","site_admin":false},"repo":{"id":104403768,"node_id":"MDEwOlJlcG9zaXRvcnkxMDQ0MDM3Njg=","name":"OpenFermion","full_name":"quantumlib/OpenFermion","owner":{"login":"quantumlib","id":31279789,"node_id":"MDEyOk9yZ2FuaXphdGlvbjMxMjc5Nzg5","avatar_url":"https://avatars1.githubusercontent.com/u/31279789?v=4","gravatar_id":"","url":"https://api.github.com/users/quantumlib","html_url":"https://github.com/quantumlib","followers_url":"https://api.github.com/users/quantumlib/followers","following_url":"https://api.github.com/users/quantumlib/following{/other_user}","gists_url":"https://api.github.com/users/quantumlib/gists{/gist_id}","starred_url":"https://api.github.com/users/quantumlib/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/quantumlib/subscriptions","organizations_url":"https://api.github.com/users/quantumlib/orgs","repos_url":"https://api.github.com/users/quantumlib/repos","events_url":"https://api.github.com/users/quantumlib/events{/privacy}","received_events_url":"https://api.github.com/users/quantumlib/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/quantumlib/OpenFermion","description":"OpenFermion: The Electronic Structure Package For Quantum Computers.","fork":false,"url":"https://api.github.com/repos/quantumlib/OpenFermion","forks_url":"https://api.github.com/repos/quantumlib/OpenFermion/forks","keys_url":"https://api.github.com/repos/quantumlib/OpenFermion/keys{/key_id}","collaborators_url":"https://api.github.com/repos/quantumlib/OpenFermion/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/quantumlib/OpenFermion/teams","hooks_url":"https://api.github.com/repos/quantumlib/OpenFermion/hooks","issue_events_url":"https://api.github.com/repos/quantumlib/OpenFermion/issues/events{/number}","events_url":"https://api.github.com/repos/quantumlib/OpenFermion/events","assignees_url":"https://api.github.com/repos/quantumlib/OpenFermion/assignees{/user}","branches_url":"https://api.github.com/repos/quantumlib/OpenFermion/branches{/branch}","tags_url":"https://api.github.com/repos/quantumlib/OpenFermion/tags","blobs_url":"https://api.github.com/repos/quantumlib/OpenFermion/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/quantumlib/OpenFermion/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/quantumlib/OpenFermion/git/refs{/sha}","trees_url":"https://api.github.com/repos/quantumlib/OpenFermion/git/trees{/sha}","statuses_url":"https://api.github.com/repos/quantumlib/OpenFermion/statuses/{sha}","languages_url":"https://api.github.com/repos/quantumlib/OpenFermion/languages","stargazers_url":"https://api.github.com/repos/quantumlib/OpenFermion/stargazers","contributors_url":"https://api.github.com/repos/quantumlib/OpenFermion/contributors","subscribers_url":"https://api.github.com/repos/quantumlib/OpenFermion/subscribers","subscription_url":"https://api.github.com/repos/quantumlib/OpenFermion/subscription","commits_url":"https://api.github.com/repos/quantumlib/OpenFermion/commits{/sha}","git_commits_url":"https://api.github.com/repos/quantumlib/OpenFermion/git/commits{/sha}","comments_url":"https://api.github.com/repos/quantumlib/OpenFermion/comments{/number}","issue_comment_url":"https://api.github.com/repos/quantumlib/OpenFermion/issues/comments{/number}","contents_url":"https://api.github.com/repos/quantumlib/OpenFermion/contents/{+path}","compare_url":"https://api.github.com/repos/quantumlib/OpenFermion/compare/{base}...{head}","merges_url":"https://api.github.com/repos/quantumlib/OpenFermion/merges","archive_url":"https://api.github.com/repos/quantumlib/OpenFermion/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/quantumlib/OpenFermion/downloads","issues_url":"https://api.github.com/repos/quantumlib/OpenFermion/issues{/number}","pulls_url":"https://api.github.com/repos/quantumlib/OpenFermion/pulls{/number}","milestones_url":"https://api.github.com/repos/quantumlib/OpenFermion/milestones{/number}","notifications_url":"https://api.github.com/repos/quantumlib/OpenFermion/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/quantumlib/OpenFermion/labels{/name}","releases_url":"https://api.github.com/repos/quantumlib/OpenFermion/releases{/id}","deployments_url":"https://api.github.com/repos/quantumlib/OpenFermion/deployments","created_at":"2017-09-21T22:10:28Z","updated_at":"2018-06-26T22:46:31Z","pushed_at":"2018-06-26T21:56:51Z","git_url":"git://github.com/quantumlib/OpenFermion.git","ssh_url":"git@github.com:quantumlib/OpenFermion.git","clone_url":"https://github.com/quantumlib/OpenFermion.git","svn_url":"https://github.com/quantumlib/OpenFermion","homepage":"","size":3515,"stargazers_count":504,"watchers_count":504,"language":"Python","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":128,"mirror_url":null,"archived":false,"open_issues_count":21,"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":128,"open_issues":21,"watchers":504,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/377"},"html":{"href":"https://github.com/quantumlib/OpenFermion/pull/377"},"issue":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/issues/377"},"comments":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/issues/377/comments"},"review_comments":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/377/comments"},"review_comment":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/pulls/377/commits"},"statuses":{"href":"https://api.github.com/repos/quantumlib/OpenFermion/statuses/d44e98fb9440baaf32bd18bef305b5561bca3b2c"}},"author_association":"COLLABORATOR"}}
{"actor":{"display_login":"jarrodmcc"}}