Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/mit-han-lab/torchquantum int…
Browse files Browse the repository at this point in the history
…o dev
  • Loading branch information
Hanrui-Wang committed Oct 15, 2023
2 parents c9929e0 + dd3fc64 commit d2d8b79
Show file tree
Hide file tree
Showing 58 changed files with 9,408 additions and 7,245 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/functional_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python package

on:
push:
pull_request:

jobs:
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest qiskit-aer qiskit-ibmq-provider
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest -m "not skip"
12 changes: 6 additions & 6 deletions examples/param_shift_onchip_training/param_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import torchquantum as tq
import torchquantum.functional as tqf
from torchquantum.layer import SethLayer0
from torchquantum.layer.layers import SethLayer0

from torchquantum.dataset import MNIST
from torch.optim.lr_scheduler import CosineAnnealingLR
Expand All @@ -39,7 +39,6 @@ class QFCModel(tq.QuantumModule):
def __init__(self):
super().__init__()
self.n_wires = 4
self.q_device = tq.QuantumDevice(n_wires=self.n_wires)
self.encoder = tq.GeneralEncoder(tq.encoder_op_list_name_dict["4x4_ryzxy"])

self.arch = {"n_wires": self.n_wires, "n_blocks": 2, "n_layers_per_block": 2}
Expand All @@ -49,16 +48,17 @@ def __init__(self):

def forward(self, x, use_qiskit=False):
bsz = x.shape[0]
q_device = tq.QuantumDevice(n_wires=self.n_wires, bsz=bsz)
x = F.avg_pool2d(x, 6).view(bsz, 16)

if use_qiskit:
x = self.qiskit_processor.process_parameterized(
self.q_device, self.encoder, self.q_layer, self.measure, x
q_device, self.encoder, self.q_layer, self.measure, x
)
else:
self.encoder(self.q_device, x)
self.q_layer(self.q_device)
x = self.measure(self.q_device)
self.encoder(q_device, x)
self.q_layer(q_device)
x = self.measure(q_device)

x = x.reshape(bsz, 4)

Expand Down
10,701 changes: 5,590 additions & 5,111 deletions examples/param_shift_onchip_training/param_shift_onchip_training.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ opt_einsum
pathos>=0.2.7
pylatexenc>=2.10
pyscf>=2.0.1
qiskit==0.38.0
qiskit>=0.39.0
recommonmark

scipy>=1.5.2
Expand Down
1 change: 0 additions & 1 deletion test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@
"""

from .utils import *

234 changes: 184 additions & 50 deletions test/algorithm/test_hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,74 +25,208 @@
from torchquantum.algorithm import Hamiltonian
import numpy as np


def test_hamiltonian():
coeffs = [1.0, 1.0]
paulis = ["ZZ", "ZX"]
hamil = Hamiltonian(coeffs, paulis)
assert np.allclose(
hamil.get_matrix().cpu().detach().numpy(),
hamil.get_matrix().cpu().detach().numpy(),
np.array(
[[ 1.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
[ 1.+0.j, -1.+0.j, 0.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j, -1.+0.j, -1.+0.j],
[ 0.+0.j, 0.+0.j, -1.+0.j, 1.+0.j]]))

[
[1.0 + 0.0j, 1.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j],
[1.0 + 0.0j, -1.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j],
[0.0 + 0.0j, 0.0 + 0.0j, -1.0 + 0.0j, -1.0 + 0.0j],
[0.0 + 0.0j, 0.0 + 0.0j, -1.0 + 0.0j, 1.0 + 0.0j],
]
),
)

coeffs = [0.6]
paulis = ["XXZ"]
hamil = Hamiltonian(coeffs, paulis)
assert np.allclose(
hamil.get_matrix().cpu().detach().numpy(),
hamil.get_matrix().cpu().detach().numpy(),
np.array(
[[ 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j,
0.6000+0.j, 0.0000+0.j],
[ 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.0000+0.j,
0.0000+0.j, -0.6000+0.j],
[ 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.6000+0.j, 0.0000+0.j,
0.0000+0.j, 0.0000+0.j],
[ 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.6000+0.j,
0.0000+0.j, -0.0000+0.j],
[ 0.0000+0.j, 0.0000+0.j, 0.6000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j,
0.0000+0.j, 0.0000+0.j],
[ 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.6000+0.j, 0.0000+0.j, -0.0000+0.j,
0.0000+0.j, -0.0000+0.j],
[ 0.6000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j, 0.0000+0.j,
0.0000+0.j, 0.0000+0.j],
[ 0.0000+0.j, -0.6000+0.j, 0.0000+0.j, -0.0000+0.j, 0.0000+0.j, -0.0000+0.j,
0.0000+0.j, -0.0000+0.j]]))

[
[
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.6000 + 0.0j,
0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.6000 + 0.0j,
],
[
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.6000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.6000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
0.0000 + 0.0j,
0.6000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.6000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
],
[
0.6000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
0.0000 + 0.0j,
],
[
0.0000 + 0.0j,
-0.6000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
0.0000 + 0.0j,
-0.0000 + 0.0j,
],
]
),
)

hamil = Hamiltonian.from_file("test/algorithm/h2.txt")

assert np.allclose(
hamil.matrix.cpu().detach().numpy(),
hamil.matrix.cpu().detach().numpy(),
np.array(
[[-1.0636533 +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j,
0.1809312 +0.j, 0. +0.j],
[ 0. +0.j, -1.0636533 +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0.1809312 +0.j],
[ 0. +0.j, 0. +0.j, -1.8369681 +0.j,
0. +0.j, 0.1809312 +0.j, 0. +0.j,
0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j,
-1.8369681 +0.j, 0. +0.j, 0.1809312 +0.j,
0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0.1809312 +0.j,
0. +0.j, -0.24521835+0.j, 0. +0.j,
0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j,
0.1809312 +0.j, 0. +0.j, -0.24521835+0.j,
0. +0.j, 0. +0.j],
[ 0.1809312 +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j,
-1.0636533 +0.j, 0. +0.j],
[ 0. +0.j, 0.1809312 +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, -1.0636533 +0.j]]))
[
[
-1.0636533 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
-1.0636533 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
-1.8369681 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.8369681 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
-0.24521835 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
-0.24521835 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.1809312 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.0636533 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.1809312 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.0636533 + 0.0j,
],
]
),
)
print("hamiltonian test passed!")

if __name__ == '__main__':

if __name__ == "__main__":
import pdb

pdb.set_trace()
test_hamiltonian()
Loading

0 comments on commit d2d8b79

Please sign in to comment.