Constructing a circuit which performs the transformation $|x,y\rangle \to |x, x + y \bmod 4\rangle$

When faced with exercises like these, I find it hard to know how to construct the circuits due to the amount of input one needs to account for. I have seen the solution provided here however, I don't think I would have been able to solve this exercise my self.

Does anyone have any tips on a systematic way to construct a circuit like this? I start out with a circuit which solves one specific input (for example for $$x = |00\rangle, y = |01\rangle$$), but after this I get stuck. I appreciate any help!

Here are some strategies.

1. Start from a classical circuit, and do a naive transformation into a quantum circuit (e.g. AND gate becomes Toffoli onto a clean ancilla). Find ways to uncompute intermediate values and other garbage. Then optimize optimize optimize until it's compact. It helps to know a lot of trivial circuit identities when optimizing.

2. Decompose into simpler problems. For example, separate the two-bit addition into two controlled increments then figure out how to do a controlled increment.

3. Get inspiration from algebraic boolean expressions for the output values. For example, if you are incrementing a register $$x_0, x_1, x_2, ..., x_k$$ then qubit $$i$$ transitions from $$x_i$$ to $$x_i \oplus \prod_{j and this form suggests a simple circuit that you can create.

4. Brute force one output bit at a time. This only works for smaller circuits. Figure out every situation where each bit should be ON, and for each of those cases output a multi-controlled NOT targeting a clean ancilla to represent the output. Then do the same strategy in reverse to uncompute the input. Then optimize optimize optimize.

5. Diagonalize the problem. Is there a simple unitary $$U$$ that maps your problem's eigenstates to the computational basis states? Apply it, then phase the appropriate states using many-controlled Z rotations, then un-apply $$U$$. In the case of addition, the operation that does this is the QFT and you won't even need the Z rotations to be controlled.

6. Find a loop invariant that breaks the problem into constant-sized pieces. In the case of adders, the invariant is packaged up into the carry bit that propagates through ripple-carry adders. Can you find an equivalent of a carry bit for your problem?

7. Pay attention to data dependencies. Start by getting the value of qubits with no dependents correct. Then remove those qubits from the dependency graph and iterate. Two-bit addition has a very simple dependency graph, and so this approach works well on it. If the dependency graph has cycles, the problem will be much harder.

8. Apply generic problem solving (i.e. read How to Solve It). For example, get a foothold by dropping constraints. What if you were allowed to get the phases wrong, or to create junk? What if you only have to get some of the qubits correct, and are allowed to trash the others? What if you had more workspace? What if one of the input qubits was promised to be in the 0 state? The 1 state? The + state? What if you only had to do 1 bit addition, or 1-bit-register-into-2-bit-register addition?

For simplicity let's assume $$x, y \in \mathbb{N}$$, since worrying about two's complement representation for negative numbers is just annoying.

First, this function must be reversible. That means $$y$$ is restricted to the range $$0 \dots 3$$, since $$(x + 0) \bmod 4 = (x + 4) \bmod 4$$, so if we allow $$y$$ values outside of $$0 \dots 3$$ we cannot reverse the function (uniquely identify $$y$$ given the values of $$x$$ and $$(x + y) \bmod 4$$). This means the $$y$$ input can be at most two qbits in size.

We then consider that $$(x + y) \bmod 4 = (x \bmod 4) + (y \bmod 4)$$. This means that we only have to care about the two least-significant qbits of $$x$$ when calculating $$(x + y) \bmod 4$$. Thus, given $$n$$ input qbits for $$x$$ and $$2$$ input qbits for $$y$$, our circuit will only "touch" the two least-significant qbits of $$x$$ (probably as input to a CNOT) and leave the other $$n-2$$ qbits of $$x$$ alone.

For simplicity, let's build our circuit assuming $$n = 2$$. We can of course construct our circuit by just calculating the desired action on all $$2^4$$ possible input values then writing a matrix corresponding to that, but that's boring. Let's simplify it even more; let $$x = |ab\rangle$$ and $$y = |cd\rangle$$ where $$a,b,c,d \in \{0, 1\}$$; $$c$$ and $$d$$ are also our output qbits. We have the following observations:

• When $$b = d = 1$$, we want to flip $$c$$ (since addition of $$b$$ and $$d$$ overflows). This is clearly the action of a Toffoli gate.
• When $$b = 1$$, we want to flip $$d$$. For this we use a CNOT.
• When $$a = 1$$, we want to flip $$c$$. For this we again use a CNOT.

This means for $$n=2$$, our circuit is as follows: For higher values of $$n$$, we just ignore the $$n-2$$ most-significant qbits.

In general, problems like these are just that: problems. They require some amount of mathematical trickiness, reasoning, and breaking down the problem to work through. Hopefully when reading the above you can see how each step followed from the last until we can use our intuition to solve the problem. For quantum computing problems specifically, they'll become easier as you learn the valid "moves" you can make around the problem space.

• why the other cases are not considered like when $a=b=0$, $a\neq b$ and the others – Upstart Apr 2 at 7:06
• Those cases were captured implicitly. – ahelwer Apr 2 at 18:58
• can you look at my implementation of division by 8 which is based on your explaination.quantumcomputing.stackexchange.com/questions/5848/… – Upstart Apr 3 at 19:24