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 44 input bits 0,1,2,3b0,b1,b2,b3 such that it "marks" the amplitudes corresponding to the states
||0123 in {||0011,||1100,||0101}|b0b1b2b3 in {|0011,|1100,|0101}
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 ||1111|1111 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 multi-controlled phase gate, now use X-gates (quantum not) to convert each of the pure states $\ket{0011},\ket{1100},\ket{0101}$ each to $\ket{1111}$ and use MCP gate above to mark the phase. Remember to use X-gates to convert the inputs back.
what I have so far :
from qiskit import QuantumCircuit, Aer, execute
# Create the quantum circuit with 4 qubits
qc = QuantumCircuit(4)
# Step 1: Create uniform superposition of all states
qc.h([0,1,2,3]) # Apply Hadamard to all qubits
# Check state after superposition
qc.barrier()
# Function to mark a specific state to |1111>
def mark_state(state_bits):
# Convert bits to |1111>
control_qubits =[]
for i in range(4):
if state_bits[i]=='0':
qc.x(i) # Flip to 1 if needed
control_qubits.append(i)
# Apply the multi-controlled phase gate with a phase of
qc.mcx(control_qubits[:-1], control_qubits[-1]) # Use the last qubit as target
# Revert the X-gates to get back to the original state
for i in range(4):
if state_bits[i]=='0':
qc.x(i) # Revert X-gate to flip back to 0
# Step 2: Mark states |0011>,|1100>,|1010>
mark_state('0011')
qc.barrier()
mark_state('1100')
qc.barrier()
mark_state('1010')
# Barrier for clarity
qc.barrier()
# Step 3: Measurement
qc.measure_all()
2 cells that I can't manipulate :
1:
from qiskit import QuantumCircuit, QuantumRegister, Aer, execute
b = QuantumRegister(4,'b')
qc = QuantumCircuit(b)
# create uniform super position
qc.h(b[0])
qc.h(b[1])
qc.h(b[2])
qc.h(b[3])
qc.barrier()
mark_pure_states(qc, b[0], b[1], b[2], b[3])
display(qc.draw('mpl', style='iqp'))
# use a state vector simulator to obain the marked states
backend = Aer.get_backend('statevector_simulator')
job = execute(qc, backend)
result = job.result()
state_vector = result.get_statevector()
print(state_vector)
for i in range(16):
if i ==3 or i ==10 or i ==12: # are we marking the correct basis states
assert abs(state_vector[i]+0.25)<=0.001
else:
assert abs(state_vector[i]-0.25)<=0.001
2:
# 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 Uf(qc, b):
mark_pure_states(qc, b[0], b[1], b[2], b[3])
def apply_reflection_about_uniform_state(qc, input_registers):
# qc is a previously created quantum circuit
# input_registers are the registers that measure the input
# n is the number of input qubits
#
# 1. Apply Hadamard on each of the input registers.
# 2. Invert all the qubits
for i in input_registers:
qc.h(i)
qc.x(i)
n = len(input_registers)
# 3. apply a multi controlled Z gate
qc.mcp(np.pi, input_registers[0:n-1], input_registers[n-1])
for i in input_registers:
qc.x(i) # invert back
qc.h(i) # apply Hadamard back
def Grover_diffuse(qc, inputs):
Uf(qc, inputs)
apply_reflection_about_uniform_state(qc, inputs)
qc.barrier()
def create_quantum_circuit_for_grover(n_iters):
inputs = QuantumRegister(4,'b')
cbit = ClassicalRegister(4,'z')
qc = QuantumCircuit(inputs, cbit)
for i in inputs:
qc.h(i) # apply hadamard
qc.barrier()
for i in range(n_iters):
Grover_diffuse(qc, inputs)
qc.measure(inputs, cbit)
return qc
qc2= create_quantum_circuit_for_grover(4)
print('Grover Search After Four Iterations')
display(qc2.draw('mpl', style='iqp'))
from qiskit.tools.visualization import plot_histogram
# lets test after one iteration
simulator = Aer.get_backend('aer_simulator')
circ = transpile(qc2, simulator)
# Run and get counts
result = simulator.run(qc2).result()
counts = result.get_counts(circ)
display(plot_histogram(counts, title='result counts (1024 simulations)'))
assert counts['0011']+ counts['1010']+ counts['1100']>=500, "The marked states should account for more than 50% 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 50% 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!