Suppose I have 01 and 11 in classical register 1 and 2 respectively in IBM quantum experience circuit. I want 01 + 11 = 00 mod 4. Can it be done?


You can add two qubits firstly in quantum registers and then measure qubit with results and put it to classical register.

Adding two qubits modulo 4 can be do with this circuit:


First $CNOT$ is fan-out and make a "copy" of qubit $q_0$ to $q_2$, second $CNOT$ realizes XOR function between $q_1$ and $q_2$. Eventually you have $(q_0 + q_1) \mod 4$ in $q_2$

EDIT: Expanded for two qubits adder modulo 4, based on comment by Adam Levine:

Here is a code implementing a modulo 4 adder for two qubits:

include "qelib1.inc";

qreg q[6];
creg c[6];

//first input q[0]q[1]
//second input q[2]q[3]
//output q[4]q[5]

//q[0]q[1] = 01
id q[0];
x q[1];
//q[2]q[3] = 11
x q[2];
x q[3];

//first qubit:
//q[1] + q[3] - sum
cx q[1],q[5];
cx q[3],q[5];
//q[1], q[3] - carry
ccx q[1], q[3], q[4];

//second qubit:
//q[0] + q[2] - sum
cx q[0],q[4];
cx q[2],q[4];
//no carry, modulo 4 sum

$I$ and $X$ gates set an input (based on the comment), $CNOT$ gates acting on qubit $q_5$ return sum of last (lower) qubits of input numbers. Toffoli gate is used for calculation of carry form last qubits and next two $CNOT$ gates add together the carry and first (higher) qubits of input numbers. Carry from this sum is not performed as the adder is modulo 4. The result is in qubits $q_4$ and $q_5$ where higher qubit is $q_4$ and lower $q_5$.

Note: this is based on classical approach how to construct an adder. There is also quantum approach based on quantum Fourier transform. See details here: Addition on a Quantum Computer

  • $\begingroup$ Can I have a circuit for adding 2 qubits: 01 + 11 = 00 mod 4? $\endgroup$ – Adam Levine Feb 24 '20 at 6:32

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.