# Implementing conditional operators in a quantum circuit

I have 4 states say $$|00\rangle, |01\rangle,|10\rangle, |11\rangle$$. I want to add the states in a manner such that

1. $$|a\rangle=|00\rangle\otimes|01\rangle\to |00\rangle\otimes|01\rangle$$
and
2. $$|b\rangle=|10\rangle\otimes|11\rangle\to |10\rangle\otimes|01\rangle$$.

That is the sum operator where the result is saved in the second register, i.e $$|x,y\rangle\to|x, \mathrm{sum}(x,y)\rangle$$.

But there is a rule that:
if $$i=1$$ then compute case $$a$$
if $$i=2$$ then compute case $$b$$.

How do I implement this conditional statement in a quantum circuit?

• Can you clarify what you mean by "case $a$" and "case $b$"? Are the only possible inputs $|a\rangle$ and $|b\rangle$? Or do you mean something else?
– NNN
May 18 '19 at 18:02
• alex since i have made some progress regarding this question, will it be okay if I modify the question so that it is more clear May 18 '19 at 18:04
• That’s fine, yes.
– NNN
May 18 '19 at 22:07

I guess the easiest way to implement a conditional operator is to add a quantum register ($$cond_0$$) that will contain the conditional parameter and then have a succession of control-gates, controlled by $$cond_0$$.

In your case lets's call A, $$Op A$$ and B, $$Op B$$. $$op_0$$ and $$op_1$$ will be the registers involved in $$Op A$$ or $$Op B$$. Obviously the same reasoning works with operations on more than 2 qubits.

The conditional quantum circuit is then :

from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
import math
from scipy.linalg import expm

A = np.random.random((4,4))
A = expm(A*A.T*1.j*math.pi/2) #"whatever unitary defines operation A"
B = np.random.random((4,4))
B = expm(B*B.T*1.j*math.pi/2) #"whatever unitary defines operation B"

#register acting as the conditional statement
qr1 = QuantumRegister(1, name='cond')

#registers involved in the operation
qr2 = QuantumRegister(2, name='op')

#Circuit
qc = QuantumCircuit(qr1, qr2)
gate=ex.UnitaryGate(A, label="Op A").control(1)
qc.append(gate, [0,1,2])
qc.x(0)
gate=ex.UnitaryGate(B, label="Op B").control(1)
qc.append(gate, [0,1,2])
qc.x(0)

print(qc.draw('text'))


The resulting circuit is: As you can see if $$cond_0$$=$$|0\rangle$$ then $$Op A$$ is not applied but $$cond_0$$ is bit-flipped so $$Op A$$ is applied.
In the same way if $$cond_0$$=$$|1\rangle$$ then $$Op A$$ is applied but $$cond_0$$ is bit-flipped so $$Op A$$ is not applied.

The last bit-flip on $$cond_0$$ is done to restore the value of $$cond_0$$. (Potentially unnecessary)

Hope that helps.