Software Diagnostics and Conformance Testing Division home page

genadder

Home | Screenshots | QCSim | genadder | Qhdl2Jaq/Jaq2Qhdl | Generate | Download

Genadder generates a quantum circuit to add two binary numbers of any width. It is implemented in C. The number of bits is given on the command line.

The circuit generated is a ripple-carry adder. It follows the block scheme in Jozef Gruska's Quantum Computing, page 96, and uses optimized implementations by Paul E. Black (unpublished 11 July 2002) using Generate. The circuits were checked with QCSim. For N bit wide arithmetic, the circuit uses 3N qubits: 2N for inputs, N-1 ancilla, and a carry out. One of the inputs serves as the output sums. It is assumed that there is no carry in. Although it uses ancilla, they are reset to 0, thus there is no "garbage". The circuit uses 7N-6 gates, N > 1.

A simple example is a 2-bit wide adder. It uses six qubits: inp0, inp1, sum0, sum1, anc0, and carryOut. The input is inp0/1 and sum0/1. The output is sum0, sum1, and carryOut. The least significant bits are at the top.

a 2-bit wide adder circuit
Figure 1. A 2-bit Wide Adder

Notice that we could save one gate by removing the last gate, cnot(inp0, anc0), and moving the second gate, cnot(inp0, sum0), to the end. This kind of optimization could be folded into the generator, or added as a post-processing step.

Slightly more complex is a 3-bit wide adder. This is the first instance of all three variants of adders circuits: first stage (no carry in), middle stage, and final stage (no need to undo the carry). Following the picture are the jaQuzzi and QHDL files. The jaQuzzi version came from Qhdl2Jaq. Genadder declares the qubits from most to least significant so density matrix rows in QCSim correspond to the binary interpretations. We reversed the order here so the jaQuzzi images correspond with Gruska's diagrams.

a 3-bit wide adder circuit
Figure 2. A 3-bit Wide Adder

 

qubit_0="inp0"
qubit_1="sum0"
qubit_2="anc0"
qubit_3="inp1"
qubit_4="sum1"
qubit_5="anc1"
qubit_6="inp2"
qubit_7="sum2"
qubit_8="carryOut"
gate0={1:1:NOT:-:-:-:-:-:-}
gate1={1:NOT:-:-:-:-:-:-:-}
gate2={-:-:-:1:1:NOT:-:-:-}
gate3={-:-:-:1:NOT:-:-:-:-}
gate4={-:-:1:-:1:NOT:-:-:-}
gate5={-:-:-:-:-:-:1:1:NOT}
gate6={-:-:-:-:-:-:1:NOT:-}
gate7={-:-:-:-:-:1:-:1:NOT}
gate8={-:-:-:-:-:1:-:NOT:-}
gate9={-:-:1:-:1:NOT:-:-:-}
gate10={-:-:-:1:1:NOT:-:-:-}
gate11={-:-:-:1:-:NOT:-:-:-}
gate12={-:-:1:-:NOT:-:-:-:-}
gate13={1:1:NOT:-:-:-:-:-:-}
gate14={1:-:NOT:-:-:-:-:-:-}

# Quantum circuit to add 3 qubits without garbage
# Produced by genadder version 1.0
# $Header: /home/black/Quantum/GenAdder/RCS/genadder.C,v 1.1 2003/08/20 13:28:20 $
# 	For more information, contact Paul E. Black 
# Command line:
# 	3

# Declare qubits
variable inp0, sum0, anc0, inp1, sum1, anc1, inp2, sum2, carryOut: qubit;
= |000000000>;

# 	The adder itself
# sum bit 0
c2not(inp0, sum0, anc0);
cnot(inp0, sum0);
# sum bit 1
c2not(inp1, sum1, anc1);
cnot(inp1, sum1);
c2not(anc0, sum1, anc1);
# sum bit 2
c2not(inp2, sum2, carryOut);
cnot(inp2, sum2);
c2not(anc1, sum2, carryOut);
# finish bit 2
cnot(anc1, sum2);
# finish bit 1
c2not(anc0, sum1, anc1);
c2not(inp1, sum1, anc1);
cnot(inp1, anc1);
cnot(anc0, sum1);
# finish bit 0
c2not(inp0, sum0, anc0);
cnot(inp0, anc0);

Finally we show part of a 5-bit wide adder, along with its jaQuzzi and QHDL representations.

part of a 5-bit wide adder circuit
Figure 3. Part of a 5-bit Wide Adder

 

qubit_0="inp0"
qubit_1="sum0"
qubit_2="anc0"
qubit_3="inp1"
qubit_4="sum1"
qubit_5="anc1"
qubit_6="inp2"
qubit_7="sum2"
qubit_8="anc2"
qubit_9="inp3"
qubit_10="sum3"
qubit_11="anc3"
qubit_12="inp4"
qubit_13="sum4"
qubit_14="carryOut"
gate0={1:1:NOT:-:-:-:-:-:-:-:-:-:-:-:-}
gate1={1:NOT:-:-:-:-:-:-:-:-:-:-:-:-:-}
gate2={-:-:-:1:1:NOT:-:-:-:-:-:-:-:-:-}
gate3={-:-:-:1:NOT:-:-:-:-:-:-:-:-:-:-}
gate4={-:-:1:-:1:NOT:-:-:-:-:-:-:-:-:-}
gate5={-:-:-:-:-:-:1:1:NOT:-:-:-:-:-:-}
gate6={-:-:-:-:-:-:1:NOT:-:-:-:-:-:-:-}
gate7={-:-:-:-:-:1:-:1:NOT:-:-:-:-:-:-}
gate8={-:-:-:-:-:-:-:-:-:1:1:NOT:-:-:-}
gate9={-:-:-:-:-:-:-:-:-:1:NOT:-:-:-:-}
gate10={-:-:-:-:-:-:-:-:1:-:1:NOT:-:-:-}
gate11={-:-:-:-:-:-:-:-:-:-:-:-:1:1:NOT}
gate12={-:-:-:-:-:-:-:-:-:-:-:-:1:NOT:-}
gate13={-:-:-:-:-:-:-:-:-:-:-:1:-:1:NOT}
gate14={-:-:-:-:-:-:-:-:-:-:-:1:-:NOT:-}
gate15={-:-:-:-:-:-:-:-:1:-:1:NOT:-:-:-}
gate16={-:-:-:-:-:-:-:-:-:1:1:NOT:-:-:-}
gate17={-:-:-:-:-:-:-:-:-:1:-:NOT:-:-:-}
gate18={-:-:-:-:-:-:-:-:1:-:NOT:-:-:-:-}
gate19={-:-:-:-:-:1:-:1:NOT:-:-:-:-:-:-}
gate20={-:-:-:-:-:-:1:1:NOT:-:-:-:-:-:-}
gate21={-:-:-:-:-:-:1:-:NOT:-:-:-:-:-:-}
gate22={-:-:-:-:-:1:-:NOT:-:-:-:-:-:-:-}
gate23={-:-:1:-:1:NOT:-:-:-:-:-:-:-:-:-}
gate24={-:-:-:1:1:NOT:-:-:-:-:-:-:-:-:-}
gate25={-:-:-:1:-:NOT:-:-:-:-:-:-:-:-:-}
gate26={-:-:1:-:NOT:-:-:-:-:-:-:-:-:-:-}
gate27={1:1:NOT:-:-:-:-:-:-:-:-:-:-:-:-}
gate28={1:-:NOT:-:-:-:-:-:-:-:-:-:-:-:-}

# Quantum circuit to add 5 qubits without garbage
# Produced by genadder version 1.0
# $Header: /home/black/Quantum/GenAdder/RCS/genadder.C,v 1.1 2003/08/20 13:28:20 $
# 	For more information, contact Paul E. Black 
# Command line:
# 	5

# Declare qubits
variable inp0, sum0, anc0, inp1, sum1, anc1, inp2, sum2, anc2, inp3,
sum3, anc3, inp4, sum4, carryOut: qubit;
= |000000000000000>;

# 	The adder itself
# sum bit 0
c2not(inp0, sum0, anc0);
cnot(inp0, sum0);
# sum bit 1
c2not(inp1, sum1, anc1);
cnot(inp1, sum1);
c2not(anc0, sum1, anc1);
# sum bit 2
c2not(inp2, sum2, anc2);
cnot(inp2, sum2);
c2not(anc1, sum2, anc2);
# sum bit 3
c2not(inp3, sum3, anc3);
cnot(inp3, sum3);
c2not(anc2, sum3, anc3);
# sum bit 4
c2not(inp4, sum4, carryOut);
cnot(inp4, sum4);
c2not(anc3, sum4, carryOut);
# finish bit 4
cnot(anc3, sum4);
# finish bit 3
c2not(anc2, sum3, anc3);
c2not(inp3, sum3, anc3);
cnot(inp3, anc3);
cnot(anc2, sum3);
# finish bit 2
c2not(anc1, sum2, anc2);
c2not(inp2, sum2, anc2);
cnot(inp2, anc2);
cnot(anc1, sum2);
# finish bit 1
c2not(anc0, sum1, anc1);
c2not(inp1, sum1, anc1);
cnot(inp1, anc1);
cnot(anc0, sum1);
# finish bit 0
c2not(inp0, sum0, anc0);
cnot(inp0, anc0);

Genadder was originally written by Paul E. Black August 2003.

Download


Send comments to: webmaster-sdct
Privacy Policy

Created Wed Aug 20 10:07:24 2003
by Paul E. Black

Updated Thu Nov 20 15:30:57 2003
by Paul E. Black

This page's URL is: http://hissa.nist.gov/~black/Quantum/genadder.html