# How do I translate a quantum circuit for computing x + y mod 8 into a program? I have made the above quantum circuit that gives the mod $$8=2^3$$ operation between $$|x\rangle$$ and $$|y\rangle$$. Now I want to write its corresponding program using some language. Where can I do that i.e. online or on my hardware? And what programming language can I use for this purpose? Also please check my circuit for correctness.

• Comments are not for extended discussion; this conversation has been moved to chat. May 18, 2019 at 18:09
• Could you clarify what operation between x and y you want to do with this circuit, and what is the role of the 7th qubit? It doesn't look like an ancilla, since you don't uncompute it. May 20, 2019 at 16:03
• The 7th qubit stores the 'OR' of the 2nd and 5th qubit, afterwards yes it has to be uncomputed May 20, 2019 at 16:56
• Urgh, that flickering sample gate in the bottom left is a bug in the current version of quirk... May 20, 2019 at 22:34
• What is the "mod 8 operation between x and y"? Are you trying to compute x mod y? x+y modulo 8? x=y modulo 8? Your question isn't clear. May 20, 2019 at 22:35

I'm not sure if you are trying to represent $$x$$ and $$y$$ in little-endian or big-endian format, but either way the circuit does not look correct.

Let's consider the case of $$x = 2$$ and $$y = 2$$: they are conveniently represented the same way in LE and BE formats, as $$010$$, so the encoding will only matter when reading out the answer.

• You start with $$|010\ 010\ 0\rangle$$ (writing the states of the qubits in top-to-bottom order).
• After the first 3 gates you'll get $$|010\ 010\ 1\rangle$$ - indeed, OR of these wires is 1.
• The next three gates are controlled on the first qubit, which is in state $$|0\rangle$$, so we can ignore them: still $$|010\ 010\ 1\rangle$$.
• The second-to-last CNOT is executed: $$|010\ 000\ 1\rangle$$.
• The last CNOT has no effect again, so the final answer is $$000$$ - no matter whether you read it as little-endian or big-endian, it yields an incorrect sum of 0.

I believe this circuit doesn't handle the second carry bit properly.

For reference, here is the Q# code I wrote to test this circuit and find a test case for which it fails (you mentioned in the chat that you tried Quantum Development Kit, so this might be helpful):

// Allocate x, y and auxiliary qubits
using ((x, y, a) = (Qubit, Qubit, Qubit())) {
for (xint in 0..7) {
for (yint in 0..7) {
// x and y qubits start in |0⟩ state; set them to their starting values - in little-endian
ApplyPauliFromBitString(PauliX, true, IntAsBoolArray(xint, 3), x);
ApplyPauliFromBitString(PauliX, true, IntAsBoolArray(yint, 3), y);

// Calculate x + y mod 8 using the circuit
// (assume the wires top to bottom are x0, x1, x2, y0, y1, y2, a)
CCNOT(x, y, a);
CNOT(x, a);
CNOT(y, a);
Controlled X([x, y, a], y);
CCNOT(x, y, y);
CNOT(x, y);
CNOT(x, y);
CNOT(x, y);

// Read out the result from y register (still as LE)
let sum = MeasureInteger(LittleEndian(y));
if (sum != (xint + yint) % 8) {
Message(\$"Incorrect sum for x = {xint}, y = {yint}: sum = {sum}");
}

// Reset all qubits before next iteration
ResetAll(x);
ResetAll(y);
Reset(a);
}
}
}


You can switch it to using big-endian by wrapping IntAsBoolArray and y register in measurement line in Reverse function to reverse the order of qubits.

As a side note, Q# has a library implementation of an adder, you can look it up here.

• Yes you are right i see my mistake. The second carry is not being computed correctly. How to remove this error? May 22, 2019 at 5:39
• I would take a paper on quantum adders and implement the case for 3 qubits. The simplest one is arxiv.org/abs/quant-ph/0008033, if you need fewer ancilla qubits you can look at arxiv.org/abs/quant-ph/0410184v1 (this is also the one implemented in Q# library). May 22, 2019 at 6:42