Grover’s Algorithm

Grover’s algorithm and helper functions.

Todo:

  • W gate construction (or perhaps -W gate based on Mermin’s book)
  • Generalize the algorithm for an unknown function that returns 1 on multiple qubit states, not just one.
  • Implement _represent_ZGate in OracleGate
class sympy.physics.quantum.grover.OracleGate[source]

A black box gate.

The gate marks the desired qubits of an unknown function by flipping the sign of the qubits. The unknown function returns true when it finds its desired qubits and false otherwise.

Parameters :

qubits : int

Number of qubits.

oracle : callable

A callable function that returns a boolean on a computational basis.

Examples

Apply an Oracle gate that flips the sign of |2> on different qubits:

>>> from sympy.physics.quantum.qubit import IntQubit
>>> from sympy.physics.quantum.qapply import qapply
>>> from sympy.physics.quantum.grover import OracleGate
>>> f = lambda qubits: qubits == IntQubit(2)
>>> v = OracleGate(2, f)
>>> qapply(v*IntQubit(2))
-|2>
>>> qapply(v*IntQubit(3))
|3>
search_function[source]

The unknown function that helps find the sought after qubits.

targets[source]

A tuple of target qubits.

class sympy.physics.quantum.grover.WGate[source]

General n qubit W Gate in Grover’s algorithm.

The gate performs the operation 2|phi><phi| - 1 on some qubits. |phi> = (tensor product of n Hadamards)*(|0> with n qubits)

Parameters :

nqubits : int

The number of qubits to operate on

sympy.physics.quantum.grover.superposition_basis(nqubits)[source]

Creates an equal superposition of the computational basis.

Parameters :

nqubits : int

The number of qubits.

Returns :

state : Qubit

An equal superposition of the computational basis with nqubits.

Examples

Create an equal superposition of 2 qubits:

>>> from sympy.physics.quantum.grover import superposition_basis
>>> superposition_basis(2)
|0>/2 + |1>/2 + |2>/2 + |3>/2
sympy.physics.quantum.grover.grover_iteration(qstate, oracle)[source]

Applies one application of the Oracle and W Gate, WV.

Parameters :

qstate : Qubit

A superposition of qubits.

oracle : OracleGate

The black box operator that flips the sign of the desired basis qubits.

Returns :

Qubit : The qubits after applying the Oracle and W gate.

Examples

Perform one iteration of grover’s algorithm to see a phase change:

>>> from sympy.physics.quantum.qapply import qapply
>>> from sympy.physics.quantum.qubit import IntQubit
>>> from sympy.physics.quantum.grover import OracleGate
>>> from sympy.physics.quantum.grover import superposition_basis
>>> from sympy.physics.quantum.grover import grover_iteration
>>> numqubits = 2
>>> basis_states = superposition_basis(numqubits)
>>> f = lambda qubits: qubits == IntQubit(2)
>>> v = OracleGate(numqubits, f)
>>> qapply(grover_iteration(basis_states, v))
|2>
sympy.physics.quantum.grover.apply_grover(oracle, nqubits, iterations=None)[source]

Applies grover’s algorithm.

Parameters :

oracle : callable

The unknown callable function that returns true when applied to the desired qubits and false otherwise.

Returns :

state : Expr

The resulting state after Grover’s algorithm has been iterated.

Examples

Apply grover’s algorithm to an even superposition of 2 qubits:

>>> from sympy.physics.quantum.qapply import qapply
>>> from sympy.physics.quantum.qubit import IntQubit
>>> from sympy.physics.quantum.grover import apply_grover
>>> f = lambda qubits: qubits == IntQubit(2)
>>> qapply(apply_grover(f, 2))
|2>

Previous topic

Gates

Next topic

QFT

This Page