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.

  • 1
    $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ May 18, 2019 at 18:09
  • 1
    $\begingroup$ 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. $\endgroup$ May 20, 2019 at 16:03
  • $\begingroup$ The 7th qubit stores the 'OR' of the 2nd and 5th qubit, afterwards yes it has to be uncomputed $\endgroup$
    – Upstart
    May 20, 2019 at 16:56
  • $\begingroup$ Urgh, that flickering sample gate in the bottom left is a bug in the current version of quirk... $\endgroup$ May 20, 2019 at 22:34
  • 2
    $\begingroup$ 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. $\endgroup$ May 20, 2019 at 22:35

1 Answer 1


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[3], Qubit[3], 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[1], y[1], a);
            CNOT(x[1], a);
            CNOT(y[1], a);
            Controlled X([x[0], y[0], a], y[2]);
            CCNOT(x[0], y[0], y[1]);
            CNOT(x[0], y[0]);
            CNOT(x[1], y[1]);
            CNOT(x[2], y[2]);

            // 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

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.

  • $\begingroup$ Yes you are right i see my mistake. The second carry is not being computed correctly. How to remove this error? $\endgroup$
    – Upstart
    May 22, 2019 at 5:39
  • $\begingroup$ 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). $\endgroup$ May 22, 2019 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.