Skip to documentation
← Algorithm guides

Algorithm guide · arithmetic

Half adder

Add two input bits: sum A ⊕ B and carry AB. Compare the PDF's clean-output and in-place circuits.

Circuit size: 3 or 4 qubits · one-bit inputs

Before you begin

XOR · AND · CX and CCX truth tables

Review quantum fundamentals →
Notation used in this lesson
|ψ⟩ and ⟨ψ|
A state vector (ket) and its conjugate transpose (bra). ⟨a|b⟩ is their inner product.
α, β and i
α and β are complex amplitudes; i²=−1. A probability is the squared magnitude |α|².
XOR, · and ⊗
XOR adds bits modulo two. In s·x, multiply corresponding bits and XOR them. ⊗ combines registers or operations on different wires.
U†, Σ and Tr
U† is the adjoint (inverse for a unitary). Σ sums terms. Tr is the matrix trace, the sum of diagonal entries.
Probability, shots and angles
Ideal probabilities describe the model; measured shot frequencies fluctuate. Angles inside trigonometric expressions are in radians unless explicitly labeled turns.

1. The problem

Add two individual bits A and B. Produce a sum bit S and carry C while keeping the overall transformation reversible. Ordinary irreversible logic cannot simply erase its inputs inside a unitary circuit.

2. How it works

CX computes XOR into a target. CCX toggles a clean carry target exactly when both inputs are 1. The in-place version writes the sum into B after computing the carry; the clean-output version preserves both inputs.

  1. Prepare

    Encode A and B and initialize the extra output wire(s) to zero.

  2. Compute carry

    In the in-place version, CCX(A,B→carry) first computes AB.

  3. Compute sum

    CX(A→B) replaces B by A XOR B. Clean-output mode instead XORs both inputs into a separate sum wire.

3. The mathematics, one idea at a time

S=A XOR B; C=AB; A+B=2C+S

This identity turns the two arithmetic output bits into an ordinary integer.

In-place map: |A,B,0⟩ → |A,A XOR B,AB⟩

A and the outputs together keep enough information to reverse the operation.

Work this through by hand

4. Smallest version: in-place 1+1

The in-place variant uses three qubits; clean-output mode uses four. Choose in-place for the smallest circuit.

Choose In-place, A=1, B=1.

StageCalculation or stateWhat it means
PrepareA=1, B=1, carry=0No addition has happened yet.
CCXcarry=0 XOR (1·1)=1Compute carry before overwriting B.
CXB=1 XOR 1=0B now stores the low sum bit.

Expected outcomes for this example

These are ideal predictions for the stated configuration, not a result already measured by your browser. Rows describing alternative modes or combined events are labeled explicitly.

Outcome or eventIdeal probabilityInterpretation
10100% idealc1c0 = carry|sum = 1|0, the binary number two.

5. Load the worked example in Composer

The worked example can use smaller or simpler inputs than the gallery defaults. The button below loads the exact circuit used in this lesson.

Circuit version
In-place · 3 qubits
Input A
1
Input B
1
  1. Save any current circuit you want to keep, then open the worked example. Loading it does not execute a paid hardware job.
  2. Trace the preparation, main operations and measurements against the worked table. Check the gate editor for controls, targets and angles.
  3. Start with an ideal noiseless simulator compatible with the circuit. Mid-circuit measurement, reset and feed-forward require support from the chosen backend.
  4. Inspect the live preview. For a recorded experiment, open Execute, set the available backend options and shots, and Run. Sign in when prompted to retain it in My Jobs.
  5. Read the output as described below before changing one setting and repeating. The guide describes the original example; edits can change the answer.

6. Read the output correctly

Select Classical bits: only carry and sum are recorded in c1c0.

The Qubits view also contains the preserved A wire; that full state label is not the two-bit answer.

A bit label is a register value; its bar height is a probability or count. For S shots and observed count k, the sampled frequency is k/S. A typical sampling scale for probability p is √[p(1−p)/S]; this is not a strict error bound. Finite samples do not have to match the ideal percentages exactly.

7. Common mistakes & limits

  • Reversing the in-place gate order loses the original B needed to compute AB.
  • A half adder has no carry-in. The full-adder card is needed to add a third input bit.

8. Check your understanding

What does A=0, B=1 produce?

Reveal the reasoning

S=1 and C=0, so the classical output is 01.

9. Further study

The lesson’s register mappings, supported inputs and loadable example follow Quantket’s implemented circuit. A reference may use different wire names, bit order or resource conventions.

Gallery reference: defaults, definitions & implementation notes

Inputs & default settings

Set A and B, choose whether to preserve B, then inspect the CX/CCX circuit. Select Classical bits in Composer for the sum/carry histogram.

These are the gallery defaults. The worked example above may override them. Configure the card to change the inputs before loading its editable circuit.

Circuit version
Default: clean · Choices for the current settings: Clean outputs · 4 qubits; In-place · 3 qubits.
Input A
Default: 1 · Choices for the current settings: 0; 1.
Input B
Default: 1 · Choices for the current settings: 0; 1.

Implementation notes

This example uses the gallery's default configuration. Check Run details for the settings used in your execution.

Clean output: two CX gates write A ⊕ B into a fresh sum qubit, then CCX writes AB into a fresh carry qubit. A and B stay unchanged. It uses 4 qubits and 3 logical gates. In Composer, choose Classical bits to read c1c0 as carry then sum.

Default example: predictions & circuit facts

These values describe the unedited default setup. They are predictions and circuit properties, not measured results from your saved execution.

Sum bit
0
Carry-out
1
Histogram c1c0
10
Logical gates
2 CX + 1 CCX

Read your saved result

Check the submitted circuit, measured register and execution settings in Run details. Exact results give computed probabilities; sampled results show frequencies from the recorded shots. Edited circuits and noise can change the expected outcome. The algorithm label records the circuit's origin; it does not certify an edited circuit or a measured result.