From ad120d4536713d53ba1058b5dc9aa42b43e94459 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:29:38 +0000 Subject: [PATCH 01/17] updated workflows ; --- .github/workflows/pull_request.yaml | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index f6a991b0..bb9feb89 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -6,13 +6,28 @@ on: branches: [main] jobs: build: - runs-on: ubuntu-latest + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ ubuntu-latest, macos-latest] + python: ["3.8", "3.9"] + # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows + # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions steps: - - uses: actions/checkout@v2 - - name: Set up python - uses: actions/setup-python@v2 + - uses: actions/checkout@v3 + - name: Setup Python + uses: actions/setup-python@v4 with: - python-version: '3.9' + python-version: ${{ matrix.python }} + + #print the current Python version + - name: Display Python version + run: python -c "import sys; print(sys.version)" + # print OS version + - name: Display os version + run: python -c "import platform; print(platform.system())" - name: Pip install package run: | python -m pip install --upgrade pip From 2283ba3c4673257e87caea66b85a70be21a019d6 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:35:49 +0000 Subject: [PATCH 02/17] updated pull_request.yaml --- .github/workflows/pull_request.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index bb9feb89..22833dac 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -15,13 +15,13 @@ jobs: python: ["3.8", "3.9"] # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions + steps: - uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python }} - #print the current Python version - name: Display Python version run: python -c "import sys; print(sys.version)" From fd6532d64efcbd48953a5f36176fb7f4a6ca3c6c Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:39:55 +0000 Subject: [PATCH 03/17] reformat --- .github/workflows/pull_request.yaml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 22833dac..e1b642e9 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -13,11 +13,8 @@ jobs: matrix: os: [ ubuntu-latest, macos-latest] python: ["3.8", "3.9"] - # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows - # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions - steps: - - uses: actions/checkout@v3 + #- uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v4 with: From 676734c32e4dc1093c9167310131ef3d70ccd78d Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:40:55 +0000 Subject: [PATCH 04/17] reformat2 --- .github/workflows/pull_request.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index e1b642e9..6c54d7de 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -14,7 +14,7 @@ jobs: os: [ ubuntu-latest, macos-latest] python: ["3.8", "3.9"] steps: - #- uses: actions/checkout@v3 + - uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v4 with: From 289cab5737b655c10e411d495ce34e0af0bee116 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:43:13 +0000 Subject: [PATCH 05/17] changes to workflow --- .github/workflows/pull_request.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 6c54d7de..d795a6ab 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -7,13 +7,15 @@ on: jobs: build: - runs-on: ${{ matrix.os }} - strategy: matrix: os: [ ubuntu-latest, macos-latest] python: ["3.8", "3.9"] + # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows + # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions + steps: + - runs-on: ${{ matrix.os }} - uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v4 From 86f148b678e73260768f07b63eba1001a0a5b38d Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:44:26 +0000 Subject: [PATCH 06/17] changes to workflow --- .github/workflows/pull_request.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index d795a6ab..f923bf09 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -14,8 +14,8 @@ jobs: # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions + runs-on: ${{ matrix.os }} steps: - - runs-on: ${{ matrix.os }} - uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v4 From ac1608836e1888c8e3c6a649af955e16b2307cca Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:46:16 +0000 Subject: [PATCH 07/17] changes to workflow --- .github/workflows/pull_request.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index f923bf09..103af90b 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -16,7 +16,6 @@ jobs: runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v4 with: From d8414535dd2a9a02177cfd21d4c61037c8bf6864 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:50:16 +0000 Subject: [PATCH 08/17] yaml workflow fixes --- .github/workflows/pull_request.yaml | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 103af90b..2f71f3c3 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -11,21 +11,22 @@ jobs: matrix: os: [ ubuntu-latest, macos-latest] python: ["3.8", "3.9"] - # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows - # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions + # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows + # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions runs-on: ${{ matrix.os }} steps: - - name: Setup Python + - uses: actions/checkout@v3 + - name: Set up Python uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python }} - #print the current Python version + with: + python-version: ${{ matrix.python }} + #print the current Python version - name: Display Python version run: python -c "import sys; print(sys.version)" # print OS version - name: Display os version - run: python -c "import platform; print(platform.system())" + run: python -c "import platform; print(platform.system())" - name: Pip install package run: | python -m pip install --upgrade pip From 76cd2e4bc1922a84f693c5f3dbabc7dcfe45c3ef Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:53:40 +0000 Subject: [PATCH 09/17] test workflow --- .github/workflows/pull_request.yaml | 74 ++++++++++++++++++----------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 2f71f3c3..65af1b6c 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -4,41 +4,59 @@ on: branches: [main] push: branches: [main] +#jobs: +# build: +# +# strategy: +# matrix: +# os: [ ubuntu-latest, macos-latest] +# python: ["3.8", "3.9"] +# # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows +# # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions +# +# runs-on: ${{ matrix.os }} +# steps: +# - uses: actions/checkout@v3 +# - name: Set up Python +# uses: actions/setup-python@v4 +# with: +# python-version: ${{ matrix.python }} +# #print the current Python version +# - name: Display Python version +# run: python -c "import sys; print(sys.version)" +# # print OS version +# - name: Display os version +# run: python -c "import platform; print(platform.system())" +# - name: Pip install package +# run: | +# python -m pip install --upgrade pip +# pip install . +# - name: Poetry install package +# run: | +# pip install poetry +# poetry install +# - name: Linting +# run: | +# poetry run pydocstyle --convention=google +# poetry run isort . --check +# poetry run black . --check +# poetry run flake8 . --ignore E501,F401,W503 --count +# continue-on-error: True jobs: build: + runs-on: ${{ matrix.os }} strategy: matrix: - os: [ ubuntu-latest, macos-latest] - python: ["3.8", "3.9"] - # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows - # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions + os: [ubuntu-latest, macos-latest] + python-version: ["3.7", "3.8", "3.9", "3.10"] - runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - - name: Set up Python + - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python }} - #print the current Python version + with: + python-version: ${{ matrix.python-version }} + # You can test your matrix by printing the current Python version - name: Display Python version - run: python -c "import sys; print(sys.version)" - # print OS version - - name: Display os version - run: python -c "import platform; print(platform.system())" - - name: Pip install package - run: | - python -m pip install --upgrade pip - pip install . - - name: Poetry install package - run: | - pip install poetry - poetry install - - name: Linting - run: | - poetry run pydocstyle --convention=google - poetry run isort . --check - poetry run black . --check - poetry run flake8 . --ignore E501,F401,W503 --count - continue-on-error: True + run: python -c "import sys; print(sys.version)" \ No newline at end of file From b69311ba614159e725c365f23f6aca9c84fc8a0a Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:56:40 +0000 Subject: [PATCH 10/17] test workflow updates --- .github/workflows/pull_request.yaml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 65af1b6c..5137df30 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -57,6 +57,10 @@ jobs: uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - # You can test your matrix by printing the current Python version - - name: Display Python version - run: python -c "import sys; print(sys.version)" \ No newline at end of file + # print the current Python version + - name: Display Python version and system OS + run: python -c "import sys; import platform; print(sys.version, platform.system())" + - name: Pip install package + run: | + python -m pip install --upgrade pip + pip install . \ No newline at end of file From cead4fdec1ec58e8daf8f1d38a0ff5668c508b6e Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 12:58:05 +0000 Subject: [PATCH 11/17] workflow fixed --- .github/workflows/pull_request.yaml | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 5137df30..7cb4f843 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -49,7 +49,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest] - python-version: ["3.7", "3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9"] steps: - uses: actions/checkout@v3 @@ -63,4 +63,15 @@ jobs: - name: Pip install package run: | python -m pip install --upgrade pip - pip install . \ No newline at end of file + pip install . + - name: Poetry install package + run: | + pip install poetry + poetry install + - name: Linting + run: | + poetry run pydocstyle --convention=google + poetry run isort . --check + poetry run black . --check + poetry run flake8 . --ignore E501,F401,W503 --count + continue-on-error: True \ No newline at end of file From 365ffea359e4089b3af2771045ed6e0290e36a26 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 13:01:05 +0000 Subject: [PATCH 12/17] updated git workflow CI --- .github/workflows/pull_request.yaml | 41 +++-------------------------- 1 file changed, 3 insertions(+), 38 deletions(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 7cb4f843..bc815118 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -4,44 +4,7 @@ on: branches: [main] push: branches: [main] -#jobs: -# build: -# -# strategy: -# matrix: -# os: [ ubuntu-latest, macos-latest] -# python: ["3.8", "3.9"] -# # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows -# # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions -# -# runs-on: ${{ matrix.os }} -# steps: -# - uses: actions/checkout@v3 -# - name: Set up Python -# uses: actions/setup-python@v4 -# with: -# python-version: ${{ matrix.python }} -# #print the current Python version -# - name: Display Python version -# run: python -c "import sys; print(sys.version)" -# # print OS version -# - name: Display os version -# run: python -c "import platform; print(platform.system())" -# - name: Pip install package -# run: | -# python -m pip install --upgrade pip -# pip install . -# - name: Poetry install package -# run: | -# pip install poetry -# poetry install -# - name: Linting -# run: | -# poetry run pydocstyle --convention=google -# poetry run isort . --check -# poetry run black . --check -# poetry run flake8 . --ignore E501,F401,W503 --count -# continue-on-error: True + jobs: build: @@ -50,6 +13,8 @@ jobs: matrix: os: [ubuntu-latest, macos-latest] python-version: ["3.8", "3.9"] +# # os: [ ubuntu-latest, macos-latest, windows-latest] TODO: add windows +# # python: [ "3.7", "3.8", "3.9", "3.10", '3.11'] TODO: add more python versions steps: - uses: actions/checkout@v3 From b4b482b8825b1e58cecf50c22ad13031976ef606 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 16:32:00 +0000 Subject: [PATCH 13/17] unit tests for noncontextual_op added. noncontextual op updated from_hamiltonian method to allow noncon input --- .../2.3 CS-VQE for Electronic Structure.ipynb | 2 +- notebooks/Untitled.ipynb | 632 ------------------ symmer/symplectic/noncontextual_op.py | 28 +- .../test_symplectic/test_noncontextual_op.py | 278 ++++++++ 4 files changed, 296 insertions(+), 644 deletions(-) delete mode 100644 notebooks/Untitled.ipynb create mode 100644 tests/test_symplectic/test_noncontextual_op.py diff --git a/notebooks/2. Applications/2.3 CS-VQE for Electronic Structure.ipynb b/notebooks/2. Applications/2.3 CS-VQE for Electronic Structure.ipynb index d58ccceb..e5068812 100644 --- a/notebooks/2. Applications/2.3 CS-VQE for Electronic Structure.ipynb +++ b/notebooks/2. Applications/2.3 CS-VQE for Electronic Structure.ipynb @@ -1814,7 +1814,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.8.16" }, "vscode": { "interpreter": { diff --git a/notebooks/Untitled.ipynb b/notebooks/Untitled.ipynb deleted file mode 100644 index d5d4186a..00000000 --- a/notebooks/Untitled.ipynb +++ /dev/null @@ -1,632 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 163, - "id": "e8f40371", - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "from symmer.chemistry import MoleculeBuilder\n", - "import os\n", - "import numpy as np\n", - "from openfermion import FermionOperator\n", - "from symmer import PauliwordOp" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "df966575", - "metadata": {}, - "outputs": [], - "source": [ - "notebook_dir = os.getcwd()\n", - "# notebook_dir = os.path.dirname(cwd)\n", - "symmer_dir = os.path.dirname(notebook_dir)\n", - "test_dir = os.path.join(symmer_dir, 'tests')\n", - "ham_data_dir = os.path.join(test_dir, 'hamiltonian_data')\n", - "\n", - "if not os.path.isdir(ham_data_dir):\n", - " raise ValueError('cannot find data dir')\n", - " \n", - "filename = 'H4_STO-3G_SINGLET_JW.json'" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "2b869063", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'/Users/lex/Documents/PhD/symmer/tests/hamiltonian_data'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ham_data_dir" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "69f03b4d", - "metadata": {}, - "outputs": [], - "source": [ - "speciesname = 'HCl_STO-3G_SINGLET_JW.json'\n", - "\n", - "with open(os.path.join(ham_data_dir, speciesname), 'r') as infile:\n", - " molecule_geometries = json.load(infile)\n", - "\n", - "\n", - "# # build the molecule\n", - "mol_data = molecule_geometries['data']\n", - "geometry = mol_data['geometry']\n", - "charge = mol_data['charge']\n", - "basis = mol_data['basis']\n", - "spin = 0\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d0cbbf75", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 89, - "id": "8659c4ed", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Molecule geometry:\n", - "Cl\t0.0\t0.0\t0.07452\n", - "H\t0.0\t0.0\t-1.266846\n", - "\n", - "FCI converged? True\n", - "\n", - "HF energy: -455.1354456708121\n", - "MP2 energy: None\n", - "CCSD energy: -455.1570668271786\n", - "CISD energy: None\n", - "FCI energy: None\n", - "\n", - "\n", - "Number of qubits: 20\n" - ] - } - ], - "source": [ - "molecule = MoleculeBuilder(geometry=geometry,\n", - " charge=charge, \n", - " basis=basis,\n", - " spin=spin, \n", - " run_fci=False, \n", - " run_mp2=False,\n", - " run_cisd=False,\n", - "# run_ccsd=False,\n", - " print_info=True,\n", - " symmetry='True')" - ] - }, - { - "cell_type": "code", - "execution_count": 90, - "id": "df3324f0", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 90, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "molecule.pyscf_obj.pyscf_hf" - ] - }, - { - "cell_type": "code", - "execution_count": 91, - "id": "d8bb2e8a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('C2v',)\n", - "Coov\n" - ] - } - ], - "source": [ - "groupname = molecule.pyscf_obj.pyscf_hf.mol.groupname,\n", - "print(groupname)\n", - "topgroup=molecule.pyscf_obj.pyscf_hf.mol.topgroup\n", - "print(topgroup)" - ] - }, - { - "cell_type": "code", - "execution_count": 92, - "id": "2bbf20dd", - "metadata": {}, - "outputs": [], - "source": [ - "from pyscf import symm\n", - "\n", - "\n", - "orbsym = symm.label_orb_symm(molecule.pyscf_obj.pyscf_hf.mol,\n", - " molecule.pyscf_obj.pyscf_hf.mol.irrep_name,\n", - " molecule.pyscf_obj.pyscf_hf.mol.symm_orb,\n", - " molecule.pyscf_obj.pyscf_hf.mo_coeff)" - ] - }, - { - "cell_type": "code", - "execution_count": 93, - "id": "0c9ba396", - "metadata": {}, - "outputs": [], - "source": [ - "# orb_sym_labels = np.zeros((len(orbsym)*2), dtype=object)\n", - "\n", - "# orb_sym_labels[0::2] = orbsym\n", - "# orb_sym_labels[1::2] = orbsym" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "db584a48", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 94, - "id": "ad1cabe6", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array(['A1', 'A1', 'A1', 'A1', 'A1', 'A1', 'B1', 'B1', 'B2', 'B2', 'A1',\n", - " 'A1', 'A1', 'A1', 'B1', 'B1', 'B2', 'B2'], dtype=' "NoncontextualOp": """ Given a PauliwordOp, extract from it a noncontextual sub-Hamiltonian by the specified strategy """ - if strategy == 'diag': - return cls._diag_noncontextual_op(H) - elif strategy == 'basis': - return cls._from_basis_noncontextual_op(H, basis) - elif strategy.find('DFS') != -1: - _, strategy = strategy.split('_') - return cls._dfs_noncontextual_op(H, strategy=strategy, runtime=DFS_runtime) - elif strategy.find('SingleSweep') != -1: - _, strategy = strategy.split('_') - return cls._single_sweep_noncontextual_operator(H, strategy=strategy) + if H.is_noncontextual: + warnings.warn('input H is already noncontextual ignoring strategy') + return cls.from_PauliwordOp(H) else: - raise ValueError(f'Unrecognised noncontextual operator strategy {strategy}') + if strategy == 'diag': + return cls._diag_noncontextual_op(H) + elif strategy == 'basis': + return cls._from_basis_noncontextual_op(H, basis) + elif strategy.find('DFS') != -1: + _, strategy = strategy.split('_') + return cls._dfs_noncontextual_op(H, strategy=strategy, runtime=DFS_runtime) + elif strategy.find('SingleSweep') != -1: + _, strategy = strategy.split('_') + return cls._single_sweep_noncontextual_operator(H, strategy=strategy) + else: + raise ValueError(f'Unrecognised noncontextual operator strategy {strategy}') @classmethod def _diag_noncontextual_op(cls, H: PauliwordOp): diff --git a/tests/test_symplectic/test_noncontextual_op.py b/tests/test_symplectic/test_noncontextual_op.py new file mode 100644 index 00000000..e0e59ee0 --- /dev/null +++ b/tests/test_symplectic/test_noncontextual_op.py @@ -0,0 +1,278 @@ +import pytest +import numpy as np +from symmer.symplectic import PauliwordOp, NoncontextualOp, QuantumState + +noncon_problem = { + 'H_dict': {'IIII': (-0.09706626816762845+0j), + 'IIIZ': (-0.22343153690813597+0j), + 'IIZI': (-0.22343153690813597+0j), + 'IIZZ': (0.17441287612261608+0j), + 'IZII': (0.17141282644776884+0j), + 'IZIZ': (0.12062523483390426+0j), + 'IZZI': (0.16592785033770355+0j), + 'ZIII': (0.17141282644776884+0j), + 'ZIIZ': (0.16592785033770355+0j), + 'ZIZI': (0.12062523483390426+0j), + 'ZZII': (0.16868898170361213+0j), + 'XXYY': (-0.0453026155037993+0j), + 'XYYX': (0.0453026155037993+0j), + 'YXXY': (0.0453026155037993+0j), + 'YYXX': (-0.0453026155037993+0j)}, + 'E': -1.1372838344885023, + 'reference_state': np.array([1, 1, 0, 0]), + 'partial_reference_state': QuantumState( + np.array([[1, 1, 0, 0], + [1, 1, 1, 1]]), + np.array([1/np.sqrt(2), 1/np.sqrt(2)])) +} + +H_con_dict = {'II': (0.104907), + 'IZ': (0.2038683), + 'ZI': (-0.238925), + 'ZZ': (0.2386317), + 'IX': (0.1534837), + 'IY': (0.1503439), + 'ZX': (0.0679678), + 'ZY': (0.2538080), + 'XI': (0.0994848), + 'XZ': (-0.044597), + 'YI': (-0.274103), + 'YZ': (-0.078968), + 'XX': (-0.292164), + 'XY': (-0.183966), + 'YX': (-0.058251), + 'YY': (-0.212114) + } + +def test_init_contextual_input(): + """ + Check error is thrown if a contextual operator symplectic matrix is used as input to NoncontextualOp + Returns: + + """ + H_con = PauliwordOp.from_dictionary(H_con_dict) + symp_matrix = H_con.symp_matrix + coeff_vec = H_con.coeff_vec + with pytest.raises(AssertionError): + NoncontextualOp(symp_matrix, coeff_vec) + + +def test_init_noncontextual_input(): + """ + Check error is thrown if a contextual operator symplectic matrix is used as input to NoncontextualOp + Returns: + + """ + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + + symp_matrix = H_noncon.symp_matrix + coeff_vec = H_noncon.coeff_vec + noncon_op = NoncontextualOp(symp_matrix, coeff_vec) + assert noncon_op.is_noncontextual + + +#################################### +# Testing noncontextual optimizers # +#################################### + +def test_solve_brute_force_discrete_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='brute_force', + ref_state=None, + discrete_optimization_order=None, + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +# def test_solve_binary_relaxation_no_ref(): +# H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) +# H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) +# +# H_noncon_op.solve(strategy='binary_relaxation', +# ref_state=None, +# discrete_optimization_order=None, +# num_anneals=None) +# assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_brute_force_PUSO_discrete_first_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='brute_force_PUSO', + ref_state=None, + discrete_optimization_order='first', # <- HERE + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_brute_force_PUSO_discrete_last_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='brute_force_PUSO', + ref_state=None, + discrete_optimization_order='last', # <- HERE + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_brute_force_QUSO_discrete_first_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='brute_force_QUSO', + ref_state=None, + discrete_optimization_order='first', # <- HERE + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_brute_force_QUSO_discrete_last_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='brute_force_QUSO', + ref_state=None, + discrete_optimization_order='last', # <- HERE + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_annealing_PUSO_discrete_first_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='annealing_PUSO', + ref_state=None, + discrete_optimization_order='first', # <- HERE + num_anneals=1_000) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_annealing_PUSO_discrete_last_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='annealing_PUSO', + ref_state=None, + discrete_optimization_order='last', # <- HERE + num_anneals=1_000) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_annealing_QUSO_discrete_first_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='annealing_QUSO', + ref_state=None, + discrete_optimization_order='first', # <- HERE + num_anneals=1_000) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_annealing_QUSO_discrete_last_no_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + H_noncon_op.solve(strategy='annealing_QUSO', + ref_state=None, + discrete_optimization_order='last', # <- HERE + num_anneals=1_000) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + +### + +def test_solve_full_reference_state(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + reference = noncon_problem['reference_state'] + + H_noncon_op.solve(strategy='brute_force', + ref_state=reference, + discrete_optimization_order=None, + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + +### + +def test_solve_brute_force_discrete_partial_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + partial_reference_state = noncon_problem['partial_reference_state'] + + H_noncon_op.solve(strategy='brute_force', + ref_state=partial_reference_state, + discrete_optimization_order=None, + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +# def test_solve_binary_relaxation_partial_ref(): +# H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) +# H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) +# +# partial_reference_state = noncon_problem['partial_reference_state'] +# +# H_noncon_op.solve(strategy='binary_relaxation', +# ref_state=partial_reference_state, +# discrete_optimization_order=None, +# num_anneals=None) +# assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_brute_force_PUSO_discrete_partial_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + partial_reference_state = noncon_problem['partial_reference_state'] + + H_noncon_op.solve(strategy='brute_force_PUSO', + ref_state=partial_reference_state, + discrete_optimization_order='first', # <- HERE + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_brute_force_QUSO_discrete_partial_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + partial_reference_state = noncon_problem['partial_reference_state'] + + H_noncon_op.solve(strategy='brute_force_QUSO', + ref_state=partial_reference_state, + discrete_optimization_order='first', # <- HERE + num_anneals=None) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_annealing_PUSO_discrete_partial_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + partial_reference_state = noncon_problem['partial_reference_state'] + + H_noncon_op.solve(strategy='annealing_PUSO', + ref_state=partial_reference_state, + discrete_optimization_order='first', # <- HERE + num_anneals=1_000) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + + +def test_solve_annealing_QUSO_discrete_partial_ref(): + H_noncon = PauliwordOp.from_dictionary(noncon_problem['H_dict']) + H_noncon_op = NoncontextualOp.from_PauliwordOp(H_noncon) + + partial_reference_state = noncon_problem['partial_reference_state'] + + H_noncon_op.solve(strategy='annealing_QUSO', + ref_state=partial_reference_state, + discrete_optimization_order='first', # <- HERE + num_anneals=1_000) + assert np.isclose(H_noncon_op.energy, noncon_problem['E']) + From 60dc05a847387d9530425dc5f2a7f1e7747b1547 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 16:35:40 +0000 Subject: [PATCH 14/17] run pytest on different python and os versions --- .github/workflows/pull_request.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index bc815118..dd4f84a2 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -39,4 +39,7 @@ jobs: poetry run isort . --check poetry run black . --check poetry run flake8 . --ignore E501,F401,W503 --count - continue-on-error: True \ No newline at end of file + continue-on-error: True + - name: Testing + run: | + poetry run pytest \ No newline at end of file From e144d1964b36e66f549f9ea15eaac7c6b0afcfd6 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 16:49:54 +0000 Subject: [PATCH 15/17] removing evolution test --- .../test_evolution_qasm_to_PauliwordOp.py | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 tests/test_evolution/test_evolution_qasm_to_PauliwordOp.py diff --git a/tests/test_evolution/test_evolution_qasm_to_PauliwordOp.py b/tests/test_evolution/test_evolution_qasm_to_PauliwordOp.py deleted file mode 100644 index 3b8955dd..00000000 --- a/tests/test_evolution/test_evolution_qasm_to_PauliwordOp.py +++ /dev/null @@ -1,21 +0,0 @@ -import pytest -import numpy as np -from symmer.evolution import qasm_to_PauliwordOp -from symmer.symplectic import QuantumState -from qiskit.circuit.library import EfficientSU2 -from qiskit.opflow import CircuitStateFn - -@pytest.mark.parametrize("n_qubits", [1,2,3,4,5,6,7]) -def test_against_qiskit(n_qubits): - - qc = EfficientSU2(n_qubits, reps=2).decompose() - qc = qc.bind_parameters(np.random.random(qc.num_parameters)) - psi1 = CircuitStateFn(qc) - - qc_pwop = qasm_to_PauliwordOp(qc.qasm(), reverse=True) - psi2 = qc_pwop * QuantumState([[0]*n_qubits]) - - assert np.all(np.isclose( - psi1.to_spmatrix().T.toarray(), psi2.to_sparse_matrix.toarray() - ) - ) \ No newline at end of file From 4e47d503d6b74a01815bec0a493e5be7e2a32e86 Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 17:28:21 +0000 Subject: [PATCH 16/17] coverage updated --- .github/workflows/testing_and_coverage.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/testing_and_coverage.yaml b/.github/workflows/testing_and_coverage.yaml index bff7ec86..f27a4a52 100644 --- a/.github/workflows/testing_and_coverage.yaml +++ b/.github/workflows/testing_and_coverage.yaml @@ -26,6 +26,7 @@ jobs: pip install codecov pip install pytest-cov pytest --cov=./ --cov-report=xml + pytest tests --cov=./symmer --cov-report=xml codecov - name: Upload coverage to Codecov uses: codecov/codecov-action@v3.1.0 From 75d91e3601718a489e0e1e8cec5f05bc6de53e5b Mon Sep 17 00:00:00 2001 From: AlexisRalli Date: Thu, 9 Mar 2023 17:52:33 +0000 Subject: [PATCH 17/17] updated codecov --- .DS_Store | Bin 10244 -> 10244 bytes .github/workflows/testing_and_coverage.yaml | 26 ++++++-------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/.DS_Store b/.DS_Store index e4345436a6ea58516e2625f16290f9202266b39f..eb6b427258a386e4fee13e0fddf94553a51bf38c 100644 GIT binary patch delta 79 zcmZn(XbG6$&nU7nU^hRb$Yve^Wk!B>h75)hhFpdmhGd3(hO*5Hg7O^fCYI(p3Z`b0 a-6WqQu^l#-NR_f~W>@&lviXS!6EgrD?-Zc` delta 72 zcmZn(XbG6$&nUbxU^hRb@MazXWya0Ef&v^IrY0sj3Z|AOlM96GCohp)&S7C>rlVkN aVGa`5JV~mQbz+0uW_E?&ESnWXnV13IL=-Im diff --git a/.github/workflows/testing_and_coverage.yaml b/.github/workflows/testing_and_coverage.yaml index f27a4a52..c9d323d2 100644 --- a/.github/workflows/testing_and_coverage.yaml +++ b/.github/workflows/testing_and_coverage.yaml @@ -1,4 +1,4 @@ -name: Testing and Coverage +name: CodeCov on: [push] @@ -7,9 +7,6 @@ jobs: name: Testing and Coverage runs-on: ubuntu-latest - #strategy: - # matrix: - # python-version: ["3.8", "3.9"] steps: - uses: actions/checkout@v2 @@ -21,17 +18,10 @@ jobs: run: | python -m pip install --upgrade pip pip install . - - name: Run tests and collect coverage - run: | - pip install codecov - pip install pytest-cov - pytest --cov=./ --cov-report=xml - pytest tests --cov=./symmer --cov-report=xml - codecov - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3.1.0 - with: - directory: ./coverage/reports/ - env_vars: OS,PYTHON - files: /home/runner/work/symmer/symmer/coverage.xml - flags: tests + - name: Generate Report + run: | + pip install coverage + pip install pytest + coverage run -m pytest + - name: Upload Coverage to Codecov + uses: codecov/codecov-action@v2