-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Speed up
GroverOperator
and add GlobalPhase to its decomposition (#…
…4666) **Context:** 1. The `GroverOperator` is applied by `DefaultQubit` by applying its matrix for up to 12 wires (incl). However, `GroverOperator` has a lot of structure as it is `2*P-1` for a projector `P`, and a custom application rule is faster than constructing and applying the matrix. 2. The `compute_matrix` method of `GroverOperator` in addition uses a rather contrived way to construct the matrix `np.full((dim, dim), normalization_constant)`. Instead, we can simply use the broadcasting behaviour of numpy and do `normalization_constant - np.eye(dim)` (timings below). 3. The `GroverOperator` decomposition is only correct up to a minus sign (or phase of $\pi$). **Description of the Change:** 1. Adds a dispatch registration for `apply_operation` in `/qubit/apply_operation.py` for `GroverOperator` that is not based on the matrix representation of `GroverOperator`. 2. Modifies the `compute_matrix` method of `GroverOperator` to skip repetitive calling of `np.kron` and `np.outer` to construct a trivial matrix. 3. Introduces a `GlobalPhase($\pi$)` gate in the decomposition. **Benefits:** Speedups and correct decomposition. **Possible Drawbacks:** N/A **Related GitHub Issues:** Timing code for the matrix construction: ```python def f(n_wires): s1 = np.array([1, 1]) / np.sqrt(2) # uniform superposition state |s> s = reduce(np.kron, list(it.repeat(s1, n_wires))) # Grover diffusion operator return 2 * np.outer(s, s) - np.identity(2**n_wires) for N in [3, 6, 9, 12]: dim = 2**N print(f"N={N}") %timeit f(N) %timeit 2 / dim - np.eye(dim) print() ``` ![image](https://github.com/PennyLaneAI/pennylane/assets/20772922/a1b477d3-30ec-4aa5-b655-0cbdcc1caf0a) Timings for the different `apply_operation` options, for three scenarios: Number of wires for operation and state is equal, number of wires for state is that for operation +4, and number of wires for state is double of that for operation. Given the results, we could do einsum for `len(op.wires)<8` and the dispatch method for bigger wire counts. ![Screenshot from 2023-10-12 14-03-56](https://github.com/PennyLaneAI/pennylane/assets/20772922/5a24dc6c-9732-4875-ab74-36b800eafd44) ![image](https://github.com/PennyLaneAI/pennylane/assets/20772922/8e4ce6b7-f72a-46f2-8371-7a577a02bd75) ![image](https://github.com/PennyLaneAI/pennylane/assets/20772922/91442c01-cc2e-4549-9316-576c242b7720) --------- Co-authored-by: Matthew Silverman <[email protected]>
- Loading branch information
Showing
5 changed files
with
334 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.