{"id":24774996,"url":"https://github.com/HirokiHamaguchi/fastwedge","last_synced_at":"2025-10-12T00:31:12.950Z","repository":{"id":216888879,"uuid":"595449106","full_name":"HirokiHamaguchi/fastwedge","owner":"HirokiHamaguchi","description":"fast computation of k-RDM and wedge product","archived":false,"fork":false,"pushed_at":"2024-01-12T22:46:35.000Z","size":4246,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T18:54:15.452Z","etag":null,"topics":["physics","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HirokiHamaguchi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-31T05:03:52.000Z","updated_at":"2024-02-22T18:53:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"112aebf0-856c-4737-b75b-bca7b0426cbd","html_url":"https://github.com/HirokiHamaguchi/fastwedge","commit_stats":null,"previous_names":["hari64boli64/fastwedge","hirokihamaguchi/fastwedge"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/HirokiHamaguchi/fastwedge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HirokiHamaguchi%2Ffastwedge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HirokiHamaguchi%2Ffastwedge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HirokiHamaguchi%2Ffastwedge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HirokiHamaguchi%2Ffastwedge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HirokiHamaguchi","download_url":"https://codeload.github.com/HirokiHamaguchi/fastwedge/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HirokiHamaguchi%2Ffastwedge/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279009509,"owners_count":26084609,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["physics","python"],"created_at":"2025-01-29T06:37:26.011Z","updated_at":"2025-10-12T00:31:12.942Z","avatar_url":"https://github.com/HirokiHamaguchi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastwedge\n\n縮約密度行列とwedge積の高速な計算を行います。\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"imgs/2RDM.png\" width=\"50%\"\u003e\u003c/img\u003e\n\u003c/p\u003e\n\n- [fastwedge](#fastwedge)\n  - [Installation](#installation)\n  - [Usage](#usage)\n    - [visTensor](#vistensor)\n    - [vis4dTensorNest](#vis4dtensornest)\n    - [visTensor2dReal](#vistensor2dreal)\n    - [fast\\_compute\\_k\\_rdm](#fast_compute_k_rdm)\n    - [fast\\_wedge](#fast_wedge)\n    - [fast\\_wedge\\_topM](#fast_wedge_topm)\n    - [test](#test)\n- [LICENSE](#license)\n\n\n## Installation\n\nGoogle Colab上での利用を主に想定しています。\n\n```python\n!pip install openfermion\n!pip install git+https://github.com/hari64boli64/fastwedge.git\n```\n\n## Usage\n\n### visTensor\n\n$2,4,6,8$次元のテンソルのビジュアライズを行います。\n\n$2n$ 次元のテンソルに対して、 $1,2,...,n$ 番目のindexを左側に、 $n+1,n+2,...,2n$ 番目のindexを上側に並べています。\n\n以下に実行例を載せます。\n\n```python\nimport fastwedge\nimport numpy as np\n\ntensor = np.resize(np.arange(3**4), (3, 3, 3, 3))\nfastwedge.visTensor(tensor)\n```\n\n![example_of_visTensor](imgs/example_of_visTensor.png)\n\n### vis4dTensorNest\n\n$4$ 次元のテンソルのビジュアライズを入れ子状にして行います。\n\n$4$ 次元のテンソルに対して、それを $2$ 次元配列の各要素が $2$ 次元配列であると捉え、 $1,2$ 番目のindexを左側と上側に並べた後、各要素毎に対して $3,4$ 番目のindexを再び左側と上側に並べています。\n\n以下に実行例を載せます。\n\n```python\nimport fastwedge\nimport numpy as np\n\ntensor = np.resize(np.arange(3**4), (3, 3, 3, 3))\nfastwedge.vis4dTensorNest(tensor)\n```\n\n![example_of_vis4dTensorNest](imgs/example_of_vis4dTensorNest.png)\n\n### visTensor2dReal\n\n$2$次元のテンソルのビジュアライズを行います。\n\nただし、これは実数のテンソルのみを入力として受け付け、絶対値ではなくそのままの値を表示させます。\n\n### fast_compute_k_rdm\n\nk-RDMを高速に計算します。\n\nフェルミオン系の量子多体状態 $\\rho$ に対して、その $k$ -RDM (Reduced Density Matrix, 縮約密度行列)の行列要素を\n\n$$\n\\begin{align*}\n    ^k D^{p_1 \\cdots p_k}_{q_1 \\cdots q_k} = \\mathrm{Tr}[c_{p_1}^\\dagger \\cdots c_{p_k}^\\dagger c_{q_1} \\cdots c_{q_k} \\rho]\n\\end{align*}\n$$\n\nにより定義します。ここで、 $c_i^{(\\dagger)}$ は $i$ 番目のフェルミオンに関する消滅(生成)演算子を表します。\n\nこの計算結果で返ってくるテンソルは、例えば $k=2$ の場合、以下の要請を満たします。\n\n```python\nimport fastwedge\nimport numpy as np\nimport openfermion\n\nk = 2\nn_qubits = 5\nvec = np.random.normal(size=2**n_qubits)\n\nfast_k_rdm = fastwedge.fast_compute_k_rdm(k, vec)\n\nassert np.isclose(\n    fast_k_rdm[1][2][3][4],\n    vec.conj() @\n    openfermion.get_sparse_operator(\n        openfermion.FermionOperator(f\"{1}^ {2}^ {3} {4}\"),\n        n_qubits).toarray() @\n    vec\n)\n```\n\n### fast_wedge\n\nwedge積を高速に計算します。\n\nWedge積の定義は、以下のように与えられます。\n\n$$\n\\begin{align*}\n    \\left( a \\wedge b\\right)\n     ^{i_{1}, i_{2}, ..., i_{p},..., i_{p+q}}\n     _{j_{1}, j_{2}, ...,j_{p}, ..., j_{p+q} } =\n    \\left(\\frac{1}{N!}\\right)^{2}\n    \\sum_{\\pi, \\sigma}\\epsilon(\\pi)\\epsilon(\\sigma)\n    a_{\\pi(j_{1}), \\pi(j_{2}), ..., \\pi(j_{p}) }\n     ^{ \\sigma(i_{1}), \\sigma(i_{2}), ..., \\sigma(i_{p})}\n    b_{\\pi(j_{p+1}), \\pi(j_{p+2}), ..., \\pi(j_{p+q}) }\n     ^{ \\sigma(i_{p+1}), \\sigma(i_{p+2}), ..., \\sigma(i_{p+q})}\n\\end{align*}\n$$\n\nただし、 $a$ と $b$ はそれぞれ $p$ -RDM、 $q$ -RDMを表すテンソルで、フェルミオンの半交換関係に起因して行列要素同士が符号で結びついています。\n\nまた、 $\\pi, \\sigma$ はそれぞれ $(p+q)$ 個の添字に関する巡回を表し、 $\\epsilon(\\pi)$ などは巡回操作の符号を表すものとします。\n\nその素朴なeinsumによる`wedge`関数の実装は、googleの提供する[Openfermionライブラリ](https://quantumai.google/reference/python/openfermion/linalg/wedge)に提供されています。\n\nこの計算結果で返ってくるテンソルは、例えば $k=2$ の場合、以下の要請を満たします。\n\n```python\nimport fastwedge\nimport numpy as np\nimport openfermion\n\nQ, p, q = 5, 1, 2\nleft_tensor = np.random.random(tuple(Q for _ in range(2*p)))\\\n    + 1j*np.random.random(tuple(Q for _ in range(2*p)))\nright_tensor = np.random.random(tuple(Q for _ in range(2*q)))\\\n    + 1j*np.random.random(tuple(Q for _ in range(2*q)))\nleft_index_ranks = (p, p)\nright_index_ranks = (q, q)\n\nfast_ans = fastwedge.fast_wedge(left_tensor, right_tensor,\n                                left_index_ranks, right_index_ranks),\nslow_ans = openfermion.wedge(left_tensor, right_tensor,\n                             left_index_ranks, right_index_ranks)\n\nassert(np.allclose(fast_ans, slow_ans))\n```\n\nただし、以下の事に十分注意して下さい。\n\n例として、 $a,b,p,q,N,Q$ を以下の様に定義します。\n\n$$\n\\begin{align}\n    \u0026a_0^0=1,a_1^0=2,a_0^1=3,a_1^1=4 \\\\\n    \u0026b_0^0=1,b_1^0=2,b_0^1=3,b_1^1=4 \\\\\n    \u0026p=1,q=1,N=2,Q=2\n\\end{align}\n$$\n\nこの時、\n\n$$\n\\begin{align}\n     \u0026 (a \\wedge b)^{0, 1}_{0, 1} \\\\\n    =\u0026 \\left(\\frac{1}{N!}\\right)^{2}\n        \\sum_{\\pi, \\sigma}\\epsilon(\\pi)\\epsilon(\\sigma)\n        a_{\\pi(0)}^{\\sigma(0)}b_{\\pi(1)}^{\\sigma(1)} \\\\\n    =\u0026 \\frac{1}{4} ((1 * 1 * a_{0}^{0} b_{1}^{1})+(-1 * 1 * a_{1}^{0} b_{0}^{1})\n                    +(1 * -1 * a_{0}^{1} b_{1}^{0})+(-1 * -1 * a_{1}^{1} b_{0}^{0})) \\\\\n    =\u0026 \\frac{1}{4} ((1 * 1 *1 * 4)+(-1 * 1 * 2 * 3)\n                    +(1 * -1 * 3 * 2)+(-1 * -1 * 4 * 1)) \\\\\n    =\u0026 -1\n\\end{align}\n$$\n\nと`-1`ですが、以下のコードは、`-1`でなく、`example_tensor[0,1,0,1]=(1+0j)`と`1`を返します。\n\n```python\nexample_a = np.array([[1, 2], [3, 4]])\nexample_b = np.array([[1, 2], [3, 4]])\nexample_tensor = wedge(example_a, example_b, (1, 1), (1, 1))\nprint(f\"{example_tensor[0,1,0,1]=}\")\n```\n\nこれは、\n\n$$\n    tensor[0,1,0,1]=(a \\wedge b)^{0, 1}_{1, 0}\n$$\n\nと、openfermionの仕様として`\u003ca^ b^ c d\u003e = D_{dc}^{ab}`である事に基づいています。\n\n### fast_wedge_topM\n\n(更新中)\n\n### test\n\n単体テストを行う関数です。上記で述べた要請を満たしているかをチェックしています。\n\n開発時以外に使う必要はありません。\n\nGithub Actionsを用いて実行しています。\n\n# LICENSE\n\nOpen Sourceです。\n\n[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHirokiHamaguchi%2Ffastwedge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FHirokiHamaguchi%2Ffastwedge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHirokiHamaguchi%2Ffastwedge/lists"}