From a5325753e75672acf697c0a03664c2d4f29bc1cc Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Sun, 15 Oct 2023 19:01:29 -0400 Subject: [PATCH 01/11] running example to test functionality --- .github/workflows/example_tests.yaml | 32 +++++++++++++++++++++++++ .github/workflows/functional_tests.yaml | 3 +++ 2 files changed, 35 insertions(+) create mode 100644 .github/workflows/example_tests.yaml diff --git a/.github/workflows/example_tests.yaml b/.github/workflows/example_tests.yaml new file mode 100644 index 00000000..d8db7db3 --- /dev/null +++ b/.github/workflows/example_tests.yaml @@ -0,0 +1,32 @@ +# 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 diff --git a/.github/workflows/functional_tests.yaml b/.github/workflows/functional_tests.yaml index af549120..ed42b3e5 100644 --- a/.github/workflows/functional_tests.yaml +++ b/.github/workflows/functional_tests.yaml @@ -36,3 +36,6 @@ jobs: - name: Test with pytest run: | pytest -m "not skip" + - name: Test Examples + run: | + python3 examples/qubit_rotation/qubit_rotation.py From 1d7fece886aac260f5db4c3db224845f166cfb65 Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Sun, 15 Oct 2023 19:09:43 -0400 Subject: [PATCH 02/11] fixing attempt --- .github/workflows/functional_tests.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/functional_tests.yaml b/.github/workflows/functional_tests.yaml index ed42b3e5..9b26ef72 100644 --- a/.github/workflows/functional_tests.yaml +++ b/.github/workflows/functional_tests.yaml @@ -36,6 +36,9 @@ 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 From 931233743c01577ba67efabc15cd4166e3d432db Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 24 Nov 2023 12:45:34 -0500 Subject: [PATCH 03/11] added tests and slight modification to rotation --- .github/workflows/example_tests.yaml | 15 ++++++++++++++- examples/qubit_rotation/qubit_rotation.py | 14 +++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/.github/workflows/example_tests.yaml b/.github/workflows/example_tests.yaml index d8db7db3..6cf931f4 100644 --- a/.github/workflows/example_tests.yaml +++ b/.github/workflows/example_tests.yaml @@ -29,4 +29,17 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test Examples run: | - python3 examples/qubit_rotation/qubit_rotation.py + 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 diff --git a/examples/qubit_rotation/qubit_rotation.py b/examples/qubit_rotation/qubit_rotation.py index be2dfc82..bae1e803 100644 --- a/examples/qubit_rotation/qubit_rotation.py +++ b/examples/qubit_rotation/qubit_rotation.py @@ -6,6 +6,7 @@ import torchquantum as tq import torch from torchquantum.measurement import expval_joint_analytical +import argparse class OptimizationModel(torch.nn.Module): @@ -42,7 +43,7 @@ def train(model, device, optimizer): # main function to run the optimization -def main(): +def main(n_epochs): seed = 0 torch.manual_seed(seed) @@ -50,7 +51,6 @@ def main(): 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): @@ -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) From beef18260daa53263cb20ba032cce1075873a3a9 Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 24 Nov 2023 15:57:53 -0500 Subject: [PATCH 04/11] [minor] fix path to h2.txt file for vqe --- examples/vqe/new_simple_vqe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/vqe/new_simple_vqe.py b/examples/vqe/new_simple_vqe.py index b5a83953..31cab355 100644 --- a/examples/vqe/new_simple_vqe.py +++ b/examples/vqe/new_simple_vqe.py @@ -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}, From 12a552ae1bf397a799fc9248fa03887081a44d13 Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 24 Nov 2023 16:07:10 -0500 Subject: [PATCH 05/11] [minor] updated to fully handle not having qiskit --- .github/workflows/example_tests.yaml | 1 + examples/regression/new_run_regression.py | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/example_tests.yaml b/.github/workflows/example_tests.yaml index 6cf931f4..e5f7425c 100644 --- a/.github/workflows/example_tests.yaml +++ b/.github/workflows/example_tests.yaml @@ -43,3 +43,4 @@ jobs: 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 diff --git a/examples/regression/new_run_regression.py b/examples/regression/new_run_regression.py index fdeb5cd4..30fc3aa7 100644 --- a/examples/regression/new_run_regression.py +++ b/examples/regression/new_run_regression.py @@ -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) except: pass - # final valid - valid_test(dataflow, q_device, "valid", model, device, True) - if __name__ == "__main__": main() From 4565221f9b2e32f349d3d269c913c0008b501217 Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 24 Nov 2023 16:28:15 -0500 Subject: [PATCH 06/11] [minor] added a missing layer --- .github/workflows/example_tests.yaml | 1 + torchquantum/layer/layers.py | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/example_tests.yaml b/.github/workflows/example_tests.yaml index e5f7425c..580e7f14 100644 --- a/.github/workflows/example_tests.yaml +++ b/.github/workflows/example_tests.yaml @@ -44,3 +44,4 @@ jobs: 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 diff --git a/torchquantum/layer/layers.py b/torchquantum/layer/layers.py index 9f129acc..c7be23c4 100644 --- a/torchquantum/layer/layers.py +++ b/torchquantum/layer/layers.py @@ -51,6 +51,7 @@ "CXCXCXLayer", "SWAPSWAPLayer", "RXYZCXLayer0", + "U3CU3Layer0", "QFTLayer", ] From 83345b4d133d73cea760464f28870d31d1efdf8d Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 24 Nov 2023 16:34:35 -0500 Subject: [PATCH 07/11] [minor] fixed grover example dependency --- .github/workflows/example_tests.yaml | 1 + examples/grover/grover_example_sudoku.py | 4 ++-- torchquantum/algorithm/__init__.py | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/example_tests.yaml b/.github/workflows/example_tests.yaml index 580e7f14..9ee0ea2e 100644 --- a/.github/workflows/example_tests.yaml +++ b/.github/workflows/example_tests.yaml @@ -45,3 +45,4 @@ jobs: 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 diff --git a/examples/grover/grover_example_sudoku.py b/examples/grover/grover_example_sudoku.py index 25761594..4969eea2 100644 --- a/examples/grover/grover_example_sudoku.py +++ b/examples/grover/grover_example_sudoku.py @@ -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. @@ -90,4 +90,4 @@ def XOR(input0, input1, output): print("b = ", key[1]) print("c = ", key[2]) print("d = ", key[3]) - print("") \ No newline at end of file + print("") diff --git a/torchquantum/algorithm/__init__.py b/torchquantum/algorithm/__init__.py index 623d71b7..7dfb672a 100644 --- a/torchquantum/algorithm/__init__.py +++ b/torchquantum/algorithm/__init__.py @@ -25,3 +25,4 @@ from .vqe import * from .hamiltonian import * from .qft import * +from .grover import * From 16376803f5be001983dbb4358d9ef3793a1dc9b4 Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 24 Nov 2023 16:44:51 -0500 Subject: [PATCH 08/11] [minor] adding imports for pulse --- examples/optimal_control/optimal_control.py | 2 +- examples/optimal_control/optimal_control_gaussian.py | 2 +- examples/optimal_control/optimal_control_multi_qubit.py | 6 +++--- torchquantum/__init__.py | 1 + torchquantum/pulse/__init__.py | 1 + 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/optimal_control/optimal_control.py b/examples/optimal_control/optimal_control.py index 438e135b..89601153 100644 --- a/examples/optimal_control/optimal_control.py +++ b/examples/optimal_control/optimal_control.py @@ -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) diff --git a/examples/optimal_control/optimal_control_gaussian.py b/examples/optimal_control/optimal_control_gaussian.py index 861cc92a..559e6127 100644 --- a/examples/optimal_control/optimal_control_gaussian.py +++ b/examples/optimal_control/optimal_control_gaussian.py @@ -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) diff --git a/examples/optimal_control/optimal_control_multi_qubit.py b/examples/optimal_control/optimal_control_multi_qubit.py index 023d4f3c..148b526c 100644 --- a/examples/optimal_control/optimal_control_multi_qubit.py +++ b/examples/optimal_control/optimal_control_multi_qubit.py @@ -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], diff --git a/torchquantum/__init__.py b/torchquantum/__init__.py index c8aed9ed..b1529623 100644 --- a/torchquantum/__init__.py +++ b/torchquantum/__init__.py @@ -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 diff --git a/torchquantum/pulse/__init__.py b/torchquantum/pulse/__init__.py index 5a4539de..d281a73f 100644 --- a/torchquantum/pulse/__init__.py +++ b/torchquantum/pulse/__init__.py @@ -25,4 +25,5 @@ from .utils import * from .sesolve import sesolve from .mesolve import mesolve +from .pulses import * # from .smesolve import smesolve From 60b055f0ff96c38488f97cff66a93e7cf9041afc Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 24 Nov 2023 16:47:19 -0500 Subject: [PATCH 09/11] [minor] fixed typo in save_load --- examples/save_load_example/save_load.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/save_load_example/save_load.py b/examples/save_load_example/save_load.py index 1022c3aa..c5a6c57a 100644 --- a/examples/save_load_example/save_load.py +++ b/examples/save_load_example/save_load.py @@ -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) From 52391de372fa358137010a0574d8d087e8ffb394 Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 24 Nov 2023 17:15:05 -0500 Subject: [PATCH 10/11] [minor] added test + fixed layer import --- .github/workflows/example_tests.yaml | 1 + torchquantum/layer/layers.py | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/example_tests.yaml b/.github/workflows/example_tests.yaml index 9ee0ea2e..8d0f1df6 100644 --- a/.github/workflows/example_tests.yaml +++ b/.github/workflows/example_tests.yaml @@ -46,3 +46,4 @@ jobs: 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 diff --git a/torchquantum/layer/layers.py b/torchquantum/layer/layers.py index c7be23c4..9d4e5f61 100644 --- a/torchquantum/layer/layers.py +++ b/torchquantum/layer/layers.py @@ -53,6 +53,7 @@ "RXYZCXLayer0", "U3CU3Layer0", "QFTLayer", + "SethLayer0", ] From 15bfb8c67a195f57a4283fe00b55c2f9a2ef4f3d Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Fri, 1 Dec 2023 21:04:31 -0500 Subject: [PATCH 11/11] moved the test locations --- .github/workflows/example_tests.yaml | 49 ------------------------- .github/workflows/functional_tests.yaml | 19 +++++++++- 2 files changed, 18 insertions(+), 50 deletions(-) delete mode 100644 .github/workflows/example_tests.yaml diff --git a/.github/workflows/example_tests.yaml b/.github/workflows/example_tests.yaml deleted file mode 100644 index 8d0f1df6..00000000 --- a/.github/workflows/example_tests.yaml +++ /dev/null @@ -1,49 +0,0 @@ -# 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 diff --git a/.github/workflows/functional_tests.yaml b/.github/workflows/functional_tests.yaml index 9b26ef72..7b9dfb50 100644 --- a/.github/workflows/functional_tests.yaml +++ b/.github/workflows/functional_tests.yaml @@ -41,4 +41,21 @@ jobs: pip install --editable . - name: Test Examples run: | - python3 examples/qubit_rotation/qubit_rotation.py + 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