2
$\begingroup$

enter image description here

This is the circuit I want to implement. But I am unable to do this. basically How I can tell qiskit that "if the qubit is in |+> state then operate H gate on qubit 2 or if the qubit is in|-> state then apply ZH gate in the qubit 2

$\endgroup$
1
  • 1
    $\begingroup$ Hi there, its generally preferred if questions are presented using LaTex/MathJax formatting rather than attaching photos. $\endgroup$
    – Condo
    Jul 5 at 16:21

1 Answer 1

1
$\begingroup$

The c_if instruction can be used to make the result of a measurement saved to a classical register adjust whether a gate is or is not performed. Note that, since you do a Hadamard gate first in either scenario, the real dependence on measurement result is whether you also do a $Z$.

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer
from qiskit.quantum_info import random_statevector
from qiskit.visualization import plot_bloch_multivector
from qiskit.extensions import Initialize
instate = random_statevector(2)
qr = QuantumRegister(2)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr)
qc.append(Initialize(instate), [0])
qc.h(1)
qc.cz(0, 1)
qc.h(0)
qc.measure(0, 0)
qc.h(0)
qc.h(1)
qc.z(1).c_if(cr, 1)

If you run this circuit, plot, and compare the second qubit's Bloch Sphere to the original random input, you will find them the same, as in this case below, where the measurement resulted in $|-\rangle$ and therefore applied the $Z$:

plot_bloch_multivector(instate)

Input State

sim = Aer.get_backend('aer_simulator')
qc.save_statevector()
out_vector = sim.run(qc).result().get_statevector()
plot_bloch_multivector(out_vector)

Output State

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.