Question: Grover Search Marking Circuit Design a quantum circuit with 4 4 input bits 0 , 1 , 2 , 3 b 0 , b 1
Grover Search Marking Circuit
Design a quantum circuit with input bits bbbb such that it "marks" the amplitudes corresponding to the states
in bbbb in
by inverting their amplitudes, leaving all other states unchanged. In this problem, you are asked to achieve this without using ancillary qubits or a result bit.
Let us consider the simple problem of inverting the amplitude corresponding to leaving all the others unchanged. Show that this can be performed using a "multi controlled phase gate" with a phase of radians.
Using the idea of a multicontrolled phase gate, now use Xgates quantum not to convert each of the pure states $ketketket$ each to $ket$ and use MCP gate above to mark the phase. Remember to use Xgates to convert the inputs back.
what I have so far :
from qiskit import QuantumCircuit, Aer, execute
# Create the quantum circuit with qubits
qc QuantumCircuit
# Step : Create uniform superposition of all states
qch # Apply Hadamard to all qubits
# Check state after superposition
qcbarrier
# Function to mark a specific state to
def markstatestatebits:
# Convert bits to
controlqubits
for i in range:
if statebitsi:
qcxi # Flip to if needed
controlqubits.appendi
# Apply the multicontrolled phase gate with a phase of
qcmcxcontrolqubits: controlqubits # Use the last qubit as target
# Revert the Xgates to get back to the original state
for i in range:
if statebitsi:
qcxi # Revert Xgate to flip back to
# Step : Mark states
markstate
qcbarrier
markstate
qcbarrier
markstate
# Barrier for clarity
qcbarrier
# Step : Measurement
qcmeasureall
cells that I can't manipulate :
:
from qiskit import QuantumCircuit, QuantumRegister, Aer, execute
b QuantumRegisterb
qc QuantumCircuitb
# create uniform super position
qchb
qchb
qchb
qchb
qcbarrier
markpurestatesqc b b b b
displayqcdrawmpl style'iqp'
# use a state vector simulator to obain the marked states
backend Aer.getbackendstatevectorsimulator'
job executeqc backend
result job.result
statevector result.getstatevector
printstatevector
for i in range:
if i or i or i : # are we marking the correct basis states
assert absstatevectori
else:
assert absstatevectori
:
# Let's run Grover's algorithm
# let's now implement Grover's search to find a value
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, Aer, execute
def Ufqc b:
markpurestatesqc b b b b
def applyreflectionaboutuniformstateqc inputregisters:
# qc is a previously created quantum circuit
# inputregisters are the registers that measure the input
# n is the number of input qubits
#
# Apply Hadamard on each of the input registers.
# Invert all the qubits
for i in inputregisters:
qchi
qcxi
n leninputregisters
# apply a multi controlled Z gate
qcmcpnppi inputregisters:n inputregistersn
for i in inputregisters:
qcxi # invert back
qchi # apply Hadamard back
def Groverdiffuseqc inputs:
Ufqc inputs
applyreflectionaboutuniformstateqc inputs
qcbarrier
def createquantumcircuitforgroverniters:
inputs QuantumRegisterb
cbit ClassicalRegisterz
qc QuantumCircuitinputs cbit
for i in inputs:
qchi # apply hadamard
qcbarrier
for i in rangeniters:
Groverdiffuseqc inputs
qcmeasureinputs cbit
return qc
qc createquantumcircuitforgrover
printGrover Search After Four Iterations'
displayqcdrawmpl style'iqp'
from qiskit.tools.visualization import plothistogram
# lets test after one iteration
simulator Aer.getbackendaersimulator'
circ transpileqc simulator
# Run and get counts
result simulator.runqcresult
counts result.getcountscirc
displayplothistogramcounts title'result counts simulations
assert counts counts counts "The marked states should account for more than of the counts. Are you implementing the Marking circuit correctly?"
I am getting this assertion error:
AssertionError: The marked states should account for more than of the counts. Are you implementing the Marking circuit correctly?
Thank you for your help!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
