Skip to content

Commit

Permalink
Deprecate QuantumInstance and Opflow (Qiskit/qiskit#9176)
Browse files Browse the repository at this point in the history
* Deprecate QuantumInstance and Opflow

* Add .. deprecated:: to init modules

* Add link to url guide

* Remove internal use of opflow

* Fix black

* Discard old changes in deprecation.py

* Update decorators

* Remove  from docstring

* Update sphinx deprecations, fix lint, fix tests

* Remove hanging Deprecated in docstring

* Remove old Deprecation messages

* Revert "Remove old Deprecation messages"

This reverts commit 6924130e12ffdfde971ff1e25434ee84bf983482.

* Revert "Remove hanging Deprecated in docstring"

This reverts commit cfb04f50e6a30a33955b57be4ab5bbdb9035923c.

* Revert "Remove  from docstring"

This reverts commit 50e5954ecd4a729e7101e8ea4bf493240ae8b59e.

* Change Deprecation to Deprecated

* Remove catch warnings in main code

* Fix missing decorators

* Fix tests

* Shorten message

* Fix black

* Shorten qi message

* Remove checks for QDrift's internal use of opflow (changed in this PR)

* Add warning catchers to unit tests

* Remove opflow from qaoa ansatz tests

* Remove opflow from non-related tests

* Restore some opflow tests for completion

* Fix black

* Remove catch warnings

* Refactor adaptVQE tests, remove opflow from observables evaluator

* Fix tests, lint

Fix tests, lint

Fix CI failures

Fix tests CI

Fix unit tests

Fix CI again

Fix lint

Restore vqd test

Remove unused import

* Delete qobj test to fix CI until Qiskit/qiskit#9322 is merged

* Refactor gradients test warnings

* Remove warning filter

* Refactor rest of tests to assert warnings

* Fix formatting

* Restore unchanged unit tests

Restore opflow test case

Restore estimator test

Restore unchanged tests

* Go back to old opflow test setup

* Revert "Refactor gradients test warnings", only keep qi assertWarns

This reverts commit 9c0a37f3bc3068a9b1ccd122298cfefe556d4135.

* Restore opflow unittests

* Fix tests

* Fix lint

* Fix qaoa repeated test

* Add module deprecation warning

* Divide message in multiple lines

* Fix lint

* :mod:: -> :mod:

* Deprecate Z2 symmetries

---------

Co-authored-by: ElePT <epenatap@gmail.com>
Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
Co-authored-by: woodsp-ibm <woodsp@us.ibm.com>
  • Loading branch information
4 people committed Apr 18, 2023
1 parent 3b66f3e commit 7cf39df
Show file tree
Hide file tree
Showing 29 changed files with 1,256 additions and 850 deletions.
3 changes: 2 additions & 1 deletion qiskit_algorithms/observables_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from qiskit import QuantumCircuit
from qiskit.opflow import PauliSumOp
from qiskit.quantum_info import SparsePauliOp
from .exceptions import AlgorithmError
from .list_or_dict import ListOrDict
from ..primitives import BaseEstimator
Expand Down Expand Up @@ -88,7 +89,7 @@ def _handle_zero_ops(
"""Replaces all occurrence of operators equal to 0 in the list with an equivalent ``PauliSumOp``
operator."""
if observables_list:
zero_op = PauliSumOp.from_list([("I" * observables_list[0].num_qubits, 0)])
zero_op = SparsePauliOp.from_list([("I" * observables_list[0].num_qubits, 0)])
for ind, observable in enumerate(observables_list):
if observable == 0:
observables_list[ind] = zero_op
Expand Down
23 changes: 16 additions & 7 deletions test/evolvers/test_evolution_problem.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.
# (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
Expand Down Expand Up @@ -51,7 +51,8 @@ def test_init_default(self):
def test_init_all(self):
"""Tests that all fields are initialized correctly."""
t_parameter = Parameter("t")
hamiltonian = t_parameter * Z + Y
with self.assertWarns(DeprecationWarning):
hamiltonian = t_parameter * Z + Y
time = 2
initial_state = One
aux_operators = [X, Y]
Expand All @@ -66,15 +67,17 @@ def test_init_all(self):
t_param=t_parameter,
param_value_dict=param_value_dict,
)
expected_hamiltonian = Y + t_parameter * Z

expected_hamiltonian = Y + t_parameter * Z
expected_time = 2
expected_initial_state = One
expected_aux_operators = [X, Y]
expected_t_param = t_parameter
expected_param_value_dict = {t_parameter: 3.2}

self.assertEqual(evo_problem.hamiltonian, expected_hamiltonian)
with self.assertWarns(DeprecationWarning):
self.assertEqual(evo_problem.hamiltonian, expected_hamiltonian)

self.assertEqual(evo_problem.time, expected_time)
self.assertEqual(evo_problem.initial_state, expected_initial_state)
self.assertEqual(evo_problem.aux_operators, expected_aux_operators)
Expand All @@ -93,7 +96,9 @@ def test_validate_params(self):
param_x = Parameter("x")
param_y = Parameter("y")
with self.subTest(msg="Parameter missing in dict."):
hamiltonian = param_x * X + param_y * Y
with self.assertWarns(DeprecationWarning):
hamiltonian = param_x * X + param_y * Y

param_dict = {param_y: 2}
with self.assertWarns(DeprecationWarning):
evolution_problem = EvolutionProblem(
Expand All @@ -103,7 +108,9 @@ def test_validate_params(self):
evolution_problem.validate_params()

with self.subTest(msg="Empty dict."):
hamiltonian = param_x * X + param_y * Y
with self.assertWarns(DeprecationWarning):
hamiltonian = param_x * X + param_y * Y

param_dict = {}
with self.assertWarns(DeprecationWarning):
evolution_problem = EvolutionProblem(
Expand All @@ -113,7 +120,9 @@ def test_validate_params(self):
evolution_problem.validate_params()

with self.subTest(msg="Extra parameter in dict."):
hamiltonian = param_x * X + param_y * Y
with self.assertWarns(DeprecationWarning):
hamiltonian = param_x * X + param_y * Y

param_dict = {param_y: 2, param_x: 1, Parameter("z"): 1}
with self.assertWarns(DeprecationWarning):
evolution_problem = EvolutionProblem(
Expand Down
37 changes: 19 additions & 18 deletions test/evolvers/trotterization/test_trotter_qrte.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021, 2022.
# (C) Copyright IBM 2021, 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
Expand All @@ -13,7 +13,6 @@
"""Test TrotterQRTE."""

import unittest

from test.python.opflow import QiskitOpflowTestCase
from ddt import ddt, data, unpack
import numpy as np
Expand Down Expand Up @@ -52,18 +51,19 @@ def setUp(self):
algorithm_globals.random_seed = self.seed
backend_statevector = BasicAer.get_backend("statevector_simulator")
backend_qasm = BasicAer.get_backend("qasm_simulator")
self.quantum_instance = QuantumInstance(
backend=backend_statevector,
shots=1,
seed_simulator=self.seed,
seed_transpiler=self.seed,
)
self.quantum_instance_qasm = QuantumInstance(
backend=backend_qasm,
shots=8000,
seed_simulator=self.seed,
seed_transpiler=self.seed,
)
with self.assertWarns(DeprecationWarning):
self.quantum_instance = QuantumInstance(
backend=backend_statevector,
shots=1,
seed_simulator=self.seed,
seed_transpiler=self.seed,
)
self.quantum_instance_qasm = QuantumInstance(
backend=backend_qasm,
shots=8000,
seed_simulator=self.seed,
seed_transpiler=self.seed,
)
self.backends_dict = {
"qi_sv": self.quantum_instance,
"qi_qasm": self.quantum_instance_qasm,
Expand Down Expand Up @@ -123,10 +123,11 @@ def test_trotter_qrte_trotter_single_qubit_aux_ops(self):
with self.subTest(msg=f"Test {backend_name} backend."):
algorithm_globals.random_seed = 0
backend = self.backends_dict[backend_name]
expectation = ExpectationFactory.build(
operator=operator,
backend=backend,
)
with self.assertWarns(DeprecationWarning):
expectation = ExpectationFactory.build(
operator=operator,
backend=backend,
)
with self.assertWarns(DeprecationWarning):
trotter_qrte = TrotterQRTE(quantum_instance=backend, expectation=expectation)
evolution_result = trotter_qrte.evolve(evolution_problem)
Expand Down
126 changes: 68 additions & 58 deletions test/minimum_eigensolvers/test_adapt_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,59 +37,65 @@ class TestAdaptVQE(QiskitAlgorithmsTestCase):
def setUp(self):
super().setUp()
algorithm_globals.random_seed = 42
self.h2_op = PauliSumOp.from_list(
[
("IIII", -0.8105479805373266),
("ZZII", -0.2257534922240251),
("IIZI", +0.12091263261776641),
("ZIZI", +0.12091263261776641),
("IZZI", +0.17218393261915543),
("IIIZ", +0.17218393261915546),
("IZIZ", +0.1661454325638243),
("ZZIZ", +0.1661454325638243),
("IIZZ", -0.2257534922240251),
("IZZZ", +0.16892753870087926),
("ZZZZ", +0.17464343068300464),
("IXIX", +0.04523279994605788),
("ZXIX", +0.04523279994605788),
("IXZX", -0.04523279994605788),
("ZXZX", -0.04523279994605788),
]
)
self.excitation_pool = [
PauliSumOp(
SparsePauliOp(["IIIY", "IIZY"], coeffs=[0.5 + 0.0j, -0.5 + 0.0j]), coeff=1.0
),
PauliSumOp(
SparsePauliOp(["ZYII", "IYZI"], coeffs=[-0.5 + 0.0j, 0.5 + 0.0j]), coeff=1.0
),
PauliSumOp(
SparsePauliOp(
["ZXZY", "IXIY", "IYIX", "ZYZX", "IYZX", "ZYIX", "ZXIY", "IXZY"],
coeffs=[
-0.125 + 0.0j,
0.125 + 0.0j,
-0.125 + 0.0j,
0.125 + 0.0j,
0.125 + 0.0j,
-0.125 + 0.0j,
0.125 + 0.0j,
-0.125 + 0.0j,
],

with self.assertWarns(DeprecationWarning):
self.h2_op = PauliSumOp.from_list(
[
("IIII", -0.8105479805373266),
("ZZII", -0.2257534922240251),
("IIZI", +0.12091263261776641),
("ZIZI", +0.12091263261776641),
("IZZI", +0.17218393261915543),
("IIIZ", +0.17218393261915546),
("IZIZ", +0.1661454325638243),
("ZZIZ", +0.1661454325638243),
("IIZZ", -0.2257534922240251),
("IZZZ", +0.16892753870087926),
("ZZZZ", +0.17464343068300464),
("IXIX", +0.04523279994605788),
("ZXIX", +0.04523279994605788),
("IXZX", -0.04523279994605788),
("ZXZX", -0.04523279994605788),
]
)
self.excitation_pool = [
PauliSumOp(
SparsePauliOp(["IIIY", "IIZY"], coeffs=[0.5 + 0.0j, -0.5 + 0.0j]), coeff=1.0
),
PauliSumOp(
SparsePauliOp(["ZYII", "IYZI"], coeffs=[-0.5 + 0.0j, 0.5 + 0.0j]), coeff=1.0
),
coeff=1.0,
),
]
self.initial_state = QuantumCircuit(QuantumRegister(4))
self.initial_state.x(0)
self.initial_state.x(1)
self.ansatz = EvolvedOperatorAnsatz(self.excitation_pool, initial_state=self.initial_state)
self.optimizer = SLSQP()
PauliSumOp(
SparsePauliOp(
["ZXZY", "IXIY", "IYIX", "ZYZX", "IYZX", "ZYIX", "ZXIY", "IXZY"],
coeffs=[
-0.125 + 0.0j,
0.125 + 0.0j,
-0.125 + 0.0j,
0.125 + 0.0j,
0.125 + 0.0j,
-0.125 + 0.0j,
0.125 + 0.0j,
-0.125 + 0.0j,
],
),
coeff=1.0,
),
]
self.initial_state = QuantumCircuit(QuantumRegister(4))
self.initial_state.x(0)
self.initial_state.x(1)
self.ansatz = EvolvedOperatorAnsatz(
self.excitation_pool, initial_state=self.initial_state
)
self.optimizer = SLSQP()

def test_default(self):
"""Default execution"""
calc = AdaptVQE(VQE(Estimator(), self.ansatz, self.optimizer))
res = calc.compute_minimum_eigenvalue(operator=self.h2_op)

with self.assertWarns(DeprecationWarning):
res = calc.compute_minimum_eigenvalue(operator=self.h2_op)

expected_eigenvalue = -1.85727503

Expand Down Expand Up @@ -117,7 +123,8 @@ def test_converged(self):
VQE(Estimator(), self.ansatz, self.optimizer),
gradient_threshold=1e-3,
)
res = calc.compute_minimum_eigenvalue(operator=self.h2_op)
with self.assertWarns(DeprecationWarning):
res = calc.compute_minimum_eigenvalue(operator=self.h2_op)

self.assertEqual(res.termination_criterion, TerminationCriterion.CONVERGED)

Expand All @@ -127,13 +134,14 @@ def test_maximum(self):
VQE(Estimator(), self.ansatz, self.optimizer),
max_iterations=1,
)
res = calc.compute_minimum_eigenvalue(operator=self.h2_op)
with self.assertWarns(DeprecationWarning):
res = calc.compute_minimum_eigenvalue(operator=self.h2_op)

self.assertEqual(res.termination_criterion, TerminationCriterion.MAXIMUM)

def test_eigenvalue_threshold(self):
"""Test for the eigenvalue_threshold attribute."""
operator = PauliSumOp.from_list(
operator = SparsePauliOp.from_list(
[
("XX", 1.0),
("ZX", -0.5),
Expand All @@ -142,8 +150,8 @@ def test_eigenvalue_threshold(self):
)
ansatz = EvolvedOperatorAnsatz(
[
PauliSumOp.from_list([("YZ", 0.4)]),
PauliSumOp.from_list([("ZY", 0.5)]),
SparsePauliOp.from_list([("YZ", 0.4)]),
SparsePauliOp.from_list([("ZY", 0.5)]),
],
initial_state=QuantumCircuit(2),
)
Expand All @@ -163,7 +171,8 @@ def test_threshold_attribute(self):
VQE(Estimator(), self.ansatz, self.optimizer),
threshold=1e-3,
)
res = calc.compute_minimum_eigenvalue(operator=self.h2_op)
with self.assertWarns(DeprecationWarning):
res = calc.compute_minimum_eigenvalue(operator=self.h2_op)

self.assertEqual(res.termination_criterion, TerminationCriterion.CONVERGED)

Expand Down Expand Up @@ -203,9 +212,9 @@ def test_vqe_solver(self):
"""Test to check if the VQE solver remains the same or not"""
solver = VQE(Estimator(), self.ansatz, self.optimizer)
calc = AdaptVQE(solver)
_ = calc.compute_minimum_eigenvalue(operator=self.h2_op)

self.assertEqual(solver.ansatz, calc.solver.ansatz)
with self.assertWarns(DeprecationWarning):
_ = calc.compute_minimum_eigenvalue(operator=self.h2_op)
self.assertEqual(solver.ansatz, calc.solver.ansatz)

def test_gradient_calculation(self):
"""Test to check if the gradient calculation"""
Expand All @@ -219,7 +228,8 @@ def test_gradient_calculation(self):
def test_supports_aux_operators(self):
"""Test that auxiliary operators are supported"""
calc = AdaptVQE(VQE(Estimator(), self.ansatz, self.optimizer))
res = calc.compute_minimum_eigenvalue(operator=self.h2_op, aux_operators=[self.h2_op])
with self.assertWarns(DeprecationWarning):
res = calc.compute_minimum_eigenvalue(operator=self.h2_op, aux_operators=[self.h2_op])

expected_eigenvalue = -1.85727503

Expand Down
Loading

0 comments on commit 7cf39df

Please sign in to comment.