Question: Can you fix the following code: [ from qiskit import QuantumRegister, QuantumCircuit from numpy import pi def mark _ pure _ states ( qc ,

Can you fix the following code:
[from qiskit import QuantumRegister, QuantumCircuit
from numpy import pi
def mark_pure_states(qc, b0, b1, b2, b3):
# mark the components corresponding to the pure states |0011>,|1100>,|0101>
# your code here
raise NotImplementedError
b = QuantumRegister(4,'b')
qc = QuantumCircuit(b)
mark_pure_states(qc, b[0], b[1], b[2], b[3])
qc.draw('mpl', style='iqp')]
such that it passes the following two tests.
Test 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]
Test 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?"]

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!