Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add grover_operator function #13365

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion qiskit/circuit/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@
.. autofunction:: unitary_overlap
.. autofunction:: phase_estimation

.. autofunction:: grover_operator


N-local circuits
================
Expand Down Expand Up @@ -596,6 +598,6 @@
from .hidden_linear_function import HiddenLinearFunction, hidden_linear_function
from .iqp import IQP, iqp, random_iqp
from .phase_estimation import PhaseEstimation, phase_estimation
from .grover_operator import GroverOperator
from .grover_operator import GroverOperator, grover_operator
from .phase_oracle import PhaseOracle
from .overlap import UnitaryOverlap, unitary_overlap
270 changes: 269 additions & 1 deletion qiskit/circuit/library/grover_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,266 @@
from typing import List, Optional, Union
import numpy

from qiskit.circuit import QuantumCircuit, QuantumRegister, AncillaRegister
from qiskit.circuit import QuantumCircuit, QuantumRegister, AncillaRegister, AncillaQubit
from qiskit.exceptions import QiskitError
from qiskit.quantum_info import Statevector, Operator, DensityMatrix
from qiskit.utils.deprecation import deprecate_func
from .standard_gates import MCXGate
from .generalized_gates import DiagonalGate


def grover_operator(
oracle: QuantumCircuit | Statevector,
state_preparation: QuantumCircuit | None = None,
zero_reflection: QuantumCircuit | DensityMatrix | Operator | None = None,
reflection_qubits: list[int] | None = None,
insert_barriers: bool = False,
name: str = "Q",
):
r"""Construct the Grover operator.

Grover's search algorithm [1, 2] consists of repeated applications of the so-called
Grover operator used to amplify the amplitudes of the desired output states.
This operator, :math:`\mathcal{Q}`, consists of the phase oracle, :math:`\mathcal{S}_f`,
zero phase-shift or zero reflection, :math:`\mathcal{S}_0`, and an
input state preparation :math:`\mathcal{A}`:

.. math::
\mathcal{Q} = \mathcal{A} \mathcal{S}_0 \mathcal{A}^\dagger \mathcal{S}_f

In the standard Grover search we have :math:`\mathcal{A} = H^{\otimes n}`:

.. math::
\mathcal{Q} = H^{\otimes n} \mathcal{S}_0 H^{\otimes n} \mathcal{S}_f
= D \mathcal{S_f}

The operation :math:`D = H^{\otimes n} \mathcal{S}_0 H^{\otimes n}` is also referred to as
diffusion operator. In this formulation we can see that Grover's operator consists of two
steps: first, the phase oracle multiplies the good states by -1 (with :math:`\mathcal{S}_f`)
and then the whole state is reflected around the mean (with :math:`D`).

This class allows setting a different state preparation, as in quantum amplitude
amplification (a generalization of Grover's algorithm), :math:`\mathcal{A}` might not be
a layer of Hardamard gates [3].

The action of the phase oracle :math:`\mathcal{S}_f` is defined as

.. math::
\mathcal{S}_f: |x\rangle \mapsto (-1)^{f(x)}|x\rangle

where :math:`f(x) = 1` if :math:`x` is a good state and 0 otherwise. To highlight the fact
that this oracle flips the phase of the good states and does not flip the state of a result
qubit, we call :math:`\mathcal{S}_f` a phase oracle.

Note that you can easily construct a phase oracle from a bitflip oracle by sandwiching the
controlled X gate on the result qubit by a X and H gate. For instance

.. parsed-literal::

Bitflip oracle Phaseflip oracle
q_0: ──■── q_0: ────────────■────────────
┌─┴─┐ ┌───┐┌───┐┌─┴─┐┌───┐┌───┐
out: ┤ X ├ out: ┤ X ├┤ H ├┤ X ├┤ H ├┤ X ├
└───┘ └───┘└───┘└───┘└───┘└───┘

There is some flexibility in defining the oracle and :math:`\mathcal{A}` operator. Before the
Grover operator is applied in Grover's algorithm, the qubits are first prepared with one
application of the :math:`\mathcal{A}` operator (or Hadamard gates in the standard formulation).
Thus, we always have operation of the form
:math:`\mathcal{A} \mathcal{S}_f \mathcal{A}^\dagger`. Therefore it is possible to move
bitflip logic into :math:`\mathcal{A}` and leaving the oracle only to do phaseflips via Z gates
based on the bitflips. One possible use-case for this are oracles that do not uncompute the
state qubits.

The zero reflection :math:`\mathcal{S}_0` is usually defined as

.. math::
\mathcal{S}_0 = 2 |0\rangle^{\otimes n} \langle 0|^{\otimes n} - \mathbb{I}_n

where :math:`\mathbb{I}_n` is the identity on :math:`n` qubits.
By default, this class implements the negative version
:math:`2 |0\rangle^{\otimes n} \langle 0|^{\otimes n} - \mathbb{I}_n`, since this can simply
be implemented with a multi-controlled Z sandwiched by X gates on the target qubit and the
introduced global phase does not matter for Grover's algorithm.

Examples:

We can construct a Grover operator just from the phase oracle:

.. plot::
:include-source:
:context:

from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import grover_operator

oracle = QuantumCircuit(2)
oracle.z(0) # good state = first qubit is |1>
grover_op = grover_operator(oracle, insert_barriers=True)
grover_op.draw("mpl")

We can also modify the state preparation:

.. plot::
:include-source:
:context:

oracle = QuantumCircuit(1)
oracle.z(0) # the qubit state |1> is the good state
state_preparation = QuantumCircuit(1)
state_preparation.ry(0.2, 0) # non-uniform state preparation
grover_op = grover_operator(oracle, state_preparation)
grover_op.draw("mpl")

In addition, we can also mark which qubits the zero reflection should act on. This
is useful in case that some qubits are just used as scratch space but should not affect
the oracle:

.. plot::
:include-source:
:context:

oracle = QuantumCircuit(4)
oracle.z(3)
reflection_qubits = [0, 3]
state_preparation = QuantumCircuit(4)
state_preparation.cry(0.1, 0, 3)
state_preparation.ry(0.5, 3)
grover_op = grover_operator(oracle, state_preparation, reflection_qubits=reflection_qubits)
grover_op.draw("mpl")


The oracle and the zero reflection can also be passed as :mod:`qiskit.quantum_info`
objects:

.. plot::
:include-source:
:context:

from qiskit.quantum_info import Statevector, DensityMatrix, Operator

mark_state = Statevector.from_label("011")
reflection = 2 * DensityMatrix.from_label("000") - Operator.from_label("III")
grover_op = grover_operator(oracle=mark_state, zero_reflection=reflection)
grover_op.draw("mpl")

For a large number of qubits, the multi-controlled X gate used for the zero-reflection
can be synthesized in different fashions. Depending on the number of available qubits,
the compiler will choose a different implementation:

.. code-block:: python

from qiskit import transpile, Qubit
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import grover_operator

oracle = QuantumCircuit(10)
oracle.z(oracle.qubits)
grover_op = grover_operator(oracle)

# without extra qubit space, the MCX synthesis is expensive
basis_gates = ["u", "cx"]
tqc = transpile(grover_op, basis_gates=basis_gates)
is_2q = lambda inst: len(inst.qubits) == 2
print("2q depth w/o scratch qubits:", tqc.depth(filter_function=is_2q)) # > 350

# add extra bits that can be used as scratch space
grover_op.add_bits([Qubit() for _ in range(num_qubits)])
tqc = transpile(grover_op, basis_gates=basis_gates)
print("2q depth w/ scratch qubits:", tqc.depth(filter_function=is_2q)) # < 100

Args:
oracle: The phase oracle implementing a reflection about the bad state. Note that this
is not a bitflip oracle, see the docstring for more information.
state_preparation: The operator preparing the good and bad state.
For Grover's algorithm, this is a n-qubit Hadamard gate and for amplitude
amplification or estimation the operator :math:`\mathcal{A}`.
zero_reflection: The reflection about the zero state, :math:`\mathcal{S}_0`.
reflection_qubits: Qubits on which the zero reflection acts on.
insert_barriers: Whether barriers should be inserted between the reflections and A.
name: The name of the circuit.

References:
[1]: L. K. Grover (1996), A fast quantum mechanical algorithm for database search,
`arXiv:quant-ph/9605043 <https://arxiv.org/abs/quant-ph/9605043>`_.
[2]: I. Chuang & M. Nielsen, Quantum Computation and Quantum Information,
Cambridge: Cambridge University Press, 2000. Chapter 6.1.2.
[3]: Brassard, G., Hoyer, P., Mosca, M., & Tapp, A. (2000).
Quantum Amplitude Amplification and Estimation.
`arXiv:quant-ph/0005055 <http://arxiv.org/abs/quant-ph/0005055>`_.
"""
# We inherit the ancillas/qubits structure from the oracle, if it is given as circuit.
if isinstance(oracle, QuantumCircuit):
circuit = oracle.copy_empty_like(name=name, vars_mode="drop")
else:
circuit = QuantumCircuit(oracle.num_qubits, name=name)

# (1) Add the oracle.
# If the oracle is given as statevector, turn it into a circuit that implements the
# reflection about the state.
if isinstance(oracle, Statevector):
diagonal = DiagonalGate((-1) ** oracle.data)
circuit.append(diagonal, circuit.qubits)
else:
circuit.compose(oracle, inplace=True)

if insert_barriers:
circuit.barrier()

# (2) Add the inverse state preparation.
# For this we need to know the target qubits that we apply the zero reflection to.
# If the reflection qubits are not given, we assume they are the qubits that are not
# of type ``AncillaQubit`` in the oracle.
if reflection_qubits is None:
reflection_qubits = [
i for i, qubit in enumerate(circuit.qubits) if not isinstance(qubit, AncillaQubit)
]

if state_preparation is None:
circuit.h(reflection_qubits) # H is self-inverse
else:
circuit.compose(state_preparation.inverse(), inplace=True)

if insert_barriers:
circuit.barrier()

# (3) Add the zero reflection.
if zero_reflection is None:
num_reflection = len(reflection_qubits)

circuit.x(reflection_qubits)
if num_reflection == 1:
circuit.z(
reflection_qubits[0]
) # MCX does not support 0 controls, hence this is separate
else:
mcx = MCXGate(num_reflection - 1)

circuit.h(reflection_qubits[-1])
circuit.append(mcx, reflection_qubits)
circuit.h(reflection_qubits[-1])
circuit.x(reflection_qubits)

elif isinstance(zero_reflection, (Operator, DensityMatrix)):
diagonal = DiagonalGate(zero_reflection.data.diagonal())
circuit.append(diagonal, circuit.qubits)

else:
circuit.compose(zero_reflection, inplace=True)

if insert_barriers:
circuit.barrier()

# (4) Add the state preparation.
if state_preparation is None:
circuit.h(reflection_qubits)
else:
circuit.compose(state_preparation, inplace=True)

# minus sign
circuit.global_phase = numpy.pi

return circuit


class GroverOperator(QuantumCircuit):
Expand Down Expand Up @@ -150,6 +406,13 @@ class GroverOperator(QuantumCircuit):
«state_2: ┤2 ├┤1 ├┤ UCRZ(pi/4) ├┤ H ├
« └─────────────────┘└───────────────┘└────────────┘└───┘

.. seealso::

The :func:`.grover_operator` implements the same functionality but keeping the
:class:`.MCXGate` abstract, such that the compiler may choose the optimal decomposition.
Comment on lines +411 to +412
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

Suggested change
The :func:`.grover_operator` implements the same functionality but keeping the
:class:`.MCXGate` abstract, such that the compiler may choose the optimal decomposition.
The :func:`.grover_operator` implements the same functionality but keeping the
:class:`.MCXGate` abstract, such that the compiler may choose the optimal decomposition.
We recommend using :func:`.grover_operator` for performance reasons.

We recommend using :func:`.grover_operator` for performance reasons, which does not
wrap the circuit into an opaque gate.

References:
[1]: L. K. Grover (1996), A fast quantum mechanical algorithm for database search,
`arXiv:quant-ph/9605043 <https://arxiv.org/abs/quant-ph/9605043>`_.
Expand All @@ -160,6 +423,11 @@ class GroverOperator(QuantumCircuit):
`arXiv:quant-ph/0005055 <http://arxiv.org/abs/quant-ph/0005055>`_.
"""

@deprecate_func(
since="1.3",
additional_msg="Use qiskit.circuit.library.grover_operator instead.",
pending=True,
)
def __init__(
self,
oracle: Union[QuantumCircuit, Statevector],
Expand Down
19 changes: 19 additions & 0 deletions releasenotes/notes/clib-grover-op-cb032144e899ed0d.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
features_circuits:
- |
Added :func:`.grover_operator` to construct a Grover operator circuit, used in e.g.
Grover's algorithm and amplitude estimation/amplification. This function is similar to
:class:`.GroverOperator`, but does not require choosing the implementation of the
multi-controlled X gate a-priori and let's the compiler choose the optimal decomposition
instead. In addition to this, it does not wrap the circuit into an opaque gate and is
faster as less decompositions are required to transpile.

Example::

from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import grover_operator

oracle = QuantumCircuit(2)
oracle.z(0) # good state = first qubit is |1>
grover_op = grover_operator(oracle, insert_barriers=True)
print(grover_op.draw())
Loading