Skip to content

Commit

Permalink
Restore unchanged unit tests
Browse files Browse the repository at this point in the history
Restore opflow test case

Restore estimator test

Restore unchanged tests
  • Loading branch information
ElePT committed Apr 17, 2023
1 parent e9e6a4f commit 8b3136a
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 18 deletions.
22 changes: 15 additions & 7 deletions test/python/algorithms/eigensolvers/test_vqd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

""" Test VQD """
"""Test VQD"""

import unittest
from test.python.algorithms import QiskitAlgorithmsTestCase
Expand All @@ -19,13 +19,9 @@
from ddt import data, ddt

from qiskit import QuantumCircuit
from qiskit.algorithms.eigensolvers import VQD
from qiskit.algorithms.eigensolvers import VQD, VQDResult
from qiskit.algorithms import AlgorithmError
from qiskit.algorithms.optimizers import (
COBYLA,
L_BFGS_B,
SLSQP,
)
from qiskit.algorithms.optimizers import COBYLA, L_BFGS_B, SLSQP, SPSA
from qiskit.algorithms.state_fidelities import ComputeUncompute
from qiskit.circuit.library import TwoLocal, RealAmplitudes
from qiskit.opflow import PauliSumOp
Expand Down Expand Up @@ -207,6 +203,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
@data(H2_PAULI, H2_OP, H2_SPARSE_PAULI)
def test_vqd_optimizer(self, op):
"""Test running same VQD twice to re-use optimizer, then switch optimizer"""

vqd = VQD(
estimator=self.estimator,
fidelity=self.fidelity,
Expand All @@ -231,6 +228,17 @@ def run_check():
vqd.optimizer = L_BFGS_B()
run_check()

with self.subTest("Batched optimizer replace"):
vqd.optimizer = SLSQP(maxiter=60, max_evals_grouped=10)
run_check()

with self.subTest("SPSA replace"):
# SPSA takes too long to converge, so we will
# only check that it runs with no errors.
vqd.optimizer = SPSA(maxiter=5, learning_rate=0.01, perturbation=0.01)
result = vqd.compute_eigenvalues(operator=op)
self.assertIsInstance(result, VQDResult)

@data(H2_PAULI, H2_OP, H2_SPARSE_PAULI)
def test_optimizer_list(self, op):
"""Test sending an optimizer list"""
Expand Down
6 changes: 4 additions & 2 deletions test/python/algorithms/gradients/test_estimator_gradient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2019, 2023.
# (C) Copyright IBM 2019, 2021.
#
# 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
Expand All @@ -14,7 +14,7 @@
"""Test Estimator Gradients"""

import unittest
from test.python.algorithms.gradients.logging_primitives import LoggingEstimator

import numpy as np
from ddt import ddt, data, unpack

Expand All @@ -35,6 +35,8 @@
from qiskit.quantum_info.random import random_pauli_list
from qiskit.test import QiskitTestCase

from .logging_primitives import LoggingEstimator

gradient_factories = [
lambda estimator: FiniteDiffEstimatorGradient(estimator, epsilon=1e-6, method="central"),
lambda estimator: FiniteDiffEstimatorGradient(estimator, epsilon=1e-6, method="forward"),
Expand Down
3 changes: 2 additions & 1 deletion test/python/algorithms/gradients/test_qgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""Test QGT."""

import unittest
from test.python.algorithms.gradients.logging_primitives import LoggingEstimator
from ddt import ddt, data

import numpy as np
Expand All @@ -26,6 +25,8 @@
from qiskit.primitives import Estimator
from qiskit.test import QiskitTestCase

from .logging_primitives import LoggingEstimator


@ddt
class TestQGT(QiskitTestCase):
Expand Down
7 changes: 5 additions & 2 deletions test/python/algorithms/gradients/test_sampler_gradient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2019, 2023.
# (C) Copyright IBM 2019, 2021.
#
# 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
Expand All @@ -15,9 +15,10 @@

import unittest
from typing import List
from test.python.algorithms.gradients.logging_primitives import LoggingSampler

import numpy as np
from ddt import ddt, data

from qiskit import QuantumCircuit
from qiskit.algorithms.gradients import (
FiniteDiffSamplerGradient,
Expand All @@ -32,6 +33,8 @@
from qiskit.result import QuasiDistribution
from qiskit.test import QiskitTestCase

from .logging_primitives import LoggingSampler

gradient_factories = [
lambda sampler: FiniteDiffSamplerGradient(sampler, epsilon=1e-6, method="central"),
lambda sampler: FiniteDiffSamplerGradient(sampler, epsilon=1e-6, method="forward"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2023.
# (C) Copyright IBM 2018, 2021.
#
# 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
Expand All @@ -12,6 +12,7 @@

"""Test the Sampler VQE."""


import unittest
from functools import partial
from test.python.algorithms import QiskitAlgorithmsTestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def test_run_d_1_with_aux_ops(self):
var_qite = VarQITE(
ansatz, init_param_values, var_principle, estimator, num_timesteps=25
)

evolution_result = var_qite.evolve(evolution_problem)

aux_ops = evolution_result.aux_ops_evaluated

parameter_values = evolution_result.parameter_values[-1]
Expand Down Expand Up @@ -128,6 +128,7 @@ def test_run_d_1_with_aux_ops(self):
ansatz, init_param_values, var_principle, estimator, num_timesteps=25
)
evolution_result = var_qite.evolve(evolution_problem)

aux_ops = evolution_result.aux_ops_evaluated

parameter_values = evolution_result.parameter_values[-1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def test_run_d_1_with_aux_ops(self):
ansatz, init_param_values, var_principle, estimator, num_timesteps=25
)
evolution_result = var_qrte.evolve(evolution_problem)

aux_ops = evolution_result.aux_ops_evaluated

parameter_values = evolution_result.parameter_values[-1]
Expand Down Expand Up @@ -158,6 +159,7 @@ def test_run_d_1_with_aux_ops(self):
ansatz, init_param_values, var_principle, estimator, num_timesteps=25
)
evolution_result = var_qrte.evolve(evolution_problem)

aux_ops = evolution_result.aux_ops_evaluated

parameter_values = evolution_result.parameter_values[-1]
Expand Down
10 changes: 9 additions & 1 deletion test/python/circuit/library/test_qft.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""Test library of QFT circuits."""

import unittest
import warnings
import numpy as np
from ddt import ddt, data, unpack

Expand Down Expand Up @@ -184,11 +185,18 @@ def __init__(self, *_args, **_kwargs):
# We don't want to issue a warning on mutation until we know that the values are
# finalised; this is because a user might want to mutate the number of qubits and the
# approximation degree. In these cases, wait until we try to build the circuit.
with self.assertWarns(DeprecationWarning):
with warnings.catch_warnings(record=True) as caught_warnings:
warnings.filterwarnings(
"always",
category=RuntimeWarning,
module=r"qiskit\..*",
message=r".*precision loss in QFT.*",
)
qft = QFT()
# Even with the approximation this will trigger the warning.
qft.num_qubits = 1080
qft.approximation_degree = 20
self.assertFalse(caught_warnings)

# Short-circuit the build method so it exits after input validation, but without actually
# spinning the CPU to build a huge, useless object.
Expand Down
3 changes: 1 addition & 2 deletions test/python/opflow/opflow_test_case.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2023.
# (C) Copyright IBM 2018, 2020.
#
# 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
Expand All @@ -12,7 +12,6 @@

"""Opflow Test Case"""

import warnings
from qiskit.test import QiskitTestCase


Expand Down
3 changes: 2 additions & 1 deletion test/python/primitives/test_estimator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022, 2023.
# (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
Expand All @@ -13,6 +13,7 @@
"""Tests for Estimator."""

import unittest

import numpy as np
from ddt import data, ddt, unpack

Expand Down

0 comments on commit 8b3136a

Please sign in to comment.