Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
NVIDIA
GitHub Repository: NVIDIA/cuda-q-academic
Path: blob/main/qis-examples/solutions/grovers-solution.ipynb
586 views
Kernel: Unknown Kernel
# SPDX-License-Identifier: Apache-2.0 AND CC-BY-NC-4.0 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # 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.

These are solutions to exercises from the grovers.ipynb notebook.

# EXERCISE 1 SOLUTION # Define U_9 so that U_9|1001> = -|1001> but fixes # all other computational basis states |x> # Set the number of qubits for the computational basis states num_qubits = 4 @cudaq.kernel def U9(qubits: cudaq.qvector, aux_qubit: cudaq.qubit): x(qubits[1]) x(qubits[2]) z.ctrl(qubits, aux_qubit) x(qubits[1]) x(qubits[2]) # Create a kernel to test our defintion of U9 @cudaq.kernel def flip_phase_of_9_only(num_qubits : int): qubits = cudaq.qvector(num_qubits) aux_qubit = cudaq.qubit() # Place the aux_qubit in the minus state x(aux_qubit) h(aux_qubit) # Initialize the the qubits in a state of equal superposition equal_superposition(qubits) # Apply the U9 kernel U9(qubits, aux_qubit) # Get the state of the flip_phase_of95_only kernel result_with_aux_qubit = cudaq.get_state(flip_phase_of_9_only,num_qubits) print('The phases of the computational basis states after an application of U9 are', check_phase(result_with_aux_qubit))
# EXERCISE 2 SOLUTION # Define a phase oracle that flips the phase of both 1111 and 1001, fixing all other computational basis states # assuming that the aux_qubit is initialized in the minus state @cudaq.kernel def phase_oracle(qubits: cudaq.qvector, aux_qubit: cudaq.qubit): U9(qubits, aux_qubit) U15(qubits, aux_qubit)

EXERCISE 3 SOLUTION

f(000)=f(110)=1,f(x)=0 otherwise.f(000) = f(110) = 1, \quad f(x) = 0 \text{ otherwise}.

Applying Grover's algorithm, we note that the total number of states is N=23=8N = 2^3 = 8 and the number of solutions is t=2t = 2. The superposition of solutions is given by G=12(000+110), |G\rangle = \frac{1}{\sqrt{2}}(|000\rangle + |110\rangle), while the equal superposition of the remaining states is B=182(001+010+100+011+101+111). |B\rangle = \frac{1}{\sqrt{8-2}}(|001\rangle + |010\rangle + |100\rangle + |011\rangle + |101\rangle + |111\rangle).

As we have N=23=8N = 2^3 = 8, the angle θ\theta satisfies: θ=arcsin(tN)=arcsin(28)=arcsin(12)=π6. \theta = \arcsin\left(\sqrt{\frac{t}{N}}\right) = \arcsin\left(\sqrt{\frac{2}{8}}\right) = \arcsin\left(\frac{1}{2}\right) = \frac{\pi}{6}. We solve for mm: (2m+1)θ=π2(2m+1)π6=π22m+1=3m=1. (2m+1)\theta = \frac{\pi}{2} \quad \Rightarrow \quad (2m+1)\frac{\pi}{6} = \frac{\pi}{2} \quad \Rightarrow \quad 2m+1 = 3 \quad \Rightarrow \quad m = 1. Thus, a single iteration of the Grover diffusion operator maps ξ|\xi\rangle exactly to G|G\rangle, ensuring that the subsequent measurement yields a valid solution with 100%100\% probability!

Try writing the code for this. In particular, you will need to create new code for the phase oracle.