Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPS emulator for LUCJ circuits #334

Open
wants to merge 81 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
7bbd1b7
add to_mpo method to MolecularHamiltonian class
bartandrews Oct 25, 2024
b283f11
specify class name as MPOModel
bartandrews Oct 25, 2024
15c5823
fix comment
bartandrews Oct 25, 2024
5795e04
remove print statements
bartandrews Oct 25, 2024
acc9845
add docstrings for TeNPy classes
bartandrews Oct 25, 2024
38078be
add lucj_circuit_as_mps function
bartandrews Oct 27, 2024
7b8113b
update how-to notebook
bartandrews Oct 28, 2024
e0b26dc
clean-up code
bartandrews Oct 28, 2024
a96b5e3
fix docstrings
bartandrews Oct 28, 2024
c9a93be
fix docstring 2
bartandrews Oct 28, 2024
0103d47
import annotations
bartandrews Oct 28, 2024
28de05f
fix docstring 3
bartandrews Oct 28, 2024
c32dc85
fix molecular Hamiltonian MPO conversion
bartandrews Oct 29, 2024
5afd692
fix type hint
bartandrews Oct 29, 2024
b84ac28
remove unused import
bartandrews Oct 29, 2024
6049511
import ordering
bartandrews Oct 29, 2024
cc380c7
fix type hint import
bartandrews Oct 29, 2024
56159ea
fix type hints in gates.py
bartandrews Oct 29, 2024
3820e25
fix type hints in lucj_circuit.py
bartandrews Oct 29, 2024
94b7d93
fix type hint for util.py
bartandrews Oct 29, 2024
18078c9
fix another type hint in lucj_circuit.py
bartandrews Oct 29, 2024
b980c3b
add from_molecular_hamiltonian static method
bartandrews Nov 1, 2024
cd0f1ce
directly import UCJOpSpinBalanced for type hint
bartandrews Nov 1, 2024
ea4f56f
update notebook
bartandrews Nov 1, 2024
e608dc5
update notebook 2
bartandrews Nov 1, 2024
a975567
fix formatting
bartandrews Nov 4, 2024
7af99aa
resolve conflict in __init__ file
bartandrews Nov 4, 2024
13fd307
resolve conflicts
bartandrews Nov 4, 2024
8eb2749
Merge branch 'main' into mpo
bartandrews Nov 4, 2024
fa52080
start transition from qubit to fermionic gate names
bartandrews Nov 4, 2024
20c24ce
finish transition from qubit to fermionic gate names
bartandrews Nov 6, 2024
6be63f1
minor change to NNgate
bartandrews Nov 6, 2024
e997491
apply UCJ operator directly
bartandrews Nov 7, 2024
0db2929
add final_orbital_rotation
bartandrews Nov 7, 2024
12263d5
remove decimal_places argument of from_molecular_hamiltonian
bartandrews Nov 8, 2024
b8f35fb
fix final_orbital_rotation bug
bartandrews Nov 8, 2024
205b3c4
add zip loop in lucj_circuit
bartandrews Nov 8, 2024
f2b9e90
add keyword-only arguments
bartandrews Nov 8, 2024
e99a1fd
fix angles in orbital_rotation
bartandrews Nov 8, 2024
bdf583b
leverage current_basis in lucj_circuit_as_mps
bartandrews Nov 8, 2024
18ae3f3
closed-form expressions for gates
bartandrews Nov 8, 2024
d25d18d
remove flag from __init__ in MolecularHamiltonianMPOModel
bartandrews Nov 8, 2024
e387940
delete __init__ method from MolecularHamiltonianMPOModel
bartandrews Nov 10, 2024
c931665
reduce numpy use
bartandrews Nov 10, 2024
b40e4e3
streamline sym_cons_basis
bartandrews Nov 10, 2024
9fe8ed9
remove unnecesary phase factor in apply_orbital_rotation
bartandrews Nov 10, 2024
994b651
get rid of conj argument for givens_rotation
bartandrews Nov 10, 2024
43aa2b2
add test_apply_orbital_rotation
bartandrews Nov 12, 2024
e25a1d1
fix norb definition
bartandrews Nov 12, 2024
1e6858e
remove MolecularChain class
bartandrews Nov 12, 2024
9fbdfc9
tidy lucj_circuit_test
bartandrews Nov 12, 2024
b9cf3a1
refactor bitstring_to_mps
bartandrews Nov 12, 2024
f6c7fe6
Merge branch 'main' into mpo
bartandrews Nov 13, 2024
debce23
tidy MolecularHamiltonianMPOModel
bartandrews Nov 14, 2024
dc7d07f
simplify for loops
bartandrews Nov 14, 2024
2e1777c
tidy product_state_as_mps utility function
bartandrews Nov 14, 2024
c7dcc6b
rename lucj_circuit_as_mps to apply_ucj_op_spin_balanced
bartandrews Nov 14, 2024
3207948
fix notebook
bartandrews Nov 14, 2024
1300a13
major gate refactoring
bartandrews Nov 15, 2024
cee4538
improve docstrings
bartandrews Nov 15, 2024
c4f305e
add more gate tests
bartandrews Nov 21, 2024
0311020
fix notebook
bartandrews Nov 21, 2024
0f84845
fix typo
bartandrews Nov 21, 2024
df7bab5
address review comments
bartandrews Nov 22, 2024
697bbc7
add time argument to apply_diag_coulomb_evolution function
bartandrews Nov 22, 2024
1f7c322
add random molecular Hamiltonian to all gate tests
bartandrews Nov 22, 2024
3007015
start simplifying molecular Hamiltonian
bartandrews Nov 22, 2024
eb0fbc9
Merge branch 'qiskit-community:main' into mpo
bartandrews Nov 23, 2024
8f0640c
simplify two_body_tensor loop in molecular Hamiltonian
bartandrews Nov 23, 2024
62f9ec5
fix minor errors
bartandrews Nov 23, 2024
7ed0f2c
further simplify two_body_tensor loop in molecular Hamiltonian
bartandrews Nov 23, 2024
6efd6d7
refactor molecular hamiltonian
bartandrews Nov 28, 2024
e2ab247
expand molecular Hamiltonian test
bartandrews Nov 29, 2024
c6621ca
Merge branch 'main' into mpo
bartandrews Nov 29, 2024
7ab4480
Merge branch 'main' into mpo
kevinsung Dec 7, 2024
8b4e6c1
revert mol hamiltonian test comparison
kevinsung Dec 8, 2024
864b0b0
update test
kevinsung Dec 20, 2024
fb7f9a8
simplify bitstring_to_mps
kevinsung Dec 20, 2024
f4f63c8
simplify molecular hamiltonian MPO (sign issue still present)
kevinsung Dec 20, 2024
74e4579
simplify test
kevinsung Dec 20, 2024
fc11ae3
add test case
kevinsung Dec 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/api/ffsim.tenpy.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ffsim.tenpy
===========

.. automodule:: ffsim.tenpy
:members:
:special-members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ ffsim.linalg
ffsim.optimize
ffsim.qiskit
ffsim.random
ffsim.tenpy
ffsim.testing
```
1 change: 1 addition & 0 deletions docs/how-to-guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:maxdepth: 1

lucj
lucj_mps
entanglement-forging
fermion-operator
qiskit-circuits
Expand Down
453 changes: 453 additions & 0 deletions docs/how-to-guides/lucj_mps.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
"numpy",
"opt_einsum",
"orjson",
"physics-tenpy",
"pyscf >= 2.7",
"qiskit >= 1.1",
"scipy",
Expand Down
3 changes: 2 additions & 1 deletion python/ffsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

"""ffsim is a software library for fast simulation of fermionic quantum circuits."""

from ffsim import contract, linalg, optimize, qiskit, random, testing
from ffsim import contract, linalg, optimize, qiskit, random, tenpy, testing
from ffsim.cistring import init_cache
from ffsim.gates import (
apply_diag_coulomb_evolution,
Expand Down Expand Up @@ -175,6 +175,7 @@
"slater_determinant_rdms",
"spin_square",
"strings_to_addresses",
"tenpy",
"testing",
"trace",
]
41 changes: 41 additions & 0 deletions python/ffsim/tenpy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# (C) Copyright IBM 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Code that uses TeNPy, e.g. for emulating quantum circuits."""

from ffsim.tenpy.gates.abstract_gates import (
apply_single_site,
apply_two_site,
)
from ffsim.tenpy.gates.basic_gates import (
givens_rotation,
num_interaction,
num_num_interaction,
on_site_interaction,
)
from ffsim.tenpy.gates.diag_coulomb import apply_diag_coulomb_evolution
from ffsim.tenpy.gates.orbital_rotation import apply_orbital_rotation
from ffsim.tenpy.gates.ucj import apply_ucj_op_spin_balanced
from ffsim.tenpy.hamiltonians.molecular_hamiltonian import MolecularHamiltonianMPOModel
from ffsim.tenpy.util import bitstring_to_mps

__all__ = [
"apply_ucj_op_spin_balanced",
"apply_diag_coulomb_evolution",
"apply_orbital_rotation",
"apply_single_site",
"apply_two_site",
"bitstring_to_mps",
"givens_rotation",
"MolecularHamiltonianMPOModel",
"num_interaction",
"num_num_interaction",
"on_site_interaction",
]
9 changes: 9 additions & 0 deletions python/ffsim/tenpy/gates/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# (C) Copyright IBM 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
84 changes: 84 additions & 0 deletions python/ffsim/tenpy/gates/abstract_gates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# (C) Copyright IBM 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""TeNPy abstract gates."""

import numpy as np
import tenpy.linalg.np_conserved as npc
from tenpy.algorithms.tebd import TEBDEngine
from tenpy.linalg.charges import LegPipe
from tenpy.networks.site import SpinHalfFermionSite

# ignore lowercase argument and variable checks to maintain TeNPy naming conventions
# ruff: noqa: N803, N806

# define sites
shfs = SpinHalfFermionSite(cons_N="N", cons_Sz="Sz")
shfsc = LegPipe([shfs.leg, shfs.leg])


def apply_single_site(eng: TEBDEngine, U1: np.ndarray, site: int) -> None:
r"""Apply a single-site gate to an MPS.

Args:
eng: The TEBD engine.
U1: The single-site quantum gate.
site: The gate will be applied to `site` on the MPS.

Returns:
None
"""
U1_npc = npc.Array.from_ndarray(U1, [shfs.leg, shfs.leg.conj()], labels=["p", "p*"])
psi = eng.get_resume_data()["psi"]
psi.apply_local_op(site, U1_npc)


def apply_two_site(
eng: TEBDEngine,
U2: np.ndarray,
sites: tuple[int, int],
*,
norm_tol: float = 1e-8,
) -> None:
r"""Apply a two-site gate to an MPS.

Args:
eng: The TEBD engine.
U2: The two-site quantum gate.
sites: The gate will be applied to adjacent sites `(site1, site2)` on the MPS.
norm_tol: The norm error above which we recanonicalize the MPS. In general, the
application of a two-site gate to an MPS with truncation may degrade its
canonical form. To mitigate this, we explicitly bring the MPS back into
canonical form, if the Frobenius norm of the `site-resolved norm errors array <https://tenpy.readthedocs.io/en/latest/reference/tenpy.networks.mps.MPS.html#tenpy.networks.mps.MPS.norm_test>`_
is greater than `norm_tol`.

Returns:
None
"""

# check that sites are adjacent
if abs(sites[0] - sites[1]) != 1:
raise ValueError("sites must be adjacent")

# check whether to transpose gate
if sites[0] > sites[1]:
U2 = U2.T

# apply NN gate between (site1, site2)
U2_npc = npc.Array.from_ndarray(
U2, [shfsc, shfsc.conj()], labels=["(p0.p1)", "(p0*.p1*)"]
)
U2_npc_split = U2_npc.split_legs()
eng.update_bond(max(sites), U2_npc_split)

# recanonicalize psi if below error threshold
psi = eng.get_resume_data()["psi"]
if np.linalg.norm(psi.norm_test()) > norm_tol:
psi.canonical_form_finite()
Loading
Loading