def __init__(self): self.spec = set() self.spec = self.spec.union(STANDARD_INSTRUCTIONS.keys()) self.spec = self.spec.union(STANDARD_GATES.keys()) self.spec = self.spec.union({'DAGGER', 'CONTROLLED'})
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ############################################################################## """Module for amplitude amplification, for use in algorithms such as Grover's algorithm. See G. Brassard, P. Hoyer, M. Mosca (2000) `Quantum Amplitude Amplification and Estimation <https://arxiv.org/abs/quant-ph/0005055>`_ for more information. """ import numpy as np import pyquil.quil as pq from pyquil.gates import H, X, Z, RZ, STANDARD_GATES from grove.utils.utility_programs import ControlledProgramBuilder STANDARD_GATE_NAMES = list(STANDARD_GATES.keys()) X_GATE = np.array([[0, 1], [1, 0]]) X_GATE_LABEL = "NOT" HADAMARD_DIFFUSION_LABEL = "HADAMARD_DIFFUSION" def diffusion_program(qubits): diffusion_program = pq.Program() dim = 2**len(qubits) hadamard_diffusion_matrix = np.diag([1.0] + [-1.0] * (dim - 1)) diffusion_program.defgate(HADAMARD_DIFFUSION_LABEL, hadamard_diffusion_matrix) instruction_tuple = (HADAMARD_DIFFUSION_LABEL, ) + tuple(qubits) diffusion_program.inst(instruction_tuple) return diffusion_program
"""Module for Grover's algorithm. Based off standalone grover implementation. Uses the amplitude amplification library.""" import numpy as np import pyquil.quil as pq from pyquil.gates import H, X, STANDARD_GATES import grove.alpha.amplification.amplification as amp STANDARD_GATE_NAMES = STANDARD_GATES.keys() def grover(oracle, qubits, num_iter=None): """ Implementation of Grover's Algorithm for a given oracle. The query qubit will be left in the zero state afterwards. :param Program oracle: An oracle defined as a Program. It should send |x> to (-1)^f(x)|x>, where the range of f is {0, 1}. :param list(int) qubits: List of qubits for Grover's Algorithm. :param int num_iter: The number of iterations to repeat the algorithm for. The default is the integer closest to :math:`\\frac{\\pi}{4}\sqrt{N}`, where :math:`N` is the size of the domain. :return: A program corresponding to the desired instance of Grover's Algorithm. :rtype: Program """