Skip to content

Commit

Permalink
make ffsim dependency optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung committed Jan 2, 2024
1 parent d17f8cc commit 815048d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 14 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 10 additions & 8 deletions qiskit_cold_atom/fermions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion qiskit_cold_atom/fermions/ffsim_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions qiskit_cold_atom/providers/cold_atom_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 11 additions & 2 deletions test/test_ffsim_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand All @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 815048d

Please sign in to comment.