2
$\begingroup$

I have problem with implementation of the circuit below. How can I write down the conditional X-gate in the following circuit?

enter image description here

$\endgroup$
3
$\begingroup$

This can be done in qiskit with c_if operation.

To construct the circuit that you are interested in in OPENQASM, you can do it as follow:

OPENQASM 2.0;
include "qelib1.inc";

qreg q[2];
creg cw[1];
creg cw2[1];


z q[0];
h q[1];
h q[0];
h q[1];
z q[0];
barrier q[0],q[1];
measure q[0] -> cw[0];
barrier q[0],q[1];
if (cw==1) x q[1];
barrier q[0],q[1];
measure q[1] -> cw2[0];

The result circuit is:

enter image description here


Also look at this question and answer: Composing teleportation circuit in Qiskit Circuit Composer


Update: You can also run this with Qiskit as:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi

qreg_q = QuantumRegister(2, 'q')
creg_cw = ClassicalRegister(1, 'cw')
creg_cw2 = ClassicalRegister(1, 'cw2')
circuit = QuantumCircuit(qreg_q, creg_cw, creg_cw2)

circuit.z(qreg_q[0])
circuit.h(qreg_q[1])
circuit.h(qreg_q[0])
circuit.h(qreg_q[1])
circuit.z(qreg_q[0])
circuit.barrier(qreg_q[0], qreg_q[1])
circuit.measure(qreg_q[0], creg_cw[0])
circuit.barrier(qreg_q[0], qreg_q[1])
circuit.x(qreg_q[1]).c_if(creg_cw, 1)
circuit.barrier(qreg_q[0], qreg_q[1])
circuit.measure(qreg_q[1], creg_cw2[0])

circuit.draw('mpl', style={'name': 'iqx'})

It should be noted that if you compose your circuit in OPENQASM, there is an option that would allow you to convert it to Qiskit code. Look for this tab in circuit composer:

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Can you provide the equivalent quantum composer code? Thank you $\endgroup$ – Farhad Nov 4 at 19:02
  • 1
    $\begingroup$ Thank you very much! $\endgroup$ – Farhad Nov 4 at 19:13
  • $\begingroup$ @Farhad You are very welcome :) $\endgroup$ – KAJ226 Nov 4 at 19:24

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.