Skip to content

Commit

Permalink
Merge branch 'dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
01110011011101010110010001101111 authored Sep 15, 2023
2 parents 566e1ff + 8db9260 commit 7ac5fa3
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 32 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ print(tq.measure(x, n_shots=2048))

We also prepare many example and tutorials using TorchQuantum.

For **beginning level**, you may check [QNN for MNIST](examples/simple_mnist), [Quantum Convolution (Quanvolution)](examples/quanvolution) and [Quantum Kernel Method](examples/quantum_kernel_method), and [Quantum Regression](examples/regression).
For **beginning level**, you may check [QNN for MNIST](examples/mnist), [Quantum Convolution (Quanvolution)](examples/quanvolution) and [Quantum Kernel Method](examples/quantum_kernel_method), and [Quantum Regression](examples/regression).

For **intermediate level**, you may check [Amplitude Encoding for MNIST](examples/amplitude_encoding_mnist), [Clifford gate QNN](examples/clifford_qnn), [Save and Load QNN models](examples/save_load_example), [PauliSum Operation](examples/PauliSumOp), [How to convert tq to Qiskit](examples/converter_tq_qiskit).

For **expert**, you may check [Parameter Shift on-chip Training](examples/param_shift_onchip_training), [VQA Gradient Pruning](examples/gradient_pruning), [VQE](examples/simple_vqe), [VQA for State Prepration](examples/train_state_prep), [QAOA (Quantum Approximate Optimization Algorithm)](examples/qaoa).
For **expert**, you may check [Parameter Shift on-chip Training](examples/param_shift_onchip_training), [VQA Gradient Pruning](examples/gradient_pruning), [VQE](examples/vqe), [VQA for State Prepration](examples/train_state_prep), [QAOA (Quantum Approximate Optimization Algorithm)](examples/qaoa).


## Usage
Expand Down Expand Up @@ -238,8 +238,8 @@ Train a quantum circuit to perform VQE task.
Quito quantum computer as in [simple_vqe.py](./examples/simple_vqe/simple_vqe.py)
script:
```python
cd examples/simple_vqe
python simple_vqe.py
cd examples/vqe
python vqe.py
```

## MNIST Example
Expand All @@ -248,8 +248,8 @@ Train a quantum circuit to perform MNIST classification task and deploy on the r
Quito quantum computer as in [mnist_example.py](./examples/simple_mnist/mnist_example_no_binding.py)
script:
```python
cd examples/simple_mnist
python mnist_example.py
cd examples/mnist
python mnist.py
```

## Files
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def forward(self, qdev: tq.QuantumDevice):
def __init__(self):
super().__init__()
self.n_wires = 4
self.encoder = tq.GeneralEncoder(tq.encoder_op_list_name_dict["4x4_u3rx"])
self.encoder = tq.GeneralEncoder(tq.encoder_op_list_name_dict["4x4_u3_h_rx"])

self.q_layer = self.QLayer()
self.measure = tq.MeasureAll(tq.PauliZ)
Expand Down
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
5 changes: 3 additions & 2 deletions test/hadamard_grad/test_hadamard_grad.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from examples.hadamard_grad.circ import Circ1, Circ2, Circ3
from examples.hadamard_grad.hadamard_grad import hadamard_grad
import pytest


@pytest.mark.skip
def test_hadamard_grad():
"""
We assume the circuits have unique and ordered parameters for now.
Expand Down Expand Up @@ -37,4 +38,4 @@ def test_hadamard_grad():


if __name__ == "__main__":
test_hadamard_grad()
test_hadamard_grad()
3 changes: 2 additions & 1 deletion test/plugin/test_qiskit_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from torchquantum.util import switch_little_big_endian_state

import torch
import pytest

pauli_str_op_dict = {
"X": X,
Expand All @@ -41,7 +42,7 @@
"I": I,
}


@pytest.mark.skip
def test_expval_observable():
# seed = 0
# random.seed(seed)
Expand Down
5 changes: 2 additions & 3 deletions torchquantum/density/density_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
__all__ = [
"func_name_dict",
"mat_dict",
"apply_unitary_einsum",
"apply_unitary_bmm",
"apply_unitary_density_einsum",
"apply_unitary_density_bmm",
"hadamard",
"Dhadamard",
"shadamard",
"paulix",
"pauliy",
Expand Down
14 changes: 14 additions & 0 deletions torchquantum/encoding/encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,20 @@ def __init__(self):
{"input_idx": [12, 13, 14], "func": "u3", "wires": [3]},
{"input_idx": [15], "func": "rx", "wires": [3]},
],
"4x4_u3_h_rx": [
{"input_idx": [0, 1, 2], "func": "u3", "wires": [0]},
{"input_idx": [3], "func": "rx", "wires": [0]},
{"func": "h", "wires": [0]},
{"func": "h", "wires": [1]},
{"func": "h", "wires": [2]},
{"func": "h", "wires": [3]},
{"input_idx": [4, 5, 6], "func": "u3", "wires": [1]},
{"input_idx": [7], "func": "rx", "wires": [1]},
{"input_idx": [8, 9, 10], "func": "u3", "wires": [2]},
{"input_idx": [11], "func": "rx", "wires": [2]},
{"input_idx": [12, 13, 14], "func": "u3", "wires": [3]},
{"input_idx": [15], "func": "rx", "wires": [3]},
],
"4x4_ryzxy": [
{"input_idx": [0], "func": "ry", "wires": [0]},
{"input_idx": [1], "func": "ry", "wires": [1]},
Expand Down
17 changes: 11 additions & 6 deletions torchquantum/plugin/qiskit/qiskit_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def append_fixed_gate(circ, func, params, wires, inverse):
circ.swap(*wires)
elif func == "sswap":
# square root of swap
from torchquantum.plugin.qiskit_unitary_gate import UnitaryGate
from torchquantum.plugin.qiskit.qiskit_unitary_gate import UnitaryGate

mat = mat_dict["sswap"].detach().cpu().numpy()
mat = switch_little_big_endian_matrix(mat)
Expand Down Expand Up @@ -308,7 +308,7 @@ def append_fixed_gate(circ, func, params, wires, inverse):
or func == "qubitunitaryfast"
or func == "qubitunitarystrict"
):
from torchquantum.plugin.qiskit_unitary_gate import UnitaryGate
from torchquantum.plugin.qiskit.qiskit_unitary_gate import UnitaryGate

mat = np.array(params)
mat = switch_little_big_endian_matrix(mat)
Expand Down Expand Up @@ -512,7 +512,7 @@ def tq2qiskit(
circ.swap(*module.wires)
elif module.name == "SSWAP":
# square root of swap
from torchquantum.plugin.qiskit_unitary_gate import UnitaryGate
from torchquantum.plugin.qiskit.qiskit_unitary_gate import UnitaryGate

mat = module.matrix.data.cpu().numpy()
mat = switch_little_big_endian_matrix(mat)
Expand Down Expand Up @@ -547,7 +547,7 @@ def tq2qiskit(
or module.name == "TrainableUnitary"
or module.name == "TrainableUnitaryStrict"
):
from torchquantum.plugin.qiskit_unitary_gate import UnitaryGate
from torchquantum.plugin.qiskit.qiskit_unitary_gate import UnitaryGate

mat = module.params[0].data.cpu().numpy()
mat = switch_little_big_endian_matrix(mat)
Expand Down Expand Up @@ -661,10 +661,15 @@ def op_history2qiskit_expand_params(n_wires, op_history, bsz):
for i in range(bsz):
circ = QuantumCircuit(n_wires)
for op in op_history:
if "params" in op.keys() and op["params"] is not None:
param = op["params"][i]
else:
param = None

append_fixed_gate(
circ, op["name"], op["params"][i], op["wires"], op["inverse"]
circ, op["name"], param, op["wires"], op["inverse"]
)

circs_all.append(circ)

return circs_all
Expand Down
1 change: 0 additions & 1 deletion torchquantum/pulse/hardware/__init__.py

This file was deleted.

11 changes: 0 additions & 11 deletions torchquantum/pulse/hardware/hardware.py

This file was deleted.

0 comments on commit 7ac5fa3

Please sign in to comment.