Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verifying Example Functionality in GitHub Actions #206

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/example_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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: Test Examples
run: |
python3 examples/qubit_rotation/qubit_rotation.py --epochs 1
python3 examples/vqe/vqe.py --epochs 1 --steps_per_epoch 1
python3 examples/train_unitary_prep/train_unitary_prep.py --epochs 1
python3 examples/train_state_prep/train_state_prep.py --epochs 1
python3 examples/superdense_coding/superdense_coding_torchquantum.py
python3 examples/regression/run_regression.py --epochs 1
python3 examples/param_shift_onchip_training/param_shift.py
python3 examples/mnist/mnist_2qubit_4class.py --epochs 1
python3 examples/hadamard_grad/circ.py
python3 examples/encoder_examples/encoder_8x2ry.py
python3 examples/converter_tq_qiskit/convert.py
python3 examples/amplitude_encoding_mnist/mnist_new.py --epochs 1
python3 examples/amplitude_encoding_mnist/mnist_example.py --epochs 1
python3 examples/PauliSumOp/pauli_sum_op.py
python3 examples/regression/new_run_regression.py --epochs 1
python3 examples/quanvolution/quanvolution_trainable_quantum_layer.py --epochs 1
python3 examples/grover/grover_example_sudoku.py
python3 examples/param_shift_onchip_training/param_shift.py
23 changes: 23 additions & 0 deletions .github/workflows/functional_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,26 @@ jobs:
- name: Test with pytest
run: |
pytest -m "not skip"
- name: Install TorchQuantum
run: |
pip install --editable .
- name: Test Examples
run: |
python3 examples/qubit_rotation/qubit_rotation.py --epochs 1
python3 examples/vqe/vqe.py --epochs 1 --steps_per_epoch 1
python3 examples/train_unitary_prep/train_unitary_prep.py --epochs 1
python3 examples/train_state_prep/train_state_prep.py --epochs 1
python3 examples/superdense_coding/superdense_coding_torchquantum.py
python3 examples/regression/run_regression.py --epochs 1
python3 examples/param_shift_onchip_training/param_shift.py
python3 examples/mnist/mnist_2qubit_4class.py --epochs 1
python3 examples/hadamard_grad/circ.py
python3 examples/encoder_examples/encoder_8x2ry.py
python3 examples/converter_tq_qiskit/convert.py
python3 examples/amplitude_encoding_mnist/mnist_new.py --epochs 1
python3 examples/amplitude_encoding_mnist/mnist_example.py --epochs 1
python3 examples/PauliSumOp/pauli_sum_op.py
python3 examples/regression/new_run_regression.py --epochs 1
python3 examples/quanvolution/quanvolution_trainable_quantum_layer.py --epochs 1
python3 examples/grover/grover_example_sudoku.py
python3 examples/param_shift_onchip_training/param_shift.py
4 changes: 2 additions & 2 deletions examples/grover/grover_example_sudoku.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"""

import torchquantum as tq
from torchquantum.algorithms import Grover
from torchquantum.algorithm import Grover


# To simplify the process, we can compile this set of comparisons into a list of clauses for convenience.
Expand Down Expand Up @@ -90,4 +90,4 @@ def XOR(input0, input1, output):
print("b = ", key[1])
print("c = ", key[2])
print("d = ", key[3])
print("")
print("")
2 changes: 1 addition & 1 deletion examples/optimal_control/optimal_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
dtype=torch.complex64,
)

pulse = tq.QuantumPulseDirect(n_steps=4, hamil=[[0, 1], [1, 0]])
pulse = tq.pulse.QuantumPulseDirect(n_steps=4, hamil=[[0, 1], [1, 0]])

optimizer = optim.Adam(params=pulse.parameters(), lr=5e-3)

Expand Down
2 changes: 1 addition & 1 deletion examples/optimal_control/optimal_control_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
dtype=torch.complex64,
)

pulse = tq.QuantumPulseGaussian(hamil=[[0, 1], [1, 0]])
pulse = tq.pulse.QuantumPulseGaussian(hamil=[[0, 1], [1, 0]])

optimizer = optim.Adam(params=pulse.parameters(), lr=5e-3)

Expand Down
6 changes: 3 additions & 3 deletions examples/optimal_control/optimal_control_multi_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
dtype=torch.complex64,
)

pulse_q0 = tq.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
pulse_q1 = tq.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
pulse_q01 = tq.QuantumPulseDirect(
pulse_q0 = tq.pulse.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
pulse_q1 = tq.pulse.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
pulse_q01 = tq.pulse.QuantumPulseDirect(
n_steps=10,
hamil=[
[1, 0, 0, 0],
Expand Down
14 changes: 11 additions & 3 deletions examples/qubit_rotation/qubit_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import torchquantum as tq
import torch
from torchquantum.measurement import expval_joint_analytical
import argparse


class OptimizationModel(torch.nn.Module):
Expand Down Expand Up @@ -42,15 +43,14 @@ def train(model, device, optimizer):


# main function to run the optimization
def main():
def main(n_epochs):
seed = 0
torch.manual_seed(seed)

use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

model = OptimizationModel()
n_epochs = 200
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

for epoch in range(1, n_epochs + 1):
Expand All @@ -65,4 +65,12 @@ def main():


if __name__ == "__main__":
main()
parser = argparse.ArgumentParser(
prog="Qubit Rotation",
description="Specify Parameters for Qubit Rotation Optimization Example",
)
parser.add_argument(
"--epochs", type=int, default=200, help="number of training epochs"
)
args = parser.parse_args()
main(args.epochs)
5 changes: 2 additions & 3 deletions examples/regression/new_run_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,11 @@ def main():
model.set_qiskit_processor(processor_simulation)
valid_test(dataflow, q_device, "test", model, device, qiskit=True)

# final valid
valid_test(dataflow, q_device, "valid", model, device, True)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: originally, this would raise an error since outside of the try/except, qiskit hasn’t been imported. I made the choice to move it inside the try so we don’t have to worry about the qiskit dependency if a user just wants to run it with pure TQ.

except:
pass

# final valid
valid_test(dataflow, q_device, "valid", model, device, True)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion examples/save_load_example/save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def save_load3():
# print(model.q_layer.rx0._parameters)

traced_cell = torch.jit.trace(model, (x))
torch.jit.save(traced_cell, "model_trace.pth")
torch.jit.save(traced_cell, "model_trace.pt")

loaded_trace = torch.jit.load("model_trace.pt")
y2 = loaded_trace(x)
Expand Down
2 changes: 1 addition & 1 deletion examples/vqe/new_simple_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from torchquantum.plugin import qiskit2tq_op_history

if __name__ == "__main__":
hamil = Hamiltonian.from_file("./examples/simple_vqe/h2.txt")
hamil = Hamiltonian.from_file("./examples/vqe/h2.txt")

ops = [
{'name': 'u3', 'wires': 0, 'trainable': True},
Expand Down
1 change: 1 addition & 0 deletions torchquantum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from .noise_model import *
from .algorithm import *
from .dataset import *
from .pulse import *

# here we check whether the Qiskit parameterization bug is fixed, if not, a
# warning message will be printed
Expand Down
1 change: 1 addition & 0 deletions torchquantum/algorithm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
from .vqe import *
from .hamiltonian import *
from .qft import *
from .grover import *
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main fix is just adding this import; it must’ve gotten lost somehow when merging everything.

3 changes: 3 additions & 0 deletions torchquantum/layer/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@
"CXCXCXLayer",
"SWAPSWAPLayer",
"RXYZCXLayer0",
"U3CU3Layer0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that all in layers.py appears not to have all of the layers which was causing errors for one of the examples. I fixed the layer needed for the example but haven’t added all the layers yet since we will reformat the layers shortly and that can be done then. For now, just an FYI that not all the layers are in all at the moment.

"QFTLayer",
"SethLayer0",
"EntangleLinear",
"EntanglePairwise",
"EntangleFull",
"EntangleCircular",
"EntanglementLayer",
"SethLayer0",
]


Expand Down
1 change: 1 addition & 0 deletions torchquantum/pulse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
from .utils import *
from .sesolve import sesolve
from .mesolve import mesolve
from .pulses import *
# from .smesolve import smesolve
Loading