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 markpurestatesqc b b b b:
# mark the components corresponding to the pure states
# your code here
raise NotImplementedError
b QuantumRegisterb
qc QuantumCircuitb
markpurestatesqc b b b b
qcdrawmpl style'iqp'
such that it passes the following two tests.
Test :
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
Test :
# 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?"
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
