Grover’s algorithm and helper functions.
Todo:
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
oracle : callable
|
---|
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>
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
|
---|
Creates an equal superposition of the computational basis.
Parameters : | nqubits : int
|
---|---|
Returns : | state : Qubit
|
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
Applies one application of the Oracle and W Gate, WV.
Parameters : | qstate : Qubit
oracle : OracleGate
|
---|---|
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>
Applies grover’s algorithm.
Parameters : | oracle : callable
|
---|---|
Returns : | state : Expr
|
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>