diff --git a/qiskit_neko/tests/machine_learning/test_neural_network_classifier.py b/qiskit_neko/tests/machine_learning/test_neural_network_classifier.py index 914613a..2f7d608 100644 --- a/qiskit_neko/tests/machine_learning/test_neural_network_classifier.py +++ b/qiskit_neko/tests/machine_learning/test_neural_network_classifier.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2022. +# (C) Copyright IBM 2022, 2023. # # This code is licensed under the Apache License, Version 2.0. You may # obtain a copy of this license in the LICENSE.txt file in the root directory @@ -13,10 +13,13 @@ """Tests for quantum neural networks classifier.""" import numpy as np from ddt import ddt, data -from qiskit_algorithms.optimizers import COBYLA -from qiskit_algorithms.utils import algorithm_globals + from qiskit.primitives import Sampler as ReferenceSampler from qiskit_aer.primitives import Sampler as AerSampler + +from qiskit_algorithms.optimizers import COBYLA +from qiskit_algorithms.utils import algorithm_globals + from qiskit_machine_learning.algorithms.classifiers import VQC from qiskit_neko import decorators diff --git a/qiskit_neko/tests/machine_learning/test_neural_networks.py b/qiskit_neko/tests/machine_learning/test_neural_networks.py deleted file mode 100644 index d53a53b..0000000 --- a/qiskit_neko/tests/machine_learning/test_neural_networks.py +++ /dev/null @@ -1,64 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2022. -# -# This code is licensed under the Apache License, Version 2.0. You may -# obtain a copy of this license in the LICENSE.txt file in the root directory -# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. -# -# Any modifications or derivative works of this code must retain this -# copyright notice, and modified files need to carry a notice indicating -# that they have been altered from the originals. - -"""Tests for quantum neural networks.""" - -import numpy as np - -from qiskit import QuantumCircuit -from qiskit.circuit import Parameter -from qiskit.opflow import StateFn, PauliSumOp, PauliExpectation, Gradient -from qiskit.utils import QuantumInstance - -from qiskit_machine_learning.neural_networks import OpflowQNN - -from qiskit_neko.tests import base -from qiskit_neko import decorators - - -class TestNeuralNetworks(base.BaseTestCase): - """Test adapted from the qiskit_machine_learning tutorials.""" - - def setUp(self): - super().setUp() - if hasattr(self.backend.options, "seed_simulator"): - self.backend.set_options(seed_simulator=42) - - @decorators.component_attr("terra", "backend", "machine_learning") - def test_neural_networks(self): - """Test the execution of quantum neural networks using OpflowQNN""" - - expval = PauliExpectation() - gradient = Gradient() - qi_sv = QuantumInstance(self.backend) - - params1 = [Parameter("input1"), Parameter("weight1")] - qc1 = QuantumCircuit(1) - qc1.h(0) - qc1.ry(params1[0], 0) - qc1.rx(params1[1], 0) - qc_sfn1 = StateFn(qc1) - - h1 = StateFn(PauliSumOp.from_list([("Z", 1.0), ("X", 1.0)])) - op1 = ~h1 @ qc_sfn1 - - qnn1 = OpflowQNN(op1, [params1[0]], [params1[1]], expval, gradient, qi_sv) - - rng = np.random.default_rng(seed=42) - input1 = rng.random(size=qnn1.num_inputs) - weights1 = rng.random(size=qnn1.num_weights) - - qnn1_forward = qnn1.forward(input1, weights1) - qnn1_backward = qnn1.backward(input1, weights1) - - self.assertAlmostEqual(qnn1_forward[0][0], 0.08242345, delta=0.1) - self.assertAlmostEqual(qnn1_backward[1][0][0], [0.2970094], delta=0.1) diff --git a/qiskit_neko/tests/machine_learning/test_neural_networks_primitives.py b/qiskit_neko/tests/machine_learning/test_neural_networks_primitives.py index 8652c62..4f0c4d7 100644 --- a/qiskit_neko/tests/machine_learning/test_neural_networks_primitives.py +++ b/qiskit_neko/tests/machine_learning/test_neural_networks_primitives.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2022. +# (C) Copyright IBM 2022, 2023. # # This code is licensed under the Apache License, Version 2.0. You may # obtain a copy of this license in the LICENSE.txt file in the root directory @@ -18,6 +18,7 @@ from qiskit.circuit import Parameter from qiskit.primitives import Sampler as ReferenceSampler, Estimator as ReferenceEstimator from qiskit.quantum_info import SparsePauliOp + from qiskit_aer.primitives import Sampler as AerSampler, Estimator as AerEstimator from qiskit_machine_learning.neural_networks import SamplerQNN, EstimatorQNN diff --git a/qiskit_neko/tests/machine_learning/test_neural_networks_twolayer.py b/qiskit_neko/tests/machine_learning/test_neural_networks_twolayer.py deleted file mode 100644 index cb1a4e8..0000000 --- a/qiskit_neko/tests/machine_learning/test_neural_networks_twolayer.py +++ /dev/null @@ -1,70 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2022. -# -# This code is licensed under the Apache License, Version 2.0. You may -# obtain a copy of this license in the LICENSE.txt file in the root directory -# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. -# -# Any modifications or derivative works of this code must retain this -# copyright notice, and modified files need to carry a notice indicating -# that they have been altered from the originals. - -"""Tests for quantum neural networks Two Layer QNN.""" - -import numpy as np - -from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap -from qiskit.opflow import PauliSumOp -from qiskit.utils import QuantumInstance - -from qiskit_machine_learning.neural_networks import TwoLayerQNN - -from qiskit_neko.tests import base -from qiskit_neko import decorators - - -class TestTwoLayerQNN(base.BaseTestCase): - """Test adapted from the qiskit_machine_learning tutorials.""" - - def setUp(self): - super().setUp() - if hasattr(self.backend.options, "seed_simulator"): - self.backend.set_options(seed_simulator=42) - - @decorators.component_attr("terra", "backend", "machine_learning") - def test_two_layer_qnn(self): - """Test the execution of quantum neural networks using OpflowQNN""" - - num_qubits = 3 - qi_sv = QuantumInstance(self.backend) - - fm = ZZFeatureMap(num_qubits, reps=2) - ansatz = RealAmplitudes(num_qubits, reps=1) - - observable = PauliSumOp.from_list([("Z" * num_qubits, 1)]) - - qnn3 = TwoLayerQNN( - num_qubits, feature_map=fm, ansatz=ansatz, observable=observable, quantum_instance=qi_sv - ) - - rng = np.random.default_rng(seed=42) - input3 = rng.random(size=qnn3.num_inputs) - weights3 = rng.random(size=qnn3.num_weights) - - qnn3_forward = qnn3.forward(input3, weights3) - qnn3_backward = qnn3.backward(input3, weights3) - - qnn3_backward_ideal = [ - 0.0404151, - 0.20428904, - -0.29863051, - 0.15322033, - 0.10620678, - -0.2779404, - ] - - self.assertAlmostEqual(qnn3_forward[0][0], -0.66604201, delta=0.1) - qnn3_backward_result = qnn3_backward[1][0][0].tolist() - for count, ele in enumerate(qnn3_backward_ideal): - self.assertAlmostEqual(qnn3_backward_result[count], ele, delta=0.1) diff --git a/qiskit_neko/tests/primitives/test_vqe.py b/qiskit_neko/tests/primitives/test_vqe.py index 6b182e8..6c5fa1a 100644 --- a/qiskit_neko/tests/primitives/test_vqe.py +++ b/qiskit_neko/tests/primitives/test_vqe.py @@ -1,6 +1,6 @@ # This code is part of Qiskit. # -# (C) Copyright IBM 2022. +# (C) Copyright IBM 2022, 2023. # # This code is licensed under the Apache License, Version 2.0. You may # obtain a copy of this license in the LICENSE.txt file in the root directory @@ -12,13 +12,14 @@ """Test primitives with vqe.""" -from qiskit_algorithms import VQE, SamplingVQE -from qiskit_algorithms.optimizers import SPSA from qiskit.circuit.library import TwoLocal -from qiskit.opflow import PauliSumOp from qiskit.primitives import BackendEstimator, BackendSampler from qiskit.quantum_info import SparsePauliOp -from qiskit.utils import algorithm_globals + +from qiskit_algorithms import VQE, SamplingVQE +from qiskit_algorithms.optimizers import SPSA +from qiskit_algorithms.utils import algorithm_globals + from qiskit_aer.primitives import Estimator, Sampler from qiskit_neko import decorators @@ -38,7 +39,7 @@ def setUp(self): def test_sampling_vqe(self): """Test the execution of SamplingVQE with BackendSampler.""" sampler = BackendSampler(self.backend) - operator = PauliSumOp(SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12])) + operator = SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12]) ansatz = TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz") optimizer = SPSA() sampling_vqe = SamplingVQE(sampler, ansatz, optimizer) @@ -51,7 +52,7 @@ def test_sampling_vqe(self): def test_aer_sampling_vqe(self): """Test the aer sampler with SamplingVQE.""" sampler = Sampler(backend_options={"seed_simulator": 42}) - operator = PauliSumOp(SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12])) + operator = SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12]) ansatz = TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz") optimizer = SPSA() sampling_vqe = SamplingVQE(sampler, ansatz, optimizer) @@ -64,7 +65,7 @@ def test_aer_sampling_vqe(self): def test_vqe(self): """Test the execution of VQE with BackendEstimator.""" estimator = BackendEstimator(self.backend) - operator = PauliSumOp(SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12])) + operator = SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12]) ansatz = TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz") optimizer = SPSA() sampling_vqe = VQE(estimator, ansatz, optimizer) @@ -77,7 +78,7 @@ def test_vqe(self): def test_aer_vqe(self): """Test the execution of VQE with Aer Estimator.""" estimator = Estimator(backend_options={"seed_simulator": 42}) - operator = PauliSumOp(SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12])) + operator = SparsePauliOp(["ZZ", "IZ", "II"], coeffs=[1, -0.5, 0.12]) ansatz = TwoLocal(rotation_blocks=["ry", "rz"], entanglement_blocks="cz") optimizer = SPSA() sampling_vqe = VQE(estimator, ansatz, optimizer)