Skip to content

Commit 4146339

Browse files
authored
Merge 1eff16f into 6700f3c
2 parents 6700f3c + 1eff16f commit 4146339

File tree

6 files changed

+92
-90
lines changed

6 files changed

+92
-90
lines changed

docs/tutorials/06_grover.ipynb

Lines changed: 43 additions & 65 deletions
Large diffs are not rendered by default.

qiskit_algorithms/amplitude_amplifiers/amplification_problem.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
from collections.abc import Callable
1717
from typing import Any, List, cast
1818

19+
from qiskit.version import get_version_info
1920
from qiskit.circuit import QuantumCircuit, Gate
20-
from qiskit.circuit.library import GroverOperator
2121
from qiskit.quantum_info import Statevector
2222

23+
if get_version_info() >= "2.1.0":
24+
from qiskit.circuit.library import grover_operator as grover_operator_builder
25+
else:
26+
from qiskit.circuit.library import GroverOperator as grover_operator_builder
27+
2328

2429
class AmplificationProblem:
2530
"""The amplification problem is the input to amplitude amplification algorithms, like Grover.
@@ -205,10 +210,10 @@ def grover_operator(self) -> QuantumCircuit | None:
205210
206211
Returns:
207212
The Grover operator, or None if neither the Grover operator nor the
208-
:math:`\mathcal{A}` operator is set.
213+
:math:`\mathcal{A}` operator is set.
209214
"""
210215
if self._grover_operator is None:
211-
return GroverOperator(self.oracle, self.state_preparation)
216+
return grover_operator_builder(self.oracle, self.state_preparation)
212217
return self._grover_operator
213218

214219
@grover_operator.setter

qiskit_algorithms/amplitude_amplifiers/grover.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ class Grover(AmplitudeAmplifier):
9696
9797
\mathcal{Q} = \mathcal{A} \mathcal{S}_0 \mathcal{A}^\dagger \mathcal{S}_f.
9898
99-
For more information, see the :class:`~qiskit.circuit.library.GroverOperator` in the
100-
circuit library.
99+
For more information, see the :func:`~qiskit.circuit.library.grover_operator` function in the
100+
circuit library or the :class:`~qiskit.circuit.library.GroverOperator` class if you're using a
101+
version of Qiskit older than 2.1.0.
101102
102103
References:
103104
[1]: L. K. Grover (1996), A fast quantum mechanical algorithm for database search,
@@ -217,9 +218,7 @@ def amplify(self, amplification_problem: AmplificationProblem) -> "GroverResult"
217218
iterator: Iterator[int] = iter(self._iterations)
218219
else:
219220
max_iterations = max(10, 2**amplification_problem.oracle.num_qubits)
220-
max_power = np.ceil(
221-
2 ** (len(amplification_problem.grover_operator.reflection_qubits) / 2)
222-
)
221+
max_power = np.ceil(2 ** (len(amplification_problem.objective_qubits) / 2))
223222
iterator = self._iterations
224223

225224
result = GroverResult()

qiskit_algorithms/amplitude_estimators/estimation_problem.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This code is part of a Qiskit project.
22
#
3-
# (C) Copyright IBM 2020, 2024.
3+
# (C) Copyright IBM 2020, 2025.
44
#
55
# This code is licensed under the Apache License, Version 2.0. You may
66
# obtain a copy of this license in the LICENSE.txt file in the root directory
@@ -17,9 +17,13 @@
1717
from collections.abc import Callable
1818

1919
import numpy
20-
2120
from qiskit.circuit import QuantumCircuit, QuantumRegister
22-
from qiskit.circuit.library import GroverOperator
21+
from qiskit.version import get_version_info
22+
23+
if get_version_info() >= "2.1.0":
24+
from qiskit.circuit.library import grover_operator as grover_operator_builder
25+
else:
26+
from qiskit.circuit.library import GroverOperator as grover_operator_builder
2327

2428

2529
class EstimationProblem:
@@ -167,7 +171,8 @@ def grover_operator(self) -> QuantumCircuit | None:
167171
r"""Get the :math:`\mathcal{Q}` operator, or Grover operator.
168172
169173
If the Grover operator is not set, we try to build it from the :math:`\mathcal{A}` operator
170-
and `objective_qubits`. This only works if `objective_qubits` is a list of integers.
174+
and `objective_qubits` using the :func:`~qiskit.circuit.library.grover_operator` function.
175+
This only works if `objective_qubits` is a list of integers.
171176
172177
Returns:
173178
The Grover operator, or None if neither the Grover operator nor the
@@ -190,15 +195,18 @@ def grover_operator(self) -> QuantumCircuit | None:
190195
oracle.h(self.objective_qubits[-1])
191196

192197
# construct the grover operator
193-
return GroverOperator(oracle, self.state_preparation)
198+
return grover_operator_builder(oracle, self.state_preparation)
194199

195200
@grover_operator.setter
196201
def grover_operator(self, grover_operator: QuantumCircuit | None) -> None:
197202
r"""Set the :math:`\mathcal{Q}` operator.
198203
199204
Args:
200205
grover_operator: The new :math:`\mathcal{Q}` operator. If set to ``None``,
201-
the default construction via ``qiskit.circuit.library.GroverOperator`` is used.
206+
the default construction via :func:`~qiskit.circuit.library.grover_operator` is
207+
used when accessing the property using Qiskit in its 2.1.0 version or an older
208+
one. If using Qiskit in an older version, the
209+
:class:`~qiskit.circuit.library.GroverOperator` is instead used.
202210
"""
203211
self._grover_operator = grover_operator
204212

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
fixes:
3+
- |
4+
:class:`AmplificationProblem` and :class:`EstimationProblem` now use the function
5+
:func:`~qiskit.circuit.library.grover_operator` to construct the Grover operator
6+
by default when using Qiskit in its 2.1.0 version or an older one, instead of the
7+
class :class:`~qiskit.circuit.library.GroverOperator`, to follow with the latter's
8+
deprecation in Qiskit 2.10. In particular, if these classes are instantiated without
9+
providing a Grover operator and the new default construction is used, it won't be
10+
possible to access the attributes of the deprecated class.

test/test_grover.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
from test import QiskitAlgorithmsTestCase
1818

1919
import numpy as np
20-
from ddt import data, ddt
20+
from ddt import data, ddt, idata, unpack
2121
from qiskit import QuantumCircuit
22-
from qiskit.circuit.library import GroverOperator, PhaseOracle, PhaseOracleGate
22+
from qiskit.circuit.library import grover_operator, GroverOperator, PhaseOracle, PhaseOracleGate
2323
from qiskit.primitives import StatevectorSampler
2424
from qiskit.quantum_info import Operator, Statevector
2525
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
@@ -36,24 +36,24 @@ def setUp(self):
3636
super().setUp()
3737
oracle = QuantumCircuit(2)
3838
oracle.cz(0, 1)
39-
self._expected_grover_op = GroverOperator(oracle=oracle)
4039

41-
@data("oracle_only", "oracle_and_stateprep")
42-
def test_groverop_getter(self, kind):
40+
@idata(product(["oracle_only", "oracle_and_stateprep"], [GroverOperator, grover_operator]))
41+
@unpack
42+
def test_groverop_getter(self, kind, oracle_builder):
4343
"""Test the default construction of the Grover operator."""
4444
oracle = QuantumCircuit(2)
4545
oracle.cz(0, 1)
4646

4747
if kind == "oracle_only":
4848
problem = AmplificationProblem(oracle, is_good_state=["11"])
49-
expected = GroverOperator(oracle)
49+
expected = oracle_builder(oracle)
5050
else:
5151
stateprep = QuantumCircuit(2)
5252
stateprep.ry(0.2, [0, 1])
5353
problem = AmplificationProblem(
5454
oracle, state_preparation=stateprep, is_good_state=["11"]
5555
)
56-
expected = GroverOperator(oracle, stateprep)
56+
expected = oracle_builder(oracle, stateprep)
5757

5858
self.assertEqual(Operator(expected), Operator(problem.grover_operator))
5959

@@ -204,11 +204,12 @@ def test_run_state_vector_oracle(self):
204204
result = grover.amplify(problem)
205205
self.assertIn(result.top_measurement, ["11"])
206206

207-
def test_run_custom_grover_operator(self):
207+
@data(GroverOperator, grover_operator)
208+
def test_run_custom_grover_operator(self, oracle_builder):
208209
"""Test execution with a grover operator oracle"""
209210
oracle = QuantumCircuit(2)
210211
oracle.cz(0, 1)
211-
grover_op = GroverOperator(oracle)
212+
grover_op = oracle_builder(oracle)
212213
problem = AmplificationProblem(
213214
oracle=oracle, grover_operator=grover_op, is_good_state=["11"]
214215
)
@@ -225,15 +226,16 @@ def test_optimal_num_iterations(self):
225226
actual = Grover.optimal_num_iterations(num_solutions, num_qubits)
226227
self.assertEqual(actual, expected)
227228

228-
def test_construct_circuit(self):
229+
@data(GroverOperator, grover_operator)
230+
def test_construct_circuit(self, oracle_builder):
229231
"""Test construct_circuit"""
230232
oracle = QuantumCircuit(2)
231233
oracle.cz(0, 1)
232234
problem = AmplificationProblem(oracle, is_good_state=["11"])
233235
grover = Grover()
234236
constructed = grover.construct_circuit(problem, 2, measurement=False)
235237

236-
grover_op = GroverOperator(oracle)
238+
grover_op = oracle_builder(oracle)
237239
expected = QuantumCircuit(2)
238240
expected.h([0, 1])
239241
expected.compose(grover_op.power(2), inplace=True)

0 commit comments

Comments
 (0)