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

[Code together] Add matrix form to Grover Operator #1553

Merged
merged 52 commits into from Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
863b6c2
Added a check that wires are specified for observables
ingstra Aug 17, 2021
858e97a
Added a check that wires are specified for observables
ingstra Aug 17, 2021
4bc94be
Merge branch 'master' into master
antalszava Aug 17, 2021
1e7cb1b
Reformatted with black
ingstra Aug 17, 2021
1199087
Merge branch 'master' of github.com:ingstra/pennylane
ingstra Aug 17, 2021
4286d9d
Properly reformat with black
ingstra Aug 17, 2021
06b6732
Tried to fix test for checking argument when instantiating Observable
ingstra Aug 17, 2021
15d3b61
Fix argument check for Operator instantiation
ingstra Aug 18, 2021
4362869
Improved error message when no arguments are given to instantiation o…
ingstra Aug 18, 2021
ceeb15c
Improved exception handling when no arguments are given to instantiat…
ingstra Aug 18, 2021
b1a9cf2
added two options for the matrix construction of the Grover diffusion…
ingstra Aug 18, 2021
ec1418a
Add original files (because I worked on master before)
ingstra Aug 18, 2021
db7265a
Merge branch 'PennyLaneAI:master' into master
ingstra Aug 18, 2021
84454d7
Reformatted with black
ingstra Aug 18, 2021
1abb0f5
add accidentally removed file
ingstra Aug 18, 2021
f50914d
Add to changelog
ingstra Aug 18, 2021
7455123
Add matrix attribute to Grover template
ingstra Aug 18, 2021
7930374
Add to changelog
ingstra Aug 18, 2021
fdb011c
Fix formatting
ingstra Aug 18, 2021
084ae89
Add accidentally deleted file
ingstra Aug 18, 2021
bde14b2
Merge branch 'master' into grover_matrix
ingstra Aug 18, 2021
45e1c31
Fix formatting
ingstra Aug 18, 2021
a42594f
Merge branch 'grover_matrix' of github.com:ingstra/pennylane into gro…
ingstra Aug 18, 2021
5d45294
Fix formatting
ingstra Aug 18, 2021
3f27d2d
Fix formatting
ingstra Aug 18, 2021
b22c9c2
Add to changelog
ingstra Aug 18, 2021
efec5bb
Add to changelog
ingstra Aug 18, 2021
bececbd
Merge branch 'master' into master
antalszava Aug 18, 2021
8c870a9
Merge branch 'master' into grover_matrix
antalszava Aug 18, 2021
d444f38
Slight reformulation of error message
ingstra Aug 18, 2021
3048a94
Update .github/CHANGELOG.md
ingstra Aug 18, 2021
f113d3f
Remove print (previously used for diagnostic reasons)
ingstra Aug 18, 2021
3d764cc
Parametrize Grover diffusion matrix test for different number of wires
ingstra Aug 18, 2021
34297c6
Hardcode the initial state H|0>
ingstra Aug 18, 2021
218a328
Merge branch 'grover_matrix' of github.com:ingstra/pennylane into gro…
ingstra Aug 18, 2021
92f2eab
Merge branch 'PennyLaneAI:master' into master
ingstra Aug 18, 2021
9bd9b9a
Merge branch 'master' into grover_matrix
ingstra Aug 18, 2021
4307d80
Remove doctstring to inherit it instead
ingstra Aug 18, 2021
49be032
Edit changelog and fix conflicts
ingstra Aug 18, 2021
4efc18a
Reformatting
ingstra Aug 18, 2021
fce3ee3
Add back accidentally removed file...
ingstra Aug 18, 2021
cb238ab
Edit matrix property to allow _matrix to be classmethod
ingstra Aug 18, 2021
73a3c6b
Forgot to make _matrix classmethod (fixed)
ingstra Aug 18, 2021
c5817ab
Fix codefactor complaint about arguments in _matrix
ingstra Aug 18, 2021
b4ef83e
Add back change that I accidentally removed when having trouble mergi…
ingstra Aug 18, 2021
9f8a42b
Hardcode the initial state H|0> also in test_grover.py
ingstra Aug 18, 2021
8970688
Spelling error
ingstra Aug 18, 2021
0f54839
change Grover test to check probabilities instead of matrix
ingstra Aug 18, 2021
459dedc
Reformat with black
ingstra Aug 18, 2021
96af7dc
Merge branch 'master' into grover_matrix
antalszava Aug 19, 2021
2d4a608
Merge branch 'master' into grover_matrix
antalszava Aug 19, 2021
30bcfe2
Merge branch 'master' into grover_matrix
antalszava Aug 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 52 additions & 7 deletions pennylane/templates/subroutines/grover.py
Expand Up @@ -17,7 +17,9 @@
import pennylane as qml
from pennylane.operation import AnyWires, Operation
from pennylane.ops import Hadamard, PauliZ, MultiControlledX

import numpy as np
import itertools
import functools

class GroverOperator(Operation):
r"""Performs the Grover Diffusion Operator.
Expand Down Expand Up @@ -107,6 +109,17 @@ def __init__(self, wires=None, work_wires=None, do_queue=True, id=None):
self.work_wires = work_wires
super().__init__(wires=wires, do_queue=do_queue, id=id)


def __getMultiControlledX(self):
ctrl_str = "0" * (len(self.wires) - 1)
return MultiControlledX(
control_values=ctrl_str,
control_wires=self.wires[:-1],
wires=self.wires[-1],
work_wires=self.work_wires,
)


def expand(self):
ctrl_str = "0" * (len(self.wires) - 1)

Expand All @@ -115,15 +128,47 @@ def expand(self):
Hadamard(wire)

PauliZ(self.wires[-1])
MultiControlledX(
control_values=ctrl_str,
control_wires=self.wires[:-1],
wires=self.wires[-1],
work_wires=self.work_wires,
)
self.__getMultiControlledX()
PauliZ(self.wires[-1])

for wire in self.wires[:-1]:
Hadamard(wire)

return tape


def _matrix1(self):
""" Matrix representation of the Grover diffusion operator """
nbr_wires = len(self.wires)

# state |0>
state_0 = [1, 0]

s1 = Hadamard.matrix @ state_0
ingstra marked this conversation as resolved.
Show resolved Hide resolved

# uniform superposition state
s = functools.reduce(np.kron, list(itertools.repeat(s1, nbr_wires)))

# Grover diffusion operator
G = 2*np.outer(s,s) - np.identity(2**nbr_wires)
return G


def _matrix(self):
""" Matrix representation of the Grover diffusion operator """

nbr_wires = len(self.wires)

oplist = list(itertools.repeat(Hadamard.matrix, nbr_wires-1))
oplist.append(PauliZ.matrix)

CX = self.__getMultiControlledX()

CX = CX.matrix


M = functools.reduce(np.kron, oplist)
G = M @ CX @ M


return G