From 815048d7c6a764547fbf6e458f5d78056bcfd262 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Tue, 2 Jan 2024 14:06:19 -0500 Subject: [PATCH] make ffsim dependency optional --- .github/workflows/main.yml | 26 +++++++++++++++++++ qiskit_cold_atom/fermions/__init__.py | 18 +++++++------ qiskit_cold_atom/fermions/ffsim_backend.py | 2 +- .../providers/cold_atom_provider.py | 11 ++++++-- requirements.txt | 1 - setup.py | 1 + test/test_ffsim_backend.py | 13 ++++++++-- tox.ini | 4 +++ 8 files changed, 62 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2438b57..2ac13b4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -94,3 +94,29 @@ jobs: with: name: html_docs path: docs/_build/html + ffsim: + name: tests-ffsim-python${{ matrix.python-version }}-${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + python-version: [3.11] + os: ["ubuntu-latest", "macOS-latest"] + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Pip cache + uses: actions/cache@v2 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-${{ matrix.python-version }}-pip-tests-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.python-version }}-pip-tests- + ${{ runner.os }}-${{ matrix.python-version }}-pip- + ${{ runner.os }}-${{ matrix.python-version }} + - name: Install Deps + run: python -m pip install -U tox setuptools virtualenv wheel + - name: Run ffsim tests + run: tox -e ffsim \ No newline at end of file diff --git a/qiskit_cold_atom/fermions/__init__.py b/qiskit_cold_atom/fermions/__init__.py index 4747437..d9ec7d2 100644 --- a/qiskit_cold_atom/fermions/__init__.py +++ b/qiskit_cold_atom/fermions/__init__.py @@ -89,20 +89,22 @@ """ -from qiskit_cold_atom.fermions.fermion_simulator_backend import FermionSimulator from qiskit_cold_atom.fermions.base_fermion_backend import BaseFermionBackend from qiskit_cold_atom.fermions.fermion_circuit_solver import FermionCircuitSolver - from qiskit_cold_atom.fermions.fermion_gate_library import ( - FermionicGate, - LoadFermions, - Phase, - Hop, - Interaction, FermiHubbard, + FermionicGate, FRXGate, FRYGate, FRZGate, + Hop, + Interaction, + LoadFermions, + Phase, ) +from qiskit_cold_atom.fermions.fermion_simulator_backend import FermionSimulator -from qiskit_cold_atom.fermions.ffsim_backend import FfsimBackend +try: + from qiskit_cold_atom.fermions.ffsim_backend import FfsimBackend +except ImportError: + pass diff --git a/qiskit_cold_atom/fermions/ffsim_backend.py b/qiskit_cold_atom/fermions/ffsim_backend.py index fc746cb..81e3931 100644 --- a/qiskit_cold_atom/fermions/ffsim_backend.py +++ b/qiskit_cold_atom/fermions/ffsim_backend.py @@ -19,7 +19,7 @@ from collections import Counter from typing import Any, Dict, List, Optional, Union -import ffsim +import ffsim # pylint: disable=import-error import numpy as np import scipy.linalg from qiskit import QuantumCircuit diff --git a/qiskit_cold_atom/providers/cold_atom_provider.py b/qiskit_cold_atom/providers/cold_atom_provider.py index adeca72..f51cc56 100644 --- a/qiskit_cold_atom/providers/cold_atom_provider.py +++ b/qiskit_cold_atom/providers/cold_atom_provider.py @@ -28,7 +28,6 @@ from qiskit_cold_atom.exceptions import QiskitColdAtomError from qiskit_cold_atom.fermions import FermionSimulator -from qiskit_cold_atom.fermions import FfsimBackend from qiskit_cold_atom.spins import SpinSimulator from qiskit_cold_atom.providers.fermionic_tweezer_backend import ( @@ -40,6 +39,13 @@ ) from qiskit_cold_atom.providers.collective_spin_backend import CollectiveSpinSimulator +try: + from qiskit_cold_atom.fermions import FfsimBackend + + HAVE_FFSIM = True +except ImportError: + HAVE_FFSIM = False + # Default location of the credentials file _DEFAULT_COLDATOM_FILE = os.path.join( os.path.expanduser("~"), ".qiskit", "cold_atom_credentials.conf" @@ -86,8 +92,9 @@ def __init__(self, credentials: Optional[Dict[str, Union[str, List[str]]]] = Non SpinSimulator(provider=self), FermionicTweezerSimulator(provider=self), CollectiveSpinSimulator(provider=self), - FfsimBackend(provider=self), ] + if HAVE_FFSIM: + backends.append(FfsimBackend(provider=self)) if credentials is not None: try: diff --git a/requirements.txt b/requirements.txt index 1abf7e3..6d3c5fa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,3 @@ qiskit-terra>= 0.17.1, <=0.25.2.1 qiskit-nature>=0.4.4,<=0.6.2 qiskit-aer>=0.8.0, <=0.12.2 matplotlib>=3.3 -ffsim==0.0.17 \ No newline at end of file diff --git a/setup.py b/setup.py index 221e12e..8a9082e 100644 --- a/setup.py +++ b/setup.py @@ -52,6 +52,7 @@ keywords="qiskit sdk quantum cold atoms", packages=setuptools.find_packages(include=["qiskit_cold_atom", "qiskit_cold_atom.*"]), install_requires=REQUIREMENTS, + extras_require={"ffsim": ["ffsim==0.0.17"]}, include_package_data=True, python_requires=">=3.8", zip_safe=False, diff --git a/test/test_ffsim_backend.py b/test/test_ffsim_backend.py index a02cc33..4863d0f 100644 --- a/test/test_ffsim_backend.py +++ b/test/test_ffsim_backend.py @@ -13,8 +13,10 @@ """ffsim backend tests.""" import math +import unittest import numpy as np +from qiskit.test import QiskitTestCase from qiskit_cold_atom.fermions import ( FermiHubbard, @@ -23,7 +25,13 @@ Interaction, Phase, ) -from qiskit_cold_atom.fermions.ffsim_backend import FfsimBackend + +try: + from qiskit_cold_atom.fermions.ffsim_backend import FfsimBackend + + HAVE_FFSIM = True +except ImportError: + HAVE_FFSIM = False def _random_occupations(norb: int, nelec: tuple[int, int], seed=None): @@ -49,7 +57,8 @@ def _fidelity(counts1: dict[str, int], counts2: dict[str, int]) -> float: return result**2 -class TestFfsimBackend: +@unittest.skipUnless(HAVE_FFSIM, "requires ffsim") +class TestFfsimBackend(QiskitTestCase): """Test FfsimBackend.""" def test_hop_gate(self): diff --git a/tox.ini b/tox.ini index d4a3685..26cc883 100644 --- a/tox.ini +++ b/tox.ini @@ -33,3 +33,7 @@ skip_install = true deps = allowlist_externals = rm commands = rm -rf {toxinidir}/docs/stubs/ {toxinidir}/docs/_build + +[testenv:ffsim] +deps = .[ffsim] +commands = python -m unittest test/test_ffsim_backend.py \ No newline at end of file